Skip to content

Commit 444606b

Browse files
committed
Updated code and readme.
1 parent 737acf7 commit 444606b

File tree

4 files changed

+173
-66
lines changed

4 files changed

+173
-66
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@
1010
This project is based on my interest in learning to play the piano in my spare time. As everyone knows, learning to play an instrument is difficult and requires a lot of time and dedication. In my case, I am a computer engineer and for lack of time I decided to start a project to start playing the piano using a software teacher implemented with LED lights on each piano key. My interest in this software has increased throughout the development and has ended up becoming a bot that plays the piano.
1111
</p>
1212

13-
## **PROGRAMS OF THIS PROJECT**
13+
## **PROGRAMS AVAILABLE ON THIS PROJECT**
1414

1515
### PIANO PLAYING BOT
1616

1717
- Program *pianoPlayPi.py* is the program to play with Servo motors.
1818
- Folder *partitures* contains example music sheets to play with previous program.
19+
- **Actual version of the code supports only multiple notes with same tempo in the same thread. Multi-thread is supported for simulate two virtual hands with their own partiture.**
1920

2021
### VIRTUAL PIANO
2122

2223
- Program *buzzerPi.py* is a program to play songs with a passive buzzer using the frequencies of piano notes. To play multiple notes at the same time you need as many as notes are wanted to play at the same time.
24+
- **Actual version of the code only supports one note each time.**
2325

2426
### LEARNING TO PLAY THE PIANO
2527

2628
- Program *ledPi.py* is a program to learn to play the piano using a servo controller for switching LEDs, each LED corresponds to one note, you need as many LEDs as notes are wanted to play.
2729
- This program was the first implementation and was designed to learn to play the piano by placing a LED on top of each key to play it every time a LED was turned on.
30+
- **Actual version of the code supports multiple notes with same tempo in the same thread. No multi-thread support yet.**
2831

2932
**GOOGLE ASSISTANT SUPPORT**
3033

buzzerPi.py

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
# under certain conditions; type `show c' for details.
88

99
########################################
10-
# Imports #
10+
# Imports #
1111
########################################
1212
import time
1313
import sys
1414
import RPi.GPIO as GPIO
15+
#end Imports.
1516

1617
########################################
17-
# Initialize buzzer #
18+
# Initialize buzzer #
1819
########################################
1920

2021
#Disable warnings (optional)
@@ -23,19 +24,21 @@
2324
#Select GPIO mode
2425
GPIO.setmode(GPIO.BCM);
2526

26-
#Set BUZZER - pin 23 as output
27+
#Set BUZZER - pin 23 as output (as many as notes)
2728
BUZZER =23;
2829
GPIO.setup(BUZZER,GPIO.OUT);
30+
#end Initialize buzzer.
2931

3032
########################################
31-
# Global variables #
33+
# Global variables #
3234
########################################
3335

3436
# Song to be played.
3537
song = [];
38+
#end Global variables.
3639

3740
########################################
38-
# Music notes available #
41+
# Music notes available #
3942
########################################
4043

4144
# Frecuency of each note.
@@ -65,29 +68,45 @@
6568
########################################
6669

6770
# Values of each type of note.
68-
redonda = 0.8;
69-
blanca = 0.4;
70-
negra = 0.3;
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;
7179
corchea = 0.2;
7280
semicorchea = 0.1;
81+
ssemicorchea = 0.05;
82+
sssemicorchea = 0.025;
7383

7484
# Each tempo with each value.
7585
tempo_notes = {
86+
"dr":dobleredonda,
7687
"r":redonda,
88+
"b+":blancaplus,
89+
"b-":blancasemi,
7790
"b":blanca,
91+
"n+":negraplus,
92+
"n-":negrasemi,
7893
"n":negra,
7994
"c":corchea,
80-
"sc":semicorchea
95+
"sc":semicorchea,
96+
"ssc":ssemicorchea,
97+
"sssc":sssemicorchea
8198
};
99+
#end Note tempo defines.
82100

83101
########################################
84-
# play_note function #
102+
# usage function #
85103
########################################
86104
def usage(program_name):
87105
print("Usage:\n\tpython3 "+program_name+" partitures/partiture.txt\nor:\n\t./"+program_name+" partitures/partiture.txt");
106+
#end usage function.
88107

89108
########################################
90-
# play_note function #
109+
# play_note function #
91110
########################################
92111
def play_note(the_note):
93112
halveWaveTime = 1 / (the_note[0] * 2 );
@@ -98,9 +117,10 @@ def play_note(the_note):
98117
GPIO.output(BUZZER, False);
99118
time.sleep(halveWaveTime);
100119
time.sleep(the_note[1] *0.1);
120+
#end play_note function.
101121

102122
########################################
103-
# get_partitures function #
123+
# get_partitures function #
104124
########################################
105125
def get_partiture(partiture):
106126
try:
@@ -116,9 +136,10 @@ def get_partiture(partiture):
116136
song.append(tupla);
117137
except:
118138
continue;
139+
#end get_partitures function.
119140

120141
#########################################
121-
# Read arguments and get partiture #
142+
# Read arguments and get partitures #
122143
#########################################
123144

124145
# Get program name.
@@ -138,11 +159,22 @@ def get_partiture(partiture):
138159

139160
# Get the partiture.
140161
get_partiture(partiture);
162+
#end Read arguments and get partitures.
141163

