Class to play tones, various sounds and music in MML format using a PWM channel.
PwmSound.h@0:185bcd9f8e19, 2014-03-30 (annotated)
- Committer:
- paulg
- Date:
- Sun Mar 30 22:50:27 2014 +0000
- Revision:
- 0:185bcd9f8e19
- Child:
- 1:67056b9df9ff
Initial release.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
paulg | 0:185bcd9f8e19 | 1 | /****************************************************************************** |
paulg | 0:185bcd9f8e19 | 2 | * File: PwmSound.h |
paulg | 0:185bcd9f8e19 | 3 | * Author: Paul Griffith |
paulg | 0:185bcd9f8e19 | 4 | * Created: 25 Mar 2014 |
paulg | 0:185bcd9f8e19 | 5 | * Last Edit: see below |
paulg | 0:185bcd9f8e19 | 6 | * Version: see below |
paulg | 0:185bcd9f8e19 | 7 | * |
paulg | 0:185bcd9f8e19 | 8 | * Description: |
paulg | 0:185bcd9f8e19 | 9 | * Definitions for PwmSound class. |
paulg | 0:185bcd9f8e19 | 10 | * |
paulg | 0:185bcd9f8e19 | 11 | * Copyright (c) 2014 Paul Griffith, MIT License |
paulg | 0:185bcd9f8e19 | 12 | * |
paulg | 0:185bcd9f8e19 | 13 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
paulg | 0:185bcd9f8e19 | 14 | * of this software and associated documentation files (the "Software"), to |
paulg | 0:185bcd9f8e19 | 15 | * deal in the Software without restriction, including without limitation the |
paulg | 0:185bcd9f8e19 | 16 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
paulg | 0:185bcd9f8e19 | 17 | * sell copies of the Software, and to permit persons to whom the Software is |
paulg | 0:185bcd9f8e19 | 18 | * furnished to do so, subject to the following conditions: |
paulg | 0:185bcd9f8e19 | 19 | * |
paulg | 0:185bcd9f8e19 | 20 | * The above copyright notice and this permission notice shall be included in |
paulg | 0:185bcd9f8e19 | 21 | * all copies or substantial portions of the Software. |
paulg | 0:185bcd9f8e19 | 22 | * |
paulg | 0:185bcd9f8e19 | 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
paulg | 0:185bcd9f8e19 | 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
paulg | 0:185bcd9f8e19 | 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
paulg | 0:185bcd9f8e19 | 26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
paulg | 0:185bcd9f8e19 | 27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
paulg | 0:185bcd9f8e19 | 28 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
paulg | 0:185bcd9f8e19 | 29 | * IN THE SOFTWARE. |
paulg | 0:185bcd9f8e19 | 30 | * |
paulg | 0:185bcd9f8e19 | 31 | * Modifications: |
paulg | 0:185bcd9f8e19 | 32 | * Ver Date By Details |
paulg | 0:185bcd9f8e19 | 33 | * 0.00 25Mar14 PG File created. |
paulg | 0:185bcd9f8e19 | 34 | * 1.00 30Mar14 PG Initial release |
paulg | 0:185bcd9f8e19 | 35 | * |
paulg | 0:185bcd9f8e19 | 36 | ******************************************************************************/ |
paulg | 0:185bcd9f8e19 | 37 | |
paulg | 0:185bcd9f8e19 | 38 | #ifndef MBED_PWMSOUND_H |
paulg | 0:185bcd9f8e19 | 39 | #define MBED_PWMSOUND_H |
paulg | 0:185bcd9f8e19 | 40 | |
paulg | 0:185bcd9f8e19 | 41 | #include "mbed.h" |
paulg | 0:185bcd9f8e19 | 42 | |
paulg | 0:185bcd9f8e19 | 43 | class PwmSound { |
paulg | 0:185bcd9f8e19 | 44 | //private: |
paulg | 0:185bcd9f8e19 | 45 | |
paulg | 0:185bcd9f8e19 | 46 | public: |
paulg | 0:185bcd9f8e19 | 47 | enum MusicStyle { STACCATO, NORMAL, LEGATO }; |
paulg | 0:185bcd9f8e19 | 48 | |
paulg | 0:185bcd9f8e19 | 49 | PwmSound(PinName pin); |
paulg | 0:185bcd9f8e19 | 50 | |
paulg | 0:185bcd9f8e19 | 51 | void tone(float frequency, float duration); //tones |
paulg | 0:185bcd9f8e19 | 52 | void tone(float frequency); |
paulg | 0:185bcd9f8e19 | 53 | void stop(void); |
paulg | 0:185bcd9f8e19 | 54 | void duration(float duration); |
paulg | 0:185bcd9f8e19 | 55 | void volume(float volume); |
paulg | 0:185bcd9f8e19 | 56 | |
paulg | 0:185bcd9f8e19 | 57 | void bip(int n = 1); //beeps and other sounds |
paulg | 0:185bcd9f8e19 | 58 | void beep(int n = 1); |
paulg | 0:185bcd9f8e19 | 59 | void bleep(int n = 1); |
paulg | 0:185bcd9f8e19 | 60 | void buzz(int n = 1); |
paulg | 0:185bcd9f8e19 | 61 | void siren(int n = 1); |
paulg | 0:185bcd9f8e19 | 62 | void trill(int n = 1); |
paulg | 0:185bcd9f8e19 | 63 | void phone(int n = 1); |
paulg | 0:185bcd9f8e19 | 64 | |
paulg | 0:185bcd9f8e19 | 65 | void note(int number, int length); //musical note |
paulg | 0:185bcd9f8e19 | 66 | void tempo(int tempo); |
paulg | 0:185bcd9f8e19 | 67 | void style(MusicStyle style); |
paulg | 0:185bcd9f8e19 | 68 | |
paulg | 0:185bcd9f8e19 | 69 | void tune(unsigned char* t); //play tune from data in array |
paulg | 0:185bcd9f8e19 | 70 | |
paulg | 0:185bcd9f8e19 | 71 | private: |
paulg | 0:185bcd9f8e19 | 72 | PwmOut _pin; |
paulg | 0:185bcd9f8e19 | 73 | float _duration; //duration of tone in seconds |
paulg | 0:185bcd9f8e19 | 74 | float _volume; //crude volume 0.0-1.0 => 0-50% duty cycle |
paulg | 0:185bcd9f8e19 | 75 | int _tempo; //pace of music in beats per minute (32-255) |
paulg | 0:185bcd9f8e19 | 76 | //one beat equals one quarter note (ie a crotchet) |
paulg | 0:185bcd9f8e19 | 77 | int _length; //length of note (1-64), 1 = whole note, 4 = quarter etc |
paulg | 0:185bcd9f8e19 | 78 | MusicStyle _style; |
paulg | 0:185bcd9f8e19 | 79 | |
paulg | 0:185bcd9f8e19 | 80 | //the following support continuous two-tone sounds in background |
paulg | 0:185bcd9f8e19 | 81 | void _setup(float freq1, float freq2, float duration); |
paulg | 0:185bcd9f8e19 | 82 | void _sustain(void); |
paulg | 0:185bcd9f8e19 | 83 | Ticker _sustainTkr; |
paulg | 0:185bcd9f8e19 | 84 | float _freq1; |
paulg | 0:185bcd9f8e19 | 85 | float _freq2; |
paulg | 0:185bcd9f8e19 | 86 | bool _beat; |
paulg | 0:185bcd9f8e19 | 87 | bool _playing; |
paulg | 0:185bcd9f8e19 | 88 | }; |
paulg | 0:185bcd9f8e19 | 89 | |
paulg | 0:185bcd9f8e19 | 90 | #endif |
paulg | 0:185bcd9f8e19 | 91 | |
paulg | 0:185bcd9f8e19 | 92 | // END of PwmSound.h |