This Arduino-based Music player can play a two-voice polyphony tune in convenient notation as MML (Music Macro Language) strings, and display the synchronized lyrics on a 16x2 Character LCD display. Momentary push buttons are used to start/stop the music.
The example plays Do-Re-Mi (from The Sound of Music) - one speaker plays the melodic notes while the second speaker plays the harmonic (left-hand notes).
The music playing happens in the background using the ProtoStax_MmlMusicPlayer Arduino library, allowing you to do other tasks like handling button presses, displaying synchronized lyrics, etc, in the main loop.
You can replace the music with your own favorite easily. I'll show you how to transcribe a musical notation to an MML string, and prepare the lyrics to display.
Here is a demo:
Step 1 : Prepare the HardwareI'm using the ProtoStax Enclosure system here. It is a modular enclosure and you can add functionality and peripherals to it by swapping out pieces. I started with the ProtoStax Enclosure for Arduino, and swapped out the Top Plate with the ProtoStax LCD Kit V2 (which has cut outs and mounting holes for an I2C LCD module (included)). I then swapped out one of the Short-Side walls with a ProtoStax Kit for Momentary Push Button Switches (which includes two panel-mount push button switches).
I used some jumper wires to wire the circuit according to the circuit diagram below. I soldered some jumper wires to the momentary push button switches. Since I was running out of GND pins, I used one from the ICSP header on the Arduino Uno (that one used the lone female jumper wire).
Before you close everything up, you'll first want to adjust the potentiometer on the LCD module to adjust the contrast for best viewing.
You can download the library and the example sketch from the GitHub page (link below), which also has instructions on how to get it running.
The sketch utilizes the following libraries:
- ProtoStax_MmlMusicPlayer
- MmlMusic ( ProtoStax_MmlMusicPlayer extends the MmlMusic library, so we need the base library)
- JC_Button (used for button presses)
- LiquidCrystal_I2C (for the LCD display)
Once you have installed the ProtoStax_MmlMusicPlayer library and its dependencies, you can open the PlayTunes example sketch from the Arduino IDE.
Compile and upload the sketch to your Music Player. Press one button to start the music, and when the music is playing, pressing the other button stops it (which buttons depends on how set up your connections!)
You will need to first adjust the potentiometer on your LCD module to get the best contrast for viewing. You should have done this step 1: Preparing the Hardware. If you have not done so, don't worry. You can open up the top plate of the ProtoStax Enclosure to get access the LCD module and potentiometer - make the adjustments as you see fit, and then close everything up again!
Music Macro Language - MML and ProtoStax_MmlMusicPlayerThis project uses the ProtoStax_MmlMusicPlayer library, which makes it easy for you to play sounds/music on the Arduino.
You can specify an entire score to play as a string written in MML (Music Macro Language) syntax. For example, here's the score for Do-Re-Mi (and the chords):
const char doReMi[] PROGMEM = {"T120 O5"
">C.2< R4"
"C.4 D8 E.4 C8 E4 C4"
"E4 R4 D.4 E8 F8 F8 E8 D8 F.2 R4"
"E.4 F8 G.4 E8 G4 E4 G4 R4 F.4 G8"
"A8 A8 G8 F8 A4 R4 R2 G4 R8 C8 D8 E8 F8 G8"
"A1 A4 R8 D8 E8 F#8 G8 A8"
"B1 B4 R8 E8 F#8 G#8 A8 B8 >C2<"
"R4 B8 B-8 A4 F4 B4 G4"
">C8< B8 A8 G8 F8 E8 D8 C8 D8 C8 R4 R2"
"C.4 D8 E.4 C8 E4 C4"
"E4 R4 D.4 E8 F8 F8 E8 D8 F.2 R4"
"E.4 F8 G.4 E8 G4 E4 G4 R4 F.4 G8"
"A8 A8 G8 F8 A4 R4 R2 G4 R8 C8 D8 E8 F8 G8"
"A1 A4 R8 D8 E8 F#8 G8 A8"
"B1 D4 R8 E8 F#8 G#8 A8 B8 >C2<"
"R4 B8 B-8 A4 F4 B4 G4"
"C8 B8 A8 G8 F8 E8 D8 C8 G4 F4 >C4< R4" };
const char doReMiChords[] PROGMEM = {"T120 O4 "
"R8 C8 D8 E8 F8 G8 A8 B8"
"R4 E4 G4 E4 G4 E4"
"G4 >C4< A4 G4 A4 G4 A4 F4 A4 G4"
"R4 C4 E4 G4 E4 G4 E4 >C4< B4 G4"
"B4 G4 D4 G4 G-4 F4 E4 G4 B8 B-8 A8 G8"
"F4 E4 D4 G4 F#4 A4 D2 G4 F#4"
"E4 D4 G#4 B4 E2 A4 G#4 A4 E4 F4 A4 G4 B4"
">C4< R4 R2 R4 B8 A8 B8 G8 A8 B8"
"R4 E4 G4 E4 G4 E4"
"G4 >C4< A4 G4 A4 G4 A4 F4 A4 G4"
"R4 C4 E4 G4 E4 G4 E4 >C4< B4 G4"
"B4 G4 D4 G4 G-4 F4 E4 G4 B8 B-8 A8 G8"
"F4 E4 D4 G4 F#4 A4 D2 G4 F#4"
"E4 D4 G#4 B4 E2 A4 G#4 A4 E4 F4 A4 G4 B4"
">C4< R4 R2 R4 G4 >C4< C4"};
The T120 specifies the tempo, and the O5 (or O4) specifies the Octave. The notes are then presented as CDEFGAB etc and with the note duration as C8 (eighth note), etc.
You can also create multiple instances of ProtoStax_MmlMusicPlayer to play multiple scores - the number is only limited by the number of available Timers you have. The entire score(s) is(are) played in the background, allowing you to do other activities in your main loop.
You create an instance
ProtoStax_MmlMusicPlayer player1;
ProtoStax_MmlMusicPlayer player2;
In your setup, you can specify which output pin is used to connect the speaker:
void setup() {
....
player1.begin(TONE_PIN_1);
...
player2.begin(TONE_PIN_2);
}
To play
player1.play(doReMi);
// or if the string has been defined in PROGMEM as is the case in this example
player1.play_P(doReMi);
player2.play_P(doReMiChords);
This kicks of the entire score to be played in the background and you can continue with your processing (the call is non-blocking)!
You may be familiar with the Arduino Tone library that can be used to play tones on a piezo speaker. It uses a Timer (Timer2) on the Arduino Uno to generate a square wave on a specified pin to activate the piezo/speaker. You can generate a tone with a given frequency and duration, and the call is non-blocking. However, it only plays a single tone or note.
With the ProtoStax_MmlMusicPlayer Library, you can specify an entire score to play, and not just a single tone. The score is represented as an MML string, and is a concise and easy to use notation. There are also libraries of MML scores available, so you can use them (or use them for inspiration).
Not only that, you can create multiple instances of ProtoStax_MmlMusicPlayer to play multiple scores simultaneously (as is shown in this case).
I have used this library to provide background music and sound effects to an endless runner game - that is the topic of another post though! π
Happy Making! π
Sridhar
Comments