142164
#########################################
143-
# Play Song #
165+
# Play Song #
144166
#########################################
145167
print('Playing piano, press Ctrl-C to quit...')
146-
while True:
147-
for note in song:
148-
play_note(note);
168+
try:
169+
while True:
170+
reset();
171+
for note in song:
172+
play_note(note);
173+
print("The song has ended, playing again...");
174+
except KeyboardInterrupt:
175+
reset();
176+
print("Turning off the piano.");
177+
sys.exit(1);
178+
#end Play Song.
179+
180+
#end Program.

ledPi.py

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@
77
# under certain conditions; type `show c' for details.
88

99
########################################
10-
# Imports #
10+
# Imports #
1111
########################################
1212
import busio
1313
import time
1414
import sys
1515
from adafruit_pca9685 import PCA9685
1616
from board import SCL, SDA
17+
#end Imports.
1718

1819
########################################
19-
# Initialize I2C bus #
20+
# Initialize I2C bus #
2021
########################################
2122

2223
# Create the I2C bus interface.
@@ -27,19 +28,22 @@
2728

2829
# Set the PWM frequency to 60hz.
2930
pca.frequency = 60;
31+
#end Initialize I2C bus.
3032

3133
########################################
32-
# Global variables #
34+
# Global variables #
3335
########################################
3436

3537
# Led brightness.
3638
on = 0xffff;
3739
off = 0;
40+
3841
# Song to be played.
3942
song = [];
43+
#end Global variables.
4044

4145
########################################
42-
# Music notes available #
46+
# Music notes available #
4347
########################################
4448

4549
# Channel of each LED.
@@ -63,50 +67,71 @@
6367
"si":si,
6468
"do2":do2
6569
};
70+
#end Music notes available.
6671

6772
########################################
68-
# Tempo notes defines #
73+
# Note tempo defines #
6974
########################################
7075

7176
# Values of each type of note.
72-
redonda = 0.8;
73-
blanca = 0.4;
74-
negra = 0.3;
77+
dobleredonda = 3.2
78+
redonda = 1.6;
79+
blancaplus = 1.2;
80+
blancasemi = 1;
81+
blanca = 0.8;
82+
negraplus = 0.6;
83+
negrasemi = 0.5;
84+
negra = 0.4;
7585
corchea = 0.2;
7686
semicorchea = 0.1;
87+
ssemicorchea = 0.05;
88+
sssemicorchea = 0.025;
7789

7890
# Each tempo with each value.
7991
tempo_notes = {
92+
"dr":dobleredonda,
8093
"r":redonda,
94+
"b+":blancaplus,
95+
"b-":blancasemi,
8196
"b":blanca,
97+
"n+":negraplus,
98+
"n-":negrasemi,
8299
"n":negra,
83100
"c":corchea,
84-
"sc":semicorchea
101+
"sc":semicorchea,
102+
"ssc":ssemicorchea,
103+
"sssc":sssemicorchea
85104
};
105+
#end Note tempo defines.
86106

87107
########################################
88-
# play_note function #
108+
# usage function #
89109
########################################
90110
def usage(program_name):
91111
print("Usage:\n\tpython3 "+program_name+" partitures/partiture.txt\nor:\n\t./"+program_name+" partitures/partiture.txt");
112+
#end usage function.
92113

93114
########################################
94-
# play_note function #
115+
# play_note function #
95116
########################################
96117
def play_note(the_note):
97-
the_note[0].duty_cycle = on;
98-
time.sleep(the_note[1]);
99-
the_note[0].duty_cycle = off;
118+
for note in the_notes:
119+
note[0].duty_cycle = on;
120+
time.sleep(the_notes[1]);
121+
for note in the_notes:
122+
note[0].duty_cycle = off;
123+
#end play_note function.
100124

101125
########################################
102-
# reset function #
126+
# reset function #
103127
########################################
104128
def reset():
105129
for note in music_notes:
106130
music_notes[note].duty_cycle = off;
131+
#end reset function.
107132

108133
########################################
109-
# get_partitures function #
134+
# get_partitures function #
110135
########################################
111136
def get_partiture(partiture):
112137
try:
@@ -115,16 +140,21 @@ def get_partiture(partiture):
115140
print("No partiture called partitures/"+str(partiture)+" found.");
116141
sys.exit(1);
117142
song_content = song_file.read().split("\n");
118-
for note in song_content:
143+
for notes in song_content:
119144
try:
120-
values = note.split(",");
121-
tupla = (music_notes[values[0]],tempo_notes[values[1]]);
122-
song.append(tupla);
145+
tuple_notes = notes.split("\t");
146+
list_notes = [];
147+
for note in tuple_notes:
148+
values = note.split(",");
149+
tupla = (music_notes[values[0]],tempo_notes[values[1]]);
150+
list_notes.append(tupla);
151+
song.append(list_notes);
123152
except:
124153
continue;
154+
#end get_partitures function.
125155

126156
#########################################
127-
# Read arguments and get partiture #
157+
# Read arguments and get partitures #
128158
#########################################
129159

130160
# Get program name.
@@ -150,13 +180,22 @@ def get_partiture(partiture):
150180

151181
# Get the partiture.
152182
get_partiture(partiture);
183+
#end Read arguments and get partitures.
153184

154185
#########################################
155-
# Play Song #
186+
# Play Song #
156187
#########################################
157188
print('Playing piano, press Ctrl-C to quit...')
158-
while True:
189+
try:
190+
while True:
191+
reset();
192+
for note in song:
193+
play_note(note);
194+
print("The song has ended, playing again...");
195+
except KeyboardInterrupt:
159196
reset();
160-
for note in song:
161-
play_note(note);
162-
197+
print("Turning off the piano.");
198+
sys.exit(1);
199+
#end Play Song.
200+
201+
#end Program.

0 commit comments

Comments
 (0)