#!/usr/bin/env python # coding: utf-8 # In[17]: from numpy import * get_ipython().run_line_magic('matplotlib', 'notebook') from matplotlib.pyplot import * import audio.wave from IPython.display import Audio # In[5]: T = 3.0 df = 44100 dt = 1.0 / 44100 f = 440.0 # In[3]: t = r_[0.0:T:dt] t # In[6]: x = sin(2*pi*f*t) # In[15]: v = r_[0:10] v < 5 # In[9]: tf = t[t < 20 / 1000.0] # 20 ms xf = x[t < 20 / 1000.0] # In[12]: fig, axes = subplots() axes.plot(tf, xf, "+") axes.grid() # In[27]: audio.wave.write(10*x, "A4.wav") Audio("A4.wav") # In[28]: def make_tone(n=4): f = 440.0 * 2 ** (n - 4) x = sin(2*pi*f*t) filename = "A" + str(n) + ".wav" audio.wave.write(x, filename) return x # In[32]: for i in range(0,11): x = make_tone(i) display(mean(x * x), 27.5 * 2 ** i) display(Audio("A" + str(i) + ".wav")) # In[41]: x = make_tone(n=20) Audio("A20.wav") # In[39]: 27.5 * (2 ** 100) f, a = subplots() a.plot(x) # In[48]: A4 = make_tone(n=4) A4q = audio.wave.read("A4.wav")[0] fig, axes = subplots() axes.plot(A4[:512], "b+", alpha=0.5, label="original") axes.plot(A4q[:512], "g+", alpha=0.5, label="write/read") axes.legend() # In[49]: mean(abs(A4 - A4q)) # In[51]: raw = open("A4.wav").read() raw[:512] # In[62]: from bitstream import * import warnings; warnings.simplefilter('ignore', DeprecationWarning) stream = BitStream(raw[:512]) print stream.read(str, 4) _ = stream.read(str, 4) print stream.read(str, 4) print stream.read(str, 4) _ = stream.read(str, 6) print stream.read(uint16).newbyteorder() # LE <-> BE print stream.read(uint32).newbyteorder()