Skip to content
Browse files

tabs -> spacetabs, fix indentation

  • Loading branch information...
1 parent 198b6bb commit dbf0fe8e2478500341c07654ad12a4c496f495e3 Cepe committed Apr 10, 2011
Showing with 138 additions and 138 deletions.
  1. +138 −138 pysynth.py
View
276 pysynth.py
@@ -136,149 +136,149 @@
import wave, math, struct
def make_wav(song,bpm=120,transpose=0,pause=.05,boost=1.1,repeat=0,fn="out.wav"):
- f=wave.open(fn,'w')
-
- f.setnchannels(1)
- f.setsampwidth(2)
- f.setframerate(44100)
- f.setcomptype('NONE','Not Compressed')
-
- bpmfac = 120./bpm
-
- def length(l):
- return 88200./l*bpmfac
-
- def waves2(hz,l):
- a=44100./hz
- b=float(l)/44100.*hz
- return [a,round(b)]
-
- def sixteenbit(x):
- return struct.pack('h', round(32000*x))
-
- def asin(x):
- return math.sin(2.*math.pi*x)
-
- def render2(a,b,vol):
- b2 = (1.-pause)*b
- l=waves2(a,b2)
- ow=""
- q=int(l[0]*l[1])
-
- # harmonics are frequency-dependent:
- lf = math.log(a)
- lf_fac = (lf-3.) / harm_max
- if lf_fac > 1: harm = 0
- else: harm = 2. * (1-lf_fac)
- decay = 2. / lf
- t = (lf-3.) / (8.5-3.)
- volfac = 1. + .8 * t * math.cos(math.pi/5.3*(lf-3.))
-
- for x in range(q):
- fac=1.
- if x<100: fac=x/80.
- if 100<=x<300: fac=1.25-(x-100)/800.
- if x>q-400: fac=1.-((x-q+400)/400.)
- s = float(x)/float(q)
- dfac = 1. - s + s * decay
- ow=ow+sixteenbit((asin(float(x)/l[0])
- +harm*asin(float(x)/(l[0]/2.))
- +.5*harm*asin(float(x)/(l[0]/4.)))/4.*fac*vol*dfac*volfac)
- fill = max(int(ex_pos - curpos - q), 0)
- f.writeframesraw((ow)+(sixteenbit(0)*fill))
- return q + fill
-
- ##########################################################################
- # Write to output file (in WAV format)
- ##########################################################################
-
- print "Writing to file", fn
- curpos = 0
- ex_pos = 0.
- for rp in range(repeat+1):
- for nn, x in enumerate(song):
- if not nn % 4:
- print "[%u/%u]\t" % (nn+1,len(song))
- if x[0]!='r':
- if x[0][-1] == '*':
- vol = boost
- note = x[0][:-1]
- else:
- vol = 1.
- note = x[0]
- try:
- a=pitchhz[note]
- except:
- a=pitchhz[note + '4'] # default to fourth octave
- a = a * 2**transpose
- if x[1] < 0:
- b=length(-2.*x[1]/3.)
- else:
- b=length(x[1])
- ex_pos = ex_pos + b
- curpos = curpos + render2(a,b,vol)
-
- if x[0]=='r':
- b=length(x[1])
- ex_pos = ex_pos + b
- f.writeframesraw(sixteenbit(0)*int(b))
- curpos = curpos + int(b)
-
- f.writeframes('')
- f.close()
- print
+ f=wave.open(fn,'w')
+
+ f.setnchannels(1)
+ f.setsampwidth(2)
+ f.setframerate(44100)
+ f.setcomptype('NONE','Not Compressed')
+
+ bpmfac = 120./bpm
+
+ def length(l):
+ return 88200./l*bpmfac
+
+ def waves2(hz,l):
+ a=44100./hz
+ b=float(l)/44100.*hz
+ return [a,round(b)]
+
+ def sixteenbit(x):
+ return struct.pack('h', round(32000*x))
+
+ def asin(x):
+ return math.sin(2.*math.pi*x)
+
+ def render2(a,b,vol):
+ b2 = (1.-pause)*b
+ l=waves2(a,b2)
+ ow=""
+ q=int(l[0]*l[1])
+
+ # harmonics are frequency-dependent:
+ lf = math.log(a)
+ lf_fac = (lf-3.) / harm_max
+ if lf_fac > 1: harm = 0
+ else: harm = 2. * (1-lf_fac)
+ decay = 2. / lf
+ t = (lf-3.) / (8.5-3.)
+ volfac = 1. + .8 * t * math.cos(math.pi/5.3*(lf-3.))
+
+ for x in range(q):
+ fac=1.
+ if x<100: fac=x/80.
+ if 100<=x<300: fac=1.25-(x-100)/800.
+ if x>q-400: fac=1.-((x-q+400)/400.)
+ s = float(x)/float(q)
+ dfac = 1. - s + s * decay
+ ow=ow+sixteenbit((asin(float(x)/l[0])
+ +harm*asin(float(x)/(l[0]/2.))
+ +.5*harm*asin(float(x)/(l[0]/4.)))/4.*fac*vol*dfac*volfac)
+ fill = max(int(ex_pos - curpos - q), 0)
+ f.writeframesraw((ow)+(sixteenbit(0)*fill))
+ return q + fill
+
+ ##########################################################################
+ # Write to output file (in WAV format)
+ ##########################################################################
+
+ print "Writing to file", fn
+ curpos = 0
+ ex_pos = 0.
+ for rp in range(repeat+1):
+ for nn, x in enumerate(song):
+ if not nn % 4:
+ print "[%u/%u]\t" % (nn+1,len(song))
+ if x[0]!='r':
+ if x[0][-1] == '*':
+ vol = boost
+ note = x[0][:-1]
+ else:
+ vol = 1.
+ note = x[0]
+ try:
+ a=pitchhz[note]
+ except:
+ a=pitchhz[note + '4'] # default to fourth octave
+ a = a * 2**transpose
+ if x[1] < 0:
+ b=length(-2.*x[1]/3.)
+ else:
+ b=length(x[1])
+ ex_pos = ex_pos + b
+ curpos = curpos + render2(a,b,vol)
+
+ if x[0]=='r':
+ b=length(x[1])
+ ex_pos = ex_pos + b
+ f.writeframesraw(sixteenbit(0)*int(b))
+ curpos = curpos + int(b)
+
+ f.writeframes('')
+ f.close()
+ print
def mix_files(a, b, c, chann = 2, phase = -1.):
- f1 = wave.open(a,'r')
- f2 = wave.open(b,'r')
- f3 = wave.open(c,'w')
- f3.setnchannels(chann)
- f3.setsampwidth(2)
- f3.setframerate(44100)
- f3.setcomptype('NONE','Not Compressed')
- frames = min(f1.getnframes(), f2.getnframes())
-
- print "Mixing files, total length %.2f s..." % (frames / 44100.)
- d1 = f1.readframes(frames)
- d2 = f2.readframes(frames)
- for n in range(frames):
- if not n%(5*44100): print n // 44100, 's'
- if chann < 2:
- d3 = struct.pack('h',
- .5 * (struct.unpack('h', d1[2*n:2*n+2])[0] +
- struct.unpack('h', d2[2*n:2*n+2])[0]))
- else:
- d3 = ( struct.pack('h',
- phase * .3 * struct.unpack('h', d1[2*n:2*n+2])[0] +
- .7 * struct.unpack('h', d2[2*n:2*n+2])[0]) +
- struct.pack('h',
- .7 * struct.unpack('h', d1[2*n:2*n+2])[0] +
- phase * .3 * struct.unpack('h', d2[2*n:2*n+2])[0]) )
- f3.writeframesraw(d3)
- f3.close()
+ f1 = wave.open(a,'r')
+ f2 = wave.open(b,'r')
+ f3 = wave.open(c,'w')
+ f3.setnchannels(chann)
+ f3.setsampwidth(2)
+ f3.setframerate(44100)
+ f3.setcomptype('NONE','Not Compressed')
+ frames = min(f1.getnframes(), f2.getnframes())
+
+ print "Mixing files, total length %.2f s..." % (frames / 44100.)
+ d1 = f1.readframes(frames)
+ d2 = f2.readframes(frames)
+ for n in range(frames):
+ if not n%(5*44100): print n // 44100, 's'
+ if chann < 2:
+ d3 = struct.pack('h',
+ .5 * (struct.unpack('h', d1[2*n:2*n+2])[0] +
+ struct.unpack('h', d2[2*n:2*n+2])[0]))
+ else:
+ d3 = ( struct.pack('h',
+ phase * .3 * struct.unpack('h', d1[2*n:2*n+2])[0] +
+ .7 * struct.unpack('h', d2[2*n:2*n+2])[0]) +
+ struct.pack('h',
+ .7 * struct.unpack('h', d1[2*n:2*n+2])[0] +
+ phase * .3 * struct.unpack('h', d2[2*n:2*n+2])[0]) )
+ f3.writeframesraw(d3)
+ f3.close()
##########################################################################
# Synthesize demo songs
##########################################################################
if __name__ == '__main__':
- print
- print "Creating Demo Songs... (this might take about a minute)"
- print
-
- # SONG 1
- make_wav(song1, fn = "pysynth_scale.wav")
-
- # SONG 2
- make_wav(song2, bpm = 95, boost = 1.2, fn = "pysynth_anthem.wav")
-
- # SONG 3
- make_wav(song3, bpm = 132/2, pause = 0., boost = 1.1, fn = "pysynth_chopin.wav")
-
- # SONG 4
- # right hand part
- make_wav(song4_rh, bpm = 130, transpose = 1, pause = .1, boost = 1.15, repeat = 1, fn = "pysynth_bach_rh.wav")
- # left hand part
- make_wav(song4_lh, bpm = 130, transpose = 1, pause = .1, boost = 1.15, repeat = 1, fn = "pysynth_bach_lh.wav")
- # mix both files together
- mix_files("pysynth_bach_rh.wav", "pysynth_bach_lh.wav", "pysynth_bach.wav")
+ print
+ print "Creating Demo Songs... (this might take about a minute)"
+ print
+
+ # SONG 1
+ make_wav(song1, fn = "pysynth_scale.wav")
+
+ # SONG 2
+ make_wav(song2, bpm = 95, boost = 1.2, fn = "pysynth_anthem.wav")
+
+ # SONG 3
+ make_wav(song3, bpm = 132/2, pause = 0., boost = 1.1, fn = "pysynth_chopin.wav")
+
+ # SONG 4
+ # right hand part
+ make_wav(song4_rh, bpm = 130, transpose = 1, pause = .1, boost = 1.15, repeat = 1, fn = "pysynth_bach_rh.wav")
+ # left hand part
+ make_wav(song4_lh, bpm = 130, transpose = 1, pause = .1, boost = 1.15, repeat = 1, fn = "pysynth_bach_lh.wav")
+ # mix both files together
+ mix_files("pysynth_bach_rh.wav", "pysynth_bach_lh.wav", "pysynth_bach.wav")

0 comments on commit dbf0fe8

Please sign in to comment.
Something went wrong with that request. Please try again.