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))
Downloads:
Download: genfile.py 104 B
Download: writewav.py 916 B
Please login or Click Here to register for downloads
Generate Wav Files by Dan Lynch
is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
Based on a work at www.3daet.com
Permissions beyond the scope of this license may be available at http://www.3daet.com
