from pathlib import Path
import wave,json,statistics
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)]
r=[]
for n in ['centered.wav','dc-offset.wav','partial-offset.wav','mean-removed.wav']:
 _,x=readpcm(Path(__file__).parent/n);r.append({'file':n,'mean':statistics.mean(x),'first_half_mean':statistics.mean(x[:2400]),'second_half_mean':statistics.mean(x[2400:])})
assert abs(r[1]['mean']-.08)<.0001 and abs(r[3]['mean'])<.0001;assert r[2]['second_half_mean']-r[2]['first_half_mean']>.079
print(json.dumps(r,indent=2))
