A good way of saving musical notes is to store them in the EEPROM. (Electrically Erasable Programmable Read-Only Memory). To do this use the DATA diractive.
Syntax for the DATA diractive -
Symbol} DATA {Word} DataItem {, {Word} DataItem,...}
Here is an example of how to use the DATA diractive to store the characters that corespond to musical notes:
Notes DATA "C","C","G","G","A","A","G"
You can use the READ command to acces these characters. The letter "C" is located at addreaa Notes + 0, and a second letter "C" is located at Notes + 1, and so on. For example, if you want to load the last letter "G" into a byte variable called noteLetter, use the cammand:
READ Notes + 6, noteLetter
You can also store lists of numbers using the DATA diractive. Frequency and duration values that the BASIC Stamp uses for musical notes need to be stored in word variables because they are usually greater than 225. This is how to do that with a DATA diractive.
Frequencies DATA Word 2093, Word 2093, Word 3136, Word 3136,
Word 3520, Word 3520, Word 3136Because each of these values occupies two bytes, accesing them with the read command is different from accessing characters. The first 2093 is at Frequencies + 0, but the second 2093 is located at Frequencies + 2. The first 3136 is located at Frequencies + 4, and the second 3136 is locaded at Frequencies + 6.
Here is a FOR...NEXT loop that places the Notes DATA into a variable called noteLetter, then it plasec the Frequencies DATA into a variable named noteFreq - FOR index = 0 to 6
READ Notes + index, noteLetter
READ Frequencies + (index * 2), Word noteFreq
DEBUG noteLetter, " ", DEC noteeq, CR
NEXT
Here's an example program that demonstrates how to use the DATA directive to store lists and how to use the READ command to access the valuin that list.
Coding:
A better system for storing and retrieving music:
By using Bytes, instead of Words, you can write programs that store twice as much music in the BASIC Stamp.
Here's an eample that introduces how to store musical information in a way that relates to the concepts of notes, durations and rests. Tempo is also introduced. Only the note characters are stored in the Notes DATA directive because LOOKUP and LOOKDOWN commands will be used to be match up letters to their corresponding friequencies.
Notes DATA "C","D","E","C","C","D","E","C","E","F","G","E","F","G","Q"
Durations DATA 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 4, 4, 2WholeNote CON 2000
Here's a list of what each duration means:
- 1 - whole note
- 2 - half note
- 4 - quarter note
- 8 - eighth note
- 16 - sixteenth note
- 32 - thirty-second note
The "Q" in the Notes DATA is for quit, and a DO WHILE...LOOP checks foe "Q" each time through the loop. You can insert a rest between notres by inserting a "P".
When you use the lower-case version of the note, it will play a flat note. For example, if you want to play B-flat, use "b" instead of "B".
Example Program - Frere Jacques.
Coding:
Video:
How does it all work:
The Notes and Durations DATA directives combined with the WholeNote constant are used to store all of the musical data used by the program.
Even though a FOR...NEXT loop is no longer used to access the data, there still has to be a variable (index) that keeps track of which DATA entry is being read in Notes and Durations. The offset variable is used in the LOOKDOWN and LOOKUP commands to select a particular value. The NoteLetter variable stores a character accessed by the READ command. LOOKUP and LOOKDOWN commands are used to convert this character into a friequency value. This value is stored in the NoteFreq variable and used as the FREQOUT command's Freq1 argument. The noteDuration variable is used in a READ command to recieve a value from the Durations DATA. It is also used to calculate the Duration used in the FREQOUT command.
The main loop keeps executing until the letter 'Q' is read from the Notes DATA.
A READ command gets a character from the Notes DATA, and stores it in the noteLetter variable. The noteLetter variable is then used in a LOOKDOWN command to set the value of the offset variable. Remeber that offset stores a 1 if "b" is detected and a 2 if "B" is detected and a 3 if "C: is detected and so on. The offset value is then used in a LOOKUP command to figure out what the value of the noteFreq variable should be. If offset is 1, noteFreq will be 1865, if offset is 2, noteFreq will be 1976 and so on.
The READ command uses the value of index to place a value from the Durations DATA into noteDuartion.
READ Durations + index, noteDuration
Then, noteDuration is set equal to the WholeNote constant devided by the noteDuration. If note duration starts out as 4 from a READ command, it becomes 2000 / 4 = 500. If noteDuration is 8, 1500 / 8 = 250.
noteDuration = WholeNote / noteDuration
Now that noteDuration and noteFreq are determined, the FREQOUT command plays the note.
FREQOUT 9, noteDuration, noteFreq
Each time through the main loop, the index value must be increased by one. When the main loop gets back to the beggining, the first thing the probram does is read the next note, using the index variable.
index = index + 1
LOOP
I had a few problems with that one - I didnt really understand it at first. But after reading it a couple times, I'm starting to understand it more and know whats going on.
Entering musical data is much easier when all you have to do is record notes and durations. I editted the last program to play Beethoven's Fifth Symphony, just by replacing the notes and durations DATA diractives.
Adding Musical Features:
Cell phones that play musi to let you know that someone is calling have three features that were not supported by the previouse section:
- The play "dotted" notes.
- They determine the whole note duration from a value called tempo
- They play notes from more than one octave.
Dots DATA 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0
Cell phones typically take the tempo for a song in beats per minute. This is the same as saying quarter notes per minute.
BeatsPerMinute CON 200
CELL PHONE RINGTONES:
One of the most widely used way of composing, recording and posting notes is one that features strings of text that describe each note in the song. Haere is an example of how the first few notes from Beethoven's 5th look like in RTTTL format:
Beethoven5:d=8,o=7,b=125:g,g,g2d#,p,f,f,f,2d
This format for storing musical data is called RTTTL, which stand for Ringing Tone Text Transfer Language.
RTTTL Ringtone Player Application Program
This application program is pretty long; in the book they said I can download it from the parralax site, but I couldnt find it so I had to write the whole thing.
Reveille Video:
Coding:
I changed the song in the coding by simply replacing the RTTTL_File DATA directive with different strings of coding.
JollyGoodFellow video:
OK thats done with Music, I have like two more chapters to post up.
Hope you enjoyed it!











No comments:
Post a Comment