Class to play tones, various sounds and music in MML format using a PWM channel.
PwmSound.h@1:67056b9df9ff, 2014-05-06 (annotated)
- Committer:
- paulg
- Date:
- Tue May 06 13:16:28 2014 +0000
- Revision:
- 1:67056b9df9ff
- Parent:
- 0:185bcd9f8e19
Added play() function to play music written in Music Macro Language (MML) format. Removed tune() as no longer required.
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 | 1:67056b9df9ff | 35 | * 2.00 06May14 PG Added play() etc to support MML music. Removed tune() etc. |
paulg | 0:185bcd9f8e19 | 36 | * |
paulg | 0:185bcd9f8e19 | 37 | ******************************************************************************/ |
paulg | 0:185bcd9f8e19 | 38 | |
paulg | 0:185bcd9f8e19 | 39 | #ifndef MBED_PWMSOUND_H |
paulg | 0:185bcd9f8e19 | 40 | #define MBED_PWMSOUND_H |
paulg | 0:185bcd9f8e19 | 41 | |
paulg | 0:185bcd9f8e19 | 42 | #include "mbed.h" |
paulg | 0:185bcd9f8e19 | 43 | |
paulg | 0:185bcd9f8e19 | 44 | class PwmSound { |
paulg | 0:185bcd9f8e19 | 45 | //private: |
paulg | 0:185bcd9f8e19 | 46 | |
paulg | 0:185bcd9f8e19 | 47 | public: |
paulg | 0:185bcd9f8e19 | 48 | PwmSound(PinName pin); |
paulg | 0:185bcd9f8e19 | 49 | |
paulg | 1:67056b9df9ff | 50 | void tone(float frequency, float duration = 0.0); //tones |
paulg | 0:185bcd9f8e19 | 51 | void stop(void); |
paulg | 1:67056b9df9ff | 52 | void timbre(int timbre); |
paulg | 0:185bcd9f8e19 | 53 | |
paulg | 0:185bcd9f8e19 | 54 | void bip(int n = 1); //beeps and other sounds |
paulg | 1:67056b9df9ff | 55 | void bop(int n = 1); |
paulg | 0:185bcd9f8e19 | 56 | void beep(int n = 1); |
paulg | 0:185bcd9f8e19 | 57 | void bleep(int n = 1); |
paulg | 0:185bcd9f8e19 | 58 | void buzz(int n = 1); |
paulg | 0:185bcd9f8e19 | 59 | void siren(int n = 1); |
paulg | 0:185bcd9f8e19 | 60 | void trill(int n = 1); |
paulg | 0:185bcd9f8e19 | 61 | void phone(int n = 1); |
paulg | 0:185bcd9f8e19 | 62 | |
paulg | 1:67056b9df9ff | 63 | int play(char* m, int options = 0); //play tune in MML format |
paulg | 0:185bcd9f8e19 | 64 | |
paulg | 0:185bcd9f8e19 | 65 | private: |
paulg | 0:185bcd9f8e19 | 66 | PwmOut _pin; |
paulg | 1:67056b9df9ff | 67 | float _dutyCycle; //PWM duty cycle 0-50% |
paulg | 1:67056b9df9ff | 68 | |
paulg | 1:67056b9df9ff | 69 | //the following support continuous two-tone sounds in background |
paulg | 1:67056b9df9ff | 70 | void _setup(float freq1, float dur1, float freq2, float dur2); |
paulg | 1:67056b9df9ff | 71 | void _sustain(void); |
paulg | 1:67056b9df9ff | 72 | Timeout _sustainTmo; |
paulg | 1:67056b9df9ff | 73 | int _period1; //in us |
paulg | 1:67056b9df9ff | 74 | unsigned int _dur1; //in us |
paulg | 1:67056b9df9ff | 75 | int _period2; |
paulg | 1:67056b9df9ff | 76 | unsigned int _dur2; |
paulg | 1:67056b9df9ff | 77 | bool _phase; |
paulg | 1:67056b9df9ff | 78 | bool _playing; |
paulg | 1:67056b9df9ff | 79 | |
paulg | 1:67056b9df9ff | 80 | //the following support play |
paulg | 1:67056b9df9ff | 81 | void _note(int number, int length, int dots = 0); |
paulg | 1:67056b9df9ff | 82 | char _getChar(void); |
paulg | 1:67056b9df9ff | 83 | char _nextChar(void); |
paulg | 1:67056b9df9ff | 84 | int _getNumber(void); |
paulg | 1:67056b9df9ff | 85 | int _getDots(void); |
paulg | 1:67056b9df9ff | 86 | int _octave; //current octave |
paulg | 1:67056b9df9ff | 87 | int _shift; //octave shift from < and > |
paulg | 0:185bcd9f8e19 | 88 | int _tempo; //pace of music in beats per minute (32-255) |
paulg | 0:185bcd9f8e19 | 89 | //one beat equals one quarter note (ie a crotchet) |
paulg | 0:185bcd9f8e19 | 90 | int _length; //length of note (1-64), 1 = whole note, 4 = quarter etc |
paulg | 1:67056b9df9ff | 91 | float _1dot; //note length extension factors |
paulg | 1:67056b9df9ff | 92 | float _2dots; |
paulg | 1:67056b9df9ff | 93 | float _3dots; |
paulg | 1:67056b9df9ff | 94 | int _style; //music style (1-8), 6 = Staccato, 7 = Normal, 8 = Legato |
paulg | 1:67056b9df9ff | 95 | char* _mp; //current position in music string |
paulg | 1:67056b9df9ff | 96 | char _nextCh; |
paulg | 1:67056b9df9ff | 97 | bool _haveNext; |
paulg | 0:185bcd9f8e19 | 98 | }; |
paulg | 0:185bcd9f8e19 | 99 | |
paulg | 0:185bcd9f8e19 | 100 | #endif |
paulg | 0:185bcd9f8e19 | 101 | |
paulg | 1:67056b9df9ff | 102 | // END of PwmSound.h |