HAIM / scripts /generation /gen_mixset_c_v1.py
seonghyeongo's picture
scripts: add generation pipeline (A2/B/C) with secrets removed
b347b70 verified
Raw
History Blame Contribute Delete
7.39 kB
#!/usr/bin/env python3
"""Generate C1 (concat) and C2 (crossfade) MixSet from ACE-Step + MTG."""
import json, os, random, subprocess, time
from pathlib import Path
ACESTEP_DIR = Path("/ssd_data/dataset/haim_dataset/fake/acestep/samples")
MTG_DIR = Path("/ssd_data/dataset/haim_dataset/real/MTG")
C1_DIR = Path("/ssd_data/dataset/haim_dataset/C_mixing/C1_mixset_concat")
C2_DIR = Path("/ssd_data/dataset/haim_dataset/C_mixing/C2_mixset_crossfade")
TARGET = 6000
def get_duration(path):
r = subprocess.run(['ffprobe','-v','quiet','-show_entries','format=duration',
'-of','csv=p=0', str(path)], capture_output=True, text=True, timeout=5)
return float(r.stdout.strip()) if r.stdout.strip() else 0
def get_segment(path, start, duration, out_path):
"""Extract segment from audio file."""
subprocess.run([
'ffmpeg','-y','-i',str(path),'-ss',str(start),'-t',str(duration),
'-ar','44100','-ac','2','-ab','192k', str(out_path)
], capture_output=True, timeout=60)
def concat_with_gap(seg1, seg2, gap, out_path):
"""Concatenate two segments with silent gap."""
subprocess.run([
'ffmpeg','-y','-i',str(seg1),'-i',str(seg2),
'-filter_complex',
f'[0:a]apad=pad_dur={gap}[a0];[a0][1:a]concat=n=2:v=0:a=1[out]',
'-map','[out]','-ar','44100','-ac','2','-ab','192k', str(out_path)
], capture_output=True, timeout=60)
def crossfade_mix(seg1, seg2, xfade_dur, out_path):
"""Crossfade two segments."""
subprocess.run([
'ffmpeg','-y','-i',str(seg1),'-i',str(seg2),
'-filter_complex',
f'[0:a][1:a]acrossfade=d={xfade_dur}:c1=tri:c2=tri[out]',
'-map','[out]','-ar','44100','-ac','2','-ab','192k', str(out_path)
], capture_output=True, timeout=60)
def main():
random.seed(42)
ai_files = sorted(ACESTEP_DIR.glob("*.mp3"))
human_files = sorted(MTG_DIR.glob("*.mp3"))
print(f"AI: {len(ai_files)}, Human: {len(human_files)}")
C1_DIR.mkdir(parents=True, exist_ok=True)
C2_DIR.mkdir(parents=True, exist_ok=True)
# Shuffle and pair
ai_pool = list(ai_files)
human_pool = list(human_files)
random.shuffle(ai_pool)
random.shuffle(human_pool)
tmp_dir = Path("/tmp/mixset_tmp")
tmp_dir.mkdir(exist_ok=True)
for i in range(TARGET):
c1_mp3 = C1_DIR / f"C1_{i:05d}.mp3"
c2_mp3 = C2_DIR / f"C2_{i:05d}.mp3"
if c1_mp3.exists() and c2_mp3.exists():
continue
ai_src = ai_pool[i % len(ai_pool)]
human_src = human_pool[i % len(human_pool)]
try:
ai_dur = get_duration(ai_src)
human_dur = get_duration(human_src)
if ai_dur < 20 or human_dur < 20:
continue
# Random segment lengths (15-60s)
ai_seg_len = min(random.uniform(15, 60), ai_dur - 1)
human_seg_len = min(random.uniform(15, 60), human_dur - 1)
ai_start = random.uniform(0, max(0, ai_dur - ai_seg_len))
human_start = random.uniform(0, max(0, human_dur - human_seg_len))
# Extract segments
seg_ai = tmp_dir / f"ai_{i}.mp3"
seg_human = tmp_dir / f"human_{i}.mp3"
get_segment(ai_src, ai_start, ai_seg_len, seg_ai)
get_segment(human_src, human_start, human_seg_len, seg_human)
if not seg_ai.exists() or not seg_human.exists():
continue
# Randomize order (AI first or Human first)
ai_first = random.choice([True, False])
seg1 = seg_ai if ai_first else seg_human
seg2 = seg_human if ai_first else seg_ai
order = "ai_first" if ai_first else "human_first"
# C1: Concat with gap
gap = round(random.uniform(0.1, 0.5), 2)
if not c1_mp3.exists():
concat_with_gap(seg1, seg2, gap, c1_mp3)
seg1_dur = get_duration(seg1)
boundary = round(seg1_dur + gap, 3)
meta_c1 = {
"track_id": f"C1_{i:05d}",
"filename": c1_mp3.name,
"method": "concat",
"order": order,
"gap_sec": gap,
"segment_1": {
"source": ai_src.name if ai_first else human_src.name,
"type": "ai" if ai_first else "human",
"start": round(ai_start if ai_first else human_start, 2),
"duration": round(ai_seg_len if ai_first else human_seg_len, 2),
},
"segment_2": {
"source": human_src.name if ai_first else ai_src.name,
"type": "human" if ai_first else "ai",
"start": round(human_start if ai_first else ai_start, 2),
"duration": round(human_seg_len if ai_first else ai_seg_len, 2),
},
"boundary_sec": boundary,
}
with open(C1_DIR / f"C1_{i:05d}.json", "w", encoding="utf-8") as f:
json.dump(meta_c1, f, ensure_ascii=False, indent=2)
# C2: Crossfade
xfade = round(random.uniform(1, 5), 2)
if not c2_mp3.exists():
crossfade_mix(seg1, seg2, xfade, c2_mp3)
seg1_dur = get_duration(seg1)
boundary_start = round(seg1_dur - xfade, 3)
boundary_end = round(seg1_dur, 3)
meta_c2 = {
"track_id": f"C2_{i:05d}",
"filename": c2_mp3.name,
"method": "crossfade",
"order": order,
"crossfade_sec": xfade,
"segment_1": {
"source": ai_src.name if ai_first else human_src.name,
"type": "ai" if ai_first else "human",
"start": round(ai_start if ai_first else human_start, 2),
"duration": round(ai_seg_len if ai_first else human_seg_len, 2),
},
"segment_2": {
"source": human_src.name if ai_first else ai_src.name,
"type": "human" if ai_first else "ai",
"start": round(human_start if ai_first else ai_start, 2),
"duration": round(human_seg_len if ai_first else ai_seg_len, 2),
},
"boundary_start_sec": boundary_start,
"boundary_end_sec": boundary_end,
}
with open(C2_DIR / f"C2_{i:05d}.json", "w", encoding="utf-8") as f:
json.dump(meta_c2, f, ensure_ascii=False, indent=2)
# Cleanup tmp
seg_ai.unlink(missing_ok=True)
seg_human.unlink(missing_ok=True)
if (i + 1) % 100 == 0:
c1_count = len(list(C1_DIR.glob("*.mp3")))
c2_count = len(list(C2_DIR.glob("*.mp3")))
print(f"[{i+1}/{TARGET}] C1={c1_count} C2={c2_count}")
except Exception as e:
print(f"[{i}] Error: {e}")
continue
c1_final = len(list(C1_DIR.glob("*.mp3")))
c2_final = len(list(C2_DIR.glob("*.mp3")))
print(f"Done: C1={c1_final}, C2={c2_final}")
if __name__ == "__main__":
main()