"""Reproduce the authored practice files and timestamps. Python standard library only.
Run in an empty output directory: python grid-drift-reproduce.py
This is a mathematical exercise, not an AI generator or vendor test.
"""
from pathlib import Path
import math,struct,wave,csv
out=Path.cwd();rate=24000;frames=17*rate
pulse=[int(12000*(1-k/240)*math.cos(2*math.pi*1000*k/rate)) for k in range(240)]
rows=[]
for kind in ['fixed','drifting']:
 samples=[0]*frames
 for n in range(33):
  expected=.5*n;t=expected if kind=='fixed' else expected-.0005*n*(n-1)
  frame=round(t*rate);observed=frame/rate;samples[frame:frame+len(pulse)]=pulse
  rows.append({'case':kind,'elapsed_beat_index':n,'click_number':n+1,'expected_seconds':round(expected,9),'constructed_seconds':round(t,9),'rendered_onset_frame':frame,'observed_seconds':round(observed,9),'error_seconds':round(observed-expected,9),'error_ms':round((observed-expected)*1000,6)})
 with wave.open(str(out/f'grid-drift-{kind}.wav'),'wb') as w:
  w.setparams((1,2,rate,frames,'NONE','not compressed'));w.writeframes(struct.pack('<'+'h'*frames,*samples))
with (out/'grid-drift-timestamps.csv').open('w',newline='',encoding='utf-8') as f:
 w=csv.DictWriter(f,fieldnames=list(rows[0]));w.writeheader();w.writerows(rows)
print('Authored practice files and 66 timestamp rows written to',out)
