#! /usr/bin/python
## IMPORTANT NOTE:
## This Code is wrtten just to make the organ work (in an abstract sense).
## Its a bit poor and could do with a lot of work to make it respectable.
# http://home.arcor.de/mdoege/pysynth/
# http://abcnotation.com
# http://abc.sourceforge.net/
# http://abcnotation.com/tunePage?a=abc.sourceforge.net/NMD/nmd/jigs.txt/0277
import sys
from time import sleep
from quick2wire.gpio import Pin # see: http://quick2wire.com/2012/05/quick2wire-python-api-released/
music = """X: 278
T:I Do Like To Be Beside The Seaside % Nottingham Music Database
S:Toby Bennett, via EF
M:6/8
K:C
g2^g |"C"a3 g2e|"C"d2c B2c|"G7"g3 g3|"G7"g3 g2^g|"C"a3 g2e|"C"d2c B2c|"F"a3 a3\
|
"F"a3 a2^a|"G7"b3 a2f|"G7"e2d ^c2d|"C"a3 g3|"C"f3 e2^d|"D7"e3 d3|"D7"e3 d^cd|\
"G7"e3 d3|"G7"B3 g2^g|
"C"a3 g2e|"C"d2c B2c|"G7"g3 g3|"G7"g3 g2^g|"C"a3 g2e|"C"d2c B2c|"F"a3 a3|\
"F"a3 a2^a|
"F#7"b2^a b2a|"B7"b3 a2f|"Em"a2g a2g|"A7"a2g f2e|"D7"d3 a3|"G7"a2e d2e|\
"C"c3 c3|c3 ||"""
notes = {}
notes['G'] = [1,0,0,0,0,0,0]
notes['F'] = [0,1,0,0,0,0,0]
notes['E'] = [0,0,1,0,0,0,0]
notes['D'] = [0,0,0,1,0,0,0]
notes['C'] = [0,0,0,0,1,0,0]
notes['B'] = [0,0,0,0,0,1,0]
notes['A'] = [0,0,0,0,0,0,1]
notes[' '] = [0,0,0,0,0,0,0]
noteLen = 0.01
pins = [Pin(n, Pin.Out) for n in [11, 13, 15, 19, 21, 23, 25]]
# GPIO PIN
# 17 11
# 21 13
# 22 15
# 10 19
# 9 21
# 11 23
# 7 25
def playScales():
note = ['A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",
'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",
'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",
'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",
'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," ",'A','B','C','D','E','F','G'," "]
data = []
for n in note:
data.append([n,noteLen])
abc = {}
abc['data'] = data
return abc
def decodeABCsong(abc):
data = []
#split the song into bars
song = abc['song'].split("|")
allNotes = "abcdefgABCDEFG"
for bar in song:
if len(bar) < 1:
continue
# remove chords
print bar
if bar[0] == '"':
bpos = bar.find('"',1) + 1
bar = bar[bpos:]
bar = bar.replace(" ","") # remove spaces
bar = bar.replace("^","") # remove sharps
barLen = len(bar)
bar = bar.ljust(10," ") # put some spaces at the end so the loop doesn't fail
for x in range(0, barLen):
if bar[x+1].isdigit():
length = noteLen + float("0.0"+bar[x+1])
else:
length = noteLen
if bar[x] in allNotes:
note = bar[x]
data.append([note, length])
abc['data'] = data
return abc
def decodeABCstring(music):
# http://trillian.mit.edu/~jc/music/abc/doc/ABCprimer.html
abc = {}
abc['seq'] = 1 # X:
abc['title'] = "" # T:
abc['composer'] = "" # C:
abc['notes'] = "" # N:
abc['meter'] = "2/4" # M:
abc['length'] = "1/8" # L:
abc['key'] = "C" # K:
abc['song'] = ""
notes = False
music = music.split("\n")
## strip header, assume that K: is the last line of the header
for li in music:
if "X:" in li:
abc['seq'] = int(li.split(':')[1])
elif "T:" in li:
abc['title'] = li.split(':')[1].strip()
elif "C:" in li:
abc['composer'] = li.split(':')[1].strip()
elif "N:" in li:
abc['notes'] = li.split(':')[1].strip()
elif "M:" in li:
abc['meter'] = li.split(':')[1].strip()
elif "L:" in li:
abc['length'] = li.split(':')[1].strip()
elif "K:" in li:
abc['key'] = li.split(':')[1].strip()
notes = True
elif notes == True:
abc['song'] += li.strip()
return abc
def resetPins():
for pin in pins:
try:
pin.value = 0
pin.unexport()
except:
print (pin)
return
def playSong(abc):
for i in abc['data']:
try:
note = i[0].upper()
length = i[1]
for (pin, value) in zip(pins, notes[note]):
pin.value = value # play note
print note, length
sleep(length)
for (pin, value) in zip(pins, notes[' ']):
pin.value = value # a pause between notes
sleep(noteLen)
except KeyboardInterrupt:
resetPins()
return
if __name__ == "__main__":
# abcMusic = playScales()
# print abcMusic
#
# playSong(abcMusic)
# resetPins()
# sys.exit()
abcMusic = decodeABCstring(music)
abcMusic = decodeABCsong(abcMusic)
playSong(abcMusic)
resetPins()