Skip to content

Commit

Permalink
remove redundent spec_gen and fix related bug
Browse files Browse the repository at this point in the history
  • Loading branch information
leng-yue committed Mar 24, 2023
1 parent 2854013 commit 32cfec7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
2 changes: 2 additions & 0 deletions data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def get_audio(self, filename):
audio_norm = audio / self.max_wav_value
audio_norm = audio_norm.unsqueeze(0)
spec_filename = filename.replace(".wav", ".spec.pt")

# Ideally, all data generated after Mar 25 should have .spec.pt
if os.path.exists(spec_filename):
spec = torch.load(spec_filename)
else:
Expand Down
53 changes: 46 additions & 7 deletions preprocess_hubert_f0.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import torch
from glob import glob
from tqdm import tqdm
from modules.mel_processing import spectrogram_torch

import utils
import logging
logging.getLogger('numba').setLevel(logging.WARNING)

logging.getLogger("numba").setLevel(logging.WARNING)
import librosa
import numpy as np

Expand All @@ -29,11 +31,42 @@ def process_one(filename, hmodel):
wav16k = torch.from_numpy(wav16k).to(device)
c = utils.get_hubert_content(hmodel, wav_16k_tensor=wav16k)
torch.save(c.cpu(), soft_path)

f0_path = filename + ".f0.npy"
if not os.path.exists(f0_path):
f0 = utils.compute_f0_dio(wav, sampling_rate=sampling_rate, hop_length=hop_length)
f0 = utils.compute_f0_dio(
wav, sampling_rate=sampling_rate, hop_length=hop_length
)
np.save(f0_path, f0)

spec_path = filename.replace(".wav", ".spec.pt")
if not os.path.exists(spec_path):
# Process spectrogram
# The following code can't be replaced by torch.FloatTensor(wav)
# because load_wav_to_torch return a tensor that need to be normalized

audio, sr = utils.load_wav_to_torch(filename)
if sr != hps.data.sampling_rate:
raise ValueError(
"{} SR doesn't match target {} SR".format(
sr, hps.data.sampling_rate
)
)

audio_norm = audio / hps.data.max_wav_value
audio_norm = audio_norm.unsqueeze(0)

spec = spectrogram_torch(
audio_norm,
hps.data.filter_length,
hps.data.sampling_rate,
hps.data.hop_length,
hps.data.win_length,
center=False,
)
spec = torch.squeeze(spec, 0)
torch.save(spec, spec_path)


def process_batch(filenames):
print("Loading hubert for content...")
Expand All @@ -46,17 +79,23 @@ def process_batch(filenames):

if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--in_dir", type=str, default="dataset/44k", help="path to input dir")
parser.add_argument(
"--in_dir", type=str, default="dataset/44k", help="path to input dir"
)

args = parser.parse_args()
filenames = glob(f'{args.in_dir}/*/*.wav', recursive=True) # [:10]
filenames = glob(f"{args.in_dir}/*/*.wav", recursive=True) # [:10]
shuffle(filenames)
multiprocessing.set_start_method('spawn',force=True)
multiprocessing.set_start_method("spawn", force=True)

num_processes = 1
chunk_size = int(math.ceil(len(filenames) / num_processes))
chunks = [filenames[i:i + chunk_size] for i in range(0, len(filenames), chunk_size)]
chunks = [
filenames[i : i + chunk_size] for i in range(0, len(filenames), chunk_size)
]
print([len(c) for c in chunks])
processes = [multiprocessing.Process(target=process_batch, args=(chunk,)) for chunk in chunks]
processes = [
multiprocessing.Process(target=process_batch, args=(chunk,)) for chunk in chunks
]
for p in processes:
p.start()
19 changes: 0 additions & 19 deletions spec_gen.py

This file was deleted.

0 comments on commit 32cfec7

Please sign in to comment.