|
|
import os |
|
|
import subprocess |
|
|
import glob |
|
|
import sys |
|
|
from tqdm import tqdm |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BUILD_SCRIPT_DIR = r"D:\DiffSingerDatasets\MakeDiffSinger\acoustic_forced_alignment" |
|
|
BUILD_SCRIPT_PATH = os.path.join(BUILD_SCRIPT_DIR, "build_dataset.py") |
|
|
|
|
|
|
|
|
|
|
|
ADD_SCRIPT_PATH = r"D:\DiffSingerDatasets\MakeDiffSinger\variance-temp-solution\add_ph_num.py" |
|
|
|
|
|
|
|
|
BATCH_INFER_SCRIPT_PATH = r"D:\DiffSingerDatasets\MakeDiffSinger\SOME\batch_infer.py" |
|
|
|
|
|
|
|
|
SOURCE_PROCESSED_ROOT = r"D:\DiffSingerDatasets\m4singer_processed" |
|
|
|
|
|
|
|
|
OUTPUT_DATASET_ROOT = r"D:\DiffSingerDatasets\m4singer_dataset" |
|
|
|
|
|
|
|
|
DICTIONARY_PATH = r"D:\DiffSingerDatasets\SOFA\dictionary\opencpop-extension.txt" |
|
|
|
|
|
|
|
|
MODEL_CKPT_PATH = r"D:\DiffSingerDatasets\MakeDiffSinger\SOME\pretrained\0119_continuous256_5spk\model_ckpt_steps_100000_simplified.ckpt" |
|
|
|
|
|
|
|
|
CSV_FILENAME = "transcriptions.csv" |
|
|
|
|
|
|
|
|
PYTHON_EXECUTABLE = sys.executable |
|
|
print(f"Using Python executable: {PYTHON_EXECUTABLE}") |
|
|
print("-" * 50) |
|
|
|
|
|
|
|
|
def _run_command(command, description, cwd=None): |
|
|
"""Helper function to run a subprocess command and print output.""" |
|
|
print(f"\nRunning: {description}") |
|
|
print(f"Command: {' '.join(command)}") |
|
|
if cwd: |
|
|
print(f"Working Directory: {cwd}") |
|
|
print("-" * 20) |
|
|
|
|
|
try: |
|
|
|
|
|
result = subprocess.run(command, capture_output=True, text=True, check=False, cwd=cwd) |
|
|
|
|
|
|
|
|
if result.stdout: |
|
|
print("--- STDOUT ---") |
|
|
print(result.stdout) |
|
|
if result.stderr: |
|
|
print("--- STDERR ---") |
|
|
print(result.stderr) |
|
|
|
|
|
|
|
|
if result.returncode != 0: |
|
|
print(f"!!! Command failed: {description} exited with code {result.returncode}") |
|
|
return False, result.returncode |
|
|
else: |
|
|
print(f"--- Command successful: {description} ---") |
|
|
return True, result.returncode |
|
|
|
|
|
except FileNotFoundError: |
|
|
print(f"!!! Error: Script or executable not found: {command[0]}") |
|
|
print("Please check if the script paths and Python executable path are correct.") |
|
|
return False, -1 |
|
|
except Exception as e: |
|
|
print(f"!!! An unexpected error occurred while running {description}: {e}") |
|
|
return False, -2 |
|
|
|
|
|
|
|
|
def process_single_singer(singer_dir, output_root, dictionary_path, model_ckpt_path, python_exec_path): |
|
|
""" |
|
|
为单个歌手目录执行完整的建库流程。 |
|
|
|
|
|
Args: |
|
|
singer_dir (str): 歌手的源数据目录 (e.g., D:\DiffSingerDatasets\m4singer_processed\Alto-1) |
|
|
output_root (str): 输出数据集的根目录 (e.g., D:\DiffSingerDatasets\m4singer_dataset) |
|
|
dictionary_path (str): 字典文件路径 |
|
|
model_ckpt_path (str): 模型 checkpoint 文件路径 |
|
|
python_exec_path (str): 用于执行外部脚本的 Python 解释器路径 |
|
|
""" |
|
|
singer_name = os.path.basename(singer_dir) |
|
|
source_wav_dir = os.path.join(singer_dir, "wav") |
|
|
source_tg_dir = os.path.join(singer_dir, "TextGrid") |
|
|
|
|
|
|
|
|
|
|
|
target_dataset_name = f"{singer_name}-MAN" |
|
|
target_dataset_path = os.path.join(output_root, target_dataset_name) |
|
|
|
|
|
print(f"\n{'='*60}") |
|
|
print(f"=== Processing Singer: {singer_name} ===") |
|
|
print(f" Source Wavs: {source_wav_dir}") |
|
|
print(f" Source TextGrids: {source_tg_dir}") |
|
|
print(f" Target Dataset Dir: {target_dataset_path}") |
|
|
print(f"{'='*60}\n") |
|
|
|
|
|
|
|
|
print("\n--- Step 1: Running build_dataset.py ---") |
|
|
build_command = [ |
|
|
python_exec_path, |
|
|
BUILD_SCRIPT_PATH, |
|
|
"--wavs", source_wav_dir, |
|
|
"--tg", source_tg_dir, |
|
|
"--dataset", target_dataset_path |
|
|
] |
|
|
success, _ = _run_command(build_command, "build_dataset.py") |
|
|
|
|
|
if not success: |
|
|
print(f"\n!!! Skipping add_ph_num.py and batch_infer.py for {singer_name} due to build_dataset.py failure.") |
|
|
return |
|
|
|
|
|
|
|
|
print("\n--- Step 2: Running add_ph_num.py ---") |
|
|
csv_path = os.path.join(target_dataset_path, CSV_FILENAME) |
|
|
if not os.path.exists(csv_path): |
|
|
print(f"\n!!! Error: {CSV_FILENAME} not found at {csv_path} after build_dataset.py. Skipping add_ph_num.py and batch_infer.py for {singer_name}.") |
|
|
return |
|
|
|
|
|
add_command = [ |
|
|
python_exec_path, |
|
|
ADD_SCRIPT_PATH, |
|
|
csv_path, |
|
|
"--dictionary", dictionary_path |
|
|
] |
|
|
success, _ = _run_command(add_command, "add_ph_num.py") |
|
|
|
|
|
if not success: |
|
|
print(f"\n!!! Skipping batch_infer.py for {singer_name} due to add_ph_num.py failure.") |
|
|
return |
|
|
|
|
|
|
|
|
print("\n--- Step 3: Running batch_infer.py (Pitch Inference) ---") |
|
|
|
|
|
if not os.path.exists(model_ckpt_path): |
|
|
print(f"\n!!! Error: Model checkpoint not found at {model_ckpt_path}. Skipping batch_infer.py for {singer_name}.") |
|
|
return |
|
|
|
|
|
infer_command = [ |
|
|
python_exec_path, |
|
|
BATCH_INFER_SCRIPT_PATH, |
|
|
"--model", model_ckpt_path, |
|
|
"--dataset", target_dataset_path, |
|
|
"--overwrite" |
|
|
] |
|
|
success, _ = _run_command(infer_command, "batch_infer.py") |
|
|
|
|
|
if not success: |
|
|
print(f"\n!!! Pitch inference failed for {singer_name}.") |
|
|
else: |
|
|
print(f"\n=== Successfully processed all steps for Singer: {singer_name} ===") |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
|
|
if not os.path.exists(BUILD_SCRIPT_PATH): |
|
|
print(f"Error: build_dataset.py not found at {BUILD_SCRIPT_PATH}. Exiting.") |
|
|
sys.exit(1) |
|
|
if not os.path.exists(ADD_SCRIPT_PATH): |
|
|
print(f"Error: add_ph_num.py not found at {ADD_SCRIPT_PATH}. Exiting.") |
|
|
sys.exit(1) |
|
|
if not os.path.exists(BATCH_INFER_SCRIPT_PATH): |
|
|
print(f"Error: batch_infer.py not found at {BATCH_INFER_SCRIPT_PATH}. Exiting.") |
|
|
sys.exit(1) |
|
|
if not os.path.exists(DICTIONARY_PATH): |
|
|
print(f"Error: Dictionary file not found at {DICTIONARY_PATH}. Exiting.") |
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
if not os.path.exists(MODEL_CKPT_PATH): |
|
|
print(f"Warning: Model checkpoint not found at {MODEL_CKPT_PATH}. Pitch inference step will be skipped for all singers.") |
|
|
|
|
|
|
|
|
|
|
|
singer_directories = glob.glob(os.path.join(SOURCE_PROCESSED_ROOT, "*")) |
|
|
|
|
|
|
|
|
singer_directories = [d for d in singer_directories if os.path.isdir(d)] |
|
|
|
|
|
if not singer_directories: |
|
|
print(f"No singer directories found in {SOURCE_PROCESSED_ROOT}. Please check the path.") |
|
|
else: |
|
|
print(f"Found {len(singer_directories)} singer directories to process.") |
|
|
print("-" * 50) |
|
|
|
|
|
|
|
|
|
|
|
for singer_dir in tqdm(singer_directories, desc="Overall Dataset Building", leave=True): |
|
|
|
|
|
process_single_singer(singer_dir, OUTPUT_DATASET_ROOT, DICTIONARY_PATH, MODEL_CKPT_PATH, PYTHON_EXECUTABLE) |
|
|
print("\n" + "="*60 + "\n") |
|
|
|
|
|
print("\n" + "="*60) |
|
|
print("=== Finished processing all singers ===") |
|
|
print("="*60) |