Hirotaka Yoneda / beep_sound

Dependents:   kiokuryoku_game escapeFromYou junirobo_sample001 ROBOX_Sample_IRcon ... more

Committer:
RBH
Date:
Sat Oct 08 19:28:44 2016 +0000
Revision:
10:24155220eeda
Parent:
9:d899b134f9bb
Child:
11:561f2d3f7c4b
readyGo??;

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 10:24155220eeda 111 void beep_sound::onpu_stop(int octave, int note, float time_s)
RBH 10:24155220eeda 112 {
RBH 10:24155220eeda 113 playing = true;
RBH 10:24155220eeda 114 beep_sound::NoteOn(octave, note);
RBH 10:24155220eeda 115 wait(time_s);
RBH 10:24155220eeda 116 m_pwm->write(0.0f);
RBH 10:24155220eeda 117 }
RBH 0:c9832831c73f 118 void beep_sound::sinwave(float center,int speed,int width)
RBH 0:c9832831c73f 119 {
RBH 3:86238dba1529 120 for(float i=0.0f;i<6.28f;i+=0.01*speed)
RBH 0:c9832831c73f 121 {
RBH 0:c9832831c73f 122 m_pwm->period(1.0f/(center+width*(float)sin(i)));
RBH 0:c9832831c73f 123 m_pwm->write(0.5f);
RBH 0:c9832831c73f 124 wait_ms(2);
RBH 0:c9832831c73f 125 }
RBH 0:c9832831c73f 126 }
RBH 0:c9832831c73f 127 void beep_sound::setwave(float frequency,float duty,float wait)
RBH 0:c9832831c73f 128 {
RBH 0:c9832831c73f 129 m_pwm->period(1.0f/(frequency));
RBH 0:c9832831c73f 130 m_pwm->write(duty);
RBH 0:c9832831c73f 131 wait_ms(wait);
RBH 0:c9832831c73f 132 }
RBH 8:26cb94806201 133
RBH 8:26cb94806201 134
RBH 8:26cb94806201 135 //以下サンプル音集
RBH 0:c9832831c73f 136 //引数の回数だけ鳴らす
RBH 0:c9832831c73f 137 void beep_sound::beep_right(int nTimes)
RBH 0:c9832831c73f 138 {
RBH 0:c9832831c73f 139 for(int i=0; i<nTimes; i++){
RBH 4:237b642c2ffa 140 beep_sound::setGakuhu(3,11,0.1);
RBH 4:237b642c2ffa 141 beep_sound::setGakuhu(3,7,0.35);
RBH 4:237b642c2ffa 142 beep_sound::setKyuhu(0.1);
RBH 0:c9832831c73f 143 }
RBH 8:26cb94806201 144 beep_sound::playGakuhu();
RBH 0:c9832831c73f 145 }
RBH 0:c9832831c73f 146 void beep_sound::beep_wrong(int nTimes)
RBH 0:c9832831c73f 147 {
RBH 0:c9832831c73f 148 for (int i = 0; i < nTimes; i++) {
RBH 4:237b642c2ffa 149 beep_sound::setGakuhu(0, 12, 0.1);
RBH 4:237b642c2ffa 150 beep_sound::setKyuhu(0.1);
RBH 4:237b642c2ffa 151 beep_sound::setGakuhu(0, 12, 0.35);
RBH 4:237b642c2ffa 152 beep_sound::setKyuhu(0.1);
RBH 0:c9832831c73f 153 }
RBH 8:26cb94806201 154 beep_sound::playGakuhu();
RBH 0:c9832831c73f 155 }
RBH 0:c9832831c73f 156 void beep_sound::beep_1up(int nTimes)
RBH 0:c9832831c73f 157 {
RBH 0:c9832831c73f 158 for (int i = 0; i < nTimes; i++) {
RBH 6:adb925f93199 159 beep_sound::setGakuhu(3,4,0.1);
RBH 6:adb925f93199 160 beep_sound::setGakuhu(3,7,0.1);
RBH 6:adb925f93199 161 beep_sound::setGakuhu(4,4,0.1);
RBH 6:adb925f93199 162 beep_sound::setGakuhu(4,0,0.1);
RBH 6:adb925f93199 163 beep_sound::setGakuhu(4,2,0.1);
RBH 6:adb925f93199 164 beep_sound::setGakuhu(4,7,0.1);
RBH 0:c9832831c73f 165 }
RBH 8:26cb94806201 166 beep_sound::playGakuhu();
RBH 0:c9832831c73f 167 }
RBH 0:c9832831c73f 168 void beep_sound::beep_konbini(int nTimes)
RBH 0:c9832831c73f 169 {
RBH 0:c9832831c73f 170 for (int i = 0; i < nTimes; i++) {
RBH 6:adb925f93199 171 beep_sound::setGakuhu(2,6,0.2);
RBH 6:adb925f93199 172 beep_sound::setGakuhu(2,2,0.2);
RBH 6:adb925f93199 173 beep_sound::setGakuhu(1,9,0.2);
RBH 6:adb925f93199 174 beep_sound::setGakuhu(2,2,0.2);
RBH 6:adb925f93199 175 beep_sound::setGakuhu(2,4,0.2);
RBH 6:adb925f93199 176 beep_sound::setGakuhu(2,9,0.4);
RBH 6:adb925f93199 177 beep_sound::setGakuhu(1,9,0.2);
RBH 6:adb925f93199 178 beep_sound::setGakuhu(2,4,0.2);
RBH 6:adb925f93199 179 beep_sound::setGakuhu(2,6,0.2);
RBH 6:adb925f93199 180 beep_sound::setGakuhu(2,4,0.2);
RBH 6:adb925f93199 181 beep_sound::setGakuhu(1,9,0.2);
RBH 6:adb925f93199 182 beep_sound::setGakuhu(2,2,0.6);
RBH 0:c9832831c73f 183 }
RBH 8:26cb94806201 184 beep_sound::playGakuhu();
RBH 0:c9832831c73f 185 }
RBH 2:259899b723ab 186 void beep_sound::beep_readyGo(int nTimes)
RBH 2:259899b723ab 187 {
RBH 10:24155220eeda 188 //for (int i = 0; i < nTimes; i++) {
RBH 10:24155220eeda 189 beep_sound::onpu_stop(1,0,0.5);
RBH 10:24155220eeda 190 wait(0.5);
RBH 10:24155220eeda 191 beep_sound::onpu_stop(1,0,0.5);
RBH 10:24155220eeda 192 wait(0.5);
RBH 10:24155220eeda 193 beep_sound::onpu_stop(1,0,0.5);
RBH 10:24155220eeda 194 wait(0.5);
RBH 10:24155220eeda 195 beep_sound::onpu(2,0,1);
RBH 10:24155220eeda 196 //}
RBH 2:259899b723ab 197 }
RBH 0:c9832831c73f 198 void beep_sound::beep_encount(int nTimes)
RBH 0:c9832831c73f 199 {
RBH 0:c9832831c73f 200 for (int i = 0; i < nTimes; i++) {
RBH 0:c9832831c73f 201 beep_sound::onpu(4,0,0.08);
RBH 0:c9832831c73f 202 beep_sound::onpu(3,11,0.08);
RBH 0:c9832831c73f 203 beep_sound::onpu(3,10,0.08);
RBH 0:c9832831c73f 204 beep_sound::onpu(3,9,0.08);
RBH 0:c9832831c73f 205
RBH 0:c9832831c73f 206 beep_sound::onpu(3,10,0.08);
RBH 0:c9832831c73f 207 beep_sound::onpu(3,9,0.08);
RBH 0:c9832831c73f 208 beep_sound::onpu(3,8,0.08);
RBH 0:c9832831c73f 209 beep_sound::onpu(3,7,0.08);
RBH 0:c9832831c73f 210
RBH 0:c9832831c73f 211 beep_sound::onpu(3,8,0.08);
RBH 0:c9832831c73f 212 beep_sound::onpu(3,7,0.08);
RBH 0:c9832831c73f 213 beep_sound::onpu(3,6,0.08);
RBH 0:c9832831c73f 214 beep_sound::onpu(3,5,0.08);
RBH 0:c9832831c73f 215
RBH 0:c9832831c73f 216 beep_sound::onpu(3,6,0.08);
RBH 0:c9832831c73f 217 beep_sound::onpu(3,5,0.08);
RBH 0:c9832831c73f 218 beep_sound::onpu(3,4,0.08);
RBH 0:c9832831c73f 219 beep_sound::onpu(3,3,0.08);
RBH 0:c9832831c73f 220
RBH 0:c9832831c73f 221 beep_sound::onpu(3,4,0.08);
RBH 0:c9832831c73f 222 beep_sound::onpu(3,3,0.08);
RBH 0:c9832831c73f 223 beep_sound::onpu(3,2,0.08);
RBH 0:c9832831c73f 224 beep_sound::onpu(3,1,0.08);
RBH 0:c9832831c73f 225
RBH 0:c9832831c73f 226 beep_sound::onpu(3,2,0.08);
RBH 0:c9832831c73f 227 beep_sound::onpu(3,1,0.08);
RBH 0:c9832831c73f 228 beep_sound::onpu(3,0,0.08);
RBH 0:c9832831c73f 229 beep_sound::onpu(2,11,0.08);
RBH 0:c9832831c73f 230
RBH 0:c9832831c73f 231 beep_sound::onpu(3,0,0.08);
RBH 0:c9832831c73f 232 beep_sound::onpu(2,11,0.08);
RBH 0:c9832831c73f 233 beep_sound::onpu(2,10,0.08);
RBH 0:c9832831c73f 234 beep_sound::onpu(2,9,0.08);
RBH 0:c9832831c73f 235
RBH 0:c9832831c73f 236 beep_sound::onpu(2,10,0.08);
RBH 0:c9832831c73f 237 beep_sound::onpu(2,9,0.08);
RBH 0:c9832831c73f 238 beep_sound::onpu(2,8,0.08);
RBH 0:c9832831c73f 239 beep_sound::onpu(2,7,0.08);
RBH 0:c9832831c73f 240 }
RBH 0:c9832831c73f 241 wait(0.1);
RBH 0:c9832831c73f 242 }
RBH 7:5f1b0adb25ec 243 void beep_sound::beep_notif1(int nTimes){
RBH 7:5f1b0adb25ec 244 for (int i = 0; i < nTimes; i++) {
RBH 7:5f1b0adb25ec 245 beep_sound::setGakuhu(3,5,0.1);
RBH 7:5f1b0adb25ec 246 beep_sound::setGakuhu(3,10,0.1);
RBH 7:5f1b0adb25ec 247 beep_sound::setGakuhu(4,2,0.1);
RBH 7:5f1b0adb25ec 248 }
RBH 8:26cb94806201 249 beep_sound::playGakuhu();
RBH 7:5f1b0adb25ec 250 }
RBH 7:5f1b0adb25ec 251 void beep_sound::beep_notif2(int nTimes){
RBH 7:5f1b0adb25ec 252 for (int i = 0; i < nTimes; i++) {
RBH 7:5f1b0adb25ec 253 beep_sound::setGakuhu(1,2,0.1);
RBH 7:5f1b0adb25ec 254 beep_sound::setGakuhu(1,9,0.1);
RBH 7:5f1b0adb25ec 255 beep_sound::setGakuhu(2,2,0.2);
RBH 7:5f1b0adb25ec 256 }
RBH 8:26cb94806201 257 beep_sound::playGakuhu();
RBH 7:5f1b0adb25ec 258 }
RBH 7:5f1b0adb25ec 259 void beep_sound::beep_potatoKansei(int nTimes){
RBH 7:5f1b0adb25ec 260 for (int i = 0; i < nTimes; i++) {
RBH 7:5f1b0adb25ec 261 beep_sound::setGakuhu(1,7,0.25);
RBH 7:5f1b0adb25ec 262 beep_sound::setGakuhu(1,5,0.25);
RBH 7:5f1b0adb25ec 263 beep_sound::setGakuhu(1,7,0.25);
RBH 7:5f1b0adb25ec 264 beep_sound::setKyuhu(0.25);
RBH 7:5f1b0adb25ec 265 }
RBH 8:26cb94806201 266 beep_sound::playGakuhu();
RBH 7:5f1b0adb25ec 267 }