Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #! /usr/bin/python
  2.  
  3. ## IMPORTANT NOTE:
  4. ## This Code is wrtten just to make the organ work (in an abstract sense).
  5. ## Its a bit poor and could do with a lot of work to make it respectable.
  6.  
  7. # http://home.arcor.de/mdoege/pysynth/
  8. # http://abcnotation.com
  9. # http://abc.sourceforge.net/
  10. # http://abcnotation.com/tunePage?a=abc.sourceforge.net/NMD/nmd/jigs.txt/0277
  11.  
  12. import sys
  13. from time import sleep
  14. from quick2wire.gpio import Pin # see: http://quick2wire.com/2012/05/quick2wire-python-api-released/
  15.  
  16. music = """X: 278
  17. T:I Do Like To Be Beside The Seaside % Nottingham Music Database
  18. S:Toby Bennett, via EF
  19. M:6/8
  20. K:C
  21. g2^g |"C"a3 g2e|"C"d2c B2c|"G7"g3 g3|"G7"g3 g2^g|"C"a3 g2e|"C"d2c B2c|"F"a3 a3\
  22. |
  23. "F"a3 a2^a|"G7"b3 a2f|"G7"e2d ^c2d|"C"a3 g3|"C"f3 e2^d|"D7"e3 d3|"D7"e3 d^cd|\
  24. "G7"e3 d3|"G7"B3 g2^g|
  25. "C"a3 g2e|"C"d2c B2c|"G7"g3 g3|"G7"g3 g2^g|"C"a3 g2e|"C"d2c B2c|"F"a3 a3|\
  26. "F"a3 a2^a|
  27. "F#7"b2^a b2a|"B7"b3 a2f|"Em"a2g a2g|"A7"a2g f2e|"D7"d3 a3|"G7"a2e d2e|\
  28. "C"c3 c3|c3 ||"""
  29.  
  30. notes = {}
  31. notes['G'] = [1,0,0,0,0,0,0]
  32. notes['F'] = [0,1,0,0,0,0,0]
  33. notes['E'] = [0,0,1,0,0,0,0]
  34. notes['D'] = [0,0,0,1,0,0,0]
  35. notes['C'] = [0,0,0,0,1,0,0]
  36. notes['B'] = [0,0,0,0,0,1,0]
  37. notes['A'] = [0,0,0,0,0,0,1]
  38. notes[' '] = [0,0,0,0,0,0,0]
  39. noteLen = 0.01
  40. pins = [Pin(n, Pin.Out) for n in [11, 13, 15, 19, 21, 23, 25]]
  41.  
  42. # GPIO PIN
  43. #   17         11
  44. #   21         13
  45. #   22         15
  46. #   10         19
  47. #   9          21
  48. #   11         23
  49. #   7          25
  50.  
  51. def playScales():
  52.    
  53.     note = ['A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",
  54.             'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",
  55.             'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",
  56.             'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",
  57.             'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," "]
  58.     data = []
  59.     for n in note:
  60.         data.append([n,noteLen])
  61.        
  62.     abc = {}
  63.     abc['data'] = data
  64.     return abc
  65.  
  66. def decodeABCsong(abc):
  67.     data = []
  68.     #split the song into bars
  69.     song = abc['song'].split("|")
  70.     allNotes = "abcdefgABCDEFG"
  71.  
  72.     for bar in song:
  73.         if len(bar) < 1:
  74.             continue
  75.  
  76.         # remove chords
  77.         print bar
  78.         if bar[0] == '"':
  79.             bpos = bar.find('"',1) + 1
  80.             bar = bar[bpos:]
  81.  
  82.         bar = bar.replace(" ","")   # remove spaces
  83.         bar = bar.replace("^","")   # remove sharps
  84.         barLen = len(bar)
  85.         bar = bar.ljust(10," ")     # put some spaces at the end so the loop doesn't fail
  86.        
  87.         for x in range(0, barLen):
  88.             if bar[x+1].isdigit():
  89.                 length = noteLen + float("0.0"+bar[x+1])
  90.             else:
  91.                 length = noteLen
  92.  
  93.             if bar[x] in allNotes:
  94.                 note = bar[x]
  95.                 data.append([note, length])
  96.            
  97.     abc['data'] = data
  98.     return abc
  99.  
  100. def decodeABCstring(music):
  101.  
  102.     # http://trillian.mit.edu/~jc/music/abc/doc/ABCprimer.html
  103.     abc = {}
  104.     abc['seq']      = 1     # X:
  105.     abc['title']    = ""    # T:
  106.     abc['composer'] = ""    # C:
  107.     abc['notes']    = ""    # N:
  108.     abc['meter']    = "2/4" # M:
  109.     abc['length']   = "1/8" # L:
  110.     abc['key']      = "C"   # K:
  111.     abc['song']     = ""
  112.     notes = False
  113.     music = music.split("\n")
  114.     ## strip header, assume that K: is the last line of the header
  115.     for li in music:
  116.        
  117.         if "X:" in li:
  118.             abc['seq'] = int(li.split(':')[1])
  119.         elif "T:" in li:
  120.             abc['title'] = li.split(':')[1].strip()
  121.         elif "C:" in li:
  122.             abc['composer'] = li.split(':')[1].strip()
  123.         elif "N:" in li:
  124.             abc['notes'] = li.split(':')[1].strip()
  125.         elif "M:" in li:
  126.             abc['meter'] = li.split(':')[1].strip()
  127.         elif "L:" in li:
  128.             abc['length'] = li.split(':')[1].strip()
  129.         elif "K:"  in li:
  130.             abc['key'] = li.split(':')[1].strip()
  131.             notes = True
  132.         elif notes == True:
  133.             abc['song'] += li.strip()
  134.  
  135.     return abc
  136.  
  137. def resetPins():
  138.     for pin in pins:
  139.         try:
  140.             pin.value = 0
  141.             pin.unexport()
  142.         except:
  143.             print (pin)
  144.     return
  145.  
  146. def playSong(abc):
  147.  
  148.     for i in abc['data']:
  149.         try:
  150.             note = i[0].upper()
  151.             length = i[1]
  152.             for (pin, value) in zip(pins, notes[note]):
  153.                 pin.value = value   # play note
  154.                 print note, length
  155.                 sleep(length)
  156.  
  157.             for (pin, value) in zip(pins, notes[' ']):
  158.                 pin.value = value  # a pause between notes
  159.                 sleep(noteLen)
  160.        
  161.         except KeyboardInterrupt:
  162.             resetPins()
  163.  
  164.     return
  165.  
  166. if __name__ == "__main__":
  167.    
  168. #    abcMusic = playScales()
  169. #    print abcMusic
  170. #
  171. #    playSong(abcMusic)
  172. #    resetPins()
  173. #    sys.exit()
  174.  
  175.     abcMusic = decodeABCstring(music)
  176.     abcMusic = decodeABCsong(abcMusic)
  177.     playSong(abcMusic)
  178.     resetPins()