-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpianoPlayPi.py
309 lines (271 loc) · 7.18 KB
/
pianoPlayPi.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Learning to play the Piano with a Python Bot Copyright (C) 2020 Manel Lurbe Sempere
# This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
# This is free software, and you are welcome to redistribute it
# under certain conditions; type `show c' for details.
########################################
# Imports #
########################################
import time
import sys
from adafruit_servokit import ServoKit
#end Imports.
########################################
# Initialize I2C bus #
########################################
# Create the servo connection.
kit = ServoKit(channels=16)
#end Initialize I2C bus.
########################################
# Global variables #
########################################
# Servo angles.
left = 0
off = 90
right = 180
# Song to be played.
song = []
song2 = []
#end Global variables.
########################################
# Music notes available #
########################################
# Channel of each servo motor.
mt0 = kit.servo[0]
mt1 = kit.servo[1]
mt2 = kit.servo[2]
mt3 = kit.servo[3]
mt4 = kit.servo[4]
mt5 = kit.servo[5]
mt6 = kit.servo[6]
mt7 = kit.servo[7]
mt8 = kit.servo[8]
mt9 = kit.servo[9]
mt10 = kit.servo[10]
mt11 = kit.servo[11]
mt12 = kit.servo[12]
mt13 = kit.servo[13]
mt14 = kit.servo[14]
mt15 = kit.servo[15]
# Servo motors available.
motor_list = [mt0,mt1,mt2,mt3,mt4,mt5,mt6,mt7,mt8,mt9,mt10,mt11,mt12,mt13,mt14,mt15]
# Each note with each channel.
music_notes = {
"sil":(mt15,off),
# Examples for flat notes.
#"sib":(mt0,left),
#"lab":(mt0,right),
#"re2b":(mt1,right),
#"mi2b":(mt1,left),
"do0":(mt0,right),
"re0":(mt0,left),
"mi0":(mt1,right),
"fa0":(mt1,left),
"sol0":(mt2,left),
"la0":(mt2,right),
"si0":(mt3,right),
"do1":(mt3,left),
"re1":(mt4,right),
"mi1":(mt4,left),
"fa1":(mt5,right),
"sol1":(mt5,left),
"la1":(mt6,right),
"si1":(mt6,left),
"do":(mt7,left),
"re":(mt7,right),
"mi":(mt8,right),
"fa":(mt8,left),
"sol":(mt9,right),
"la":(mt9,left),
"si":(mt10,left),
"do2":(mt10,right),
"re2":(mt11,left),
"mi2":(mt11,right),
"fa2":(mt12,left),
"sol2":(mt12,right),
"la2":(mt13,left),
"si2":(mt13,right),
"do3":(mt14,left)
}
#end Music notes available.
########################################
# Note tempo defines #
########################################
# Values of each type of note.
dobleredonda = 3.2
redonda = 1.6
blancaplus = 1.2
blancasemi = 1
blanca = 0.8
negraplus = 0.6
negrasemi = 0.5
negra = 0.4
corchea = 0.2
semicorchea = 0.1
ssemicorchea = 0.05
sssemicorchea = 0.025
# Each tempo with each value.
tempo_notes = {
"dr":dobleredonda,
"r":redonda,
"b+":blancaplus,
"b-":blancasemi,
"b":blanca,
"n+":negraplus,
"n-":negrasemi,
"n":negra,
"c":corchea,
"sc":semicorchea,
"ssc":ssemicorchea,
"sssc":sssemicorchea
}
#end Note tempo defines.
########################################
# usage function #
########################################
def usage(program_name):
print("Usage:\n\tpython3 "+program_name+" music_sheets/music_sheet.txt\nor:\n\t./"+program_name+" music_sheets/music_sheet.txt")
#end usage function.
########################################
# play_note function #
########################################
def play_note(the_notes):
for note in the_notes:
(note[0])[0].angle = (note[0])[1]
time.sleep(0.01) # Wait for servo motor change.
time.sleep((the_notes[0])[1])
for note in the_notes:
(note[0])[0].angle = off
time.sleep(0.01) # Wait for servo motor change.
#end play_note function.
########################################
# reset function #
########################################
def reset():
for motor in motor_list:
motor.angle = off
#end reset function.
########################################
# get_music_sheets function #
########################################
def get_music_sheet(music_sheet):
try:
song_file = open(music_sheet,"r")
except:
print("No music_sheet called music_sheets/"+str(music_sheet)+" found.")
sys.exit(1)
song_content = song_file.read().split("\n")
for notes in song_content:
try:
tuple_notes = notes.split("\t")
list_notes = []
for note in tuple_notes:
values = note.split(",")
tupla = (music_notes[values[0]],tempo_notes[values[1]])
list_notes.append(tupla)
song.append(list_notes)
except:
continue
#end get_music_sheets function.
########################################
# get_music_sheetsAcomp function #
########################################
def get_music_sheetAcomp(music_sheet):
try:
song_file = open(music_sheet,"r")
except:
print("No music_sheet called music_sheets/"+str(music_sheet)+" found.")
sys.exit(1)
song_content = song_file.read().split("\n")
for notes in song_content:
try:
tuple_notes = notes.split("\t")
list_notes = []
for note in tuple_notes:
values = note.split(",")
tupla = (music_notes[values[0]],tempo_notes[values[1]])
list_notes.append(tupla)
song2.append(list_notes)
except:
continue
#end get_music_sheetsAcomp function.
#########################################
# Read arguments and get music_sheets #
#########################################
# Get program name.
program_name = sys.argv[0].replace("./","")
# Get number of arguments.
num_args = len(sys.argv)
# Exit program if no music_sheet are selected.
if num_args <= 1:
print("No music_sheet selected")
usage(program_name)
sys.exit(1)
# Get music_sheet name.
music_sheet = sys.argv[1]
# Get second music_sheet name if is available.
if num_args == 3:
music_sheet2 = sys.argv[2]
# If music_sheet name is reset, reset motors and end program.
if music_sheet == "reset":
reset()
print("Reset notes")
sys.exit(1)
# Get the music_sheet.
get_music_sheet(music_sheet)
# Get the second music_sheet if is available.
if num_args == 3:
get_music_sheetAcomp(music_sheet2)
#end Read arguments and get music_sheets.
#########################################
# Play Song Threaded #
#########################################
def worker(num_thread):
try:
while True:
reset()
if num_thread == 0:
for note in song:
time.sleep(0.06)
play_note(note)
print("The song has ended, playing again...")
if num_thread == 1:
for note in song2:
time.sleep(0.06)
play_note(note)
print("The song has ended, playing again...")
except KeyboardInterrupt:
if num_thread == 0:
reset()
print("Turning off the piano.")
sys.exit(1)
#end function worker.
if num_args == 3:
# import thread feature.
import threading
threads = list()
print('Playing piano, press Ctrl-C to quit...')
for i in range(2):
t = threading.Thread(target=worker,args=(i,))
threads.append(t)
t.start()
#end Play Song Threaded
else:
#########################################
# Play Song non-Threaded #
#########################################
print('Playing piano, press Ctrl-C to quit...')
try:
while True:
reset()
for note in song:
time.sleep(0.06) # Modify to your servo motor delay in position changing.
play_note(note)
print("The song has ended, playing again...")
except KeyboardInterrupt:
reset()
print("Turning off the piano.")
sys.exit(1)
#end Play Song non-Threaded.
#end Program.