Advertisement
rainman002

Convert tab indentation into music

Mar 30th, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. from struct import pack
  2. from math import sin, pi
  3. import time
  4.  
  5. x=0
  6. lines=[0,0,0,0,0,0]
  7. f=[523.25,587.33,659.26,783.99,880]
  8.  
  9. def freq(i):
  10.     return f[i%len(f)] * 2**(i/len(f))
  11.  
  12. def saw(phase):
  13.     phase = phase%2
  14.     return phase-1
  15.  
  16. def tri(phase):
  17.     phase = phase%2
  18.     if(phase>1):
  19.         phase = 2-phase
  20.     return -1 + 2*phase
  21.  
  22. fout = open('output.au', 'wb')
  23. rate = 44100
  24. leng = 750
  25. phase = 0.0
  26. phase2 = 0.0
  27. dur = rate/10
  28. # header needs size, encoding=2(8 bit pcm), sampling_rate=8000, channel=1
  29. fout.write('.snd' + pack('>5L', 24, dur*leng, 2, rate, 1))
  30. for i in range(leng):
  31.     lines[x] = raw_input().count("  ");
  32.     x2 = (x+1)%len(lines)
  33.     for seg in range(dur):
  34.         #step = 2**(lines[x]/6.0) * 880 / rate
  35.         step = freq(lines[x]) / rate
  36.         phase = phase + step
  37.         #sin_seg = tri(phase)
  38.         #step = 2**(lines[x]/6.0) * 880 / rate
  39.         step = freq(lines[x2]) / rate
  40.         phase2 = phase2 + step
  41.         sin_seg = tri(phase) + tri(phase2)
  42.         fout.write(pack('b', 63 * sin_seg))
  43.     x=x2
  44. fout.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement