19
19
########################################
20
20
21
21
#Disable warnings (optional)
22
- GPIO .setwarnings (False );
22
+ GPIO .setwarnings (False )
23
23
24
24
#Select GPIO mode
25
- GPIO .setmode (GPIO .BCM );
25
+ GPIO .setmode (GPIO .BCM )
26
26
27
27
#Set BUZZER - pin 23 as output (as many as notes)
28
- BUZZER = 23 ;
29
- GPIO .setup (BUZZER ,GPIO .OUT );
28
+ BUZZER = 23
29
+ GPIO .setup (BUZZER ,GPIO .OUT )
30
30
#end Initialize buzzer.
31
31
32
32
########################################
33
33
# Global variables #
34
34
########################################
35
35
36
36
# Song to be played.
37
- song = [];
37
+ song = []
38
38
#end Global variables.
39
39
40
40
########################################
41
41
# Music notes available #
42
42
########################################
43
43
44
44
# Frecuency of each note.
45
- do = 261.63 ;
46
- re = 293.66 ;
47
- mi = 329.63 ;
48
- fa = 349.23 ;
49
- sol = 392.00 ;
50
- la = 440.00 ;
51
- si = 493.88 ;
52
- do2 = 523.25 ;
45
+ do = 261.63
46
+ re = 293.66
47
+ mi = 329.63
48
+ fa = 349.23
49
+ sol = 392.00
50
+ la = 440.00
51
+ si = 493.88
52
+ do2 = 523.25
53
53
54
54
# Each note with each frecuency.
55
55
music_notes = {
61
61
"la" :la ,
62
62
"si" :si ,
63
63
"do2" :do2
64
- };
64
+ }
65
65
66
66
########################################
67
67
# Tempo notes defines #
68
68
########################################
69
69
70
70
# Values of each type of note.
71
71
dobleredonda = 3.2
72
- redonda = 1.6 ;
73
- blancaplus = 1.2 ;
74
- blancasemi = 1 ;
75
- blanca = 0.8 ;
76
- negraplus = 0.6 ;
77
- negrasemi = 0.5 ;
78
- negra = 0.4 ;
79
- corchea = 0.2 ;
80
- semicorchea = 0.1 ;
81
- ssemicorchea = 0.05 ;
82
- sssemicorchea = 0.025 ;
72
+ redonda = 1.6
73
+ blancaplus = 1.2
74
+ blancasemi = 1
75
+ blanca = 0.8
76
+ negraplus = 0.6
77
+ negrasemi = 0.5
78
+ negra = 0.4
79
+ corchea = 0.2
80
+ semicorchea = 0.1
81
+ ssemicorchea = 0.05
82
+ sssemicorchea = 0.025
83
83
84
84
# Each tempo with each value.
85
85
tempo_notes = {
95
95
"sc" :semicorchea ,
96
96
"ssc" :ssemicorchea ,
97
97
"sssc" :sssemicorchea
98
- };
98
+ }
99
99
#end Note tempo defines.
100
100
101
101
########################################
102
102
# usage function #
103
103
########################################
104
104
def usage (program_name ):
105
- print ("Usage:\n \t python3 " + program_name + " partitures/partiture.txt\n or:\n \t ./" + program_name + " partitures/partiture.txt" );
105
+ print ("Usage:\n \t python3 " + program_name + " partitures/partiture.txt\n or:\n \t ./" + program_name + " partitures/partiture.txt" )
106
106
#end usage function.
107
107
108
108
########################################
109
109
# play_note function #
110
110
########################################
111
111
def play_note (the_note ):
112
- halveWaveTime = 1 / (the_note [0 ] * 2 );
113
- waves = int (the_note [1 ] * the_note [0 ]);
112
+ halveWaveTime = 1 / (the_note [0 ] * 2 )
113
+ waves = int (the_note [1 ] * the_note [0 ])
114
114
for i in range (waves ):
115
- GPIO .output (BUZZER , True );
116
- time .sleep (halveWaveTime );
117
- GPIO .output (BUZZER , False );
118
- time .sleep (halveWaveTime );
119
- time .sleep (the_note [1 ] * 0.1 );
115
+ GPIO .output (BUZZER , True )
116
+ time .sleep (halveWaveTime )
117
+ GPIO .output (BUZZER , False )
118
+ time .sleep (halveWaveTime )
119
+ time .sleep (the_note [1 ] * 0.1 )
120
120
#end play_note function.
121
121
122
122
########################################
123
123
# get_partitures function #
124
124
########################################
125
125
def get_partiture (partiture ):
126
126
try :
127
- song_file = open (partiture ,"r" );
127
+ song_file = open (partiture ,"r" )
128
128
except :
129
- print ("No partiture called partitures/" + str (partiture )+ " found." );
130
- sys .exit (1 );
131
- song_content = song_file .read ().split ("\n " );
129
+ print ("No partiture called partitures/" + str (partiture )+ " found." )
130
+ sys .exit (1 )
131
+ song_content = song_file .read ().split ("\n " )
132
132
for note in song_content :
133
133
try :
134
- values = note .split ("," );
135
- tupla = (music_notes [values [0 ]],tempo_notes [values [1 ]]);
136
- song .append (tupla );
134
+ values = note .split ("," )
135
+ tupla = (music_notes [values [0 ]],tempo_notes [values [1 ]])
136
+ song .append (tupla )
137
137
except :
138
- continue ;
138
+ continue
139
139
#end get_partitures function.
140
140
141
141
#########################################
142
142
# Read arguments and get partitures #
143
143
#########################################
144
144
145
145
# Get program name.
146
- program_name = sys .argv [0 ].replace ("./" ,"" );
146
+ program_name = sys .argv [0 ].replace ("./" ,"" )
147
147
148
148
# Get number of arguments.
149
- num_args = len (sys .argv );
149
+ num_args = len (sys .argv )
150
150
151
151
# Exit program if no partiture are selected.
152
152
if num_args <= 1 :
153
- print ("No partiture selected" );
154
- usage (program_name );
155
- sys .exit (1 );
153
+ print ("No partiture selected" )
154
+ usage (program_name )
155
+ sys .exit (1 )
156
156
157
157
# Get partiture name.
158
- partiture = sys .argv [1 ];
158
+ partiture = sys .argv [1 ]
159
159
160
160
# Get the partiture.
161
- get_partiture (partiture );
161
+ get_partiture (partiture )
162
162
#end Read arguments and get partitures.
163
163
164
164
#########################################
@@ -167,14 +167,12 @@ def get_partiture(partiture):
167
167
print ('Playing piano, press Ctrl-C to quit...' )
168
168
try :
169
169
while True :
170
- reset ();
171
170
for note in song :
172
- play_note (note );
173
- print ("The song has ended, playing again..." );
171
+ play_note (note )
172
+ print ("The song has ended, playing again..." )
174
173
except KeyboardInterrupt :
175
- reset ();
176
- print ("Turning off the piano." );
177
- sys .exit (1 );
174
+ print ("Turning off the piano." )
175
+ sys .exit (1 )
178
176
#end Play Song.
179
177
180
178
#end Program.
0 commit comments