This is a simple sound library for mbed. This sound library uses a Ticker to simulate a 50% duty cycle pwm signal on any pin. The frequency of this signal can be varied to allow sound to be created by any piezoelectric device that is connected to that pin.

Dependents:   SuperMbedBall Tono

About

This library allows for a buzzer (or piezoelectric device) to output sound based on any inputted frequency. The library provides predefined note frequencies which make song writing convenient. These definitions can be found here. Some pins may not be able to drive output such as the AnalogIn pins on the LPC11U24 model. Only pin 21 seemed to work well so far on this model.

Hardware

One wire from a buzzer is connected to any pin on the mbed and the other wire is connected directly to ground. To reduce the volume of the buzzer, an optional resistor can be connected in series as shown. /media/uploads/mdu7078/buzzer001.png

Circuit diagram constructed using CircuitLab.

Usage

A music object is constructed in the following way:

Constructor

#include "Music.h"

music ms(p21);

This code snipped attaches a frequency modulating Ticker to the specified pin.

This frequency can be modified by setting:

Frequency

ms.freq(240);

To allow more complex behavior, a parsing function is included that can take in strings of formatted input and turn them into songs. This input must be formatted as specified below:

  • A capital letter must be present to specify the note, or R to specify a rest (Only letters A-G, and R are valid)
  • An octave must be specified which is any integer from 0 to 8 inclusive
  • An optional sharp sign ( # ) can be added to augment the note a half step
  • A colon ( : ) must separate the octave and note from the duration
  • The duration must be an integer greater than 0, and less than or equal to 64
  • To separate notes, a semicolon ( ; ) must be used

Furthermore, the length of the string must be specified in order for the entire string to be parsed and a tempo must be provided (beats per minute). An example of this is shown below:

Play a Song

/* This is a test song */
char s1[] = "E4:8; E4:8; R:8; E4:8; R:8; C4:8; E4:4; G4:4; R:4; G3:4; R:4;";
int len = 61;

/* Set up music pin on pin 21 */
music m1(p21);
double tempo = 180;

m1.play(s1,tempo,len);

This code plays the familiar 7-note opening to the Super Mario Brothers theme song. The possibilities for music creation are limited only by the hard memory limits of the mbed.

Committer:
mdu7078
Date:
Tue Apr 30 03:49:24 2013 +0000
Revision:
2:c33ed3d85f97
Parent:
1:51cf7b1a96bd
All functions finalized and working!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mdu7078 0:07a4aa44fe9c 1 /* * * * * * * * * * * * * * * * * * * * * * * * * * *
mdu7078 0:07a4aa44fe9c 2 * This is a simple music library for the mbed. *
mdu7078 0:07a4aa44fe9c 3 * *
mdu7078 0:07a4aa44fe9c 4 * Created by: Michael Dushkoff (mad1841@rit.edu) *
mdu7078 0:07a4aa44fe9c 5 * * * * * * * * * * * * * * * * * * * * * * * * * * */
mdu7078 0:07a4aa44fe9c 6
mdu7078 0:07a4aa44fe9c 7 #include "mbed.h"
mdu7078 0:07a4aa44fe9c 8 #include "Music.h"
mdu7078 2:c33ed3d85f97 9 #include "Notes.h"
mdu7078 0:07a4aa44fe9c 10
mdu7078 2:c33ed3d85f97 11 /*
mdu7078 2:c33ed3d85f97 12 * This is the minimal constructor that creates a music object
mdu7078 2:c33ed3d85f97 13 * with 0 initial output frequency on a specified pin
mdu7078 2:c33ed3d85f97 14 *
mdu7078 2:c33ed3d85f97 15 * M0 - The pin to output on
mdu7078 2:c33ed3d85f97 16 */
mdu7078 2:c33ed3d85f97 17 music::music(PinName M0) : _M0(M0){
mdu7078 2:c33ed3d85f97 18 _flipper.attach(this,&music::flip,0);
mdu7078 2:c33ed3d85f97 19 }
mdu7078 2:c33ed3d85f97 20
mdu7078 2:c33ed3d85f97 21 /*
mdu7078 2:c33ed3d85f97 22 * This constructs a music object which has an initial frequency
mdu7078 2:c33ed3d85f97 23 * equal to a given one
mdu7078 2:c33ed3d85f97 24 *
mdu7078 2:c33ed3d85f97 25 * M0 - The pin to output on
mdu7078 2:c33ed3d85f97 26 * freq - The initial frequency
mdu7078 2:c33ed3d85f97 27 */
mdu7078 1:51cf7b1a96bd 28 music::music(PinName M0, double freq) : _M0(M0), _freq(freq) {
mdu7078 0:07a4aa44fe9c 29 _flipper.attach(this,&music::flip,1/(2*_freq));
mdu7078 0:07a4aa44fe9c 30 }
mdu7078 0:07a4aa44fe9c 31
mdu7078 2:c33ed3d85f97 32 /*
mdu7078 2:c33ed3d85f97 33 * This changes the current output frequency to a given one
mdu7078 2:c33ed3d85f97 34 *
mdu7078 2:c33ed3d85f97 35 * freq - The given frequency
mdu7078 2:c33ed3d85f97 36 */
mdu7078 0:07a4aa44fe9c 37 void music::freq(double freq){
mdu7078 0:07a4aa44fe9c 38 _freq=freq;
mdu7078 0:07a4aa44fe9c 39 _flipper.detach();
mdu7078 0:07a4aa44fe9c 40 _flipper.attach(this,&music::flip,1/(2*_freq));
mdu7078 0:07a4aa44fe9c 41 }
mdu7078 0:07a4aa44fe9c 42
mdu7078 2:c33ed3d85f97 43 /*
mdu7078 2:c33ed3d85f97 44 * This is an internal flipper that allows for a fixed
mdu7078 2:c33ed3d85f97 45 * frequency oscillation.
mdu7078 2:c33ed3d85f97 46 */
mdu7078 0:07a4aa44fe9c 47 void music::flip(){
mdu7078 0:07a4aa44fe9c 48 _M0 = !_M0;
mdu7078 0:07a4aa44fe9c 49 }
mdu7078 0:07a4aa44fe9c 50
mdu7078 0:07a4aa44fe9c 51 /*
mdu7078 2:c33ed3d85f97 52 * This initializes a given m_song to a given size
mdu7078 2:c33ed3d85f97 53 *
mdu7078 2:c33ed3d85f97 54 * *m - The given m_song
mdu7078 2:c33ed3d85f97 55 * num - The given size
mdu7078 2:c33ed3d85f97 56 */
mdu7078 2:c33ed3d85f97 57 void music::init_song(m_song *m, int num){
mdu7078 2:c33ed3d85f97 58 m->len = 0;
mdu7078 2:c33ed3d85f97 59 m->note = new music_note[((int)(num/5))];
mdu7078 2:c33ed3d85f97 60 }
mdu7078 2:c33ed3d85f97 61
mdu7078 2:c33ed3d85f97 62 /*
mdu7078 2:c33ed3d85f97 63 * This deallocates an m_song by deallocating the notes
mdu7078 2:c33ed3d85f97 64 * that it contains and setting the length to 0
mdu7078 2:c33ed3d85f97 65 *
mdu7078 2:c33ed3d85f97 66 * *m - The given m_song
mdu7078 2:c33ed3d85f97 67 */
mdu7078 2:c33ed3d85f97 68 void music::dal_song(m_song *m){
mdu7078 2:c33ed3d85f97 69 m->len = 0;
mdu7078 2:c33ed3d85f97 70 delete [] m->note;
mdu7078 2:c33ed3d85f97 71 // free(m->note);
mdu7078 2:c33ed3d85f97 72 }
mdu7078 2:c33ed3d85f97 73
mdu7078 2:c33ed3d85f97 74 /*
mdu7078 0:07a4aa44fe9c 75 * This parses a given character array into an m_song
mdu7078 0:07a4aa44fe9c 76 * Format:
mdu7078 2:c33ed3d85f97 77 * <note_letter><[sharp]><octave>:<length>; . . .;
mdu7078 0:07a4aa44fe9c 78 * Example (Super Mario Bros.):
mdu7078 1:51cf7b1a96bd 79 * E3:8; E3:8; R:8; E3:8; R:8; C3:8; E3:4; G3:4; R:4; G2:4; R:4;
mdu7078 1:51cf7b1a96bd 80 *
mdu7078 1:51cf7b1a96bd 81 * Sharp notes are represented with the # symbol as shown below:
mdu7078 1:51cf7b1a96bd 82 * C#:4;
mdu7078 0:07a4aa44fe9c 83 *
mdu7078 0:07a4aa44fe9c 84 * song - The song character array
mdu7078 0:07a4aa44fe9c 85 * num - The number of characters in the array
mdu7078 0:07a4aa44fe9c 86 */
mdu7078 0:07a4aa44fe9c 87 m_song music::parse(char* song, int num){
mdu7078 2:c33ed3d85f97 88 //Create a new song and initialize it
mdu7078 0:07a4aa44fe9c 89 m_song out;
mdu7078 2:c33ed3d85f97 90 init_song(&out,num);
mdu7078 2:c33ed3d85f97 91
mdu7078 1:51cf7b1a96bd 92 char curnote = ' '; // Note
mdu7078 1:51cf7b1a96bd 93 int octave = 0; // Octave
mdu7078 2:c33ed3d85f97 94 int dur[] = {0,0}; // Duration
mdu7078 1:51cf7b1a96bd 95 int flag = 0; // Complete flag
mdu7078 1:51cf7b1a96bd 96 int sharp = 0; // Sharp flag
mdu7078 2:c33ed3d85f97 97 int inl = 1; // Index of dur[]
mdu7078 0:07a4aa44fe9c 98
mdu7078 1:51cf7b1a96bd 99 for (int i=0; i<num; i++){
mdu7078 1:51cf7b1a96bd 100 switch(song[i]){
mdu7078 1:51cf7b1a96bd 101 case 'A':
mdu7078 1:51cf7b1a96bd 102 //A Note
mdu7078 1:51cf7b1a96bd 103 curnote = 'A';
mdu7078 1:51cf7b1a96bd 104 break;
mdu7078 1:51cf7b1a96bd 105 case 'B':
mdu7078 1:51cf7b1a96bd 106 //B Note
mdu7078 1:51cf7b1a96bd 107 curnote = 'B';
mdu7078 1:51cf7b1a96bd 108 break;
mdu7078 1:51cf7b1a96bd 109 case 'C':
mdu7078 1:51cf7b1a96bd 110 //C Note
mdu7078 1:51cf7b1a96bd 111 curnote = 'C';
mdu7078 1:51cf7b1a96bd 112 break;
mdu7078 1:51cf7b1a96bd 113 case 'D':
mdu7078 1:51cf7b1a96bd 114 //D Note
mdu7078 1:51cf7b1a96bd 115 curnote = 'D';
mdu7078 1:51cf7b1a96bd 116 break;
mdu7078 1:51cf7b1a96bd 117 case 'E':
mdu7078 1:51cf7b1a96bd 118 //E Note
mdu7078 1:51cf7b1a96bd 119 curnote = 'E';
mdu7078 1:51cf7b1a96bd 120 break;
mdu7078 1:51cf7b1a96bd 121 case 'F':
mdu7078 1:51cf7b1a96bd 122 //F Note
mdu7078 1:51cf7b1a96bd 123 curnote = 'F';
mdu7078 1:51cf7b1a96bd 124 break;
mdu7078 1:51cf7b1a96bd 125 case 'G':
mdu7078 1:51cf7b1a96bd 126 //G Note
mdu7078 1:51cf7b1a96bd 127 curnote = 'G';
mdu7078 1:51cf7b1a96bd 128 break;
mdu7078 1:51cf7b1a96bd 129 case 'R':
mdu7078 1:51cf7b1a96bd 130 //Rest
mdu7078 1:51cf7b1a96bd 131 curnote = 'R';
mdu7078 1:51cf7b1a96bd 132 break;
mdu7078 1:51cf7b1a96bd 133 case '#':
mdu7078 1:51cf7b1a96bd 134 //Sharp set
mdu7078 1:51cf7b1a96bd 135 sharp = 1;
mdu7078 1:51cf7b1a96bd 136 break;
mdu7078 1:51cf7b1a96bd 137 case '0':
mdu7078 1:51cf7b1a96bd 138 if (flag == 0){
mdu7078 1:51cf7b1a96bd 139 //Octave set
mdu7078 2:c33ed3d85f97 140 octave = 0;
mdu7078 1:51cf7b1a96bd 141 }
mdu7078 1:51cf7b1a96bd 142 else {
mdu7078 1:51cf7b1a96bd 143 //Duration set
mdu7078 2:c33ed3d85f97 144 if (inl == 0){
mdu7078 2:c33ed3d85f97 145 //Shift
mdu7078 2:c33ed3d85f97 146 dur[0] = dur[1];
mdu7078 2:c33ed3d85f97 147
mdu7078 2:c33ed3d85f97 148 //Set
mdu7078 2:c33ed3d85f97 149 dur[1] = 0;
mdu7078 2:c33ed3d85f97 150 }
mdu7078 2:c33ed3d85f97 151 else{
mdu7078 2:c33ed3d85f97 152 //Set
mdu7078 2:c33ed3d85f97 153 dur[1] = 0;
mdu7078 2:c33ed3d85f97 154 inl=0;
mdu7078 2:c33ed3d85f97 155 }
mdu7078 1:51cf7b1a96bd 156 }
mdu7078 1:51cf7b1a96bd 157 break;
mdu7078 1:51cf7b1a96bd 158 case '1':
mdu7078 1:51cf7b1a96bd 159 if (flag == 0){
mdu7078 1:51cf7b1a96bd 160 //Octave set
mdu7078 2:c33ed3d85f97 161 octave = 1;
mdu7078 1:51cf7b1a96bd 162 }
mdu7078 1:51cf7b1a96bd 163 else {
mdu7078 1:51cf7b1a96bd 164 //Duration set
mdu7078 2:c33ed3d85f97 165 if (inl == 0){
mdu7078 2:c33ed3d85f97 166 //Shift
mdu7078 2:c33ed3d85f97 167 dur[0] = dur[1];
mdu7078 2:c33ed3d85f97 168
mdu7078 2:c33ed3d85f97 169 //Set
mdu7078 2:c33ed3d85f97 170 dur[1] = 1;
mdu7078 2:c33ed3d85f97 171 }
mdu7078 2:c33ed3d85f97 172 else{
mdu7078 2:c33ed3d85f97 173 //Set
mdu7078 2:c33ed3d85f97 174 dur[1] = 1;
mdu7078 2:c33ed3d85f97 175 inl=0;
mdu7078 2:c33ed3d85f97 176 }
mdu7078 1:51cf7b1a96bd 177 }
mdu7078 1:51cf7b1a96bd 178 break;
mdu7078 1:51cf7b1a96bd 179 case '2':
mdu7078 1:51cf7b1a96bd 180 if (flag == 0){
mdu7078 1:51cf7b1a96bd 181 //Octave set
mdu7078 2:c33ed3d85f97 182 octave = 2;
mdu7078 1:51cf7b1a96bd 183 }
mdu7078 1:51cf7b1a96bd 184 else {
mdu7078 1:51cf7b1a96bd 185 //Duration set
mdu7078 2:c33ed3d85f97 186 if (inl == 0){
mdu7078 2:c33ed3d85f97 187 //Shift
mdu7078 2:c33ed3d85f97 188 dur[0] = dur[1];
mdu7078 2:c33ed3d85f97 189
mdu7078 2:c33ed3d85f97 190 //Set
mdu7078 2:c33ed3d85f97 191 dur[1] = 2;
mdu7078 2:c33ed3d85f97 192 }
mdu7078 2:c33ed3d85f97 193 else{
mdu7078 2:c33ed3d85f97 194 //Set
mdu7078 2:c33ed3d85f97 195 dur[1] = 2;
mdu7078 2:c33ed3d85f97 196 inl=0;
mdu7078 2:c33ed3d85f97 197 }
mdu7078 1:51cf7b1a96bd 198 }
mdu7078 1:51cf7b1a96bd 199 break;
mdu7078 1:51cf7b1a96bd 200 case '3':
mdu7078 1:51cf7b1a96bd 201 if (flag == 0){
mdu7078 1:51cf7b1a96bd 202 //Octave set
mdu7078 2:c33ed3d85f97 203 octave = 3;
mdu7078 1:51cf7b1a96bd 204 }
mdu7078 1:51cf7b1a96bd 205 else {
mdu7078 1:51cf7b1a96bd 206 //Duration set
mdu7078 2:c33ed3d85f97 207 if (inl == 0){
mdu7078 2:c33ed3d85f97 208 //Shift
mdu7078 2:c33ed3d85f97 209 dur[0] = dur[1];
mdu7078 2:c33ed3d85f97 210
mdu7078 2:c33ed3d85f97 211 //Set
mdu7078 2:c33ed3d85f97 212 dur[1] = 3;
mdu7078 2:c33ed3d85f97 213 }
mdu7078 2:c33ed3d85f97 214 else{
mdu7078 2:c33ed3d85f97 215 //Set
mdu7078 2:c33ed3d85f97 216 dur[1] = 3;
mdu7078 2:c33ed3d85f97 217 inl=0;
mdu7078 2:c33ed3d85f97 218 }
mdu7078 1:51cf7b1a96bd 219 }
mdu7078 1:51cf7b1a96bd 220 break;
mdu7078 1:51cf7b1a96bd 221 case '4':
mdu7078 1:51cf7b1a96bd 222 if (flag == 0){
mdu7078 1:51cf7b1a96bd 223 //Octave set
mdu7078 2:c33ed3d85f97 224 octave = 4;
mdu7078 1:51cf7b1a96bd 225 }
mdu7078 1:51cf7b1a96bd 226 else {
mdu7078 1:51cf7b1a96bd 227 //Duration set
mdu7078 2:c33ed3d85f97 228 if (inl == 0){
mdu7078 2:c33ed3d85f97 229 //Shift
mdu7078 2:c33ed3d85f97 230 dur[0] = dur[1];
mdu7078 2:c33ed3d85f97 231
mdu7078 2:c33ed3d85f97 232 //Set
mdu7078 2:c33ed3d85f97 233 dur[1] = 4;
mdu7078 2:c33ed3d85f97 234 }
mdu7078 2:c33ed3d85f97 235 else{
mdu7078 2:c33ed3d85f97 236 //Set
mdu7078 2:c33ed3d85f97 237 dur[1] = 4;
mdu7078 2:c33ed3d85f97 238 inl=0;
mdu7078 2:c33ed3d85f97 239 }
mdu7078 1:51cf7b1a96bd 240 }
mdu7078 1:51cf7b1a96bd 241 break;
mdu7078 1:51cf7b1a96bd 242 case '5':
mdu7078 1:51cf7b1a96bd 243 if (flag == 0){
mdu7078 1:51cf7b1a96bd 244 //Octave set
mdu7078 2:c33ed3d85f97 245 octave = 5;
mdu7078 1:51cf7b1a96bd 246 }
mdu7078 1:51cf7b1a96bd 247 else {
mdu7078 1:51cf7b1a96bd 248 //Duration set
mdu7078 2:c33ed3d85f97 249 if (inl == 0){
mdu7078 2:c33ed3d85f97 250 //Shift
mdu7078 2:c33ed3d85f97 251 dur[0] = dur[1];
mdu7078 2:c33ed3d85f97 252
mdu7078 2:c33ed3d85f97 253 //Set
mdu7078 2:c33ed3d85f97 254 dur[1] = 5;
mdu7078 2:c33ed3d85f97 255 }
mdu7078 2:c33ed3d85f97 256 else{
mdu7078 2:c33ed3d85f97 257 //Set
mdu7078 2:c33ed3d85f97 258 dur[1] = 5;
mdu7078 2:c33ed3d85f97 259 inl=0;
mdu7078 2:c33ed3d85f97 260 }
mdu7078 1:51cf7b1a96bd 261 }
mdu7078 1:51cf7b1a96bd 262 break;
mdu7078 1:51cf7b1a96bd 263 case '6':
mdu7078 1:51cf7b1a96bd 264 if (flag == 0){
mdu7078 1:51cf7b1a96bd 265 //Octave set
mdu7078 2:c33ed3d85f97 266 octave = 6;
mdu7078 1:51cf7b1a96bd 267 }
mdu7078 1:51cf7b1a96bd 268 else {
mdu7078 1:51cf7b1a96bd 269 //Duration set
mdu7078 2:c33ed3d85f97 270 if (inl == 0){
mdu7078 2:c33ed3d85f97 271 //Shift
mdu7078 2:c33ed3d85f97 272 dur[0] = dur[1];
mdu7078 2:c33ed3d85f97 273
mdu7078 2:c33ed3d85f97 274 //Set
mdu7078 2:c33ed3d85f97 275 dur[1] = 6;
mdu7078 2:c33ed3d85f97 276 }
mdu7078 2:c33ed3d85f97 277 else{
mdu7078 2:c33ed3d85f97 278 //Set
mdu7078 2:c33ed3d85f97 279 dur[1] = 6;
mdu7078 2:c33ed3d85f97 280 inl=0;
mdu7078 2:c33ed3d85f97 281 }
mdu7078 1:51cf7b1a96bd 282 }
mdu7078 1:51cf7b1a96bd 283 break;
mdu7078 1:51cf7b1a96bd 284 case '7':
mdu7078 1:51cf7b1a96bd 285 if (flag == 0){
mdu7078 1:51cf7b1a96bd 286 //Octave set
mdu7078 2:c33ed3d85f97 287 octave = 7;
mdu7078 1:51cf7b1a96bd 288 }
mdu7078 1:51cf7b1a96bd 289 else {
mdu7078 1:51cf7b1a96bd 290 //Duration set
mdu7078 2:c33ed3d85f97 291 if (inl == 0){
mdu7078 2:c33ed3d85f97 292 //Shift
mdu7078 2:c33ed3d85f97 293 dur[0] = dur[1];
mdu7078 2:c33ed3d85f97 294
mdu7078 2:c33ed3d85f97 295 //Set
mdu7078 2:c33ed3d85f97 296 dur[1] = 7;
mdu7078 2:c33ed3d85f97 297 }
mdu7078 2:c33ed3d85f97 298 else{
mdu7078 2:c33ed3d85f97 299 //Set
mdu7078 2:c33ed3d85f97 300 dur[1] = 7;
mdu7078 2:c33ed3d85f97 301 inl=0;
mdu7078 2:c33ed3d85f97 302 }
mdu7078 1:51cf7b1a96bd 303 }
mdu7078 1:51cf7b1a96bd 304 break;
mdu7078 1:51cf7b1a96bd 305 case '8':
mdu7078 1:51cf7b1a96bd 306 if (flag == 0){
mdu7078 1:51cf7b1a96bd 307 //Octave set
mdu7078 2:c33ed3d85f97 308 octave = 8;
mdu7078 1:51cf7b1a96bd 309 }
mdu7078 1:51cf7b1a96bd 310 else {
mdu7078 1:51cf7b1a96bd 311 //Duration set
mdu7078 2:c33ed3d85f97 312 if (inl == 0){
mdu7078 2:c33ed3d85f97 313 //Shift
mdu7078 2:c33ed3d85f97 314 dur[0] = dur[1];
mdu7078 2:c33ed3d85f97 315
mdu7078 2:c33ed3d85f97 316 //Set
mdu7078 2:c33ed3d85f97 317 dur[1] = 8;
mdu7078 2:c33ed3d85f97 318 }
mdu7078 2:c33ed3d85f97 319 else{
mdu7078 2:c33ed3d85f97 320 //Set
mdu7078 2:c33ed3d85f97 321 dur[1] = 8;
mdu7078 2:c33ed3d85f97 322 inl=0;
mdu7078 2:c33ed3d85f97 323 }
mdu7078 1:51cf7b1a96bd 324 }
mdu7078 1:51cf7b1a96bd 325 break;
mdu7078 1:51cf7b1a96bd 326 case '9':
mdu7078 2:c33ed3d85f97 327 if (flag == 1){
mdu7078 1:51cf7b1a96bd 328 //Duration set
mdu7078 2:c33ed3d85f97 329 if (inl == 0){
mdu7078 2:c33ed3d85f97 330 //Shift
mdu7078 2:c33ed3d85f97 331 dur[0] = dur[1];
mdu7078 2:c33ed3d85f97 332
mdu7078 2:c33ed3d85f97 333 //Set
mdu7078 2:c33ed3d85f97 334 dur[1] = 9;
mdu7078 2:c33ed3d85f97 335 }
mdu7078 2:c33ed3d85f97 336 else{
mdu7078 2:c33ed3d85f97 337 //Set
mdu7078 2:c33ed3d85f97 338 dur[1] = 9;
mdu7078 2:c33ed3d85f97 339 inl=0;
mdu7078 2:c33ed3d85f97 340 }
mdu7078 1:51cf7b1a96bd 341 }
mdu7078 1:51cf7b1a96bd 342 break;
mdu7078 1:51cf7b1a96bd 343 case ':':
mdu7078 1:51cf7b1a96bd 344 flag = 1;
mdu7078 1:51cf7b1a96bd 345 break;
mdu7078 1:51cf7b1a96bd 346 case ';':
mdu7078 1:51cf7b1a96bd 347 if (flag == 1){
mdu7078 2:c33ed3d85f97 348 //Calculate duration
mdu7078 2:c33ed3d85f97 349 int t1 = (dur[0])*10;
mdu7078 2:c33ed3d85f97 350 int t2 = dur[1];
mdu7078 2:c33ed3d85f97 351 int tst = t1+t2;
mdu7078 1:51cf7b1a96bd 352
mdu7078 2:c33ed3d85f97 353 if (tst>0 && tst<=64 && curnote != ' '){
mdu7078 2:c33ed3d85f97 354 notes n;
mdu7078 2:c33ed3d85f97 355
mdu7078 2:c33ed3d85f97 356 //DEAD CODE: Will work when realloc is fixed
mdu7078 2:c33ed3d85f97 357 //Create a temporary checker
mdu7078 2:c33ed3d85f97 358 //music_note* tmpn; //= new music_note[out.len+1];
mdu7078 2:c33ed3d85f97 359
mdu7078 2:c33ed3d85f97 360 //Reallocate space for note
mdu7078 2:c33ed3d85f97 361 //tmpn = (music_note*) realloc(out.note, (out.len+1)*sizeof(music_note));
mdu7078 2:c33ed3d85f97 362 // if (tmpn == NULL){
mdu7078 2:c33ed3d85f97 363 // //Failed to allocate
mdu7078 2:c33ed3d85f97 364 // free(out.note);
mdu7078 2:c33ed3d85f97 365 // exit(1);
mdu7078 2:c33ed3d85f97 366 // }
mdu7078 2:c33ed3d85f97 367 // out.note = tmpn;
mdu7078 2:c33ed3d85f97 368
mdu7078 2:c33ed3d85f97 369 //Set frequency using lookup function
mdu7078 2:c33ed3d85f97 370 out.note[out.len].freq = n.get_freq(curnote, sharp, octave);
mdu7078 2:c33ed3d85f97 371
mdu7078 2:c33ed3d85f97 372 //Set duration
mdu7078 2:c33ed3d85f97 373 out.note[out.len].duration = tst;
mdu7078 2:c33ed3d85f97 374
mdu7078 2:c33ed3d85f97 375 //Increment current song size
mdu7078 2:c33ed3d85f97 376 out.len=out.len+1;
mdu7078 2:c33ed3d85f97 377 }
mdu7078 1:51cf7b1a96bd 378
mdu7078 1:51cf7b1a96bd 379 //Reset values
mdu7078 1:51cf7b1a96bd 380 flag = 0;
mdu7078 1:51cf7b1a96bd 381 sharp = 0;
mdu7078 2:c33ed3d85f97 382 dur[0] = 0;
mdu7078 2:c33ed3d85f97 383 dur[1] = 0;
mdu7078 1:51cf7b1a96bd 384 curnote = ' ';
mdu7078 1:51cf7b1a96bd 385 octave = 0;
mdu7078 2:c33ed3d85f97 386 inl = 1;
mdu7078 1:51cf7b1a96bd 387 }
mdu7078 1:51cf7b1a96bd 388 else{
mdu7078 1:51cf7b1a96bd 389 //Default to quarter note:
mdu7078 1:51cf7b1a96bd 390 }
mdu7078 1:51cf7b1a96bd 391 break;
mdu7078 1:51cf7b1a96bd 392 }
mdu7078 1:51cf7b1a96bd 393 }
mdu7078 0:07a4aa44fe9c 394 return out;
mdu7078 0:07a4aa44fe9c 395 }
mdu7078 0:07a4aa44fe9c 396
mdu7078 0:07a4aa44fe9c 397 /*
mdu7078 0:07a4aa44fe9c 398 * This plays a given song string at a given tempo
mdu7078 0:07a4aa44fe9c 399 *
mdu7078 0:07a4aa44fe9c 400 * song - The song character array
mdu7078 0:07a4aa44fe9c 401 * tempo - The specified beats per minute
mdu7078 0:07a4aa44fe9c 402 * num - The number of characters in the array
mdu7078 0:07a4aa44fe9c 403 */
mdu7078 0:07a4aa44fe9c 404 void music::play(char* song, double tempo, int num){
mdu7078 0:07a4aa44fe9c 405 double dl;
mdu7078 0:07a4aa44fe9c 406 m_song msng = parse(song, num);
mdu7078 0:07a4aa44fe9c 407
mdu7078 0:07a4aa44fe9c 408 /* Play m_song */
mdu7078 0:07a4aa44fe9c 409 for (int i=0; i<msng.len; i++){
mdu7078 0:07a4aa44fe9c 410 // Output the frequency:
mdu7078 0:07a4aa44fe9c 411 freq(msng.note[i].freq);
mdu7078 2:c33ed3d85f97 412
mdu7078 0:07a4aa44fe9c 413 // Calculate delay:
mdu7078 2:c33ed3d85f97 414 dl = (60*4)/(tempo*(msng.note[i].duration));
mdu7078 2:c33ed3d85f97 415
mdu7078 0:07a4aa44fe9c 416 // Wait for the note to be complete
mdu7078 0:07a4aa44fe9c 417 wait(dl);
mdu7078 0:07a4aa44fe9c 418 }
mdu7078 1:51cf7b1a96bd 419
mdu7078 2:c33ed3d85f97 420 //Stop playing
mdu7078 2:c33ed3d85f97 421 freq(0);
mdu7078 2:c33ed3d85f97 422
mdu7078 1:51cf7b1a96bd 423 //Deallocate song:
mdu7078 2:c33ed3d85f97 424 dal_song(&msng);
mdu7078 0:07a4aa44fe9c 425 }