-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMusicalBells.py
More file actions
159 lines (132 loc) · 3.77 KB
/
MusicalBells.py
File metadata and controls
159 lines (132 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/python
import RPi.GPIO as GPIO
import sys
import time
GPIO.setmode(GPIO.BCM)
# enumeration not supported on python 2.7 so making poor man enums
whole = 1
half = 2
quarter = 4
eighth = 8
# -------------------------FUNCTIONS---------------------------
def noteStrToInt(noteStr):
if noteStr == "whole":
return 1;
elif noteStr == "half":
return 2;
elif noteStr == "quarter":
return 4;
elif noteStr == "eighth":
return 8;
else:
return 0; # invalid string passed to function
def noteIntToStr(noteInt):
if noteInt == 1:
return "whole";
elif noteInt == 2:
return "half";
elif noteInt == 4:
return "quarter";
elif noteInt == 8:
return "eighth";
else:
return "INVALID"; # invalid string passed to function
# beat sleep time function
def betweenBeatSleep(note, bpm):
returnVal = (60.0/bpm) * (timeSignatureNote/note)
return returnVal
# play song function
def PlaySong():
noteCount = 0
print("\n-----BEGIN SONG-----")
for bell in bells:
# debug
print(bell + " " + notes[noteCount])
# end debug
if bell == 'W':
time.sleep(betweenBeatSleep(int(notes[noteCount]), bpm))
else:
GPIO.output(pins[bell], GPIO.LOW)
time.sleep(0.05)
GPIO.output(pins[bell], GPIO.HIGH)
time.sleep(betweenBeatSleep(int(notes[noteCount]), bpm))
noteCount += 1
return 0
# ----------------------END OF FUNCTIONS------------------------
# ------------------- variable initializations -------------------
songFileName = ""
songFileContents = ""
songFileContentsListStepOne = []
songFileContentsListStepTwo = []
bells = []
notes = []
elementEven = False
# Time signature for all songs (for now) will be 4/4. Will always use simple time signatures (quarter note gets one beat)
timeSignatureBeats = 4.0
timeSignatureNote = 4.0
# Set the bpm value with either command line inputs or if made into a module, make a function to set this
bpm = 130 # default 95 bpm
# time to sleep between quarter notes is set by user's choice of bpm (reciprocal of bpm in sec is how often in time there is a beat)
sleepTimeBeat = 60/bpm #in seconds
# ----------------------- end vaiable init -----------------------
# Command Line handling
if (len(sys.argv) > 2):
print("Too many command line arguments. Exiting...")
exit(2)
else:
songFileName = str(sys.argv[1])
# read the song file (everything you need should just be on the first line)
with open(songFileName, 'r') as f:
songFileContents = f.readline()
#debug
print("\n---SONG FILE CONTENTS---\n")
print(songFileContents)
#end debug
# Parse the song file into bells and note types
songFileContentsListStepOne = songFileContents.split(',')
#debug
print("\n---SONG FILE CONTENT LIST STEP ONE---\n")
print(songFileContentsListStepOne)
#end debug
for element in songFileContentsListStepOne:
songFileContentsListStepTwo.append(element.split('-')[0])
songFileContentsListStepTwo.append(element.split('-')[1])
#debug
print("\n---SONG FILE CONTENT LIST STEP TWO---\n")
print(songFileContentsListStepTwo)
#end debug
for element in songFileContentsListStepTwo:
if (elementEven):
notes.append(element)
elementEven = False
else:
bells.append(element)
elementEven = True
# init list with pin numbers for relays
#pinList = [2, 3, 4, 17, 27, 22, 10, 9]
# map pin numbers to bell color
pins = {
'R' : 9,
'O' : 10,
'Y' : 22,
'L' : 27,
'G' : 17,
'T' : 4,
'B' : 3,
'P' : 2
}
# loop through pins and set mode and state to 'high' (Close relays by changing to low)
#for i in pinList:
# GPIO.setup(i, GPIO.OUT)
# GPIO.output(i, GPIO.HIGH)
for bell in pins:
GPIO.setup(pins[bell], GPIO.OUT)
GPIO.output(pins[bell], GPIO.HIGH)
# play song
try:
PlaySong()
# End program cleanly with keyboard
except KeyboardInterrupt:
print " Quit"
# Reset GPIO settings
GPIO.cleanup()