def apply_lameenc(self, samples, sample_rate): try: import lameenc except ImportError: print( "Failed to import the lame encoder. Maybe it is not installed? " "To install the optional lameenc dependency of audiomentations," " do `pip install audiomentations[extras]` instead of" " `pip install audiomentations`", file=sys.stderr, ) raise assert len(samples.shape) == 1 assert samples.dtype == np.float32 int_samples = convert_float_samples_to_int16(samples) encoder = lameenc.Encoder() encoder.set_bit_rate(self.parameters["bitrate"]) encoder.set_in_sample_rate(sample_rate) encoder.set_channels(1) encoder.set_quality(7) # 2 = highest, 7 = fastest encoder.silence() mp3_data = encoder.encode(int_samples.tobytes()) mp3_data += encoder.flush() # Write a temporary MP3 file that will then be decoded tmp_dir = tempfile.gettempdir() tmp_file_path = os.path.join( tmp_dir, "tmp_compressed_{}.mp3".format(str(uuid.uuid4())[0:12]) ) with open(tmp_file_path, "wb") as f: f.write(mp3_data) degraded_samples, _ = librosa.load(tmp_file_path, sample_rate) os.unlink(tmp_file_path) return degraded_samples
def apply_pydub(self, samples, sample_rate): try: import pydub except ImportError: print( "Failed to import pydub. Maybe it is not installed? " "To install the optional pydub dependency of audiomentations," " do `pip install audiomentations[extras]` instead of" " `pip install audiomentations`", file=sys.stderr, ) raise assert len(samples.shape) == 1 assert samples.dtype == np.float32 int_samples = convert_float_samples_to_int16(samples) audio_segment = pydub.AudioSegment( int_samples.tobytes(), frame_rate=sample_rate, sample_width=int_samples.dtype.itemsize, channels=1, ) tmp_dir = tempfile.gettempdir() tmp_file_path = os.path.join( tmp_dir, "tmp_compressed_{}.mp3".format(str(uuid.uuid4())[0:12]) ) bitrate_string = "{}k".format(self.parameters["bitrate"]) file_handle = audio_segment.export(tmp_file_path, bitrate=bitrate_string) file_handle.close() degraded_samples, _ = librosa.load(tmp_file_path, sample_rate) os.unlink(tmp_file_path) return degraded_samples