このライブラリでは、単音とメロディの出力が可能です。 In this library, you can output a single tone and melody. mbedのpwmOutとDigitalOutを使ってスピーカや圧電ブザーを直接駆動します。 directly drive speaker or buzzer and DigitalOut pwmOut using the mbed. メロディデータは、mbedのローカルにtxtファイルとして保存するか、プログラム中に配列データとして保存してください。 Melody data is either stored in a txt file on the local mbed, save it as a data array in the program.
Dependents: kitchenTimer_Clock SoundLibraryExample_Melody_ProgramData M3PI_SuiviLigne
Sound.h@13:cd42abff5173, 2013-10-20 (annotated)
- Committer:
- suupen
- Date:
- Sun Oct 20 06:00:50 2013 +0000
- Revision:
- 13:cd42abff5173
- Parent:
- 8:71488a68dc58
- Child:
- 14:6e0eac903431
- Child:
- 15:d526205994e9
This sample program supported LPC1114FN28
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
suupen | 8:71488a68dc58 | 1 | /* mbed Sound Library |
suupen | 8:71488a68dc58 | 2 | * Copyright (c) 2011 suupen |
suupen | 8:71488a68dc58 | 3 | * |
suupen | 8:71488a68dc58 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
suupen | 8:71488a68dc58 | 5 | * of this software and associated documentation files (the "Software"), to deal |
suupen | 8:71488a68dc58 | 6 | * in the Software without restriction, including without limitation the rights |
suupen | 8:71488a68dc58 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
suupen | 8:71488a68dc58 | 8 | * copies of the Software, and to permit persons to whom the Software is |
suupen | 8:71488a68dc58 | 9 | * furnished to do so, subject to the following conditions: |
suupen | 8:71488a68dc58 | 10 | * |
suupen | 8:71488a68dc58 | 11 | * The above copyright notice and this permission notice shall be included in |
suupen | 8:71488a68dc58 | 12 | * all copies or substantial portions of the Software. |
suupen | 8:71488a68dc58 | 13 | * |
suupen | 8:71488a68dc58 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
suupen | 8:71488a68dc58 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
suupen | 8:71488a68dc58 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
suupen | 8:71488a68dc58 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
suupen | 8:71488a68dc58 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
suupen | 8:71488a68dc58 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
suupen | 8:71488a68dc58 | 20 | * THE SOFTWARE. |
suupen | 8:71488a68dc58 | 21 | */ |
suupen | 8:71488a68dc58 | 22 | |
suupen | 0:e3f005984c2a | 23 | /***********************************************************************/ |
suupen | 0:e3f005984c2a | 24 | /* */ |
suupen | 13:cd42abff5173 | 25 | /* Sound.h */ |
suupen | 0:e3f005984c2a | 26 | /* */ |
suupen | 0:e3f005984c2a | 27 | /* V0.1 : 2011/11/13 */ |
suupen | 8:71488a68dc58 | 28 | /* V0.2 : 2011/11/19 Added copyright notice */ |
suupen | 13:cd42abff5173 | 29 | /* V0.3 : 2013/10/20 LPC1114FN28 taiou */ |
suupen | 0:e3f005984c2a | 30 | /***********************************************************************/ |
suupen | 0:e3f005984c2a | 31 | #ifndef _SOUND_H |
suupen | 0:e3f005984c2a | 32 | #define _SOUND_H |
suupen | 0:e3f005984c2a | 33 | |
suupen | 0:e3f005984c2a | 34 | #include <string> |
suupen | 0:e3f005984c2a | 35 | #include "types.h" |
suupen | 0:e3f005984c2a | 36 | |
suupen | 0:e3f005984c2a | 37 | |
suupen | 0:e3f005984c2a | 38 | /** Sound output control class, based on a PwmOut |
suupen | 0:e3f005984c2a | 39 | * |
suupen | 0:e3f005984c2a | 40 | * Example: |
suupen | 0:e3f005984c2a | 41 | * @code |
suupen | 7:758ad151b16f | 42 | * // Output tone and merody |
suupen | 0:e3f005984c2a | 43 | * |
suupen | 6:fb02f6d9bbaf | 44 | * #include "mbed.h" |
suupen | 7:758ad151b16f | 45 | * #include "Sound.h" // sound library header |
suupen | 0:e3f005984c2a | 46 | * |
suupen | 13:cd42abff5173 | 47 | * <LPC1768> |
suupen | 13:cd42abff5173 | 48 | * Sound sound(p21, p23); // 1tu me no sound syuturyoku (pwmOut = p21, digitalOut = p23) |
suupen | 0:e3f005984c2a | 49 | * |
suupen | 4:dc8c3e896c80 | 50 | * |
suupen | 4:dc8c3e896c80 | 51 | * <scematic> |
suupen | 4:dc8c3e896c80 | 52 | * |
suupen | 4:dc8c3e896c80 | 53 | * -------- _/ |
suupen | 4:dc8c3e896c80 | 54 | * mbed(p21) -----|R:200ohm|----| | speaker |
suupen | 5:9d861f258467 | 55 | * pwmOut -------- | | |
suupen | 4:dc8c3e896c80 | 56 | * | | |
suupen | 13:cd42abff5173 | 57 | * mbed(p23) -------------------|_| |
suupen | 13:cd42abff5173 | 58 | * digitalOut \ |
suupen | 13:cd42abff5173 | 59 | * |
suupen | 13:cd42abff5173 | 60 | * <LPC1114FN28> |
suupen | 13:cd42abff5173 | 61 | * Sound sound(dp1, dp2); // 1tu me no sound syuturyoku (pwmOut = dp1, digitalOut = dp2) |
suupen | 13:cd42abff5173 | 62 | * |
suupen | 13:cd42abff5173 | 63 | * |
suupen | 13:cd42abff5173 | 64 | * <scematic> |
suupen | 13:cd42abff5173 | 65 | * |
suupen | 13:cd42abff5173 | 66 | * -------- _/ |
suupen | 13:cd42abff5173 | 67 | * mbed(dp1) -----|R:200ohm|----| | speaker |
suupen | 13:cd42abff5173 | 68 | * pwmOut -------- | | |
suupen | 13:cd42abff5173 | 69 | * | | |
suupen | 13:cd42abff5173 | 70 | * mbed(dp2) -------------------|_| |
suupen | 4:dc8c3e896c80 | 71 | * digitalOut \ |
suupen | 4:dc8c3e896c80 | 72 | * |
suupen | 0:e3f005984c2a | 73 | * //-------------------------------- |
suupen | 0:e3f005984c2a | 74 | * // "westminster chime" merody data |
suupen | 0:e3f005984c2a | 75 | * //-------------------------------- |
suupen | 0:e3f005984c2a | 76 | * const Sound::sound_t WESTMINSTER[] = { |
suupen | 0:e3f005984c2a | 77 | * // hanon siji 0:b(flat) 1:tujo 2:#(sharp) |
suupen | 0:e3f005984c2a | 78 | * // | C1 - B9 kan deno onkai(Gx ha 9x ni okikae te siji) 0xFF=end data |
suupen | 0:e3f005984c2a | 79 | * // | | time (1/1[ms]/count) |
suupen | 0:e3f005984c2a | 80 | * // | | | envelope(yoin) (1/1 [ms]/count) |
suupen | 0:e3f005984c2a | 81 | * // | | | | |
suupen | 0:e3f005984c2a | 82 | * {1,0xA4,1200,1000}, |
suupen | 0:e3f005984c2a | 83 | * {1,0xF4,1200,1000}, |
suupen | 0:e3f005984c2a | 84 | * {1,0x94,1200,1000}, |
suupen | 0:e3f005984c2a | 85 | * {1,0xC4,2400,1000}, |
suupen | 0:e3f005984c2a | 86 | * |
suupen | 0:e3f005984c2a | 87 | * {1,0xC4,1200,1000}, |
suupen | 0:e3f005984c2a | 88 | * {1,0x94,1200,1000}, |
suupen | 0:e3f005984c2a | 89 | * {1,0xA4,1200,1000}, |
suupen | 0:e3f005984c2a | 90 | * {1,0xF4,2400,1000}, |
suupen | 0:e3f005984c2a | 91 | * |
suupen | 0:e3f005984c2a | 92 | * {1,0xFF,1000,0}, // end |
suupen | 0:e3f005984c2a | 93 | * }; |
suupen | 0:e3f005984c2a | 94 | * |
suupen | 0:e3f005984c2a | 95 | * /-------------------------------------- |
suupen | 6:fb02f6d9bbaf | 96 | * * main |
suupen | 0:e3f005984c2a | 97 | * /--------------------------------------- |
suupen | 6:fb02f6d9bbaf | 98 | * int main() { |
suupen | 0:e3f005984c2a | 99 | * |
suupen | 0:e3f005984c2a | 100 | * //--------------------- |
suupen | 0:e3f005984c2a | 101 | * // enso data no settei |
suupen | 0:e3f005984c2a | 102 | * //--------------------- |
suupen | 5:9d861f258467 | 103 | * // sound.sound_enso("/local/enso.txt"); // mbed local file data "enso.txt" load (sita ni data no rei wo oite oku) |
suupen | 5:9d861f258467 | 104 | * sound.sound_enso((Sound::sound_t*)WESTMINSTER); |
suupen | 0:e3f005984c2a | 105 | * |
suupen | 5:9d861f258467 | 106 | * //--------------------------------------------------- |
suupen | 5:9d861f258467 | 107 | * // output tone |
suupen | 5:9d861f258467 | 108 | * //--------------------------------------------------- |
suupen | 5:9d861f258467 | 109 | * // tone1 |
suupen | 5:9d861f258467 | 110 | * Sound::sound_t oto = {1,0x95,200,100}; |
suupen | 5:9d861f258467 | 111 | * sound.sound_sound(oto); |
suupen | 5:9d861f258467 | 112 | * while(sound.sound_sound() == true){} |
suupen | 5:9d861f258467 | 113 | * |
suupen | 5:9d861f258467 | 114 | * // tone2 |
suupen | 5:9d861f258467 | 115 | * oto.hanon = 1; oto.onkai = 0xA5; oto.time = 2000; oto.envelope = 1000; |
suupen | 5:9d861f258467 | 116 | * sound.sound_sound(oto); |
suupen | 5:9d861f258467 | 117 | * while(sound.sound_sound() == true){} |
suupen | 0:e3f005984c2a | 118 | * |
suupen | 5:9d861f258467 | 119 | * //--------------- |
suupen | 5:9d861f258467 | 120 | * // output merody |
suupen | 5:9d861f258467 | 121 | * //-------------- |
suupen | 5:9d861f258467 | 122 | * sound.sound_enso(true); |
suupen | 0:e3f005984c2a | 123 | * |
suupen | 5:9d861f258467 | 124 | * while(1) { |
suupen | 0:e3f005984c2a | 125 | * } |
suupen | 0:e3f005984c2a | 126 | *} |
suupen | 0:e3f005984c2a | 127 | * |
suupen | 0:e3f005984c2a | 128 | * @endcode |
suupen | 0:e3f005984c2a | 129 | */ |
suupen | 0:e3f005984c2a | 130 | |
suupen | 0:e3f005984c2a | 131 | |
suupen | 0:e3f005984c2a | 132 | |
suupen | 0:e3f005984c2a | 133 | |
suupen | 0:e3f005984c2a | 134 | |
suupen | 0:e3f005984c2a | 135 | |
suupen | 0:e3f005984c2a | 136 | |
suupen | 0:e3f005984c2a | 137 | |
suupen | 0:e3f005984c2a | 138 | |
suupen | 0:e3f005984c2a | 139 | |
suupen | 0:e3f005984c2a | 140 | |
suupen | 0:e3f005984c2a | 141 | |
suupen | 0:e3f005984c2a | 142 | |
suupen | 0:e3f005984c2a | 143 | |
suupen | 0:e3f005984c2a | 144 | class Sound { |
suupen | 0:e3f005984c2a | 145 | public: |
suupen | 0:e3f005984c2a | 146 | |
suupen | 1:358449cfdcd3 | 147 | /** tone data struct |
suupen | 1:358449cfdcd3 | 148 | * |
suupen | 1:358449cfdcd3 | 149 | * @param uint8_t hanon : hanon siji 0:b(flat) 1:tujo 2:#(sharp) |
suupen | 1:358449cfdcd3 | 150 | * @param uint16_t onkai : C1 - B9 kan deno onkai(Gx ha 9x ni okikae te siji) 0x00:hatuon teisi 0xFF:enso syuryo |
suupen | 1:358449cfdcd3 | 151 | * @param uint16_t time : hatuon jikan (1/1 [ms]/count) |
suupen | 1:358449cfdcd3 | 152 | * @param uint16_t envelope : yoin(envelope) jikan (1/1 [ms]/count) |
suupen | 1:358449cfdcd3 | 153 | */ |
suupen | 0:e3f005984c2a | 154 | typedef struct { |
suupen | 0:e3f005984c2a | 155 | uint8_t hanon; // hanon siji 0:b(flat) 1:tujo 2:#(sharp) |
suupen | 0:e3f005984c2a | 156 | uint16_t onkai; // C1 - B9 kan deno onkai(Gx ha 9x ni okikae te siji) |
suupen | 0:e3f005984c2a | 157 | // 0x00:hatuon teisi 0xFF:enso syuryo |
suupen | 0:e3f005984c2a | 158 | uint16_t time; // hatuon jikan (1/1 [ms]/count) |
suupen | 0:e3f005984c2a | 159 | uint16_t envelope; // yoin(envelope) jikan (1/1 [ms]/count) |
suupen | 0:e3f005984c2a | 160 | } sound_t; |
suupen | 0:e3f005984c2a | 161 | |
suupen | 0:e3f005984c2a | 162 | /* |
suupen | 0:e3f005984c2a | 163 | // hanon no menber |
suupen | 0:e3f005984c2a | 164 | enum{ |
suupen | 0:e3f005984c2a | 165 | Z_hanonF = 0, // b |
suupen | 0:e3f005984c2a | 166 | Z_hanonN , // tujo |
suupen | 0:e3f005984c2a | 167 | Z_hanonS, // # |
suupen | 0:e3f005984c2a | 168 | }; |
suupen | 0:e3f005984c2a | 169 | */ |
suupen | 0:e3f005984c2a | 170 | |
suupen | 0:e3f005984c2a | 171 | /** Create a sound object connected to the specified PwmOut pin & DigitalOut pin |
suupen | 0:e3f005984c2a | 172 | * |
suupen | 0:e3f005984c2a | 173 | * @param pin PwmOut pin to connect to |
suupen | 0:e3f005984c2a | 174 | * @param pin DigitalOut pin to connect to |
suupen | 0:e3f005984c2a | 175 | */ |
suupen | 0:e3f005984c2a | 176 | Sound(PinName pwm , PinName kijun); |
suupen | 0:e3f005984c2a | 177 | |
suupen | 1:358449cfdcd3 | 178 | /** Check tone |
suupen | 0:e3f005984c2a | 179 | * |
suupen | 2:ed8a182cefe2 | 180 | * @param return A bool ture : output |
suupen | 3:2f5107cb0589 | 181 | * \\false: none |
suupen | 0:e3f005984c2a | 182 | */ |
suupen | 0:e3f005984c2a | 183 | bool sound_sound(void); // oto no syuturyoku jotai wo kakunin |
suupen | 0:e3f005984c2a | 184 | // true : oto ari false : oto nasi |
suupen | 1:358449cfdcd3 | 185 | /** output tone |
suupen | 1:358449cfdcd3 | 186 | * |
suupen | 1:358449cfdcd3 | 187 | * @param sound_t data : tone data set |
suupen | 1:358449cfdcd3 | 188 | */ |
suupen | 0:e3f005984c2a | 189 | void sound_sound(sound_t data); // oto no syuturyoku |
suupen | 0:e3f005984c2a | 190 | // para : oto no data |
suupen | 0:e3f005984c2a | 191 | |
suupen | 1:358449cfdcd3 | 192 | /** merody data set (file data) |
suupen | 1:358449cfdcd3 | 193 | * |
suupen | 1:358449cfdcd3 | 194 | * @param merody data file path and name (example : "/local/merodyFileName.txt") |
suupen | 1:358449cfdcd3 | 195 | */ |
suupen | 0:e3f005984c2a | 196 | bool sound_enso(char *path); // enso data wo file kara yomikomi |
suupen | 0:e3f005984c2a | 197 | // true : data kakunou OK false: data kakunou NG |
suupen | 1:358449cfdcd3 | 198 | /** merody data set (data table) |
suupen | 1:358449cfdcd3 | 199 | * |
suupen | 1:358449cfdcd3 | 200 | * @param merody data table name (example : "(Sound::sound_t*)WESTMINSTER") |
suupen | 1:358449cfdcd3 | 201 | */ |
suupen | 0:e3f005984c2a | 202 | void sound_enso(Sound::sound_t* onpudata); // enso data wo program no data table kara yomikomi |
suupen | 1:358449cfdcd3 | 203 | |
suupen | 1:358449cfdcd3 | 204 | /** check merody |
suupen | 1:358449cfdcd3 | 205 | * |
suupen | 1:358449cfdcd3 | 206 | * @param raturn ture:merody output //false:merody stop |
suupen | 1:358449cfdcd3 | 207 | */ |
suupen | 0:e3f005984c2a | 208 | bool sound_enso(void); // enso jyotai check |
suupen | 0:e3f005984c2a | 209 | // true : enso chu false : enso shuryo |
suupen | 1:358449cfdcd3 | 210 | /** request merody start or stop |
suupen | 1:358449cfdcd3 | 211 | * |
suupen | 1:358449cfdcd3 | 212 | * @param ture:start merody //false:stop merody |
suupen | 1:358449cfdcd3 | 213 | */ |
suupen | 0:e3f005984c2a | 214 | void sound_enso(bool siji); // enso start / stop |
suupen | 0:e3f005984c2a | 215 | // true : enso start false : enso stop |
suupen | 0:e3f005984c2a | 216 | |
suupen | 0:e3f005984c2a | 217 | //protected: |
suupen | 0:e3f005984c2a | 218 | private: |
suupen | 0:e3f005984c2a | 219 | PwmOut _pwm; |
suupen | 0:e3f005984c2a | 220 | DigitalOut _kijun; |
suupen | 0:e3f005984c2a | 221 | |
suupen | 0:e3f005984c2a | 222 | Ticker sound_timer; |
suupen | 0:e3f005984c2a | 223 | Timer hatuon_jikan; |
suupen | 0:e3f005984c2a | 224 | |
suupen | 0:e3f005984c2a | 225 | |
suupen | 13:cd42abff5173 | 226 | |
suupen | 13:cd42abff5173 | 227 | //131020#define Z_pwmSyuuki (1) // PWM syuuki (1/1[us]/count) syokichi = 1 |
suupen | 13:cd42abff5173 | 228 | //131020#define Z_pulseCheckSyuuki (20) // puls check syuuki (1/1 [us]/count) syokichi = 20 (10us ika deha pwm settei ga ijo ni naru) |
suupen | 13:cd42abff5173 | 229 | |
suupen | 13:cd42abff5173 | 230 | // LPC1114FN28 taiou notameni syori wo osokusuru. //131020 |
suupen | 13:cd42abff5173 | 231 | #define Z_pwmSyuuki (10) // PWM syuuki (1/1[us]/count) syokichi = 10 |
suupen | 13:cd42abff5173 | 232 | #define Z_pulseCheckSyuuki (40) // puls check syuuki (1/1 [us]/count) syokichi = 40 (10us ika deha pwm settei ga ijo ni naru) |
suupen | 0:e3f005984c2a | 233 | |
suupen | 0:e3f005984c2a | 234 | |
suupen | 0:e3f005984c2a | 235 | typedef struct{ |
suupen | 0:e3f005984c2a | 236 | uint32_t syuuki; // oto no syuuki no count suu wo kioku (1/1 [us]/count) |
suupen | 0:e3f005984c2a | 237 | uint32_t jikan; // oto no syuturyoku jikan wo kanri (1/1 [us]/count) |
suupen | 0:e3f005984c2a | 238 | uint32_t envelope; // envelope counter 0 - 1000000[us](1[s]) |
suupen | 0:e3f005984c2a | 239 | uint32_t shokichienvelope; // shokichi envelope counter 0 - 1000000[us](1[s]) |
suupen | 0:e3f005984c2a | 240 | } soundout_t; |
suupen | 0:e3f005984c2a | 241 | |
suupen | 0:e3f005984c2a | 242 | |
suupen | 0:e3f005984c2a | 243 | soundout_t O_sound; // oto no syuturyoku pwm data |
suupen | 0:e3f005984c2a | 244 | |
suupen | 0:e3f005984c2a | 245 | sound_t enso[100]; // mbed drive no enso data kakuno yo |
suupen | 0:e3f005984c2a | 246 | sound_t *ensoDataTable; // enso data no sento address kioku |
suupen | 0:e3f005984c2a | 247 | |
suupen | 0:e3f005984c2a | 248 | void sound_out(float siji, int8_t fugo); |
suupen | 0:e3f005984c2a | 249 | void pulseCheck(void); |
suupen | 0:e3f005984c2a | 250 | uint8_t F_pwmSet; // 0:zenhan hansyuuki 1:kohan hansyuuki wo request |
suupen | 0:e3f005984c2a | 251 | uint32_t C_syuukiKeika; |
suupen | 0:e3f005984c2a | 252 | uint8_t f_muonSet; // muon ji no sound_out() syori wo kurikaesu no wo fusegu |
suupen | 0:e3f005984c2a | 253 | uint32_t C_1msProcess; // 1ms syuuki syori counter (Z_pulseCheckSyuuki/1 [us]/count)) |
suupen | 0:e3f005984c2a | 254 | |
suupen | 0:e3f005984c2a | 255 | |
suupen | 0:e3f005984c2a | 256 | void sound_ensoSyori(void); |
suupen | 0:e3f005984c2a | 257 | int keikajikan; // 1tu no oto no keikajikan |
suupen | 0:e3f005984c2a | 258 | sound_t* onpu; // onpu data no sento address |
suupen | 0:e3f005984c2a | 259 | |
suupen | 0:e3f005984c2a | 260 | |
suupen | 0:e3f005984c2a | 261 | }; |
suupen | 0:e3f005984c2a | 262 | |
suupen | 0:e3f005984c2a | 263 | |
suupen | 0:e3f005984c2a | 264 | #undef _SOUND_C |
suupen | 0:e3f005984c2a | 265 | #endif // _SOUND_H |