Class to play tones, various sounds and music in MML format using a PWM channel.
Diff: PwmSound.h
- Revision:
- 0:185bcd9f8e19
- Child:
- 1:67056b9df9ff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PwmSound.h Sun Mar 30 22:50:27 2014 +0000 @@ -0,0 +1,92 @@ +/****************************************************************************** + * File: PwmSound.h + * Author: Paul Griffith + * Created: 25 Mar 2014 + * Last Edit: see below + * Version: see below + * + * Description: + * Definitions for PwmSound class. + * + * Copyright (c) 2014 Paul Griffith, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + * Modifications: + * Ver Date By Details + * 0.00 25Mar14 PG File created. + * 1.00 30Mar14 PG Initial release + * + ******************************************************************************/ + +#ifndef MBED_PWMSOUND_H +#define MBED_PWMSOUND_H + +#include "mbed.h" + +class PwmSound { +//private: + +public: + enum MusicStyle { STACCATO, NORMAL, LEGATO }; + + PwmSound(PinName pin); + + void tone(float frequency, float duration); //tones + void tone(float frequency); + void stop(void); + void duration(float duration); + void volume(float volume); + + void bip(int n = 1); //beeps and other sounds + void beep(int n = 1); + void bleep(int n = 1); + void buzz(int n = 1); + void siren(int n = 1); + 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 + +private: + PwmOut _pin; + float _duration; //duration of tone in seconds + float _volume; //crude volume 0.0-1.0 => 0-50% duty cycle + 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; +}; + +#endif + +// END of PwmSound.h \ No newline at end of file