このライブラリでは、単音とメロディの出力が可能です。 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

Committer:
suupen
Date:
Wed Nov 16 12:32:53 2011 +0000
Revision:
2:ed8a182cefe2
Parent:
1:358449cfdcd3
Child:
3:2f5107cb0589

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
suupen 0:e3f005984c2a 1 /***********************************************************************/
suupen 0:e3f005984c2a 2 /* */
suupen 0:e3f005984c2a 3 /* Sound.h */
suupen 0:e3f005984c2a 4 /* */
suupen 0:e3f005984c2a 5 /* V0.1 : 2011/11/13 */
suupen 0:e3f005984c2a 6 /***********************************************************************/
suupen 0:e3f005984c2a 7 #ifndef _SOUND_H
suupen 0:e3f005984c2a 8 #define _SOUND_H
suupen 0:e3f005984c2a 9
suupen 0:e3f005984c2a 10 #include <string>
suupen 0:e3f005984c2a 11 #include "types.h"
suupen 0:e3f005984c2a 12
suupen 0:e3f005984c2a 13
suupen 0:e3f005984c2a 14 /** Sound output control class, based on a PwmOut
suupen 0:e3f005984c2a 15 *
suupen 0:e3f005984c2a 16 * Example:
suupen 0:e3f005984c2a 17 * @code
suupen 0:e3f005984c2a 18 * // Continuously sweep the servo through it's full range
suupen 0:e3f005984c2a 19 *
suupen 0:e3f005984c2a 20 *#include "mbed.h"
suupen 0:e3f005984c2a 21 *#include "pwmSound.h" // pwm sound library header
suupen 0:e3f005984c2a 22 *
suupen 0:e3f005984c2a 23 *Sound sound(p21, p10); // 1tu me no sound syuturyoku (pwmOut = p21, digitalOut = p10)
suupen 0:e3f005984c2a 24 *
suupen 0:e3f005984c2a 25 * //--------------------------------
suupen 0:e3f005984c2a 26 * // "westminster chime" merody data
suupen 0:e3f005984c2a 27 * //--------------------------------
suupen 0:e3f005984c2a 28 * const Sound::sound_t WESTMINSTER[] = {
suupen 0:e3f005984c2a 29 * // hanon siji 0:b(flat) 1:tujo 2:#(sharp)
suupen 0:e3f005984c2a 30 * // | C1 - B9 kan deno onkai(Gx ha 9x ni okikae te siji) 0xFF=end data
suupen 0:e3f005984c2a 31 * // | | time (1/1[ms]/count)
suupen 0:e3f005984c2a 32 * // | | | envelope(yoin) (1/1 [ms]/count)
suupen 0:e3f005984c2a 33 * // | | | |
suupen 0:e3f005984c2a 34 * {1,0xA4,1200,1000},
suupen 0:e3f005984c2a 35 * {1,0xF4,1200,1000},
suupen 0:e3f005984c2a 36 * {1,0x94,1200,1000},
suupen 0:e3f005984c2a 37 * {1,0xC4,2400,1000},
suupen 0:e3f005984c2a 38 *
suupen 0:e3f005984c2a 39 * {1,0xC4,1200,1000},
suupen 0:e3f005984c2a 40 * {1,0x94,1200,1000},
suupen 0:e3f005984c2a 41 * {1,0xA4,1200,1000},
suupen 0:e3f005984c2a 42 * {1,0xF4,2400,1000},
suupen 0:e3f005984c2a 43 *
suupen 0:e3f005984c2a 44 * {1,0xFF,1000,0}, // end
suupen 0:e3f005984c2a 45 * };
suupen 0:e3f005984c2a 46 *
suupen 0:e3f005984c2a 47 * /--------------------------------------
suupen 0:e3f005984c2a 48 ** main
suupen 0:e3f005984c2a 49 * /---------------------------------------
suupen 0:e3f005984c2a 50 *int main() {
suupen 0:e3f005984c2a 51 *
suupen 0:e3f005984c2a 52 * //---------------------
suupen 0:e3f005984c2a 53 * // enso data no settei
suupen 0:e3f005984c2a 54 * //---------------------
suupen 0:e3f005984c2a 55 * // sound.sound_enso("/local/enso.txt"); // mbed local file data "enso.txt" load (sita ni data no rei wo oite oku)
suupen 0:e3f005984c2a 56 * sound.sound_enso((Sound::sound_t*)DEMEKIN);
suupen 0:e3f005984c2a 57 *
suupen 0:e3f005984c2a 58 * while(1) {
suupen 0:e3f005984c2a 59 *
suupen 0:e3f005984c2a 60 * //---------------------------------------------------
suupen 0:e3f005984c2a 61 * // tanon syuturyoku no ato ni westminster chime enso
suupen 0:e3f005984c2a 62 * //---------------------------------------------------
suupen 0:e3f005984c2a 63 * if(sound.sound_enso() == false){
suupen 0:e3f005984c2a 64 * //sound2.sound_enso((Sound::sound_t*)RAMEN);
suupen 0:e3f005984c2a 65 *
suupen 0:e3f005984c2a 66 *
suupen 0:e3f005984c2a 67 * // tanon1
suupen 0:e3f005984c2a 68 * Sound::sound_t oto = {1,0x95,200,100};
suupen 0:e3f005984c2a 69 * sound.sound_sound(oto);
suupen 0:e3f005984c2a 70 * while(sound.sound_sound() == true){}
suupen 0:e3f005984c2a 71 *
suupen 0:e3f005984c2a 72 * // tanon2
suupen 0:e3f005984c2a 73 * oto.hanon = 1; oto.onkai = 0xA5; oto.time = 2000; oto.envelope = 1000;
suupen 0:e3f005984c2a 74 * sound.sound_sound(oto);
suupen 0:e3f005984c2a 75 * while(sound.sound_sound() == true){}
suupen 0:e3f005984c2a 76 *
suupen 0:e3f005984c2a 77 * // sai enso
suupen 0:e3f005984c2a 78 * sound.sound_enso(true);
suupen 0:e3f005984c2a 79 * }
suupen 0:e3f005984c2a 80 * }
suupen 0:e3f005984c2a 81 *}
suupen 0:e3f005984c2a 82 *
suupen 0:e3f005984c2a 83 * @endcode
suupen 0:e3f005984c2a 84 */
suupen 0:e3f005984c2a 85
suupen 0:e3f005984c2a 86
suupen 0:e3f005984c2a 87
suupen 0:e3f005984c2a 88
suupen 0:e3f005984c2a 89
suupen 0:e3f005984c2a 90
suupen 0:e3f005984c2a 91
suupen 0:e3f005984c2a 92
suupen 0:e3f005984c2a 93
suupen 0:e3f005984c2a 94
suupen 0:e3f005984c2a 95
suupen 0:e3f005984c2a 96
suupen 0:e3f005984c2a 97
suupen 0:e3f005984c2a 98
suupen 0:e3f005984c2a 99 class Sound {
suupen 0:e3f005984c2a 100 public:
suupen 0:e3f005984c2a 101
suupen 1:358449cfdcd3 102 /** tone data struct
suupen 1:358449cfdcd3 103 *
suupen 1:358449cfdcd3 104 * @param uint8_t hanon : hanon siji 0:b(flat) 1:tujo 2:#(sharp)
suupen 1:358449cfdcd3 105 * @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 106 * @param uint16_t time : hatuon jikan (1/1 [ms]/count)
suupen 1:358449cfdcd3 107 * @param uint16_t envelope : yoin(envelope) jikan (1/1 [ms]/count)
suupen 1:358449cfdcd3 108 */
suupen 0:e3f005984c2a 109 typedef struct {
suupen 0:e3f005984c2a 110 uint8_t hanon; // hanon siji 0:b(flat) 1:tujo 2:#(sharp)
suupen 0:e3f005984c2a 111 uint16_t onkai; // C1 - B9 kan deno onkai(Gx ha 9x ni okikae te siji)
suupen 0:e3f005984c2a 112 // 0x00:hatuon teisi 0xFF:enso syuryo
suupen 0:e3f005984c2a 113 uint16_t time; // hatuon jikan (1/1 [ms]/count)
suupen 0:e3f005984c2a 114 uint16_t envelope; // yoin(envelope) jikan (1/1 [ms]/count)
suupen 0:e3f005984c2a 115 } sound_t;
suupen 0:e3f005984c2a 116
suupen 0:e3f005984c2a 117 /*
suupen 0:e3f005984c2a 118 // hanon no menber
suupen 0:e3f005984c2a 119 enum{
suupen 0:e3f005984c2a 120 Z_hanonF = 0, // b
suupen 0:e3f005984c2a 121 Z_hanonN , // tujo
suupen 0:e3f005984c2a 122 Z_hanonS, // #
suupen 0:e3f005984c2a 123 };
suupen 0:e3f005984c2a 124 */
suupen 0:e3f005984c2a 125
suupen 0:e3f005984c2a 126 /** Create a sound object connected to the specified PwmOut pin & DigitalOut pin
suupen 0:e3f005984c2a 127 *
suupen 0:e3f005984c2a 128 * @param pin PwmOut pin to connect to
suupen 0:e3f005984c2a 129 * @param pin DigitalOut pin to connect to
suupen 0:e3f005984c2a 130 */
suupen 0:e3f005984c2a 131 Sound(PinName pwm , PinName kijun);
suupen 0:e3f005984c2a 132
suupen 1:358449cfdcd3 133 /** Check tone
suupen 0:e3f005984c2a 134 *
suupen 2:ed8a182cefe2 135 * @param return A bool ture : output
suupen 2:ed8a182cefe2 136 * false: none
suupen 0:e3f005984c2a 137 */
suupen 0:e3f005984c2a 138 bool sound_sound(void); // oto no syuturyoku jotai wo kakunin
suupen 0:e3f005984c2a 139 // true : oto ari false : oto nasi
suupen 1:358449cfdcd3 140 /** output tone
suupen 1:358449cfdcd3 141 *
suupen 1:358449cfdcd3 142 * @param sound_t data : tone data set
suupen 1:358449cfdcd3 143 */
suupen 0:e3f005984c2a 144 void sound_sound(sound_t data); // oto no syuturyoku
suupen 0:e3f005984c2a 145 // para : oto no data
suupen 0:e3f005984c2a 146
suupen 1:358449cfdcd3 147 /** merody data set (file data)
suupen 1:358449cfdcd3 148 *
suupen 1:358449cfdcd3 149 * @param merody data file path and name (example : "/local/merodyFileName.txt")
suupen 1:358449cfdcd3 150 */
suupen 0:e3f005984c2a 151 bool sound_enso(char *path); // enso data wo file kara yomikomi
suupen 0:e3f005984c2a 152 // true : data kakunou OK false: data kakunou NG
suupen 1:358449cfdcd3 153 /** merody data set (data table)
suupen 1:358449cfdcd3 154 *
suupen 1:358449cfdcd3 155 * @param merody data table name (example : "(Sound::sound_t*)WESTMINSTER")
suupen 1:358449cfdcd3 156 */
suupen 0:e3f005984c2a 157 void sound_enso(Sound::sound_t* onpudata); // enso data wo program no data table kara yomikomi
suupen 1:358449cfdcd3 158
suupen 1:358449cfdcd3 159 /** check merody
suupen 1:358449cfdcd3 160 *
suupen 1:358449cfdcd3 161 * @param raturn ture:merody output //false:merody stop
suupen 1:358449cfdcd3 162 */
suupen 0:e3f005984c2a 163 bool sound_enso(void); // enso jyotai check
suupen 0:e3f005984c2a 164 // true : enso chu false : enso shuryo
suupen 1:358449cfdcd3 165 /** request merody start or stop
suupen 1:358449cfdcd3 166 *
suupen 1:358449cfdcd3 167 * @param ture:start merody //false:stop merody
suupen 1:358449cfdcd3 168 */
suupen 0:e3f005984c2a 169 void sound_enso(bool siji); // enso start / stop
suupen 0:e3f005984c2a 170 // true : enso start false : enso stop
suupen 0:e3f005984c2a 171
suupen 0:e3f005984c2a 172 //protected:
suupen 0:e3f005984c2a 173 private:
suupen 0:e3f005984c2a 174 PwmOut _pwm;
suupen 0:e3f005984c2a 175 DigitalOut _kijun;
suupen 0:e3f005984c2a 176
suupen 0:e3f005984c2a 177 Ticker sound_timer;
suupen 0:e3f005984c2a 178 Timer hatuon_jikan;
suupen 0:e3f005984c2a 179
suupen 0:e3f005984c2a 180
suupen 0:e3f005984c2a 181 #define Z_pwmSyuuki (1) // PWM syuuki (1/1[us]/count) syokichi = 1
suupen 0:e3f005984c2a 182 #define Z_pulseCheckSyuuki (20) // puls check syuuki (1/1 [us]/count) syokichi = 20 (10us ika deha pwm settei ga ijo ni naru)
suupen 0:e3f005984c2a 183
suupen 0:e3f005984c2a 184
suupen 0:e3f005984c2a 185 typedef struct{
suupen 0:e3f005984c2a 186 uint32_t syuuki; // oto no syuuki no count suu wo kioku (1/1 [us]/count)
suupen 0:e3f005984c2a 187 uint32_t jikan; // oto no syuturyoku jikan wo kanri (1/1 [us]/count)
suupen 0:e3f005984c2a 188 uint32_t envelope; // envelope counter 0 - 1000000[us](1[s])
suupen 0:e3f005984c2a 189 uint32_t shokichienvelope; // shokichi envelope counter 0 - 1000000[us](1[s])
suupen 0:e3f005984c2a 190 } soundout_t;
suupen 0:e3f005984c2a 191
suupen 0:e3f005984c2a 192
suupen 0:e3f005984c2a 193 soundout_t O_sound; // oto no syuturyoku pwm data
suupen 0:e3f005984c2a 194
suupen 0:e3f005984c2a 195 sound_t enso[100]; // mbed drive no enso data kakuno yo
suupen 0:e3f005984c2a 196 sound_t *ensoDataTable; // enso data no sento address kioku
suupen 0:e3f005984c2a 197
suupen 0:e3f005984c2a 198 void sound_out(float siji, int8_t fugo);
suupen 0:e3f005984c2a 199 void pulseCheck(void);
suupen 0:e3f005984c2a 200 uint8_t F_pwmSet; // 0:zenhan hansyuuki 1:kohan hansyuuki wo request
suupen 0:e3f005984c2a 201 uint32_t C_syuukiKeika;
suupen 0:e3f005984c2a 202 uint8_t f_muonSet; // muon ji no sound_out() syori wo kurikaesu no wo fusegu
suupen 0:e3f005984c2a 203 uint32_t C_1msProcess; // 1ms syuuki syori counter (Z_pulseCheckSyuuki/1 [us]/count))
suupen 0:e3f005984c2a 204
suupen 0:e3f005984c2a 205
suupen 0:e3f005984c2a 206 void sound_ensoSyori(void);
suupen 0:e3f005984c2a 207 int keikajikan; // 1tu no oto no keikajikan
suupen 0:e3f005984c2a 208 sound_t* onpu; // onpu data no sento address
suupen 0:e3f005984c2a 209
suupen 0:e3f005984c2a 210
suupen 0:e3f005984c2a 211 };
suupen 0:e3f005984c2a 212
suupen 0:e3f005984c2a 213
suupen 0:e3f005984c2a 214 #undef _SOUND_C
suupen 0:e3f005984c2a 215 #endif // _SOUND_H