from pathlib import Path
import wave,json
def readpcm(path):
 with wave.open(str(path),'rb') as w:
  rate=w.getframerate();n=w.getnframes();width=w.getsampwidth();b=w.readframes(n)
 return rate,[int.from_bytes(b[i:i+width],'little',signed=True)/(2**(width*8-1)-1) for i in range(0,len(b),width)]
rate,x=readpcm(Path(__file__).parent/'quiet-onset.wav');r=[]
for t in [.001,.01]:
 i=next(i for i,v in enumerate(x) if abs(v)>t);r.append({'absolute_threshold':t,'first_sample_above':i,'seconds':i/rate})
assert r[0]['seconds']<.26 and r[1]['seconds']>.35
print(json.dumps(r,indent=2))
