Tool for playing 8bit like music on Piezo speaker.
PlayTone.h@2:9a8ae3a8e5ea, 2017-02-25 (annotated)
- Committer:
- TeaPack_CZ
- Date:
- Sat Feb 25 22:20:47 2017 +0000
- Revision:
- 2:9a8ae3a8e5ea
- Parent:
- 1:90161871ad88
revert incremental transpose level to absolute.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TeaPack_CZ | 0:b8d34e475dd9 | 1 | #ifndef PLAYTONE |
TeaPack_CZ | 0:b8d34e475dd9 | 2 | #define PLAYTONE |
TeaPack_CZ | 0:b8d34e475dd9 | 3 | |
TeaPack_CZ | 0:b8d34e475dd9 | 4 | #include "mbed.h" |
TeaPack_CZ | 0:b8d34e475dd9 | 5 | #include "Piezo.h" |
TeaPack_CZ | 0:b8d34e475dd9 | 6 | |
TeaPack_CZ | 0:b8d34e475dd9 | 7 | #ifndef NOTE |
TeaPack_CZ | 0:b8d34e475dd9 | 8 | #define NOTE |
TeaPack_CZ | 0:b8d34e475dd9 | 9 | |
TeaPack_CZ | 0:b8d34e475dd9 | 10 | struct Note { |
TeaPack_CZ | 0:b8d34e475dd9 | 11 | int idx; |
TeaPack_CZ | 0:b8d34e475dd9 | 12 | float value; |
TeaPack_CZ | 0:b8d34e475dd9 | 13 | }; |
TeaPack_CZ | 0:b8d34e475dd9 | 14 | |
TeaPack_CZ | 0:b8d34e475dd9 | 15 | #endif |
TeaPack_CZ | 0:b8d34e475dd9 | 16 | |
TeaPack_CZ | 1:90161871ad88 | 17 | /** Class for playing 8bit-like songs on PWM speaker. |
TeaPack_CZ | 1:90161871ad88 | 18 | * This class suports easy conversion from notes to sound from piezo speaker. |
TeaPack_CZ | 1:90161871ad88 | 19 | * |
TeaPack_CZ | 1:90161871ad88 | 20 | * Currently tested with LPC1768.\n\n |
TeaPack_CZ | 1:90161871ad88 | 21 | * |
TeaPack_CZ | 1:90161871ad88 | 22 | * |
TeaPack_CZ | 1:90161871ad88 | 23 | * Writen by: Jan Crha (TeaPack_CZ), 2016. |
TeaPack_CZ | 1:90161871ad88 | 24 | * |
TeaPack_CZ | 1:90161871ad88 | 25 | * Last modified: 2016-10-02 |
TeaPack_CZ | 1:90161871ad88 | 26 | * |
TeaPack_CZ | 1:90161871ad88 | 27 | * Example: |
TeaPack_CZ | 1:90161871ad88 | 28 | * @code |
TeaPack_CZ | 1:90161871ad88 | 29 | * #include "mbed.h" |
TeaPack_CZ | 1:90161871ad88 | 30 | * |
TeaPack_CZ | 1:90161871ad88 | 31 | * #include "PlayTone.h" |
TeaPack_CZ | 1:90161871ad88 | 32 | * #include "Popcorn.h" |
TeaPack_CZ | 1:90161871ad88 | 33 | * |
TeaPack_CZ | 1:90161871ad88 | 34 | * PlayTone Pt(p21); |
TeaPack_CZ | 1:90161871ad88 | 35 | * Popcorn Pop; |
TeaPack_CZ | 1:90161871ad88 | 36 | * |
TeaPack_CZ | 1:90161871ad88 | 37 | * int main() { |
TeaPack_CZ | 1:90161871ad88 | 38 | * |
TeaPack_CZ | 1:90161871ad88 | 39 | * int PopLen = Pop.GetLen(); |
TeaPack_CZ | 1:90161871ad88 | 40 | * int PopBPM = Pop.GetBPM(); |
TeaPack_CZ | 1:90161871ad88 | 41 | * |
TeaPack_CZ | 1:90161871ad88 | 42 | * Note PopcornTheme[PopLen]; |
TeaPack_CZ | 1:90161871ad88 | 43 | * Pop.GetPopcorn(PopcornTheme); |
TeaPack_CZ | 1:90161871ad88 | 44 | * |
TeaPack_CZ | 1:90161871ad88 | 45 | * Pt.setBPM(PopBPM); |
TeaPack_CZ | 1:90161871ad88 | 46 | * Pt.setStaccatoDuty(1/16.0); |
TeaPack_CZ | 1:90161871ad88 | 47 | * |
TeaPack_CZ | 1:90161871ad88 | 48 | * Pt.playStaccatoSequence(PopLen, PopcornTheme); |
TeaPack_CZ | 1:90161871ad88 | 49 | * } |
TeaPack_CZ | 1:90161871ad88 | 50 | * @endcode |
TeaPack_CZ | 1:90161871ad88 | 51 | */ |
TeaPack_CZ | 1:90161871ad88 | 52 | |
TeaPack_CZ | 0:b8d34e475dd9 | 53 | class PlayTone{ |
TeaPack_CZ | 0:b8d34e475dd9 | 54 | |
TeaPack_CZ | 0:b8d34e475dd9 | 55 | public: |
TeaPack_CZ | 1:90161871ad88 | 56 | |
TeaPack_CZ | 1:90161871ad88 | 57 | /** Constructor, chosen pin must be PWM compatible*/ |
TeaPack_CZ | 0:b8d34e475dd9 | 58 | PlayTone(PinName PS); |
TeaPack_CZ | 0:b8d34e475dd9 | 59 | |
TeaPack_CZ | 1:90161871ad88 | 60 | /** Function for setting BPM. |
TeaPack_CZ | 1:90161871ad88 | 61 | * \param Bpm range is from 40 to 260 |
TeaPack_CZ | 1:90161871ad88 | 62 | * @returns Duration of note in ms |
TeaPack_CZ | 1:90161871ad88 | 63 | */ |
TeaPack_CZ | 0:b8d34e475dd9 | 64 | int setBPM(int); |
TeaPack_CZ | 1:90161871ad88 | 65 | |
TeaPack_CZ | 1:90161871ad88 | 66 | /** Function for transposing whole set of notes. |
TeaPack_CZ | 1:90161871ad88 | 67 | */ |
TeaPack_CZ | 0:b8d34e475dd9 | 68 | void transpose(int); |
TeaPack_CZ | 0:b8d34e475dd9 | 69 | |
TeaPack_CZ | 1:90161871ad88 | 70 | /** Function for playing single tone. |
TeaPack_CZ | 1:90161871ad88 | 71 | * \param index of note from list |
TeaPack_CZ | 1:90161871ad88 | 72 | * \param length of note based from setted BPM |
TeaPack_CZ | 1:90161871ad88 | 73 | */ |
TeaPack_CZ | 0:b8d34e475dd9 | 74 | void playTone(int,float); |
TeaPack_CZ | 1:90161871ad88 | 75 | |
TeaPack_CZ | 1:90161871ad88 | 76 | /** Function for playing sequence of tones. |
TeaPack_CZ | 1:90161871ad88 | 77 | * \param sequence_length length of sequence to play |
TeaPack_CZ | 1:90161871ad88 | 78 | * \param notes[] list of notes indexes |
TeaPack_CZ | 1:90161871ad88 | 79 | * \param values[] list of note lengths based from setted BPM |
TeaPack_CZ | 1:90161871ad88 | 80 | */ |
TeaPack_CZ | 0:b8d34e475dd9 | 81 | void playSequence(int,int[],float[]); |
TeaPack_CZ | 1:90161871ad88 | 82 | |
TeaPack_CZ | 1:90161871ad88 | 83 | /** Function for playing sequence of tones. |
TeaPack_CZ | 1:90161871ad88 | 84 | * \param sequence_length length of sequence |
TeaPack_CZ | 1:90161871ad88 | 85 | * \param Notes[] list of Notes |
TeaPack_CZ | 1:90161871ad88 | 86 | */ |
TeaPack_CZ | 0:b8d34e475dd9 | 87 | void playSequence(int,Note[]); |
TeaPack_CZ | 0:b8d34e475dd9 | 88 | |
TeaPack_CZ | 1:90161871ad88 | 89 | /** Function for setting constant staccato note duty cycle based on BPM |
TeaPack_CZ | 1:90161871ad88 | 90 | * and independently on note value. |
TeaPack_CZ | 1:90161871ad88 | 91 | * \param duty in range from 0 to 1 |
TeaPack_CZ | 1:90161871ad88 | 92 | */ |
TeaPack_CZ | 0:b8d34e475dd9 | 93 | void setStaccatoDuty(float); |
TeaPack_CZ | 1:90161871ad88 | 94 | |
TeaPack_CZ | 1:90161871ad88 | 95 | /** Function for playing single tone in Staccato |
TeaPack_CZ | 1:90161871ad88 | 96 | * \sa playTone() |
TeaPack_CZ | 1:90161871ad88 | 97 | * \sa setStaccatoDuty() |
TeaPack_CZ | 1:90161871ad88 | 98 | */ |
TeaPack_CZ | 0:b8d34e475dd9 | 99 | void playStaccato(int,float); |
TeaPack_CZ | 1:90161871ad88 | 100 | |
TeaPack_CZ | 1:90161871ad88 | 101 | /** Function for playing sequence of tones in Staccato. |
TeaPack_CZ | 1:90161871ad88 | 102 | * \sa playSequence() |
TeaPack_CZ | 1:90161871ad88 | 103 | * \sa setStaccatoDuty() |
TeaPack_CZ | 1:90161871ad88 | 104 | */ |
TeaPack_CZ | 0:b8d34e475dd9 | 105 | void playStaccatoSequence(int,int[],float[]); |
TeaPack_CZ | 1:90161871ad88 | 106 | |
TeaPack_CZ | 1:90161871ad88 | 107 | /** Function for playing sequence of tones in Staccato. |
TeaPack_CZ | 1:90161871ad88 | 108 | * \sa playSequence() |
TeaPack_CZ | 1:90161871ad88 | 109 | * \sa setStaccatoDuty() |
TeaPack_CZ | 1:90161871ad88 | 110 | */ |
TeaPack_CZ | 0:b8d34e475dd9 | 111 | void playStaccatoSequence(int,Note[]); |
TeaPack_CZ | 0:b8d34e475dd9 | 112 | |
TeaPack_CZ | 1:90161871ad88 | 113 | /** Function for playing rest of given length based on set BPM |
TeaPack_CZ | 1:90161871ad88 | 114 | */ |
TeaPack_CZ | 0:b8d34e475dd9 | 115 | void silence(float); |
TeaPack_CZ | 1:90161871ad88 | 116 | |
TeaPack_CZ | 1:90161871ad88 | 117 | /** Function for stopping sounds. |
TeaPack_CZ | 1:90161871ad88 | 118 | */ |
TeaPack_CZ | 0:b8d34e475dd9 | 119 | void stop(); |
TeaPack_CZ | 0:b8d34e475dd9 | 120 | |
TeaPack_CZ | 0:b8d34e475dd9 | 121 | private: |
TeaPack_CZ | 0:b8d34e475dd9 | 122 | Piezo _speaker; |
TeaPack_CZ | 0:b8d34e475dd9 | 123 | float tone[85]; |
TeaPack_CZ | 0:b8d34e475dd9 | 124 | |
TeaPack_CZ | 0:b8d34e475dd9 | 125 | int offset; |
TeaPack_CZ | 0:b8d34e475dd9 | 126 | int _bpm; |
TeaPack_CZ | 0:b8d34e475dd9 | 127 | float _bpm_ms; |
TeaPack_CZ | 0:b8d34e475dd9 | 128 | |
TeaPack_CZ | 0:b8d34e475dd9 | 129 | float max_BPM; |
TeaPack_CZ | 0:b8d34e475dd9 | 130 | float min_BPM; |
TeaPack_CZ | 0:b8d34e475dd9 | 131 | |
TeaPack_CZ | 0:b8d34e475dd9 | 132 | float _duty; |
TeaPack_CZ | 0:b8d34e475dd9 | 133 | }; |
TeaPack_CZ | 0:b8d34e475dd9 | 134 | |
TeaPack_CZ | 0:b8d34e475dd9 | 135 | #endif |