Skip to content

Instantly share code, notes, and snippets.

@phaeta
Forked from benj02/Weirerstrass
Last active August 29, 2015 14:01
Show Gist options
  • Save phaeta/feb192a321c3d97be96b to your computer and use it in GitHub Desktop.
Save phaeta/feb192a321c3d97be96b to your computer and use it in GitHub Desktop.
# Python 3 version
import wave, struct, sys
from math import pow, sin
from functools import reduce
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 (%):', end='')
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), end=' ')
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