Skip to content

Instantly share code, notes, and snippets.

@benj02
Created January 14, 2014 07:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save benj02/8414717 to your computer and use it in GitHub Desktop.
Save benj02/8414717 to your computer and use it in GitHub Desktop.
import wave, struct, sys
from math import pow, sin
samples = 100000
terms = 50
normalizer = 32768/1.5
weier = wave.open('weierstrass.wav', 'w')
weier.setparams((1, 2, 44100, 0, 'NONE', 'not compressed'))
print "Progress (%):",
for i in range(samples):
x = float(1000*i)/samples
value = normalizer * reduce(lambda sum,n:sum+pow(2,-n)*sin(pow(2,n)*x), range(terms), 0)
if i%5000==0: print int(100*float(i)/samples),; sys.stdout.flush()
packed_value = struct.pack('h', int(value))
weier.writeframes(packed_value)
weier.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment