A simple sound library for mbed. I use the Ring Tone Text Transfer Language (RTTTL) to play song with mbed.

Dependents:   acd52832_buzzer

Committer:
adelino
Date:
Wed Dec 11 08:31:19 2013 +0000
Revision:
0:4c9dcb64e608
Child:
1:b0c0c7d002aa
A simple sound Library for mbed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
adelino 0:4c9dcb64e608 1 #ifndef BUZZER_H
adelino 0:4c9dcb64e608 2 #define BUZZER_H
adelino 0:4c9dcb64e608 3 #include "mbed.h"
adelino 0:4c9dcb64e608 4 #include <vector>
adelino 0:4c9dcb64e608 5
adelino 0:4c9dcb64e608 6 /**
adelino 0:4c9dcb64e608 7 This is a simple sound library for mbed.I use the RTTTL format
adelino 0:4c9dcb64e608 8 http://en.wikipedia.org/wiki/Ring_Tone_Transfer_Language
adelino 0:4c9dcb64e608 9
adelino 0:4c9dcb64e608 10 Exemple
adelino 0:4c9dcb64e608 11
adelino 0:4c9dcb64e608 12 @code
adelino 0:4c9dcb64e608 13 #include "Buzzer.h"
adelino 0:4c9dcb64e608 14
adelino 0:4c9dcb64e608 15 char *song = "The Simpsons:d=4,o=5,b=160:c.6,e6,f#6,8a6,g.6,e6,c6,8a,8f#,8f#,8f#,2g,8p,8p,8f#,8f#,8f#,8g,a#.,8c6,8c6,8c6,c6";
adelino 0:4c9dcb64e608 16
adelino 0:4c9dcb64e608 17 Buzzer buzzer(p21);
adelino 0:4c9dcb64e608 18
adelino 0:4c9dcb64e608 19 Music* pMusic=0; //the song
adelino 0:4c9dcb64e608 20 Note la("A#4",50); //the sound
adelino 0:4c9dcb64e608 21
adelino 0:4c9dcb64e608 22 int main()
adelino 0:4c9dcb64e608 23 {
adelino 0:4c9dcb64e608 24 pMusic= new Music(song);
adelino 0:4c9dcb64e608 25 pMusic->play(&buzzer);
adelino 0:4c9dcb64e608 26
adelino 0:4c9dcb64e608 27 buzzer.tone(&la);
adelino 0:4c9dcb64e608 28 delete(pMusic);
adelino 0:4c9dcb64e608 29 }
adelino 0:4c9dcb64e608 30
adelino 0:4c9dcb64e608 31 */
adelino 0:4c9dcb64e608 32
adelino 0:4c9dcb64e608 33 // notes definition
adelino 0:4c9dcb64e608 34 #define NOTE_R 0
adelino 0:4c9dcb64e608 35
adelino 0:4c9dcb64e608 36 //octave 3
adelino 0:4c9dcb64e608 37 #define NOTE_C3 7644
adelino 0:4c9dcb64e608 38 #define NOTE_CS3 7215
adelino 0:4c9dcb64e608 39 #define NOTE_D3 6810
adelino 0:4c9dcb64e608 40 #define NOTE_DS3 6428
adelino 0:4c9dcb64e608 41 #define NOTE_E3 6067
adelino 0:4c9dcb64e608 42 #define NOTE_F3 5727
adelino 0:4c9dcb64e608 43 #define NOTE_FS3 5405
adelino 0:4c9dcb64e608 44 #define NOTE_G3 5102
adelino 0:4c9dcb64e608 45 #define NOTE_GS3 4815
adelino 0:4c9dcb64e608 46 #define NOTE_A3 4545
adelino 0:4c9dcb64e608 47 #define NOTE_AS3 4290
adelino 0:4c9dcb64e608 48 #define NOTE_B3 4049
adelino 0:4c9dcb64e608 49
adelino 0:4c9dcb64e608 50 //octave 4
adelino 0:4c9dcb64e608 51 #define NOTE_C4 3822
adelino 0:4c9dcb64e608 52 #define NOTE_CS4 3607
adelino 0:4c9dcb64e608 53 #define NOTE_D4 3405
adelino 0:4c9dcb64e608 54 #define NOTE_DS4 3214
adelino 0:4c9dcb64e608 55 #define NOTE_E4 3033
adelino 0:4c9dcb64e608 56 #define NOTE_F4 2863
adelino 0:4c9dcb64e608 57 #define NOTE_FS4 2702
adelino 0:4c9dcb64e608 58 #define NOTE_G4 2551
adelino 0:4c9dcb64e608 59 #define NOTE_GS4 2408
adelino 0:4c9dcb64e608 60 #define NOTE_A4 2272
adelino 0:4c9dcb64e608 61 #define NOTE_AS4 2145
adelino 0:4c9dcb64e608 62 #define NOTE_B4 2024
adelino 0:4c9dcb64e608 63
adelino 0:4c9dcb64e608 64 //octave 5
adelino 0:4c9dcb64e608 65 #define NOTE_C5 1911
adelino 0:4c9dcb64e608 66 #define NOTE_CS5 1804
adelino 0:4c9dcb64e608 67 #define NOTE_D5 1702
adelino 0:4c9dcb64e608 68 #define NOTE_DS5 1607
adelino 0:4c9dcb64e608 69 #define NOTE_E5 1516
adelino 0:4c9dcb64e608 70 #define NOTE_F5 1432
adelino 0:4c9dcb64e608 71 #define NOTE_FS5 1351
adelino 0:4c9dcb64e608 72 #define NOTE_G5 1275
adelino 0:4c9dcb64e608 73 #define NOTE_GS5 1204
adelino 0:4c9dcb64e608 74 #define NOTE_A5 1136
adelino 0:4c9dcb64e608 75 #define NOTE_AS5 1072
adelino 0:4c9dcb64e608 76 #define NOTE_B5 1012
adelino 0:4c9dcb64e608 77
adelino 0:4c9dcb64e608 78 //octave 6
adelino 0:4c9dcb64e608 79 #define NOTE_C6 956
adelino 0:4c9dcb64e608 80 #define NOTE_CS6 903
adelino 0:4c9dcb64e608 81 #define NOTE_D6 852
adelino 0:4c9dcb64e608 82 #define NOTE_DS6 803
adelino 0:4c9dcb64e608 83 #define NOTE_E6 759
adelino 0:4c9dcb64e608 84 #define NOTE_F6 716
adelino 0:4c9dcb64e608 85 #define NOTE_FS6 676
adelino 0:4c9dcb64e608 86 #define NOTE_G6 638
adelino 0:4c9dcb64e608 87 #define NOTE_GS6 602
adelino 0:4c9dcb64e608 88 #define NOTE_A6 568
adelino 0:4c9dcb64e608 89 #define NOTE_AS6 536
adelino 0:4c9dcb64e608 90 #define NOTE_B6 506
adelino 0:4c9dcb64e608 91
adelino 0:4c9dcb64e608 92 //octave 7
adelino 0:4c9dcb64e608 93 #define NOTE_C7 477
adelino 0:4c9dcb64e608 94 #define NOTE_CS7 451
adelino 0:4c9dcb64e608 95 #define NOTE_D7 426
adelino 0:4c9dcb64e608 96 #define NOTE_DS7 402
adelino 0:4c9dcb64e608 97 #define NOTE_E7 379
adelino 0:4c9dcb64e608 98 #define NOTE_F7 358
adelino 0:4c9dcb64e608 99 #define NOTE_FS7 338
adelino 0:4c9dcb64e608 100 #define NOTE_G7 319
adelino 0:4c9dcb64e608 101 #define NOTE_GS7 301
adelino 0:4c9dcb64e608 102 #define NOTE_A7 284
adelino 0:4c9dcb64e608 103 #define NOTE_AS7 268
adelino 0:4c9dcb64e608 104 #define NOTE_B7 253
adelino 0:4c9dcb64e608 105 //------------------------------
adelino 0:4c9dcb64e608 106 class Note
adelino 0:4c9dcb64e608 107 {
adelino 0:4c9dcb64e608 108 public:
adelino 0:4c9dcb64e608 109 /** Construct a Note (sound) object.
adelino 0:4c9dcb64e608 110 *
adelino 0:4c9dcb64e608 111 * @param _note the note in range (A,a,B,b,C,c,D,d,E,e,F,f)
adelino 0:4c9dcb64e608 112 * @param _isSharp exemple A4 or A#4
adelino 0:4c9dcb64e608 113 * @param _duration the time duration
adelino 0:4c9dcb64e608 114 */
adelino 0:4c9dcb64e608 115
adelino 0:4c9dcb64e608 116 Note(char _note,bool _isSharp,char _octave,int _duration);
adelino 0:4c9dcb64e608 117
adelino 0:4c9dcb64e608 118 /** Construct a Note (sound) object.
adelino 0:4c9dcb64e608 119 *
adelino 0:4c9dcb64e608 120 * @param str exemple "C%4" or "C#4"
adelino 0:4c9dcb64e608 121 * @param _duration the time duration
adelino 0:4c9dcb64e608 122 */
adelino 0:4c9dcb64e608 123
adelino 0:4c9dcb64e608 124 Note(char* str,int _duration);// exemple "C%4" or "C#4"
adelino 0:4c9dcb64e608 125
adelino 0:4c9dcb64e608 126
adelino 0:4c9dcb64e608 127 //setter
adelino 0:4c9dcb64e608 128
adelino 0:4c9dcb64e608 129 //getter
adelino 0:4c9dcb64e608 130
adelino 0:4c9dcb64e608 131 /// @returns the note period (PwmOutput)
adelino 0:4c9dcb64e608 132 int getNotePeriod_us(void)const ;
adelino 0:4c9dcb64e608 133
adelino 0:4c9dcb64e608 134 /// @returns is Sharp
adelino 0:4c9dcb64e608 135 bool getIsSharp(void) const;
adelino 0:4c9dcb64e608 136
adelino 0:4c9dcb64e608 137 /// @returns the note char
adelino 0:4c9dcb64e608 138 char getNoteChar(void) const;
adelino 0:4c9dcb64e608 139
adelino 0:4c9dcb64e608 140 /// @returns the octave
adelino 0:4c9dcb64e608 141 char getOctave(void) const;
adelino 0:4c9dcb64e608 142
adelino 0:4c9dcb64e608 143 /// @returns the duartion in ms
adelino 0:4c9dcb64e608 144 int getDuration(void) const;
adelino 0:4c9dcb64e608 145
adelino 0:4c9dcb64e608 146 ///use in debugging
adelino 0:4c9dcb64e608 147 void toString(char* buffer);
adelino 0:4c9dcb64e608 148
adelino 0:4c9dcb64e608 149
adelino 0:4c9dcb64e608 150 protected:
adelino 0:4c9dcb64e608 151
adelino 0:4c9dcb64e608 152 int calculatePeriod_us(void);
adelino 0:4c9dcb64e608 153
adelino 0:4c9dcb64e608 154 void parse(const char* _str);
adelino 0:4c9dcb64e608 155
adelino 0:4c9dcb64e608 156 char myNoteChar;
adelino 0:4c9dcb64e608 157 bool myIsSharp;
adelino 0:4c9dcb64e608 158 char myOctave; // 3->5
adelino 0:4c9dcb64e608 159 int myPeriod_us;
adelino 0:4c9dcb64e608 160 int myDuration;
adelino 0:4c9dcb64e608 161
adelino 0:4c9dcb64e608 162 };
adelino 0:4c9dcb64e608 163 //----------------------------------
adelino 0:4c9dcb64e608 164
adelino 0:4c9dcb64e608 165
adelino 0:4c9dcb64e608 166 class Buzzer:public PwmOut
adelino 0:4c9dcb64e608 167 {
adelino 0:4c9dcb64e608 168 public:
adelino 0:4c9dcb64e608 169 /** Construct a Buzzer object.
adelino 0:4c9dcb64e608 170 *
adelino 0:4c9dcb64e608 171 * @param _pwmOut select the PwmOutput
adelino 0:4c9dcb64e608 172 */
adelino 0:4c9dcb64e608 173
adelino 0:4c9dcb64e608 174 Buzzer(PinName _pwmOut);
adelino 0:4c9dcb64e608 175 //
adelino 0:4c9dcb64e608 176 ~Buzzer();
adelino 0:4c9dcb64e608 177
adelino 0:4c9dcb64e608 178 /// The Buzzer tone a simple sound
adelino 0:4c9dcb64e608 179 void tone(const Note* _note);
adelino 0:4c9dcb64e608 180
adelino 0:4c9dcb64e608 181
adelino 0:4c9dcb64e608 182 protected:
adelino 0:4c9dcb64e608 183
adelino 0:4c9dcb64e608 184 };
adelino 0:4c9dcb64e608 185
adelino 0:4c9dcb64e608 186
adelino 0:4c9dcb64e608 187 //----------------------------------
adelino 0:4c9dcb64e608 188 class Music
adelino 0:4c9dcb64e608 189 {
adelino 0:4c9dcb64e608 190 public:
adelino 0:4c9dcb64e608 191 /** Construct a Music object.
adelino 0:4c9dcb64e608 192 *
adelino 0:4c9dcb64e608 193 * @param p the RTTTL song
adelino 0:4c9dcb64e608 194 */
adelino 0:4c9dcb64e608 195 Music(const char* p);
adelino 0:4c9dcb64e608 196
adelino 0:4c9dcb64e608 197 ~Music();
adelino 0:4c9dcb64e608 198
adelino 0:4c9dcb64e608 199 /// play the music
adelino 0:4c9dcb64e608 200 void play(Buzzer* _buzzer);
adelino 0:4c9dcb64e608 201
adelino 0:4c9dcb64e608 202 /// @returns the music lenght
adelino 0:4c9dcb64e608 203 int getNumbersNotes(void) const;
adelino 0:4c9dcb64e608 204
adelino 0:4c9dcb64e608 205 protected:
adelino 0:4c9dcb64e608 206 int nbNotes;
adelino 0:4c9dcb64e608 207 vector<Note*> myTabNotes;
adelino 0:4c9dcb64e608 208
adelino 0:4c9dcb64e608 209
adelino 0:4c9dcb64e608 210 };
adelino 0:4c9dcb64e608 211
adelino 0:4c9dcb64e608 212 #endif