-
-
Save phaeta/feb192a321c3d97be96b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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