Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: kiokuryoku_game escapeFromYou junirobo_sample001 ROBOX_Sample_IRcon ... more
Diff: beep_sound.cpp
- Revision:
- 3:86238dba1529
- Parent:
- 2:259899b723ab
- Child:
- 4:237b642c2ffa
diff -r 259899b723ab -r 86238dba1529 beep_sound.cpp
--- a/beep_sound.cpp Sat Jun 04 17:50:58 2016 +0000
+++ b/beep_sound.cpp Tue Jun 28 09:47:46 2016 +0000
@@ -1,5 +1,8 @@
#include "beep_sound.h"
#include "math.h"
+#include <queue>
+
+
beep_sound::beep_sound(PinName pwm)
{
m_pwm = new PwmOut(pwm);
@@ -11,20 +14,64 @@
delete m_pwm;
}
+void beep_sound::stop(){
+ m_pwm->write(0.0f);
+ // キューが空でなければ次の音へ
+ if(otoQue.empty() != 1){
+ beep_sound::playGakuhu();
+ }
+}
+
+// 音の再生
+void beep_sound::NoteOn(int octave, int note)
+{
+ frequency = frequencyTable[note] << octave;
+ m_pwm->period(1.0f/(float)frequency);
+ m_pwm->write(0.5f);
+}
+
+// 1音ずつ音符情報をキューに追加
+void beep_sound::setGakuhu(int argOc, int argNo, float argTi)
+{
+ buf.octave = argOc;
+ buf.note = argNo;
+ buf.time_s = argTi;
+ otoQue.push(buf);
+}
+
+// 休符をキューに追加
+void beep_sound::setKyuhu(float argTi)
+{
+ buf.octave = 1000;
+ buf.note = 0;
+ buf.time_s = argTi;
+ otoQue.push(buf);
+}
+
+// 楽譜キューを再生、音停止の割り込み予約
+// Timeoutの日本語リファレンスには書いていないが、
+// thisを引数にしないとコンパイルエラーになるので注意
+void beep_sound::playGakuhu()
+{
+ beep_sound::NoteOn(otoQue.front().octave, otoQue.front().note);
+ interruptStop.attach(this, &beep_sound::stop, otoQue.front().time_s);
+ otoQue.pop();
+}
+
void beep_sound::SetFrequency(int octave, int note, bool on_off)
{
- static int preFrequency = 0;
- if(on_off == true){
- int frequency = frequencyTable[note] << octave;
- if(frequency != preFrequency){
- m_pwm->period(1.0f/(float)frequency);
- m_pwm->write(0.5f);
- }
- preFrequency = frequency;
- }else{
- m_pwm->write(0.0f);
- preFrequency = 0;
+ static int preFrequency = 0;
+ if(on_off == true){
+ int frequency = frequencyTable[note] << octave;
+ if(frequency != preFrequency){
+ m_pwm->period(1.0f/(float)frequency);
+ m_pwm->write(0.5f);
}
+ preFrequency = frequency;
+ }else{
+ m_pwm->write(0.0f);
+ preFrequency = 0;
+ }
}
void beep_sound::onpu(int octave, int note, float time_s)
@@ -45,7 +92,7 @@
}
void beep_sound::sinwave(float center,int speed,int width)
{
- for(float i=0.0;i<6.28;i+=0.01*speed)
+ for(float i=0.0f;i<6.28f;i+=0.01*speed)
{
m_pwm->period(1.0f/(center+width*(float)sin(i)));
m_pwm->write(0.5f);