Sunday, February 21, 2010

RobotC Sample Music program

A topic came up over at the RobotC forums about how to play music in RobotC. I spent some time messing with it a few months ago, so I spent some time researching it again. Please keep in mind I haven’t played a musical instrument or read sheet music since I quit band in 6th grade back in 1979.

First thing I did was to find some very easy and free sheet music with familiar tunes. I found this place, but I am certain there are plenty of places to find music. Then I did some research on the notes and such and I reverted to the reliable Wikipedia. Then I spent some time in NXT-G figuring out which tones go with which note.

I then created this cheat sheet which I keep by my side as I am translating the sheet music to variables in the RobotC array.

So here is what I came up with. A sample program to start with when I find new sheet music that I want to convert to RobotC music. Here is a link to the sheet music I used.

// Frequency (note)
// Example: A_3 is A in the third octave
// Ash_3 is A# in the third octave
int C_3 = 261;
int Csh_3 = 275;
int D_3 = 293;
int Dsh_3 = 310;
int E_3 = 329;
int F_3 = 348;
int Fsh_3 = 370;
int G_3 = 392;
int Gsh_3 = 412;
int A_3 = 440;
int Ash_3 = 466;
int B_3 = 496;
int C_4 = 524;
int Csh_4 = 555;
int D_4 = 590;
int Dsh_4 = 621;
int E_4 = 656;
int F_4 = 695;
int Fsh_4 = 738;
int G_4 = 788;
int Gsh_4 = 830;
int A_4 = 877;
int Ash_4 = 929;
int B_4 = 987;
int C_5 = 1054;
int Csh_5 = 1104;
int D_5 = 1188;
int Dsh_5 = 1251;
int E_5 = 1322;
int F_5 = 1401;
int Fsh_5 = 1488;
int G_5 = 1590;
int Gsh_5 = 1646;
int A_5 = 1770;
int Ash_5 = 1841;
int B_5 = 1995;
//Duration
int Dwhole = 200;
int Whole = 100;
int Half = 50;
int Quarter = 25;
int Eighth = 13;
int Sixteenth = 6;

int Notes[][]= //currently “Ode to Joy”, Change these notes for a new song
{
{F_4, Quarter},
{F_4, Quarter},
{G_4, Quarter},
{A_4, Quarter},
{A_4, Quarter},
{G_4, Quarter},
{F_4, Quarter},
{E_4, Quarter},
{D_4, Quarter},
{D_4, Quarter},
{E_4, Quarter},
{F_4, Quarter},
{F_4, Quarter},
{E_4, Eighth},
{E_4, Half},
{F_4, Quarter},
{F_4, Quarter},
{G_4, Quarter},
{A_4, Quarter},
{A_4, Quarter},
{G_4, Quarter},
{F_4, Quarter},
{E_4, Quarter},
{D_4, Quarter},
{D_4, Quarter},
{E_4, Quarter},
{F_4, Quarter},
{E_4, Quarter},
{D_4, Eighth},
{D_4, Half},
{E_4, Quarter},
{E_4, Quarter},
{F_4, Quarter},
{D_4, Quarter},
{E_4, Quarter},
{F_4, Eighth},
{G_4, Eighth},
{F_4, Quarter},
{D_4, Quarter},
{E_4, Quarter},
{F_4, Eighth},
{G_4, Eighth},
{F_4, Quarter},
{E_4, Quarter},
{D_4, Quarter},
{E_4, Quarter},
{G_3, Half},
{F_4, Quarter},
{F_4, Quarter},
{G_4, Quarter},
{A_4, Quarter},
{A_4, Quarter},
{G_4, Quarter},
{F_4, Quarter},
{E_4, Quarter},
{D_4, Quarter},
{D_4, Quarter},
{E_4, Quarter},
{F_4, Quarter},
{E_4, Quarter},
{D_4, Eighth},
{D_4, Half},
};

task main
{
for (int i=0; i<62; i++) //change the "62" to the new number of notes in the piece
{
PlayTone(Notes[i][0], Notes[i][1]);
while(bSoundActive);
wait1Msec(20);
}
}
Have Fun!

3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Quick question is there a way to incorporate rests in this way i've tried what i could but can't seem to find where to put a wait function without screwing up the code.

    ReplyDelete
  3. Great work!

    A few minor tweaks to Ode to Joy:

    The F_4's should be Fsh_4's (as you're playing Ode to Joy in D major)
    You also need to add a DottedQuarter duration which equals 37. All the Quarters immediately before single Eighth notes should be DottedQuarters. We made these changes and it sounds sweet!

    Cheers!

    ReplyDelete