code: def collate_factory(args):
      def raw_collate(batch):
          pad = (args.kernel_size - 1) // 2
  
          # input waveform length
          wave_length = args.hop_length * args.seq_len_factor
          # input spectrogram length
          spec_length = args.seq_len_factor + pad * 2
  
          # max start postion in spectrogram
          max_offsets = [
              x[1].shape[-1] - (spec_length + pad * 2) for x in batch
          ]
  
          # random start postion in spectrogram
          spec_offsets = [
              random.randint(0, offset) for offset in max_offsets
          ]
          # random start postion in waveform
          wave_offsets = [
              (offset + pad) * args.hop_length
              for offset in spec_offsets
          ]
  
          waveform_combine = [
              x[0][wave_offsets[i] : wave_offsets[i] + wave_length + 1]
              for i, x in enumerate(batch)
          ]
          specgram = [
              x[1][:, spec_offsets[i] : spec_offsets[i] + spec_length]
              for i, x in enumerate(batch)
          ]
  
          specgram = torch.stack(specgram)
          waveform_combine = torch.stack(waveform_combine)
  
          waveform = waveform_combine[:, :wave_length]
          target = waveform_combine[:, 1:]
  
          # waveform: [-1, 1], target: [0, 2**bits-1] if loss = 'crossentropy'
          if args.loss == "crossentropy":
              if args.mulaw:
                  mulaw_encode = MuLawEncoding(2**args.n_bits)
                  waveform = mulaw_encode(waveform)
                  target = mulaw_encode(target)
  
                  waveform = bits_to_normalized_waveform(
                      waveform, args.n_bits
                  )
  
              else:
                  target = normalized_waveform_to_bits(
                      target, args.n_bits
                  )
  
          return (
              waveform.unsqueeze(1),
              specgram.unsqueeze(1),
              target.unsqueeze(1),
          )
  
      return raw_collate
  
file_path: audio/examples/pipeline_wavernn/datasets.py
imports: import random
  import torch
  from processing import bits_to_normalized_waveform, normalized_waveform_to_bits
  from torch.utils.data.dataset import random_split
  from torchaudio.datasets import LIBRITTS, LJSPEECH
  from torchaudio.transforms import MuLawEncoding
label_desc: Use image node_id 2 for calling a global function with example usage: collate_factory(args) and returns: raw_collate, waveform,  specgram,  target