zzzz
Audio.cpp@1:931e45afbcb4, 2020-08-26 (annotated)
- Committer:
- mauuuuul
- Date:
- Wed Aug 26 19:45:22 2020 +0000
- Revision:
- 1:931e45afbcb4
- Parent:
- 0:6d2a1fb93e9e
zzzzzzzzz
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
irsanjul | 0:6d2a1fb93e9e | 1 | #include "Audio.h" |
irsanjul | 0:6d2a1fb93e9e | 2 | |
mauuuuul | 1:931e45afbcb4 | 3 | extern Serial dbg; |
irsanjul | 0:6d2a1fb93e9e | 4 | |
mauuuuul | 1:931e45afbcb4 | 5 | double nada[10] = {0, 247, 262, 294, 330, 349, 392, 440, 494, 523}; |
mauuuuul | 1:931e45afbcb4 | 6 | //double nadas[16] = { 6, 6, 0, 6, |
mauuuuul | 1:931e45afbcb4 | 7 | // 0, 4, 6, 0, |
mauuuuul | 1:931e45afbcb4 | 8 | // 8, 0, 0, 0, |
mauuuuul | 1:931e45afbcb4 | 9 | // 1, 0, 0, 0 }; |
mauuuuul | 1:931e45afbcb4 | 10 | // |
mauuuuul | 1:931e45afbcb4 | 11 | unsigned int nadas[8] = { 1, 2, 3, 4, |
mauuuuul | 1:931e45afbcb4 | 12 | 5, 6, 7, 8 |
mauuuuul | 1:931e45afbcb4 | 13 | }; |
mauuuuul | 1:931e45afbcb4 | 14 | |
mauuuuul | 1:931e45afbcb4 | 15 | Audio::Audio(PinName buzzer): _out(buzzer) |
irsanjul | 0:6d2a1fb93e9e | 16 | { |
irsanjul | 0:6d2a1fb93e9e | 17 | } |
mauuuuul | 1:931e45afbcb4 | 18 | |
irsanjul | 0:6d2a1fb93e9e | 19 | void Audio::SetVolume(double volume) |
irsanjul | 0:6d2a1fb93e9e | 20 | { |
irsanjul | 0:6d2a1fb93e9e | 21 | vol = volume; |
mauuuuul | 1:931e45afbcb4 | 22 | } |
irsanjul | 0:6d2a1fb93e9e | 23 | |
irsanjul | 0:6d2a1fb93e9e | 24 | void Audio::SetDuration(unsigned int milisec) |
irsanjul | 0:6d2a1fb93e9e | 25 | { |
irsanjul | 0:6d2a1fb93e9e | 26 | ms = milisec; |
irsanjul | 0:6d2a1fb93e9e | 27 | } |
mauuuuul | 1:931e45afbcb4 | 28 | |
irsanjul | 0:6d2a1fb93e9e | 29 | void Audio::PlayNote(unsigned int angka) |
irsanjul | 0:6d2a1fb93e9e | 30 | { |
mauuuuul | 1:931e45afbcb4 | 31 | if(angka > 8) angka -= 8; |
mauuuuul | 1:931e45afbcb4 | 32 | |
irsanjul | 0:6d2a1fb93e9e | 33 | double Nada = nada[angka]; |
irsanjul | 0:6d2a1fb93e9e | 34 | PlayNote(Nada*4.24, ms, vol); |
irsanjul | 0:6d2a1fb93e9e | 35 | } |
irsanjul | 0:6d2a1fb93e9e | 36 | |
irsanjul | 0:6d2a1fb93e9e | 37 | void Audio::mute() |
irsanjul | 0:6d2a1fb93e9e | 38 | { |
mauuuuul | 1:931e45afbcb4 | 39 | DigitalOut *out = new DigitalOut(_out); |
mauuuuul | 1:931e45afbcb4 | 40 | out->write(0); |
mauuuuul | 1:931e45afbcb4 | 41 | delete out; |
irsanjul | 0:6d2a1fb93e9e | 42 | } |
irsanjul | 0:6d2a1fb93e9e | 43 | |
irsanjul | 0:6d2a1fb93e9e | 44 | void Audio::PlayNote(double frequency, double duration, double volume) |
irsanjul | 0:6d2a1fb93e9e | 45 | { |
mauuuuul | 1:931e45afbcb4 | 46 | PwmOut *out = new PwmOut(_out); |
mauuuuul | 1:931e45afbcb4 | 47 | out->period(1.0/frequency); |
mauuuuul | 1:931e45afbcb4 | 48 | out->write(volume/2.0); |
irsanjul | 0:6d2a1fb93e9e | 49 | wait_ms(duration); |
mauuuuul | 1:931e45afbcb4 | 50 | out->write(0.0); |
mauuuuul | 1:931e45afbcb4 | 51 | delete out; |
irsanjul | 0:6d2a1fb93e9e | 52 | } |
mauuuuul | 1:931e45afbcb4 | 53 | |
mauuuuul | 1:931e45afbcb4 | 54 | // ----------------------------------------------------------------------------- |
mauuuuul | 1:931e45afbcb4 | 55 | MyAudio::MyAudio(PinName buzz) : Audio (buzz) |
mauuuuul | 1:931e45afbcb4 | 56 | {} |
mauuuuul | 1:931e45afbcb4 | 57 | |
mauuuuul | 1:931e45afbcb4 | 58 | void MyAudio::play(const int &code) |
mauuuuul | 1:931e45afbcb4 | 59 | { |
mauuuuul | 1:931e45afbcb4 | 60 | switch(code) { |
mauuuuul | 1:931e45afbcb4 | 61 | case 0 : |
mauuuuul | 1:931e45afbcb4 | 62 | mute(); |
mauuuuul | 1:931e45afbcb4 | 63 | break; |
mauuuuul | 1:931e45afbcb4 | 64 | |
mauuuuul | 1:931e45afbcb4 | 65 | case 1 : { |
mauuuuul | 1:931e45afbcb4 | 66 | set(500, 0.7); |
mauuuuul | 1:931e45afbcb4 | 67 | playing(3); |
mauuuuul | 1:931e45afbcb4 | 68 | set(500, 0.7); |
mauuuuul | 1:931e45afbcb4 | 69 | playing(6); |
mauuuuul | 1:931e45afbcb4 | 70 | } |
mauuuuul | 1:931e45afbcb4 | 71 | break; |
mauuuuul | 1:931e45afbcb4 | 72 | |
mauuuuul | 1:931e45afbcb4 | 73 | case 2 : { |
mauuuuul | 1:931e45afbcb4 | 74 | set(1000, 2); |
mauuuuul | 1:931e45afbcb4 | 75 | playing(5); |
mauuuuul | 1:931e45afbcb4 | 76 | playing(2); |
mauuuuul | 1:931e45afbcb4 | 77 | } |
mauuuuul | 1:931e45afbcb4 | 78 | break; |
mauuuuul | 1:931e45afbcb4 | 79 | |
mauuuuul | 1:931e45afbcb4 | 80 | case 3 : { |
mauuuuul | 1:931e45afbcb4 | 81 | set(500, 0.7); |
mauuuuul | 1:931e45afbcb4 | 82 | playing(6); |
mauuuuul | 1:931e45afbcb4 | 83 | } |
mauuuuul | 1:931e45afbcb4 | 84 | break; |
mauuuuul | 1:931e45afbcb4 | 85 | |
mauuuuul | 1:931e45afbcb4 | 86 | default: |
mauuuuul | 1:931e45afbcb4 | 87 | set(100, 0.5); |
mauuuuul | 1:931e45afbcb4 | 88 | for(int j=0; j<8; j++) { |
mauuuuul | 1:931e45afbcb4 | 89 | playing(nadas[j]); |
mauuuuul | 1:931e45afbcb4 | 90 | } |
mauuuuul | 1:931e45afbcb4 | 91 | set(); |
mauuuuul | 1:931e45afbcb4 | 92 | mute(); |
mauuuuul | 1:931e45afbcb4 | 93 | break; |
mauuuuul | 1:931e45afbcb4 | 94 | } |
mauuuuul | 1:931e45afbcb4 | 95 | } |
mauuuuul | 1:931e45afbcb4 | 96 | |
mauuuuul | 1:931e45afbcb4 | 97 | void MyAudio::set(double dur, double vol) |
mauuuuul | 1:931e45afbcb4 | 98 | { |
mauuuuul | 1:931e45afbcb4 | 99 | Audio::SetVolume(vol); |
mauuuuul | 1:931e45afbcb4 | 100 | Audio::SetDuration(dur); |
mauuuuul | 1:931e45afbcb4 | 101 | } |
mauuuuul | 1:931e45afbcb4 | 102 | |
mauuuuul | 1:931e45afbcb4 | 103 | void MyAudio::playing(unsigned int note) |
mauuuuul | 1:931e45afbcb4 | 104 | { |
mauuuuul | 1:931e45afbcb4 | 105 | Audio::PlayNote(note); |
mauuuuul | 1:931e45afbcb4 | 106 | } |
mauuuuul | 1:931e45afbcb4 | 107 | |
mauuuuul | 1:931e45afbcb4 | 108 | void MyAudio::mute() |
mauuuuul | 1:931e45afbcb4 | 109 | { |
mauuuuul | 1:931e45afbcb4 | 110 | Audio::mute(); |
mauuuuul | 1:931e45afbcb4 | 111 | } |