def test_file(tmpdir): segment = audinterface.Segment( process_func=lambda s, sr: INDEX, sampling_rate=None, resample=False, verbose=False, ) # create test file root = str(tmpdir.mkdir('wav')) file = 'file.wav' path = os.path.join(root, file) af.write(path, SIGNAL, SAMPLING_RATE) # test absolute path result = segment.process_file(path) assert all(result.levels[0] == path) assert all(result.levels[1] == INDEX.levels[0]) assert all(result.levels[2] == INDEX.levels[1]) result = segment.process_file(path, start=pd.to_timedelta('1s')) assert all(result.levels[0] == path) assert all(result.levels[1] == INDEX.levels[0] + pd.to_timedelta('1s')) assert all(result.levels[2] == INDEX.levels[1] + pd.to_timedelta('1s')) # test relative path result = segment.process_file(file, root=root) assert all(result.levels[0] == file) assert all(result.levels[1] == INDEX.levels[0]) assert all(result.levels[2] == INDEX.levels[1]) result = segment.process_file(file, root=root, start=pd.to_timedelta('1s')) assert all(result.levels[0] == file) assert all(result.levels[1] == INDEX.levels[0] + pd.to_timedelta('1s')) assert all(result.levels[2] == INDEX.levels[1] + pd.to_timedelta('1s'))
def separate_from_model(model_str,input_filename): if model_str== 'u_net_5_5': kernel_size=(5,5) if model_str== 'u_net_3_7': kernel_size=(7,3) if model_str== 'u_net_4_6': kernel_size=(6,4) audio_mix, fs = af.read(input_filename) if (model_str == 'u_net_5_5' or model_str == 'u_net_3_7' or model_str == 'u_net_4_6'): model = utls.load_unet_spleeter(kernel_size,'weights/' + model_str + '.hdf5') if fs!=44100: print("Audio must be 44.1 kHz. Exiting") audio_vocal_pred = utls.separate_from_audio(audio_mix,44100,model,wiener_filter=True) audio_acc_pred = audio_mix - audio_vocal_pred[0:len(audio_mix)] else: audio_prepared = torch.Tensor(audio_mix) result = utls.separate_umx( audio_prepared, rate=44100, model_str_or_path="weights/model5", targets=['vocals'], residual=True) audio_vocal_pred = np.array(result['vocals'][0][0]) audio_acc_pred = audio_mix[:len(audio_vocal_pred)] - audio_vocal_pred[:len(audio_mix)] base_folder = './audio/' basename = os.path.splitext(os.path.basename(input_filename))[0] af.write(base_folder + basename + '_vocals_pred.wav', audio_vocal_pred, 44100) af.write(base_folder + basename + '_acc_pred.wav',audio_acc_pred,44100)
def make_noise(text_length, filename): # checking audio file to match with nb_samples = audiofile.samples(filename) sampling_rate = audiofile.sampling_rate(filename) # in Hz # Generate random logarithmic noise noise = np.random.lognormal(0, 1, nb_samples) noise /= np.amax(np.abs(noise)) # Filter with bandpass filter nyq = 0.5 * sampling_rate low = text_length / nyq high = (50 + text_length) / nyq order = 1 b, a = signal.butter(order, [low, high], btype="band") filtered_noise = signal.lfilter(b, a, noise) # create Gaussian enveloppe t = np.linspace(start=-0.5, stop=0.5, num=nb_samples, endpoint=False) i, e = signal.gausspulse(t, fc=5, retenv=True, bwr=-6.0) out_signal = np.multiply(filtered_noise, e) out_signal *= 30 # write audio file audiofile.write(filename + ".noise.wav", out_signal, sampling_rate) print("text length was :", text_length)
def test_call(tmpdir, bit_depth_in, channels_in, format_in, sampling_rate_in, flavor, bit_depth_out, channels_out, format_out, sampling_rate_out): file_in = os.path.join(tmpdir, 'in.' + format_in) file_out = os.path.join(tmpdir, 'out.' + format_out) signal = np.zeros((channels_in, int(sampling_rate_in)), np.float32) if format_in == 'mp3': audiofile.write( f'{file_in[:-4]}.wav', signal, sampling_rate_in, int(bit_depth_in), ) os.rename(f'{file_in[:-4]}.wav', file_in) else: audiofile.write( file_in, signal, int(sampling_rate_in), int(bit_depth_in), ) flavor(file_in, file_out) assert audiofile.bit_depth(file_out) == bit_depth_out assert audiofile.channels(file_out) == channels_out assert audiofile.sampling_rate(file_out) == sampling_rate_out
def write_and_read( file, signal, sampling_rate, bit_depth=16, always_2d=False, normalize=False, ): """Write and read audio files.""" af.write(file, signal, sampling_rate, bit_depth, normalize) return af.read(file, always_2d=always_2d)
def test_process_index(tmpdir): process = audinterface.ProcessWithContext( process_func=None, sampling_rate=None, resample=False, verbose=False, ) # empty index = audformat.segmented_index() result = process.process_index(index) assert result.empty # non-empty # create file sampling_rate = 8000 signal = np.random.uniform(-1.0, 1.0, (1, 3 * sampling_rate)) root = str(tmpdir.mkdir('wav')) file = 'file.wav' path = os.path.join(root, file) af.write(path, signal, sampling_rate) # absolute paths index = audformat.segmented_index( [path] * 3, pd.timedelta_range('0s', '2s', 3), pd.timedelta_range('1s', '3s', 3), ) result = process.process_index(index) for (path, start, end), value in result.items(): signal, sampling_rate = audinterface.utils.read_audio(path, start=start, end=end) np.testing.assert_equal(signal, value) # relative paths index = audformat.segmented_index( [file] * 3, pd.timedelta_range('0s', '2s', 3), pd.timedelta_range('1s', '3s', 3), ) result = process.process_index(index, root=root) for (file, start, end), value in result.items(): signal, sampling_rate = audinterface.utils.read_audio(file, start=start, end=end, root=root) np.testing.assert_equal(signal, value)
def test_read(tmpdir, duration, offset): file = str(tmpdir.join('signal.wav')) sampling_rate = 8000 signal = sine( duration=0.1, sampling_rate=sampling_rate, channels=1, ) af.write(file, signal, sampling_rate) sig, fs = af.read(file, duration=duration, offset=offset) assert sig.shape == (0, ) assert fs == sampling_rate sig, fs = af.read(file, always_2d=True, duration=duration, offset=offset) assert sig.shape == (1, 0) assert fs == sampling_rate
def test_mp3(tmpdir, magnitude, sampling_rate, channels): # Currently we are not able to setup the Windows runner with MP3 support # https://github.com/audeering/audiofile/issues/51 if sys.platform == 'win32': return signal = sine(magnitude=magnitude, sampling_rate=sampling_rate, channels=channels) # Create wav file and use sox to convert to mp3 wav_file = str(tmpdir.join('signal.wav')) mp3_file = str(tmpdir.join('signal.mp3')) af.write(wav_file, signal, sampling_rate) subprocess.call(['sox', wav_file, mp3_file]) assert audeer.file_extension(mp3_file) == 'mp3' sig, fs = af.read(mp3_file) assert_allclose(_magnitude(sig), magnitude, rtol=0, atol=tolerance(16)) assert fs == sampling_rate assert _channels(sig) == channels if channels == 1: assert sig.ndim == 1 else: assert sig.ndim == 2 assert af.channels(mp3_file) == _channels(sig) assert af.sampling_rate(mp3_file) == sampling_rate assert af.samples(mp3_file) == _samples(sig) assert af.duration(mp3_file) == _duration(sig, sampling_rate) assert af.duration(mp3_file, sloppy=True) == sox.file_info.duration(mp3_file) assert af.bit_depth(mp3_file) is None # Test additional arguments to read with sox offset = 0.1 duration = 0.5 sig, fs = af.read(mp3_file, offset=offset, duration=duration) assert _duration(sig, sampling_rate) == duration sig, fs = af.read(mp3_file, offset=offset) # Don't test for 48000 Hz and 2 channels # https://github.com/audeering/audiofile/issues/23 if not (sampling_rate == 48000 and channels == 2): assert_allclose( _duration(sig, sampling_rate), af.duration(mp3_file) - offset, rtol=0, atol=tolerance('duration', sampling_rate), )
def fixture_publish_db(): clear_root(DB_ROOT) clear_root(pytest.FILE_SYSTEM_HOST) # create db db = audformat.testing.create_db(minimal=True) db.name = DB_NAME db['files'] = audformat.Table(audformat.filewise_index(list(DB_FILES))) db['files']['original'] = audformat.Column() db['files']['original'].set(list(DB_FILES)) for file in DB_FILES: signal = np.zeros( ( DB_FILES[file]['channels'], DB_FILES[file]['sampling_rate'], ), dtype=np.float32, ) path = os.path.join(DB_ROOT, file) audeer.mkdir(os.path.dirname(path)) audiofile.write(path, signal, DB_FILES[file]['sampling_rate'], bit_depth=DB_FILES[file]['bit_depth']) db['segments'] = audformat.Table( audformat.segmented_index( [list(DB_FILES)[0]] * 3, starts=['0s', '1s', '2s'], ends=['1s', '2s', '3s'], )) db.save(DB_ROOT) # publish db audb.publish( DB_ROOT, '1.0.0', pytest.PUBLISH_REPOSITORY, verbose=False, ) yield clear_root(DB_ROOT) clear_root(pytest.FILE_SYSTEM_HOST)
def test_folder(tmpdir, num_workers, multiprocessing): model = audinterface.Segment( process_func=lambda s, sr: INDEX, sampling_rate=None, resample=False, num_workers=num_workers, multiprocessing=multiprocessing, verbose=False, ) path = str(tmpdir.mkdir('wav')) files = [os.path.join(path, f'file{n}.wav') for n in range(3)] for file in files: af.write(file, SIGNAL, SAMPLING_RATE) result = model.process_folder(path) assert all(result.levels[0] == files) assert all(result.levels[1] == INDEX.levels[0]) assert all(result.levels[2] == INDEX.levels[1])
def empty_file(tmpdir, request): """Fixture to generate empty audio files. The request parameter allows to select the file extension. """ # Create empty audio file empty_file = os.path.join(tmpdir, 'empty-file.wav') af.write(empty_file, np.array([[]]), 16000) # Rename to match extension file_ext = request.param ofpath = audeer.replace_file_extension(empty_file, file_ext) if os.path.exists(empty_file): os.rename(empty_file, ofpath) yield ofpath if os.path.exists(ofpath): os.remove(ofpath)
def test_write_errors(): sampling_rate = 44100 # Call with unallowed bit depths expected_error = '"bit_depth" has to be one of' with pytest.raises(RuntimeError, match=expected_error): af.write('test.wav', np.zeros((1, 100)), sampling_rate, bit_depth=1) # Checking for not allowed combinations of channel and file type expected_error = ("The maximum number of allowed channels " "for 'flac' is 8. Consider using 'wav' instead.") with pytest.raises(RuntimeError, match=expected_error): write_and_read('test.flac', np.zeros((9, 100)), sampling_rate) expected_error = ("The maximum number of allowed channels " "for 'ogg' is 255. Consider using 'wav' instead.") with pytest.raises(RuntimeError, match=expected_error): write_and_read('test.ogg', np.zeros((256, 100)), sampling_rate) expected_error = ("The maximum number of allowed channels " "for 'wav' is 65535.") with pytest.raises(RuntimeError, match=expected_error): write_and_read('test.wav', np.zeros((65536, 100)), sampling_rate)
def test_process_index(tmpdir): feature = audinterface.Feature( feature_names=('o1', 'o2', 'o3'), process_func=feature_extractor, channels=range(NUM_CHANNELS), ) # empty index = audformat.segmented_index() y = feature.process_index(index) assert y.empty assert y.columns.tolist() == feature.column_names # non-empty # create file root = str(tmpdir.mkdir('wav')) file = 'file.wav' path = os.path.join(root, file) af.write(path, SIGNAL_2D, SAMPLING_RATE) y_expected = np.ones((2, NUM_CHANNELS * NUM_FEATURES)) # absolute paths index = audformat.segmented_index([path] * 2, [0, 1], [2, 3]) y = feature.process_index(index) assert y.index.get_level_values('file')[0] == path np.testing.assert_array_equal(y.values, y_expected) assert y.columns.tolist() == feature.column_names # relative paths index = audformat.segmented_index([file] * 2, [0, 1], [2, 3]) y = feature.process_index(index, root=root) assert y.index.get_level_values('file')[0] == file np.testing.assert_array_equal(y.values, y_expected) assert y.columns.tolist() == feature.column_names
def test_process_folder(tmpdir): index = audinterface.utils.signal_index(0, 1) feature = audinterface.Feature( feature_names=('o1', 'o2', 'o3'), process_func=feature_extractor, sampling_rate=None, channels=range(NUM_CHANNELS), resample=False, verbose=False, ) path = str(tmpdir.mkdir('wav')) files = [os.path.join(path, f'file{n}.wav') for n in range(3)] for file in files: af.write(file, SIGNAL_2D, SAMPLING_RATE) y = feature.process_folder(path) y_expected = np.ones((3, NUM_CHANNELS * NUM_FEATURES)) assert all(y.index.levels[0] == files) assert all(y.index.levels[1] == index.levels[0]) assert all(y.index.levels[2] == index.levels[1]) np.testing.assert_array_equal(y.values, y_expected)
import audsp import audiofile as af import sox dur = 1.0 channel_list = [1, 2] sr_list = [8000, 16000, 44100] for n_ch in channel_list: for sr_in in sr_list: wav_original = f'original__sr_{sr_in}__channels_{n_ch}.wav' x = audsp.utils.am_fm_synth( dur, num_channels=n_ch, sampling_rate=sr_in ) af.write(wav_original, x, sr_in) for sr_out in sr_list: if sr_out == sr_in: continue wav = f'resampled__sr-in_{sr_in}__sr-out_{sr_out}__channels' \ f'_{n_ch}.wav' tfm = sox.Transformer() tfm.rate(sr_out) tfm.build(wav_original, wav)
peak = 0.8 sine_freq = 1000 # Hz # generate noise noise_array = 2 * peak * np.random.rand(2 * n_samples_generated) - peak for n_ch in channel_list: for sr_in in sr_list: for signal_type in ['noise', 'sine']: wav_original = f'original-{signal_type}__sr_{sr_in}__channels_{n_ch}.wav' if signal_type is 'noise': x = noise_array[:n_ch * n_samples_generated] elif signal_type is 'sine': t = np.arange(n_samples_generated) / sr_in x = peak * np.sin(2 * np.pi * sine_freq * t) if n_ch > 1: x_extension = peak * np.cos(2 * np.pi * sine_freq * t) x = np.append(x, x_extension) af.write(wav_original, np.reshape(x, (n_ch, -1)), sr_in) for sr_out in sr_list: if sr_out == sr_in: continue wav_resampled = f'resampled-{signal_type}__sr-in_{sr_in}__sr-out_{sr_out}__channels_{n_ch}.wav' tfm = sox.Transformer() tfm.channels(1) tfm.rate(sr_out) tfm.build(wav_original, wav_resampled)
def test_process_file(tmpdir, start, end, segment): start_org = start end_org = end feature = audinterface.Feature( feature_names=('o1', 'o2', 'o3'), process_func=feature_extractor, sampling_rate=None, channels=range(NUM_CHANNELS), resample=False, segment=segment, verbose=False, ) y_expected = np.ones((1, NUM_CHANNELS * NUM_FEATURES)) # create test file root = str(tmpdir.mkdir('wav')) file = 'file.wav' path = os.path.join(root, file) af.write(path, SIGNAL_2D, SAMPLING_RATE) # test absolute path start = start_org end = end_org y = feature.process_file(path, start=start, end=end) if start is None or pd.isna(start): start = pd.to_timedelta(0) if end is None or pd.isna(end): end = pd.to_timedelta(af.duration(path), unit='s') if segment is not None: index = segment.process_file(path) start = index[0][1] end = index[0][2] assert y.index.levels[0][0] == path assert y.index.levels[1][0] == start assert y.index.levels[2][0] == end np.testing.assert_array_equal(y, y_expected) # test relative path start = start_org end = end_org y = feature.process_file(file, start=start, end=end, root=root) if start is None or pd.isna(start): start = pd.to_timedelta(0) if end is None or pd.isna(end): end = pd.to_timedelta(af.duration(path), unit='s') if segment is not None: index = segment.process_file(file, root=root) start = index[0][1] end = index[0][2] assert y.index.levels[0][0] == file assert y.index.levels[1][0] == start assert y.index.levels[2][0] == end np.testing.assert_array_equal(y, y_expected)
def test_index(tmpdir, num_workers, multiprocessing): def process_func(x, sr): dur = pd.to_timedelta(x.shape[-1] / sr, unit='s') return audinterface.utils.signal_index( '0.1s', dur - pd.to_timedelta('0.1s'), ) segment = audinterface.Segment( process_func=process_func, sampling_rate=None, resample=False, num_workers=num_workers, multiprocessing=multiprocessing, verbose=False, ) # create signal and file sampling_rate = 8000 signal = np.random.uniform(-1.0, 1.0, (1, 3 * sampling_rate)) root = str(tmpdir.mkdir('wav')) file = 'file.wav' path = os.path.join(root, file) af.write(path, signal, sampling_rate) # empty index index = audformat.segmented_index() result = segment.process_index(index) assert result.empty result = segment.process_signal_from_index(signal, sampling_rate, index) assert result.empty # segmented index without file level index = audinterface.utils.signal_index( pd.timedelta_range('0s', '2s', 3), pd.timedelta_range('1s', '3s', 3), ) expected = audinterface.utils.signal_index( index.get_level_values('start') + pd.to_timedelta('0.1s'), index.get_level_values('end') - pd.to_timedelta('0.1s'), ) result = segment.process_signal_from_index(signal, sampling_rate, index) pd.testing.assert_index_equal(result, expected) # segmented index with absolute paths index = audformat.segmented_index( [path] * 3, pd.timedelta_range('0s', '2s', 3), pd.timedelta_range('1s', '3s', 3), ) expected = audformat.segmented_index( [path] * 3, index.get_level_values('start') + pd.to_timedelta('0.1s'), index.get_level_values('end') - pd.to_timedelta('0.1s'), ) result = segment.process_index(index) pd.testing.assert_index_equal(result, expected) result = segment.process_signal_from_index(signal, sampling_rate, index) pd.testing.assert_index_equal(result, expected) # filewise index with absolute paths index = pd.Index([path], name='file') expected = audformat.segmented_index(path, '0.1s', '2.9s') result = segment.process_index(index) pd.testing.assert_index_equal(result, expected) result = segment.process_signal_from_index(signal, sampling_rate, index) pd.testing.assert_index_equal(result, expected) # segmented index with relative paths index = audformat.segmented_index( [file] * 3, pd.timedelta_range('0s', '2s', 3), pd.timedelta_range('1s', '3s', 3), ) expected = audformat.segmented_index( [file] * 3, index.get_level_values('start') + pd.to_timedelta('0.1s'), index.get_level_values('end') - pd.to_timedelta('0.1s'), ) result = segment.process_index(index, root=root) pd.testing.assert_index_equal(result, expected) result = segment.process_signal_from_index(signal, sampling_rate, index) pd.testing.assert_index_equal(result, expected) # filewise index with relative paths index = pd.Index([file], name='file') expected = audformat.segmented_index(file, '0.1s', '2.9s') result = segment.process_index(index, root=root) pd.testing.assert_index_equal(result, expected) result = segment.process_signal_from_index(signal, sampling_rate, index) pd.testing.assert_index_equal(result, expected)
import numpy as np import audiofile as af sampling_rate = 8000 noise = np.random.normal(0, 1, sampling_rate) noise /= np.amax(np.abs(noise)) af.write('MY.wav', noise, sampling_rate) af.channels('MY.wav') af.duration('MY.wav') sig, fs = af.read('MY.wav') print(fs) print(sig)
def fixture_publish_db(): clear_root(DB_ROOT) clear_root(pytest.FILE_SYSTEM_HOST) # create db db = audformat.testing.create_db(minimal=True) db.name = DB_NAME db.schemes['scheme'] = audformat.Scheme( labels=['positive', 'neutral', 'negative'] ) audformat.testing.add_table( db, 'emotion', audformat.define.IndexType.SEGMENTED, num_files=5, columns={'emotion': ('scheme', None)} ) db.schemes['speaker'] = audformat.Scheme( labels=['adam', 'eve'] ) db['files'] = audformat.Table(db.files) db['files']['speaker'] = audformat.Column(scheme_id='speaker') db['files']['speaker'].set( ['adam', 'adam', 'eve', 'eve'], index=audformat.filewise_index(db.files[:4]), ) # publish 1.0.0 db.save(DB_ROOT_VERSION['1.0.0']) audformat.testing.create_audio_files(db) archives = db['files']['speaker'].get().dropna().to_dict() audb.publish( DB_ROOT_VERSION['1.0.0'], '1.0.0', pytest.PUBLISH_REPOSITORY, archives=archives, verbose=False, ) # publish 1.1.0, add table audformat.testing.add_table( db, 'train', audformat.define.IndexType.SEGMENTED, columns={'label': ('scheme', None)} ) db.save(DB_ROOT_VERSION['1.1.0']) audformat.testing.create_audio_files(db) shutil.copy( os.path.join(DB_ROOT_VERSION['1.0.0'], 'db.csv'), os.path.join(DB_ROOT_VERSION['1.1.0'], 'db.csv'), ) audb.publish( DB_ROOT_VERSION['1.1.0'], '1.1.0', pytest.PUBLISH_REPOSITORY, verbose=False, ) # publish 1.1.1, change label db['train'].df['label'][0] = None db.save(DB_ROOT_VERSION['1.1.1']) audformat.testing.create_audio_files(db) shutil.copy( os.path.join(DB_ROOT_VERSION['1.1.0'], 'db.csv'), os.path.join(DB_ROOT_VERSION['1.1.1'], 'db.csv'), ) audb.publish( DB_ROOT_VERSION['1.1.1'], '1.1.1', pytest.PUBLISH_REPOSITORY, verbose=False, ) # publish 2.0.0, alter and remove media db.save(DB_ROOT_VERSION['2.0.0']) audformat.testing.create_audio_files(db) file = os.path.join(DB_ROOT_VERSION['2.0.0'], db.files[0]) y, sr = audiofile.read(file) y[0] = 1 audiofile.write(file, y, sr) file = db.files[-1] db.pick_files(lambda x: x != file) os.remove(audeer.safe_path(os.path.join(DB_ROOT_VERSION['2.0.0'], file))) db.save(DB_ROOT_VERSION['2.0.0']) shutil.copy( os.path.join(DB_ROOT_VERSION['1.1.1'], 'db.csv'), os.path.join(DB_ROOT_VERSION['2.0.0'], 'db.csv'), ) audb.publish( DB_ROOT_VERSION['2.0.0'], '2.0.0', pytest.PUBLISH_REPOSITORY, verbose=False, ) # publish 3.0.0, remove table db.drop_tables('train') db.save(DB_ROOT_VERSION['3.0.0']) audformat.testing.create_audio_files(db) shutil.copy( os.path.join(DB_ROOT_VERSION['2.0.0'], 'db.csv'), os.path.join(DB_ROOT_VERSION['3.0.0'], 'db.csv'), ) audb.publish( DB_ROOT_VERSION['3.0.0'], '3.0.0', pytest.PUBLISH_REPOSITORY, verbose=False, ) yield clear_root(DB_ROOT) clear_root(pytest.FILE_SYSTEM_HOST)
def __call__( self, src_path: str, dst_path: str, *, src_bit_depth: int = None, src_channels: int = None, src_sampling_rate: int = None, ): r"""Convert file to flavor. If ``bit_depth``, ``channels`` or ``sampling_rate`` of source signal are known, they can be provided. Otherwise, they will be computed using :mod:`audiofile`. Args: src_path: path to input file dst_path: path to output file src_bit_depth: bit depth src_channels: number of channels src_sampling_rate: sampling rate in Hz Raises: ValueError: if extension of output file does not match the format of the flavor RuntimeError: if a conversion is requested, but no output format is specified, and the input format is not WAV or FLAC """ src_path = audeer.safe_path(src_path) dst_path = audeer.safe_path(dst_path) # verify that extension matches the output format src_ext = audeer.file_extension(src_path).lower() dst_ext = audeer.file_extension(dst_path).lower() expected_ext = self.format or src_ext if expected_ext != dst_ext: raise ValueError( f"Extension of output file is '{dst_ext}', " f"but should be '{expected_ext}' " "to match the format of the converted file." ) if not self._check_convert( src_path, src_bit_depth, src_channels, src_sampling_rate ): # file already satisfies flavor if src_path != dst_path: shutil.copy(src_path, dst_path) else: # convert file to flavor signal, sampling_rate = audiofile.read(src_path, always_2d=True) signal = self._remix(signal) signal, sampling_rate = self._resample(signal, sampling_rate) bit_depth = ( self.bit_depth or src_bit_depth or audiofile.bit_depth(src_path) or 16 ) audiofile.write( dst_path, signal, sampling_rate, bit_depth=bit_depth, )
def pitch(st, log_level, input_file, output_file, quantize_bits, skip_normalize, skip_quantize, skip_input_filter, skip_output_filter, skip_time_stretch, custom_time_stretch): log = logging.getLogger(__name__) sh = logging.StreamHandler() sh.setFormatter(logging.Formatter('%(levelname)-8s %(message)s')) log.addHandler(sh) valid_levels = list(log_levels.keys()) if (not log_level) or (log_level.upper() not in valid_levels): log.warn(f'Invalid log-level: "{log_level}", log-level set to "INFO", ' f'valid log levels are {valid_levels}') log_level = 'INFO' log_level = log_levels[log_level] log.setLevel(log_level) log.info(f'loading: "{input_file}" at oversampled rate: {INPUT_SR}') y, s = load(input_file, sr=INPUT_SR) log.info('done loading') midrise, midtread = calc_quantize_function(quantize_bits, log) if skip_input_filter: log.info('skipping input anti aliasing filter') else: y = filter_input(y, log) resampled = scipy_resample(y, INPUT_SR, TARGET_SR, RESAMPLE_MULTIPLIER, log) if skip_quantize: log.info('skipping quantize') else: # simulate analog -> digital conversion # TODO: midtread/midrise option? resampled = quantize(resampled, midtread, quantize_bits, log) pitched = adjust_pitch(resampled, st, skip_time_stretch, log) if skip_time_stretch: ratio = len(pitched) / len(resampled) log.info( '\"skipping\" time stretch: stretching back to original length...') pitched = time_stretch(pitched, ratio) if custom_time_stretch: log.info('running custom time stretch of ratio: {custom_time_stretch}') pitched = time_stretch(pitched, custom_time_stretch) # oversample again (default factor of 4) to simulate ZOH # TODO: retest output against freq aliased sinc fn post_zero_order_hold = zero_order_hold(pitched, ZOH_MULTIPLIER, log) # TODO: try using scipy resample here? output = resample(np.asfortranarray(post_zero_order_hold), TARGET_SR * ZOH_MULTIPLIER, OUTPUT_SR) if skip_output_filter: log.info('skipping output eq filter') else: output = filter_output(output, log) # eq filter log.info(f'writing {output_file}, at sample rate {OUTPUT_SR} ' f'with skip_normalize set to {skip_normalize}') if '.mp3' in output_file: write_mp3(output_file, output, OUTPUT_SR, not skip_normalize) else: output_file = output_file af.write(output_file, output, OUTPUT_SR, '16bit', not skip_normalize) log.info(f'done! output_file at: {output_file}') return
# coming from generate.py text_length = 5000 filmename = "test_1.wav" # checking files nb_samples = audiofile.samples(filmename) sampling_rate = audiofile.sampling_rate(filmename) # in Hz # Generate random noise noise = np.random.lognormal(0, 1, nb_samples) noise /= np.amax(np.abs(noise)) # Filter with bandpass filter nyq = 0.5 * sampling_rate low = text_length / nyq high = (50 + text_length) / nyq order = 1 b, a = signal.butter(order, [low, high], btype='band') filtered_noise = signal.lfilter(b, a, noise) # create Gaussian enveloppe #t = np.linspace(start=-0.5,stop=0.5, num=nb_samples, endpoint=False) #i, e = signal.gausspulse(t, fc=5, retenv=True, bwr=-6.0) #out_signal = np.multiply(filtered_noise, e) #out_signal *= 0.3 #out_signal = i # write audio file audiofile.write(filmename + '.noise.wav', filtered_noise, sampling_rate)