1. 程式人生 > >librosa

librosa

import os
import librosa
def slice_signal(filepath, window_size, sample_rate):
"""
Helper function for slicing the audio file
by window size with [stride] percent overlap (default 50%).
"""
wav, sr = librosa.load(filepath, sr=sample_rate)
n_samples = wav.shape[0] # contains simple amplitudes
hop = int(window_size * 1)
slices = []
for end_idx in range(window_size, len(wav), hop):
start_idx = end_idx - window_size
slice_sig = wav[start_idx:end_idx]
slices.append(slice_sig)
return slices

for idx in range(4): # select four samples 沒有意義,只是每段語音的切片資料,應該合成之後再儲存為wav檔案
print('{}_e{}.wav'.format(test_noise_filenames[idx], epoch + 1))
generated_sample = fake_speech_data[idx]
filepath = os.path.join(
gen_data_path, '{}_e{}.wav'.format(test_noise_filenames[idx], epoch + 1))
wavfile.write(filepath, sample_rate, generated_sample.T)