def save_mel(dest_dir: str, data_len: int, wav_entry: WavData, mel_tensor: Tensor) -> str: chunk_dir = os.path.join(dest_dir, get_chunk_name( wav_entry.entry_id, chunksize=CHUNK_SIZE, maximum=data_len - 1)) os.makedirs(chunk_dir, exist_ok=True) dest_mel_path = os.path.join(chunk_dir, get_pytorch_filename(repr(wav_entry))) torch.save(mel_tensor, dest_mel_path) return dest_mel_path
def normalize(data: WavDataList, dest_dir: str) -> WavDataList: assert os.path.isdir(dest_dir) result = WavDataList() for values in data.items(True): chunk_dir = os.path.join(dest_dir, get_chunk_name( values.entry_id, chunksize=500, maximum=len(data) - 1)) os.makedirs(chunk_dir, exist_ok=True) dest_wav_path = os.path.join(chunk_dir, f"{values!r}.wav") normalize_file(values.wav, dest_wav_path) result.append(WavData(values.entry_id, dest_wav_path, values.duration, values.sr)) return result
def upsample(data: WavDataList, dest_dir: str, new_rate: int) -> WavDataList: assert os.path.isdir(dest_dir) result = WavDataList() for values in data.items(True): chunk_dir = os.path.join(dest_dir, get_chunk_name( values.entry_id, chunksize=500, maximum=len(data) - 1)) os.makedirs(chunk_dir, exist_ok=True) dest_wav_path = os.path.join(chunk_dir, f"{values!r}.wav") # todo assert not is_overamp upsample_file(values.wav, dest_wav_path, new_rate) result.append(WavData(values.entry_id, dest_wav_path, values.duration, new_rate)) return result
def save_plot(dest_dir: str, data_len: int, wav_entry: WavData, ds_entry: DsData, mel_tensor: Tensor) -> str: chunk_dir = os.path.join( dest_dir, get_chunk_name(wav_entry.entry_id, chunksize=CHUNK_SIZE, maximum=data_len - 1)) os.makedirs(chunk_dir, exist_ok=True) plot_melspec(mel_tensor, title=f"{repr(wav_entry)}: {ds_entry.text}") path = os.path.join(chunk_dir, f"{repr(wav_entry)}.png") plt.savefig(path, bbox_inches='tight') plt.close() return path
def remove_silence(data: WavDataList, dest_dir: str, chunk_size: int, threshold_start: float, threshold_end: float, buffer_start_ms: float, buffer_end_ms: float) -> WavDataList: assert os.path.isdir(dest_dir) result = WavDataList() for values in data.items(True): chunk_dir = os.path.join(dest_dir, get_chunk_name( values.entry_id, chunksize=500, maximum=len(data) - 1)) os.makedirs(chunk_dir, exist_ok=True) dest_wav_path = os.path.join(chunk_dir, f"{values!r}.wav") new_duration = remove_silence_file( in_path=values.wav, out_path=dest_wav_path, chunk_size=chunk_size, threshold_start=threshold_start, threshold_end=threshold_end, buffer_start_ms=buffer_start_ms, buffer_end_ms=buffer_end_ms ) result.append(WavData(values.entry_id, dest_wav_path, new_duration, values.sr)) return result
def test_1000_500_1490_is_1000_1490(self): x = get_chunk_name(1000, 500, 1490) self.assertEqual("1000-1490", x)
def test_500_500_1000_is_500_999(self): x = get_chunk_name(500, 500, 1000) self.assertEqual("500-999", x)
def test_0_500_400_is_0_400(self): x = get_chunk_name(0, 500, 400) self.assertEqual("0-400", x)
def test_0_500_1000_is_0_499(self): x = get_chunk_name(0, 500, 1000) self.assertEqual("0-499", x)