Generate Wav Files
eharetea

Generate audio files with Python for programming music!

Generate wav files with Python!

Date Created:Monday June 23rd, 2008 12:54 PM
Date Modified:Sunday August 03rd, 2008 01:18 PM

```
# File: genfile.py

import writewav
writewav.createWav( lambda x: writewav.sin(x), "file.wav", 0.5)

```

```
# File: writewav.py

import wave
from math import pi, sin, floor, cos
def quantize(f,  time = 5 ,channels = 2, framerate = 44100, amplitude = 127):
dx = 1.0 / float(framerate)
res = ""
x = 0
while x < time:
res = res + chr(int(f(x) * amplitude + amplitude)) * channels
x += dx
return res
def createWav(f, filename, time = 5 ,channels = 2, framerate = 44100, amplitude = 127):
out = wave.open(filename, "w")
out.setnchannels(channels)
out.setframerate(framerate)
out.setsampwidth(1)
out.setcomptype("NONE", "NONE")
out.writeframes(quantize(f, time, channels, framerate, amplitude))
out.close()
def dan1(x):
m = x % (2 * pi)
if m < pi / 2 or m > 3*pi/4:
return sin(m)
else:
return -2 * m / pi + 2
def square(x):
d = floor(x / pi)
m = floor(10 / pi)
if sin(cos(x*x*x)) > 0:
return d/m
else:
return -d/m
def waver(x, A, w, p):
return A*sin(2*pi*(wt-p))

```