from numpy import *
%matplotlib notebook
from matplotlib.pyplot import *
import audio.wave
from IPython.display import Audio
T = 3.0
df = 44100
dt = 1.0 / 44100
f = 440.0
t = r_[0.0:T:dt]
t
array([0.00000000e+00, 2.26757370e-05, 4.53514739e-05, ..., 2.99993197e+00, 2.99995465e+00, 2.99997732e+00])
x = sin(2*pi*f*t)
v = r_[0:10]
v < 5
array([ True, True, True, True, True, False, False, False, False, False])
tf = t[t < 20 / 1000.0] # 20 ms
xf = x[t < 20 / 1000.0]
fig, axes = subplots()
axes.plot(tf, xf, "+")
axes.grid()
audio.wave.write(10*x, "A4.wav")
Audio("A4.wav")
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
for i in range(0,11):
x = make_tone(i)
display(mean(x * x), 27.5 * 2 ** i)
display(Audio("A" + str(i) + ".wav"))
0.5
27.5
0.5
55.0
0.5
110.0
0.4999999999999999
220.0
0.4999999999999999
440.0
0.5
880.0
0.4999999999999998
1760.0
0.5000000000000001
3520.0
0.5
7040.0
0.5000000000000003
14080.0
0.5000000000000026
28160.0
x = make_tone(n=20)
Audio("A20.wav")
27.5 * (2 ** 100)
f, a = subplots()
a.plot(x)
[<matplotlib.lines.Line2D at 0x7fa471af2b90>]
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()
<matplotlib.legend.Legend at 0x7fa47146b5d0>
mean(abs(A4 - A4q))
7.62163715695231e-06
raw = open("A4.wav").read()
raw[:512]
'RIFF\xbc\t\x04\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00D\xac\x00\x00\x88X\x01\x00\x02\x00\x10\x00data\x98\t\x04\x00\x00\x00\x05\x08\x02\x10\xee\x17\xc3\x1fx\'\x05/b6\x89=rD\x17KpQvW%]vbeg\xebk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9bZ\x18`4e\xeai5n\x12r|upx\xeaz\xe9|k~m\x7f\xef\x7f\xf1\x7fq\x7fr~\xf3|\xf7z\x7fx\x8eu\'r'
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()
RIFF WAVE fmt 1 44100