from pathlib import Path
import wave, struct, json
out=Path(__file__).parent
rate=24000
rows=[]
for name,zeros in [('at-zero',0),('quarter-second',6000),('one-second',24000)]:
    samples=[0]*zeros+[8000,-8000]*120+[0]*2400
    p=out/('export-start-'+name+'.wav')
    with wave.open(str(p),'wb') as w:
        w.setparams((1,2,rate,len(samples),'NONE','not compressed'))
        w.writeframes(struct.pack('<'+'h'*len(samples),*samples))
    with wave.open(str(p),'rb') as w:
        actual=struct.unpack('<'+'h'*w.getnframes(),w.readframes(w.getnframes()))
    first=next((i for i,x in enumerate(actual) if x!=0),None)
    rows.append({'file':p.name,'sample_rate':rate,'channels':1,'sample_width_bytes':2,'first_nonzero_frame':first,'leading_zero_frames':first,'leading_zero_seconds':first/rate,'intended_marker_frame':zeros,'matches_construction':first==zeros,'basis':'Authored alternating marker preceded by exact zeros; not musical onset detection or a vendor export'})
(out/'export-start-results.json').write_text(json.dumps(rows,indent=2))
print(json.dumps(rows))
