code: def dataio_prepare(hparams):
      """This function prepares the datasets to be used in the brain class.
      It also defines the data processing pipeline through user-defined functions.
      """
      segment_size = hparams["segment_size"]
      code_hop_size = hparams["code_hop_size"]
      code_folder = pl.Path(hparams["codes_folder"])
  
      # Define audio pipeline:
      @sb.utils.data_pipeline.takes("id", "wav", "segment")
      @sb.utils.data_pipeline.provides("code", "sig")
      def audio_pipeline(utt_id, wav, segment):
          info = torchaudio.info(wav)
          audio = sb.dataio.dataio.read_audio(wav)
          audio = torchaudio.transforms.Resample(
              info.sample_rate,
              hparams["sample_rate"],
          )(audio)
  
          code = np.load(code_folder / f"{utt_id}.npy")
          code = torch.IntTensor(code)
  
          # Maps indices from the range [0, k] to [1, k+1]
          code = code + 1
  
          # Trim end of audio
          code_length = min(
              audio.shape[0] // code_hop_size, code.shape[0]
          )
          code = code[:code_length]
          audio = audio[: code_length * code_hop_size]
  
          while audio.shape[0] < segment_size:
              audio = torch.hstack([audio, audio])
              code = torch.hstack([code, code])
  
          audio = audio.unsqueeze(0)
          if segment:
              audio, code = sample_interval([audio, code], segment_size)
  
          return code, audio
  
      datasets = {}
      data_info = {
          "train": hparams["train_json"],
          "valid": hparams["valid_json"],
          "test": hparams["test_json"],
      }
      for dataset in hparams["splits"]:
          datasets[
              dataset
          ] = sb.dataio.dataset.DynamicItemDataset.from_json(
              json_path=data_info[dataset],
              replacements={"data_root": hparams["data_folder"]},
              dynamic_items=[audio_pipeline],
              output_keys=["id", "code", "sig"],
          )
  
      return datasets
  
imports: import sys
  import copy
  import random
  import pathlib
  from hyperpyyaml import load_hyperpyyaml
  import speechbrain
  from speechbrain.utils.data_utils import scalarize
  import torch
  import torchaudio
  import numpy
label_desc: Use image node_id 2 for calling a global function with example usage: dataio_prepare(hparams) and returns: datasets, code,  audio