Clean up previous raw files
Browse files- Zero-CPU/spec_gen.py +0 -87
Zero-CPU/spec_gen.py
DELETED
|
@@ -1,87 +0,0 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from tqdm import tqdm
|
| 3 |
-
from multiprocessing import Pool
|
| 4 |
-
from mel_processing import spectrogram_torch, mel_spectrogram_torch
|
| 5 |
-
from utils import load_wav_to_torch
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
class AudioProcessor:
|
| 9 |
-
def __init__(
|
| 10 |
-
self,
|
| 11 |
-
max_wav_value,
|
| 12 |
-
use_mel_spec_posterior,
|
| 13 |
-
filter_length,
|
| 14 |
-
n_mel_channels,
|
| 15 |
-
sampling_rate,
|
| 16 |
-
hop_length,
|
| 17 |
-
win_length,
|
| 18 |
-
mel_fmin,
|
| 19 |
-
mel_fmax,
|
| 20 |
-
):
|
| 21 |
-
self.max_wav_value = max_wav_value
|
| 22 |
-
self.use_mel_spec_posterior = use_mel_spec_posterior
|
| 23 |
-
self.filter_length = filter_length
|
| 24 |
-
self.n_mel_channels = n_mel_channels
|
| 25 |
-
self.sampling_rate = sampling_rate
|
| 26 |
-
self.hop_length = hop_length
|
| 27 |
-
self.win_length = win_length
|
| 28 |
-
self.mel_fmin = mel_fmin
|
| 29 |
-
self.mel_fmax = mel_fmax
|
| 30 |
-
|
| 31 |
-
def process_audio(self, filename):
|
| 32 |
-
audio, sampling_rate = load_wav_to_torch(filename)
|
| 33 |
-
audio_norm = audio / self.max_wav_value
|
| 34 |
-
audio_norm = audio_norm.unsqueeze(0)
|
| 35 |
-
spec_filename = filename.replace(".wav", ".spec.pt")
|
| 36 |
-
if self.use_mel_spec_posterior:
|
| 37 |
-
spec_filename = spec_filename.replace(".spec.pt", ".mel.pt")
|
| 38 |
-
try:
|
| 39 |
-
spec = torch.load(spec_filename)
|
| 40 |
-
except:
|
| 41 |
-
if self.use_mel_spec_posterior:
|
| 42 |
-
spec = mel_spectrogram_torch(
|
| 43 |
-
audio_norm,
|
| 44 |
-
self.filter_length,
|
| 45 |
-
self.n_mel_channels,
|
| 46 |
-
self.sampling_rate,
|
| 47 |
-
self.hop_length,
|
| 48 |
-
self.win_length,
|
| 49 |
-
self.mel_fmin,
|
| 50 |
-
self.mel_fmax,
|
| 51 |
-
center=False,
|
| 52 |
-
)
|
| 53 |
-
else:
|
| 54 |
-
spec = spectrogram_torch(
|
| 55 |
-
audio_norm,
|
| 56 |
-
self.filter_length,
|
| 57 |
-
self.sampling_rate,
|
| 58 |
-
self.hop_length,
|
| 59 |
-
self.win_length,
|
| 60 |
-
center=False,
|
| 61 |
-
)
|
| 62 |
-
spec = torch.squeeze(spec, 0)
|
| 63 |
-
torch.save(spec, spec_filename)
|
| 64 |
-
return spec, audio_norm
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
# 使用示例
|
| 68 |
-
processor = AudioProcessor(
|
| 69 |
-
max_wav_value=32768.0,
|
| 70 |
-
use_mel_spec_posterior=False,
|
| 71 |
-
filter_length=2048,
|
| 72 |
-
n_mel_channels=128,
|
| 73 |
-
sampling_rate=44100,
|
| 74 |
-
hop_length=512,
|
| 75 |
-
win_length=2048,
|
| 76 |
-
mel_fmin=0.0,
|
| 77 |
-
mel_fmax="null",
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
with open("filelists/train.list", "r") as f:
|
| 81 |
-
filepaths = [line.split("|")[0] for line in f] # 取每一行的第一部分作为audiopath
|
| 82 |
-
|
| 83 |
-
# 使用多进程处理
|
| 84 |
-
with Pool(processes=32) as pool: # 使用4个进程
|
| 85 |
-
with tqdm(total=len(filepaths)) as pbar:
|
| 86 |
-
for i, _ in enumerate(pool.imap_unordered(processor.process_audio, filepaths)):
|
| 87 |
-
pbar.update()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|