"""Prepare a licensed live guqin recording, preserving performance and tuning."""
from pathlib import Path
import subprocess,json
root=Path(__file__).resolve().parents[1]
source=root/'assets/audio/sources/yangguan-sandie-charlie-huang.ogg'
out=root/'assets/audio/yangguan-sandie-warm-v1.mp3'
duration=float(subprocess.check_output(['ffprobe','-v','error','-show_entries','format=duration','-of','csv=p=0',str(source)]))
# Gentle presence reduction; no added oscillators, quantization, pitch shifting or artificial reverb.
filters=f'highpass=f=55,equalizer=f=2800:t=q:w=0.8:g=-4,highshelf=f=4200:g=-5,lowpass=f=7500,loudnorm=I=-23:TP=-3:LRA=14,afade=t=in:d=2,afade=t=out:st={duration-5}:d=5'
subprocess.run(['ffmpeg','-y','-v','error','-i',str(source),'-af',filters,'-c:a','libmp3lame','-b:a','160k',str(out)],check=True)
meta={'title':'阳关三叠','performer':'Charlie Huang (Charles R Tsua)','recorded':'2013-10-06, Shaoyun Xuan, Birmingham, United Kingdom','instrument':'Live guqin with nylon strings','source':'https://commons.wikimedia.org/wiki/File:Guqin-Yangguan_Sandie.ogg','license':'CC BY-SA 3.0','licenseUrl':'https://creativecommons.org/licenses/by-sa/3.0/','changes':'Gentle EQ, loudness adjustment, 2-second entrance and 5-second ending fade, MP3 conversion. No synthesis or AI generation. Adapted audio licensed CC BY-SA 3.0.','durationSeconds':duration,'file':out.name}
(root/'assets/audio/score.json').write_text(json.dumps(meta,ensure_ascii=False,indent=2)+'\n')
print(json.dumps(meta,ensure_ascii=False))
