Class to play tones, various sounds and music in MML format using a PWM channel.
Diff: PwmSound.h
- Revision:
- 1:67056b9df9ff
- Parent:
- 0:185bcd9f8e19
--- a/PwmSound.h Sun Mar 30 22:50:27 2014 +0000 +++ b/PwmSound.h Tue May 06 13:16:28 2014 +0000 @@ -32,6 +32,7 @@ * Ver Date By Details * 0.00 25Mar14 PG File created. * 1.00 30Mar14 PG Initial release + * 2.00 06May14 PG Added play() etc to support MML music. Removed tune() etc. * ******************************************************************************/ @@ -44,17 +45,14 @@ //private: public: - enum MusicStyle { STACCATO, NORMAL, LEGATO }; - PwmSound(PinName pin); - void tone(float frequency, float duration); //tones - void tone(float frequency); + void tone(float frequency, float duration = 0.0); //tones void stop(void); - void duration(float duration); - void volume(float volume); + void timbre(int timbre); void bip(int n = 1); //beeps and other sounds + void bop(int n = 1); void beep(int n = 1); void bleep(int n = 1); void buzz(int n = 1); @@ -62,31 +60,43 @@ void trill(int n = 1); void phone(int n = 1); - void note(int number, int length); //musical note - void tempo(int tempo); - void style(MusicStyle style); - - void tune(unsigned char* t); //play tune from data in array + int play(char* m, int options = 0); //play tune in MML format private: PwmOut _pin; - float _duration; //duration of tone in seconds - float _volume; //crude volume 0.0-1.0 => 0-50% duty cycle + float _dutyCycle; //PWM duty cycle 0-50% + + //the following support continuous two-tone sounds in background + void _setup(float freq1, float dur1, float freq2, float dur2); + void _sustain(void); + Timeout _sustainTmo; + int _period1; //in us + unsigned int _dur1; //in us + int _period2; + unsigned int _dur2; + bool _phase; + bool _playing; + + //the following support play + void _note(int number, int length, int dots = 0); + char _getChar(void); + char _nextChar(void); + int _getNumber(void); + int _getDots(void); + int _octave; //current octave + int _shift; //octave shift from < and > int _tempo; //pace of music in beats per minute (32-255) //one beat equals one quarter note (ie a crotchet) int _length; //length of note (1-64), 1 = whole note, 4 = quarter etc - MusicStyle _style; - - //the following support continuous two-tone sounds in background - void _setup(float freq1, float freq2, float duration); - void _sustain(void); - Ticker _sustainTkr; - float _freq1; - float _freq2; - bool _beat; - bool _playing; + float _1dot; //note length extension factors + float _2dots; + float _3dots; + int _style; //music style (1-8), 6 = Staccato, 7 = Normal, 8 = Legato + char* _mp; //current position in music string + char _nextCh; + bool _haveNext; }; #endif -// END of PwmSound.h \ No newline at end of file +// END of PwmSound.h