Datasets:
Tasks:
Audio Classification
Formats:
parquet
Size:
1K - 10K
ArXiv:
Tags:
arxiv:2606.01686
music
ai-generated-music
ai-generated-music-detection
plagiarism-detection
ace-step
License:
| #!/usr/bin/env python3 | |
| """Generate C1 (concat) and C2 (crossfade) MixSet with multiple boundaries. | |
| Each track has 3-5 alternating AI/Human segments.""" | |
| 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): | |
| try: | |
| 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 | |
| except: | |
| return 0 | |
| def extract_segment(path, start, duration, out_path): | |
| 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_segments(seg_paths, gaps, out_path): | |
| """Concat multiple segments with gaps between them.""" | |
| if len(seg_paths) == 1: | |
| subprocess.run(['cp', str(seg_paths[0]), str(out_path)]) | |
| return | |
| # Build complex filter | |
| inputs = [] | |
| for s in seg_paths: | |
| inputs.extend(['-i', str(s)]) | |
| filter_parts = [] | |
| for i in range(len(seg_paths)): | |
| if i < len(seg_paths) - 1: | |
| gap = gaps[i] if i < len(gaps) else 0.2 | |
| filter_parts.append(f'[{i}:a]apad=pad_dur={gap}[a{i}]') | |
| else: | |
| filter_parts.append(f'[{i}:a]acopy[a{i}]') | |
| concat_inputs = ''.join(f'[a{i}]' for i in range(len(seg_paths))) | |
| filter_parts.append(f'{concat_inputs}concat=n={len(seg_paths)}:v=0:a=1[out]') | |
| cmd = ['ffmpeg','-y'] + inputs + [ | |
| '-filter_complex', ';'.join(filter_parts), | |
| '-map','[out]','-ar','44100','-ac','2','-ab','192k', str(out_path) | |
| ] | |
| subprocess.run(cmd, capture_output=True, timeout=120) | |
| def crossfade_segments(seg_paths, xfade_durs, out_path): | |
| """Crossfade multiple segments sequentially.""" | |
| if len(seg_paths) == 1: | |
| subprocess.run(['cp', str(seg_paths[0]), str(out_path)]) | |
| return | |
| # Chain crossfades: first two, then add next, etc. | |
| tmp_dir = Path("/tmp/xfade_tmp") | |
| tmp_dir.mkdir(exist_ok=True) | |
| current = str(seg_paths[0]) | |
| for i in range(1, len(seg_paths)): | |
| xdur = xfade_durs[i-1] if i-1 < len(xfade_durs) else 2.0 | |
| tmp_out = str(tmp_dir / f"xfade_{i}.mp3") | |
| subprocess.run([ | |
| 'ffmpeg','-y','-i',current,'-i',str(seg_paths[i]), | |
| '-filter_complex',f'[0:a][1:a]acrossfade=d={xdur}:c1=tri:c2=tri[out]', | |
| '-map','[out]','-ar','44100','-ac','2','-ab','192k', tmp_out | |
| ], capture_output=True, timeout=120) | |
| current = tmp_out | |
| subprocess.run(['cp', current, str(out_path)]) | |
| # Cleanup | |
| for f in tmp_dir.glob("xfade_*.mp3"): | |
| f.unlink() | |
| 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) | |
| ai_pool = list(ai_files) | |
| human_pool = list(human_files) | |
| random.shuffle(ai_pool) | |
| random.shuffle(human_pool) | |
| tmp_dir = Path("/tmp/mixset_segs") | |
| 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 | |
| try: | |
| # Decide number of segments (3-5) | |
| n_segments = random.randint(3, 5) | |
| ai_first = random.choice([True, False]) | |
| seg_paths = [] | |
| seg_info = [] | |
| valid = True | |
| for s in range(n_segments): | |
| is_ai = (s % 2 == 0) if ai_first else (s % 2 == 1) | |
| pool = ai_pool if is_ai else human_pool | |
| src = pool[(i * n_segments + s) % len(pool)] | |
| src_dur = get_duration(src) | |
| if src_dur < 15: | |
| valid = False | |
| break | |
| seg_len = min(random.uniform(10, 40), src_dur - 1) | |
| seg_start = random.uniform(0, max(0, src_dur - seg_len)) | |
| seg_path = tmp_dir / f"seg_{i}_{s}.mp3" | |
| extract_segment(src, seg_start, seg_len, seg_path) | |
| if not seg_path.exists() or seg_path.stat().st_size < 1000: | |
| valid = False | |
| break | |
| seg_paths.append(seg_path) | |
| seg_info.append({ | |
| "source": src.name, | |
| "type": "ai" if is_ai else "human", | |
| "start_in_source": round(seg_start, 2), | |
| "duration": round(seg_len, 2), | |
| }) | |
| if not valid or len(seg_paths) < 3: | |
| for sp in seg_paths: | |
| sp.unlink(missing_ok=True) | |
| continue | |
| # C1: Concat with gaps | |
| gaps = [round(random.uniform(0.1, 0.5), 2) for _ in range(n_segments - 1)] | |
| if not c1_mp3.exists(): | |
| concat_segments(seg_paths, gaps, c1_mp3) | |
| # Calculate boundaries | |
| boundaries = [] | |
| pos = 0 | |
| for s in range(len(seg_info)): | |
| seg_dur = get_duration(seg_paths[s]) | |
| seg_info[s]["output_start"] = round(pos, 3) | |
| seg_info[s]["output_end"] = round(pos + seg_dur, 3) | |
| pos += seg_dur | |
| if s < len(gaps): | |
| boundaries.append({ | |
| "position_sec": round(pos, 3), | |
| "gap_sec": gaps[s], | |
| "from_type": seg_info[s]["type"], | |
| "to_type": seg_info[s+1]["type"], | |
| }) | |
| pos += gaps[s] | |
| meta_c1 = { | |
| "track_id": f"C1_{i:05d}", | |
| "filename": c1_mp3.name, | |
| "method": "concat", | |
| "n_segments": len(seg_info), | |
| "n_boundaries": len(boundaries), | |
| "segments": seg_info.copy(), | |
| "boundaries": boundaries, | |
| "total_duration": round(pos, 3), | |
| } | |
| 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_durs = [round(random.uniform(1, 5), 2) for _ in range(n_segments - 1)] | |
| if not c2_mp3.exists(): | |
| crossfade_segments(seg_paths, xfade_durs, c2_mp3) | |
| # Calculate boundaries for crossfade | |
| boundaries_c2 = [] | |
| pos = 0 | |
| seg_info_c2 = [] | |
| for s in range(len(seg_info)): | |
| seg_dur = get_duration(seg_paths[s]) | |
| si = dict(seg_info[s]) | |
| si["output_start"] = round(pos, 3) | |
| si["output_end"] = round(pos + seg_dur, 3) | |
| seg_info_c2.append(si) | |
| if s < len(xfade_durs): | |
| xf_start = round(pos + seg_dur - xfade_durs[s], 3) | |
| xf_end = round(pos + seg_dur, 3) | |
| boundaries_c2.append({ | |
| "crossfade_start_sec": xf_start, | |
| "crossfade_end_sec": xf_end, | |
| "crossfade_duration": xfade_durs[s], | |
| "from_type": seg_info[s]["type"], | |
| "to_type": seg_info[s+1]["type"], | |
| }) | |
| pos += seg_dur - xfade_durs[s] | |
| else: | |
| pos += seg_dur | |
| meta_c2 = { | |
| "track_id": f"C2_{i:05d}", | |
| "filename": c2_mp3.name, | |
| "method": "crossfade", | |
| "n_segments": len(seg_info_c2), | |
| "n_boundaries": len(boundaries_c2), | |
| "segments": seg_info_c2, | |
| "boundaries": boundaries_c2, | |
| } | |
| 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 | |
| for sp in seg_paths: | |
| sp.unlink(missing_ok=True) | |
| if (i + 1) % 100 == 0: | |
| c1_n = len(list(C1_DIR.glob("*.mp3"))) | |
| c2_n = len(list(C2_DIR.glob("*.mp3"))) | |
| print(f"[{i+1}/{TARGET}] C1={c1_n} C2={c2_n}") | |
| except Exception as e: | |
| print(f"[{i}] Error: {e}") | |
| for sp in seg_paths: | |
| sp.unlink(missing_ok=True) | |
| continue | |
| print(f"Done: C1={len(list(C1_DIR.glob('*.mp3')))}, C2={len(list(C2_DIR.glob('*.mp3')))}") | |
| if __name__ == "__main__": | |
| main() | |