text: import os
  import librosa
  import numpy as np
  from asr_deepspeech.audio import get_audio_length, audio_with_sox
  
  
  class NoiseInjection(object):
      def __init__(self,
                   path=None,
                   sample_rate=16000,
                   noise_levels=(0, 0.5)):
          """
          Adds noise to an input signal with specific SNR. Higher the noise level, the more noise added.
          Modified code from https://github.com/willfrey/audio/blob/master/torchaudio/transforms.py
          """
          if not os.path.exists(path):
              print("Directory doesn't exist: {}".format(path))
              raise IOError
          self.paths = path is not None and librosa.util.find_files(path)
          self.sample_rate = sample_rate
          self.noise_levels = noise_levels
  
      def inject_noise(self, data):
          noise_path = np.random.choice(self.paths)
          noise_level = np.random.uniform(*self.noise_levels)
          return self.inject_noise_sample(data, noise_path, noise_level)
  
      def inject_noise_sample(self, data, noise_path, noise_level):
          noise_len = get_audio_length(noise_path)
          data_len = len(data) / self.sample_rate
          noise_start = np.random.rand() * (noise_len - data_len)
          noise_end = noise_start + data_len
          noise_dst = audio_with_sox(noise_path, self.sample_rate, noise_start, noise_end)
          assert len(data) == len(noise_dst)
          noise_energy = np.sqrt(noise_dst.dot(noise_dst) / noise_dst.size)
          data_energy = np.sqrt(data.dot(data) / data.size)
          data += noise_level * noise_dst * data_energy / noise_energy
          return data
  