Skip to content

Instantly share code, notes, and snippets.

@shen72
Created November 22, 2012 13:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shen72/4131157 to your computer and use it in GitHub Desktop.
Save shen72/4131157 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import wave, sys
import numpy as np
import scipy.signal as signal
import math
from mpmath import *
frq_tab = [261.6, 293.6, 329.6, 349.2, 392, 440, 493.8]
framerate = 44100
siglen = 100
seednum = 5
def gen_signal(frq_id, timelen):
t = np.arange(0, timelen, 1.0/framerate)
data = signal.chirp(t, frq_tab[frq_id], timelen, frq_tab[frq_id]) * 10000
return data.astype(np.short).tostring()
wf = wave.open(sys.argv[1], 'wb')
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(framerate)
mp.prec = siglen + 10
mp.dps = siglen + 10
num = mpf(2) ** mpf(1.0/12)
num = mpf(seednum) ** num
while num < 0.1:
num *= 10
while num > 1:
num /= 10
last = 1
bank = str(num)[2:]
for i in range(0, min(siglen, len(bank))):
curr = int(bank[i])
if curr >= 1 and curr <= 7:
wf.writeframes(gen_signal(curr - 1, 1.0/4))
last = curr
else:
wf.writeframes(gen_signal(last - 1, 1.0/4))
wf.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment