Skip to content

Instantly share code, notes, and snippets.

@HajimeKawahara
Last active January 4, 2016 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HajimeKawahara/a6773c44eba22e8642cb to your computer and use it in GitHub Desktop.
Save HajimeKawahara/a6773c44eba22e8642cb to your computer and use it in GitHub Desktop.
sound_driver for GCC
#!/usr/bin/python
import numpy as np
import pyaudio
import sys
import matplotlib
import matplotlib.pyplot as plt
import pylab
def harmony(freq, length, rate, tone):
samp=(2*np.pi)*freq/rate
ti=np.arange(0,rate*length,1)
harm=np.sum(np.sin((samp*np.tile(ti,(len(samp),1)).T).T),axis=0)
for i in range(0,len(tone)):
harm=harm+tone[i][0]*np.sum(np.sin(((2**i)*samp*np.tile(ti,(len(samp),1)).T).T+tone[i][1]),axis=0)
#edge damp
je=int(rate*0.002)
tj=np.array(range(0,len(harm[-je:])))
damp=10.0
if False:
fig=plt.figure()
ax=fig.add_subplot(111)
tall=np.array(range(0,len(harm)))
ax.plot(tall,harm)
ax.plot(tj,np.exp(-tj*tj/float(2*je*je/10.0)))
ax.plot(tall[-je:],harm[-je:]*np.exp(-tj*tj/float(2*je*je/damp)))
ax.plot(tall[0:je],harm[0:je]*np.exp(-(tj-je)*(tj-je)/float(2*je*je/damp)))
plt.show()
sys.exit("--")
harm[0:je]=harm[0:je]*np.exp(-(tj-je)*(tj-je)/float(2*je*je/damp))
harm[-je:]=harm[-je:]*np.exp(-tj*tj/float(2*je*je/damp))
return harm
def create_cline(cseq,tone,fA=440, length=0.2, rate=44100,vol=0.5):
f0=fA*(2**(-9/12.0)) # C as a fiducial frequiency
cline=[]
for iseq in range(0,len(cseq)):
chordiseq=f0*2**(np.array(cseq[iseq])/12.0)
cline = np.hstack([cline,harmony(chordiseq, length, rate,tone)])
cline = cline*vol
if False:
fig=plt.figure()
ax=fig.add_subplot(111)
ti=np.array(range(0,len(cline)))
ax.plot(ti,cline)
plt.show()
sys.exit("--")
return cline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment