Thursday, January 15, 2015

Arduino music.

The Arduino is one of the neatest microcontrollers.  For something so simple, it can do an amazing amount of projects. One such project that interested me was the Digital melody project. If you have one the the Arduinos where the Atmel chip can be removed, you can make your own project without the board once the programming is done.



For this project, all you will need is the Arduino, usb cable (for programming), computer, some wires, optional 100 ohm resistor, and an old eight ohm speaker. Usually the back of speakers on the magnet have what resistance they are. So grab the old dead radio or the like that has a speaker and see what it has. Old personal computers are likely to have one too that you can borrow for a minute.

Now you need to build the circuit. It is minimal and you should be able to set it up quickly. Resistor adds safety for protecting the Arduino.




Set up up your Arduino for programming and load the Arduino IDE program into your computer. Then load in the Digital melody sample from the menu.





The instructions for your Arduino should tell you how to set up your board. Assuming your tools are set up properly, you can now compile and up load the sketch to your Arduino.




You should now hear a set of tones that sounds like it came from a cartoon. But what if you wanted to do your own tune. No problem. All you have to do is translate the notes and timing into your Arduino sketch. If you are unfamiliar with musical notation, you might want to get some help on this. The notation for a note might look something like this:

Then you need to have the note durations.



We decided to try doing Silent Night. Of course you need the sheet music to get the notes and the timing. The sketch will provide you with the information of how to translate the notes.





So we began doing our own translation using vim to record our results. Since this is 3/4 timing, things will be tricky.



Oops every thing is in lower case and we need to add the NOTE_ to the beginning of the notes. No problem. a g~~ will convert the letters to uppercase and a :%s!^!NOTE_! will the preface to the notes. So far so good. Now we need to separate the notes from the timings. So save the file. (Note: the G4 s/b A3)



$ cut -c-7  silentnight > notes-sn

and that gives us:

$ cat notes-sn
NOTE_G3
NOTE_A3
NOTE_G3
NOTE_E3
NOTE_G3
NOTE_A3
NOTE_G3
NOTE_E3
NOTE_D4
NOTE_D4
...
...
...

$ tr '\n' ', ' < notes-sn > notes-sn.txt
$ sed -i 's/,/, /g; s/,\s\+/, /g' notes-sn.txt
$ cat notes-sn.txt
NOTE_G3, NOTE_A3, NOTE_G3, NOTE_E3, NOTE_G3, NOTE_A3, NOTE_G3, NOTE_E3, NOTE_D4, NOTE_D4, NOTE_B3, NOTE_C4, NOTE_C4, NOTE_G3, NOTE_A3, NOTE_A3, NOTE_C4, ... ... ...

Which can be easily cut and pasted into the sketch.


$ cut -d'-' -f2 silentnight  > notes-st

And that gives us

$ cat notes-st
3
8
4
1
3
8
4
1
1
4
...
...
...

Now to add the commas:

$ tr '\n' ',' < notes-st > notes-cs.csv

and that gives us which you can easily cut and paste into the sketch.

$ cat notes-cs.csv

3,8,4,1,3,8,4,1,1,4,1,1,4,1,1,...

To add spaces: 
$ sed -i 's/,/, /g; s/,\s\+/, /g' notes-cs.csv

cat notes-cs.csv
3, 8, 4, 1, 3, 8, 4, 1, 1, 4, 1, 1, 4, 1, ... ... ...

You will also need to change this line for the total number of notes.

 for (int thisNote = 0; thisNote < 8; thisNote++) {

More music!

No comments:

Post a Comment