text: import tensorflow as tf
  import speech_keras_config as Config
  
  
  # Split noise into chunks of 16000 each
  def load_noise_sample(path):
      sample, sampling_rate = tf.audio.decode_wav(
          tf.io.read_file(path), desired_channels=1
      )
      if sampling_rate == Config.sampling_rate:
          # Number of slices of 16000 each that can be generated from the noise sample
          slices = int(sample.shape[0] / Config.sampling_rate)
          sample = tf.split(sample[: slices * Config.sampling_rate], slices)
          return sample
      else:
          print("Sampling rate for {} is incorrect. Ignoring it".format(path))
          return None
  
  
  def paths_and_labels_to_dataset(audio_paths, labels):
      """Constructs a dataset of audios and labels."""
      path_ds = tf.data.Dataset.from_tensor_slices(audio_paths)
      audio_ds = path_ds.map(lambda x: path_to_audio(x))
      label_ds = tf.data.Dataset.from_tensor_slices(labels)
      return tf.data.Dataset.zip((audio_ds, label_ds))
  
  
  def path_to_audio(path):
      """Reads and decodes an audio file."""
      audio = tf.io.read_file(path)
      audio, _ = tf.audio.decode_wav(audio, 1, Config.sampling_rate)
      return audio
  
  
  def add_noise(audio, noises=None, scale=0.5):
      if noises is not None:
          # Create a random tensor of the same size as audio ranging from
          # 0 to the number of noise stream samples that we have.
          tf_rnd = tf.random.uniform(
              (tf.shape(audio)[0],), 0, noises.shape[0], dtype=tf.int32
          )
          noise = tf.gather(noises, tf_rnd, axis=0)
  
          # Get the amplitude proportion between the audio and the noise
          prop = tf.math.reduce_max(audio, axis=1) / tf.math.reduce_max(noise, axis=1)
          # x = tf.cast(tf.expand_dims(prop, axis=1), dtype=tf.int32)
          # prop = tf.keras.backend.repeat_elements(x, tf.shape(audio)[1], axis=1)
          # print(audio.get_shape().as_list()[1])
          prop = tf.repeat(tf.expand_dims(prop, axis=1), tf.shape(audio)[1], axis=1)
  
          # Adding the rescaled noise to audio
          audio = audio + noise * prop * scale
  
      return audio
  
  
  def audio_to_fft(audio):
      # Since tf.signal.fft applies FFT on the innermost dimension,
      # we need to squeeze the dimensions and then expand them again
      # after FFT
      audio = tf.squeeze(audio, axis=-1)
      fft = tf.signal.fft(
          tf.cast(tf.complex(real=audio, imag=tf.zeros_like(audio)), tf.complex64)
      )
      fft = tf.expand_dims(fft, axis=-1)
  
      # Return the absolute value of the first half of the FFT
      # which represents the positive frequencies
      return tf.math.abs(fft[:, : (audio.shape[1] // 2), :])
  