Skip to content

Instantly share code, notes, and snippets.

@ludwig
Forked from benj02/Weirerstrass
Created May 8, 2014 22:07
Show Gist options
  • Save ludwig/4277feb6af02e680e9ed to your computer and use it in GitHub Desktop.
Save ludwig/4277feb6af02e680e9ed 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