Daiki Kato / PwmOutSpeaker
Revision:
0:aba6e62b51a7
Child:
1:8ec133daca4c
diff -r 000000000000 -r aba6e62b51a7 PwmOutSpeaker.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/PwmOutSpeaker.cpp	Tue Dec 20 05:27:42 2016 +0000
@@ -0,0 +1,141 @@
+/* mbed PwmOutSpeaker Library
+ * Copyright (C) 2016 dkato
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "PwmOutSpeaker.h"
+
+PwmOutSpeaker::PwmOutSpeaker(PinName pwm_l, PinName pwm_r) : _speaker_l(pwm_l), _speaker_r(pwm_r) {
+    _bottom = 0;
+    _top = 0;
+    _playing = false;
+    outputVolume(1.0f);
+    format(16, 2);
+    frequency(44100);
+}
+
+bool PwmOutSpeaker::format(char length, char ch) {
+    if ((ch != 1) && (ch != 2)) {
+        return false;
+    }
+    switch (length) {
+        case 8:
+        case 16:
+            break;
+        default:
+            return false;
+    }
+    _length = length;
+    _channel = ch;
+    _data_cnt = 0;
+    return true;
+}
+
+bool PwmOutSpeaker::frequency(int hz) {
+    int wk_us;
+
+    switch (hz) {
+        case 48000:
+            _hz_multi = 6;
+            break;
+        case 44100:
+            _hz_multi = 5;
+            break;
+        case 32000:
+            _hz_multi = 4;
+            break;
+        case 8021:
+        case 8000:
+            _hz_multi = 1;
+            break;
+        default:
+            return false;
+    }
+    _speaker_l.write(0.5f);
+    _speaker_r.write(0.5f);
+    _playing = false;
+    _speaker_l.period_us(10);  // 100kHz
+    _speaker_r.period_us(10);  // 100kHz
+    wk_us = (int)(1000000.0f / hz * _hz_multi + 0.5f);
+    _timer.attach_us(Callback<void()>(this, &PwmOutSpeaker::sound_out), wk_us);
+    _data_cnt = 0;
+
+    return true;
+}
+
+int PwmOutSpeaker::write(uint8_t * const p_data, uint32_t data_size) {
+    int data_num;
+    int i = 0;
+    float wk_vol;
+    float wk_ofs;
+
+    if (_length == 8) {
+        data_num = data_size;
+    } else {
+        data_num = data_size / 2;
+    }
+    while (i < data_num) {
+        if (_data_cnt < (_hz_multi - 1)) {
+            _data_cnt++;
+            i += _channel;
+        } else {
+            _data_cnt = 0;
+            while (((_bottom + 2) & MSK_RING_BUFF) == _top) {
+                Thread::wait(1);
+            }
+            wk_vol = _speaker_vol;
+            wk_ofs = (1.0f - wk_vol) / 2;
+            if (_length == 8) {
+                _pwm_duty_buf[_bottom] = ((float)p_data[i++] / (float)0xff) * wk_vol + wk_ofs;
+                if (_channel == 1) {
+                    _pwm_duty_buf[_bottom + 1] = _pwm_duty_buf[_bottom];
+                } else {
+                    _pwm_duty_buf[_bottom + 1] = ((float)p_data[i++] / (float)0xff) * wk_vol + wk_ofs;
+                }
+            } else {
+                _pwm_duty_buf[_bottom] = ((float)(((int16_t *)p_data)[i++] + 0x8000) / (float)0xffff) * wk_vol + wk_ofs;
+                if (_channel == 1) {
+                    _pwm_duty_buf[_bottom + 1] = _pwm_duty_buf[_bottom];
+                } else {
+                    _pwm_duty_buf[_bottom + 1] = ((float)(((int16_t *)p_data)[i++] + 0x8000) / (float)0xffff) * wk_vol + wk_ofs;
+                }
+            }
+            _bottom = (_bottom + 2) & MSK_RING_BUFF;
+        }
+    }
+    return 0;
+}
+
+bool PwmOutSpeaker::outputVolume(float volume) {
+    if ((volume < 0.0) || (volume > 1.0)) {
+        return false;
+    }
+    _speaker_vol  = volume;
+    return true;
+}
+
+void PwmOutSpeaker::sound_out(void) {
+    if (_top != _bottom) {
+        _speaker_l.write(_pwm_duty_buf[_top + 0]);
+        _speaker_r.write(_pwm_duty_buf[_top + 1]);
+        _top = (_top + 2) & MSK_RING_BUFF;
+        _playing = true;
+    } else if (_playing) {
+        _speaker_l.write(0.5f);
+        _speaker_r.write(0.5f);
+        _playing = false;
+    } else {
+        // do nothing
+    }
+}