Hirotaka Yoneda / beep_sound

Dependents:   kiokuryoku_game escapeFromYou junirobo_sample001 ROBOX_Sample_IRcon ... more

Committer:
RBH
Date:
Sat Oct 08 19:04:56 2016 +0000
Revision:
9:d899b134f9bb
Parent:
8:26cb94806201
Child:
10:24155220eeda
mbed???????PWM?????????????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RBH 0:c9832831c73f 1 #include "beep_sound.h"
RBH 0:c9832831c73f 2 #include "math.h"
RBH 3:86238dba1529 3 #include <queue>
RBH 3:86238dba1529 4
RBH 3:86238dba1529 5
RBH 0:c9832831c73f 6 beep_sound::beep_sound(PinName pwm)
RBH 0:c9832831c73f 7 {
RBH 6:adb925f93199 8 frequency = 0;
RBH 6:adb925f93199 9 playing = false;
RBH 8:26cb94806201 10 teishi = false;
RBH 0:c9832831c73f 11 m_pwm = new PwmOut(pwm);
RBH 0:c9832831c73f 12 m_pwm->write(0.0f);
RBH 0:c9832831c73f 13 }
RBH 0:c9832831c73f 14
RBH 0:c9832831c73f 15 beep_sound::~beep_sound()
RBH 0:c9832831c73f 16 {
RBH 0:c9832831c73f 17 delete m_pwm;
RBH 0:c9832831c73f 18 }
RBH 0:c9832831c73f 19
RBH 5:a64e653b0ce1 20 void beep_sound::stop(){
RBH 3:86238dba1529 21 m_pwm->write(0.0f);
RBH 3:86238dba1529 22 // キューが空でなければ次の音へ
RBH 8:26cb94806201 23 if(otoQue.front().time_s != -1){
RBH 6:adb925f93199 24 playing = true;
RBH 8:26cb94806201 25 beep_sound::playContinue();
RBH 8:26cb94806201 26 }else if(otoQue.front().time_s == -1){
RBH 8:26cb94806201 27 otoQue.pop();
RBH 8:26cb94806201 28 playing = false;
RBH 6:adb925f93199 29 }else{
RBH 6:adb925f93199 30 playing = false;
RBH 3:86238dba1529 31 }
RBH 3:86238dba1529 32 }
RBH 3:86238dba1529 33
RBH 3:86238dba1529 34 // 音の再生
RBH 3:86238dba1529 35 void beep_sound::NoteOn(int octave, int note)
RBH 3:86238dba1529 36 {
RBH 6:adb925f93199 37 playing = true;
RBH 6:adb925f93199 38 frequency = frequencyTable[note] << octave;
RBH 6:adb925f93199 39 m_pwm->period(1.0f/(float)frequency);
RBH 6:adb925f93199 40 m_pwm->write(0.5f);
RBH 3:86238dba1529 41 }
RBH 3:86238dba1529 42
RBH 3:86238dba1529 43 // 1音ずつ音符情報をキューに追加
RBH 3:86238dba1529 44 void beep_sound::setGakuhu(int argOc, int argNo, float argTi)
RBH 3:86238dba1529 45 {
RBH 4:237b642c2ffa 46 bufG.octave = argOc;
RBH 4:237b642c2ffa 47 bufG.note = argNo;
RBH 4:237b642c2ffa 48 bufG.time_s = argTi;
RBH 4:237b642c2ffa 49 otoQue.push(bufG);
RBH 3:86238dba1529 50 }
RBH 3:86238dba1529 51
RBH 3:86238dba1529 52 // 休符をキューに追加
RBH 3:86238dba1529 53 void beep_sound::setKyuhu(float argTi)
RBH 3:86238dba1529 54 {
RBH 9:d899b134f9bb 55 bufG.octave = 10;
RBH 4:237b642c2ffa 56 bufG.note = 0;
RBH 4:237b642c2ffa 57 bufG.time_s = argTi;
RBH 4:237b642c2ffa 58 otoQue.push(bufG);
RBH 3:86238dba1529 59 }
RBH 3:86238dba1529 60
RBH 3:86238dba1529 61 // 楽譜キューを再生、音停止の割り込み予約
RBH 3:86238dba1529 62 // Timeoutの日本語リファレンスには書いていないが、
RBH 3:86238dba1529 63 // thisを引数にしないとコンパイルエラーになるので注意
RBH 3:86238dba1529 64 void beep_sound::playGakuhu()
RBH 3:86238dba1529 65 {
RBH 8:26cb94806201 66 if(playing == true){
RBH 8:26cb94806201 67 while(otoQue.front().time_s != -1) {otoQue.pop();}
RBH 8:26cb94806201 68 otoQue.pop(); //最後のフラグを削除
RBH 8:26cb94806201 69 // 最後のフラグを挿入
RBH 8:26cb94806201 70 bufG.time_s = -1;
RBH 8:26cb94806201 71 otoQue.push(bufG);
RBH 8:26cb94806201 72 beep_sound::stop();
RBH 8:26cb94806201 73 }else{
RBH 8:26cb94806201 74 // 最後のフラグを挿入
RBH 8:26cb94806201 75 bufG.time_s = -1;
RBH 8:26cb94806201 76 otoQue.push(bufG);
RBH 7:5f1b0adb25ec 77 playing = true;
RBH 7:5f1b0adb25ec 78 beep_sound::NoteOn(otoQue.front().octave, otoQue.front().note);
RBH 7:5f1b0adb25ec 79 interruptStop.attach(this, &beep_sound::stop, otoQue.front().time_s);
RBH 7:5f1b0adb25ec 80 otoQue.pop();
RBH 8:26cb94806201 81 }
RBH 8:26cb94806201 82 }
RBH 8:26cb94806201 83 void beep_sound::playContinue()
RBH 8:26cb94806201 84 {
RBH 8:26cb94806201 85 beep_sound::NoteOn(otoQue.front().octave, otoQue.front().note);
RBH 8:26cb94806201 86 interruptStop.attach(this, &beep_sound::stop, otoQue.front().time_s);
RBH 8:26cb94806201 87 otoQue.pop();
RBH 3:86238dba1529 88 }
RBH 3:86238dba1529 89
RBH 0:c9832831c73f 90 void beep_sound::SetFrequency(int octave, int note, bool on_off)
RBH 0:c9832831c73f 91 {
RBH 3:86238dba1529 92 static int preFrequency = 0;
RBH 3:86238dba1529 93 if(on_off == true){
RBH 3:86238dba1529 94 int frequency = frequencyTable[note] << octave;
RBH 3:86238dba1529 95 if(frequency != preFrequency){
RBH 3:86238dba1529 96 m_pwm->period(1.0f/(float)frequency);
RBH 3:86238dba1529 97 m_pwm->write(0.5f);
RBH 0:c9832831c73f 98 }
RBH 3:86238dba1529 99 preFrequency = frequency;
RBH 3:86238dba1529 100 }else{
RBH 3:86238dba1529 101 m_pwm->write(0.0f);
RBH 3:86238dba1529 102 preFrequency = 0;
RBH 3:86238dba1529 103 }
RBH 0:c9832831c73f 104 }
RBH 0:c9832831c73f 105 void beep_sound::onpu(int octave, int note, float time_s)
RBH 0:c9832831c73f 106 {
RBH 7:5f1b0adb25ec 107 playing = true;
RBH 8:26cb94806201 108 beep_sound::NoteOn(octave, note);
RBH 6:adb925f93199 109 interruptStop.attach(this, &beep_sound::stop, time_s);
RBH 0:c9832831c73f 110 }
RBH 0:c9832831c73f 111 void beep_sound::sinwave(float center,int speed,int width)
RBH 0:c9832831c73f 112 {
RBH 3:86238dba1529 113 for(float i=0.0f;i<6.28f;i+=0.01*speed)
RBH 0:c9832831c73f 114 {
RBH 0:c9832831c73f 115 m_pwm->period(1.0f/(center+width*(float)sin(i)));
RBH 0:c9832831c73f 116 m_pwm->write(0.5f);
RBH 0:c9832831c73f 117 wait_ms(2);
RBH 0:c9832831c73f 118 }
RBH 0:c9832831c73f 119 }
RBH 0:c9832831c73f 120 void beep_sound::setwave(float frequency,float duty,float wait)
RBH 0:c9832831c73f 121 {
RBH 0:c9832831c73f 122 m_pwm->period(1.0f/(frequency));
RBH 0:c9832831c73f 123 m_pwm->write(duty);
RBH 0:c9832831c73f 124 wait_ms(wait);
RBH 0:c9832831c73f 125 }
RBH 8:26cb94806201 126
RBH 8:26cb94806201 127
RBH 8:26cb94806201 128 //以下サンプル音集
RBH 0:c9832831c73f 129 //引数の回数だけ鳴らす
RBH 0:c9832831c73f 130 void beep_sound::beep_right(int nTimes)
RBH 0:c9832831c73f 131 {
RBH 0:c9832831c73f 132 for(int i=0; i<nTimes; i++){
RBH 4:237b642c2ffa 133 beep_sound::setGakuhu(3,11,0.1);
RBH 4:237b642c2ffa 134 beep_sound::setGakuhu(3,7,0.35);
RBH 4:237b642c2ffa 135 beep_sound::setKyuhu(0.1);
RBH 0:c9832831c73f 136 }
RBH 8:26cb94806201 137 beep_sound::playGakuhu();
RBH 0:c9832831c73f 138 }
RBH 0:c9832831c73f 139 void beep_sound::beep_wrong(int nTimes)
RBH 0:c9832831c73f 140 {
RBH 0:c9832831c73f 141 for (int i = 0; i < nTimes; i++) {
RBH 4:237b642c2ffa 142 beep_sound::setGakuhu(0, 12, 0.1);
RBH 4:237b642c2ffa 143 beep_sound::setKyuhu(0.1);
RBH 4:237b642c2ffa 144 beep_sound::setGakuhu(0, 12, 0.35);
RBH 4:237b642c2ffa 145 beep_sound::setKyuhu(0.1);
RBH 0:c9832831c73f 146 }
RBH 8:26cb94806201 147 beep_sound::playGakuhu();
RBH 0:c9832831c73f 148 }
RBH 0:c9832831c73f 149 void beep_sound::beep_1up(int nTimes)
RBH 0:c9832831c73f 150 {
RBH 0:c9832831c73f 151 for (int i = 0; i < nTimes; i++) {
RBH 6:adb925f93199 152 beep_sound::setGakuhu(3,4,0.1);
RBH 6:adb925f93199 153 beep_sound::setGakuhu(3,7,0.1);
RBH 6:adb925f93199 154 beep_sound::setGakuhu(4,4,0.1);
RBH 6:adb925f93199 155 beep_sound::setGakuhu(4,0,0.1);
RBH 6:adb925f93199 156 beep_sound::setGakuhu(4,2,0.1);
RBH 6:adb925f93199 157 beep_sound::setGakuhu(4,7,0.1);
RBH 0:c9832831c73f 158 }
RBH 8:26cb94806201 159 beep_sound::playGakuhu();
RBH 0:c9832831c73f 160 }
RBH 0:c9832831c73f 161 void beep_sound::beep_konbini(int nTimes)
RBH 0:c9832831c73f 162 {
RBH 0:c9832831c73f 163 for (int i = 0; i < nTimes; i++) {
RBH 6:adb925f93199 164 beep_sound::setGakuhu(2,6,0.2);
RBH 6:adb925f93199 165 beep_sound::setGakuhu(2,2,0.2);
RBH 6:adb925f93199 166 beep_sound::setGakuhu(1,9,0.2);
RBH 6:adb925f93199 167 beep_sound::setGakuhu(2,2,0.2);
RBH 6:adb925f93199 168 beep_sound::setGakuhu(2,4,0.2);
RBH 6:adb925f93199 169 beep_sound::setGakuhu(2,9,0.4);
RBH 6:adb925f93199 170 beep_sound::setGakuhu(1,9,0.2);
RBH 6:adb925f93199 171 beep_sound::setGakuhu(2,4,0.2);
RBH 6:adb925f93199 172 beep_sound::setGakuhu(2,6,0.2);
RBH 6:adb925f93199 173 beep_sound::setGakuhu(2,4,0.2);
RBH 6:adb925f93199 174 beep_sound::setGakuhu(1,9,0.2);
RBH 6:adb925f93199 175 beep_sound::setGakuhu(2,2,0.6);
RBH 0:c9832831c73f 176 }
RBH 8:26cb94806201 177 beep_sound::playGakuhu();
RBH 0:c9832831c73f 178 }
RBH 2:259899b723ab 179 void beep_sound::beep_readyGo(int nTimes)
RBH 2:259899b723ab 180 {
RBH 2:259899b723ab 181 for (int i = 0; i < nTimes; i++) {
RBH 8:26cb94806201 182 beep_sound::setGakuhu(1,0,0.5);
RBH 8:26cb94806201 183 beep_sound::setKyuhu(0.5);
RBH 8:26cb94806201 184 beep_sound::setGakuhu(1,0,0.5);
RBH 8:26cb94806201 185 beep_sound::setKyuhu(0.5);
RBH 8:26cb94806201 186 beep_sound::setGakuhu(1,0,0.5);
RBH 8:26cb94806201 187 beep_sound::setKyuhu(0.5);
RBH 8:26cb94806201 188 beep_sound::setGakuhu(2,0,1);
RBH 2:259899b723ab 189 }
RBH 8:26cb94806201 190 beep_sound::playGakuhu();
RBH 2:259899b723ab 191 }
RBH 0:c9832831c73f 192 void beep_sound::beep_encount(int nTimes)
RBH 0:c9832831c73f 193 {
RBH 0:c9832831c73f 194 for (int i = 0; i < nTimes; i++) {
RBH 0:c9832831c73f 195 beep_sound::onpu(4,0,0.08);
RBH 0:c9832831c73f 196 beep_sound::onpu(3,11,0.08);
RBH 0:c9832831c73f 197 beep_sound::onpu(3,10,0.08);
RBH 0:c9832831c73f 198 beep_sound::onpu(3,9,0.08);
RBH 0:c9832831c73f 199
RBH 0:c9832831c73f 200 beep_sound::onpu(3,10,0.08);
RBH 0:c9832831c73f 201 beep_sound::onpu(3,9,0.08);
RBH 0:c9832831c73f 202 beep_sound::onpu(3,8,0.08);
RBH 0:c9832831c73f 203 beep_sound::onpu(3,7,0.08);
RBH 0:c9832831c73f 204
RBH 0:c9832831c73f 205 beep_sound::onpu(3,8,0.08);
RBH 0:c9832831c73f 206 beep_sound::onpu(3,7,0.08);
RBH 0:c9832831c73f 207 beep_sound::onpu(3,6,0.08);
RBH 0:c9832831c73f 208 beep_sound::onpu(3,5,0.08);
RBH 0:c9832831c73f 209
RBH 0:c9832831c73f 210 beep_sound::onpu(3,6,0.08);
RBH 0:c9832831c73f 211 beep_sound::onpu(3,5,0.08);
RBH 0:c9832831c73f 212 beep_sound::onpu(3,4,0.08);
RBH 0:c9832831c73f 213 beep_sound::onpu(3,3,0.08);
RBH 0:c9832831c73f 214
RBH 0:c9832831c73f 215 beep_sound::onpu(3,4,0.08);
RBH 0:c9832831c73f 216 beep_sound::onpu(3,3,0.08);
RBH 0:c9832831c73f 217 beep_sound::onpu(3,2,0.08);
RBH 0:c9832831c73f 218 beep_sound::onpu(3,1,0.08);
RBH 0:c9832831c73f 219
RBH 0:c9832831c73f 220 beep_sound::onpu(3,2,0.08);
RBH 0:c9832831c73f 221 beep_sound::onpu(3,1,0.08);
RBH 0:c9832831c73f 222 beep_sound::onpu(3,0,0.08);
RBH 0:c9832831c73f 223 beep_sound::onpu(2,11,0.08);
RBH 0:c9832831c73f 224
RBH 0:c9832831c73f 225 beep_sound::onpu(3,0,0.08);
RBH 0:c9832831c73f 226 beep_sound::onpu(2,11,0.08);
RBH 0:c9832831c73f 227 beep_sound::onpu(2,10,0.08);
RBH 0:c9832831c73f 228 beep_sound::onpu(2,9,0.08);
RBH 0:c9832831c73f 229
RBH 0:c9832831c73f 230 beep_sound::onpu(2,10,0.08);
RBH 0:c9832831c73f 231 beep_sound::onpu(2,9,0.08);
RBH 0:c9832831c73f 232 beep_sound::onpu(2,8,0.08);
RBH 0:c9832831c73f 233 beep_sound::onpu(2,7,0.08);
RBH 0:c9832831c73f 234 }
RBH 0:c9832831c73f 235 wait(0.1);
RBH 0:c9832831c73f 236 }
RBH 7:5f1b0adb25ec 237 void beep_sound::beep_notif1(int nTimes){
RBH 7:5f1b0adb25ec 238 for (int i = 0; i < nTimes; i++) {
RBH 7:5f1b0adb25ec 239 beep_sound::setGakuhu(3,5,0.1);
RBH 7:5f1b0adb25ec 240 beep_sound::setGakuhu(3,10,0.1);
RBH 7:5f1b0adb25ec 241 beep_sound::setGakuhu(4,2,0.1);
RBH 7:5f1b0adb25ec 242 }
RBH 8:26cb94806201 243 beep_sound::playGakuhu();
RBH 7:5f1b0adb25ec 244 }
RBH 7:5f1b0adb25ec 245 void beep_sound::beep_notif2(int nTimes){
RBH 7:5f1b0adb25ec 246 for (int i = 0; i < nTimes; i++) {
RBH 7:5f1b0adb25ec 247 beep_sound::setGakuhu(1,2,0.1);
RBH 7:5f1b0adb25ec 248 beep_sound::setGakuhu(1,9,0.1);
RBH 7:5f1b0adb25ec 249 beep_sound::setGakuhu(2,2,0.2);
RBH 7:5f1b0adb25ec 250 }
RBH 8:26cb94806201 251 beep_sound::playGakuhu();
RBH 7:5f1b0adb25ec 252 }
RBH 7:5f1b0adb25ec 253 void beep_sound::beep_potatoKansei(int nTimes){
RBH 7:5f1b0adb25ec 254 for (int i = 0; i < nTimes; i++) {
RBH 7:5f1b0adb25ec 255 beep_sound::setGakuhu(1,7,0.25);
RBH 7:5f1b0adb25ec 256 beep_sound::setGakuhu(1,5,0.25);
RBH 7:5f1b0adb25ec 257 beep_sound::setGakuhu(1,7,0.25);
RBH 7:5f1b0adb25ec 258 beep_sound::setKyuhu(0.25);
RBH 7:5f1b0adb25ec 259 }
RBH 8:26cb94806201 260 beep_sound::playGakuhu();
RBH 7:5f1b0adb25ec 261 }