| import logging |
|
|
| import numpy as np |
| import torch |
| import torchaudio |
| import torchvision |
| from torchvision.transforms import transforms |
| from torch.nn import functional as F |
|
|
| torchaudio.set_audio_backend("soundfile") |
|
|
| def torchaudio_loader(path): |
| return torchaudio.load(path) |
|
|
| def int16_to_float32_torch(x): |
| return (x / 32767.0).type(torch.float32) |
|
|
| def float32_to_int16_torch(x): |
| x = torch.clamp(x, min=-1., max=1.) |
| return (x * 32767.).type(torch.int16) |
|
|
| DEFAULT_AUDIO_FRAME_SHIFT_MS = 10 |
|
|
| class AudioTransform: |
| def __init__(self, args): |
| self.sample_rate = args.audio_sample_rate |
| self.num_mel_bins = args.num_mel_bins |
| self.target_length = args.target_length |
| self.audio_mean = args.audio_mean |
| self.audio_std = args.audio_std |
| self.mean = [] |
| self.std = [] |
| |
| |
| |
|
|
|
|
| def __call__(self, audio_data_and_origin_sr): |
| audio_data, origin_sr = audio_data_and_origin_sr |
| if self.sample_rate != origin_sr: |
| |
| audio_data = torchaudio.functional.resample(audio_data, orig_freq=origin_sr, new_freq=self.sample_rate) |
| waveform_melspec = self.waveform2melspec(audio_data) |
| return waveform_melspec |
|
|
|
|
| def waveform2melspec(self, audio_data): |
| mel = self.get_mel(audio_data) |
| if mel.shape[0] > self.target_length: |
| |
| chunk_frames = self.target_length |
| total_frames = mel.shape[0] |
| ranges = np.array_split(list(range(0, total_frames - chunk_frames + 1)), 3) |
| |
| |
| |
| |
| if len(ranges[1]) == 0: |
| ranges[1] = [0] |
| if len(ranges[2]) == 0: |
| ranges[2] = [0] |
| |
| idx_front = np.random.choice(ranges[0]) |
| idx_middle = np.random.choice(ranges[1]) |
| idx_back = np.random.choice(ranges[2]) |
| |
| |
| |
| |
| mel_chunk_front = mel[idx_front:idx_front + chunk_frames, :] |
| mel_chunk_middle = mel[idx_middle:idx_middle + chunk_frames, :] |
| mel_chunk_back = mel[idx_back:idx_back + chunk_frames, :] |
| |
| |
| mel_fusion = torch.stack([mel_chunk_front, mel_chunk_middle, mel_chunk_back], dim=0) |
| elif mel.shape[0] < self.target_length: |
| n_repeat = int(self.target_length / mel.shape[0]) + 1 |
| |
| mel = mel.repeat(n_repeat, 1)[:self.target_length, :] |
| mel_fusion = torch.stack([mel, mel, mel], dim=0) |
| else: |
| mel_fusion = torch.stack([mel, mel, mel], dim=0) |
| mel_fusion = mel_fusion.transpose(1, 2) |
|
|
| |
| |
| mel_fusion = (mel_fusion - self.audio_mean) / (self.audio_std * 2) |
| return mel_fusion |
|
|
| def get_mel(self, audio_data): |
| |
| audio_data -= audio_data.mean() |
| mel = torchaudio.compliance.kaldi.fbank( |
| audio_data, |
| htk_compat=True, |
| sample_frequency=self.sample_rate, |
| use_energy=False, |
| window_type="hanning", |
| num_mel_bins=self.num_mel_bins, |
| dither=0.0, |
| frame_length=25, |
| frame_shift=DEFAULT_AUDIO_FRAME_SHIFT_MS, |
| ) |
| return mel |
|
|
|
|
|
|
| def get_audio_transform(args): |
| return AudioTransform(args) |
|
|
| def load_and_transform_audio( |
| audio_path, |
| transform, |
| ): |
| waveform_and_sr = torchaudio_loader(audio_path) |
| audio_outputs = transform(waveform_and_sr) |
|
|
| return audio_outputs |