PWM drives the speaker.

Files at this revision

API Documentation at this revision

Comitter:
dkato
Date:
Tue Jul 24 08:31:11 2018 +0000
Parent:
2:436529700217
Commit message:
Changed to be usable with EasyPlayback

Changed in this revision

PwmOutSpeaker.cpp Show annotated file Show diff for this revision Revisions of this file
PwmOutSpeaker.h Show annotated file Show diff for this revision Revisions of this file
diff -r 436529700217 -r 37a886d77bae PwmOutSpeaker.cpp
--- a/PwmOutSpeaker.cpp	Fri Mar 24 06:55:24 2017 +0000
+++ b/PwmOutSpeaker.cpp	Tue Jul 24 08:31:11 2018 +0000
@@ -27,15 +27,12 @@
     _bottom = 0;
     _top = 0;
     _playing = false;
-    outputVolume(1.0f);
-    format(16, 2);
+    outputVolume(1.0f, 1.0f);
+    format(16);
     frequency(44100);
 }
 
-bool PwmOutSpeaker::format(char length, char ch) {
-    if ((ch != 1) && (ch != 2)) {
-        return false;
-    }
+bool PwmOutSpeaker::format(char length) {
     switch (length) {
         case 8:
         case 16:
@@ -44,7 +41,6 @@
             return false;
     }
     _length = length;
-    _channel = ch;
     _data_cnt = 0;
     return true;
 }
@@ -81,11 +77,13 @@
     return true;
 }
 
-int PwmOutSpeaker::write(uint8_t * const p_data, uint32_t data_size) {
+int PwmOutSpeaker::write(void * const p_data, uint32_t data_size, const rbsp_data_conf_t * const p_data_conf) {
     int data_num;
     int i = 0;
-    float wk_vol;
-    float wk_ofs;
+    float wk_vol_l;
+    float wk_ofs_l;
+    float wk_vol_r;
+    float wk_ofs_r;
 
     if (_length == 8) {
         data_num = data_size;
@@ -95,40 +93,47 @@
     while (i < data_num) {
         if (_data_cnt < (_hz_multi - 1)) {
             _data_cnt++;
-            i += _channel;
+            i += 2;
         } 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;
+            wk_vol_l = _speaker_vol_l;
+            wk_ofs_l = (1.0f - wk_vol_l) / 2;
+            wk_vol_r = _speaker_vol_r;
+            wk_ofs_r = (1.0f - wk_vol_r) / 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;
-                }
+                _pwm_duty_buf[_bottom] = ((float)((uint8_t *)p_data)[i++] / (float)0xff) * wk_vol_l + wk_ofs_l;
+                _pwm_duty_buf[_bottom + 1] = ((float)((uint8_t *)p_data)[i++] / (float)0xff) * wk_vol_r + wk_ofs_r;
             } 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;
-                }
+                _pwm_duty_buf[_bottom] = ((float)(((int16_t *)p_data)[i++] + 0x8000) / (float)0xffff) * wk_vol_l + wk_ofs_l;
+                _pwm_duty_buf[_bottom + 1] = ((float)(((int16_t *)p_data)[i++] + 0x8000) / (float)0xffff) * wk_vol_r + wk_ofs_r;
             }
             _bottom = (_bottom + 2) & MSK_RING_BUFF;
         }
     }
+
+    if (p_data_conf != NULL) {
+        return data_size;
+    }
+
+    if (p_data_conf->p_notify_func != NULL) {
+        p_data_conf->p_notify_func(p_data, data_size, p_data_conf->p_app_data);
+    }
+
     return 0;
 }
 
-bool PwmOutSpeaker::outputVolume(float volume) {
-    if ((volume < 0.0) || (volume > 1.0)) {
+bool PwmOutSpeaker::outputVolume(float leftVolumeOut, float rightVolumeOut) {
+    if ((leftVolumeOut < 0.0) || (leftVolumeOut > 1.0)) {
         return false;
     }
-    _speaker_vol  = volume;
+    if ((rightVolumeOut < 0.0) || (rightVolumeOut > 1.0)) {
+        return false;
+    }
+    _speaker_vol_l  = leftVolumeOut;
+    _speaker_vol_r  = rightVolumeOut;
     return true;
 }
 
diff -r 436529700217 -r 37a886d77bae PwmOutSpeaker.h
--- a/PwmOutSpeaker.h	Fri Mar 24 06:55:24 2017 +0000
+++ b/PwmOutSpeaker.h	Tue Jul 24 08:31:11 2018 +0000
@@ -18,12 +18,12 @@
 #define PWMOUT_SPEAKER_H
 
 #include "mbed.h"
-#include "rtos.h"
+#include "AUDIO_RBSP.h"
 
 /** PwmOutSpeaker class
 *
 */
-class PwmOutSpeaker {
+class PwmOutSpeaker : public AUDIO_RBSP {
 public:
     /** Create a PwmOutSpeaker
      * 
@@ -32,12 +32,16 @@
      */
     PwmOutSpeaker(PinName pwm_l, PinName pwm_r);
 
+    virtual void power(bool type = true) {
+        return;
+    }
+
     /** Set I2S interface bit length and mode
      *
      * @param length Set bit length to 8 or 16 bits
      * @return true = success, false = failure
      */
-    bool format(char length, char ch = 2);
+    virtual bool format(char length);
 
     /** Set sample frequency
      *
@@ -47,7 +51,7 @@
      * Supports the following frequencies: 8kHz, 8.021kHz, 32kHz, 44.1kHz, 48kHz
      * Default is 44.1kHz
      */
-    bool frequency(int hz);
+    virtual bool frequency(int hz);
 
     /** Enqueue asynchronous write request
      *
@@ -55,7 +59,11 @@
      * @param data_size Number of bytes to write
      * @return Number of bytes written on success. negative number on error.
      */
-    int write(uint8_t * const p_data, uint32_t data_size);
+    virtual int write(void * const p_data, uint32_t data_size, const rbsp_data_conf_t * const p_data_conf = NULL);
+
+    virtual int read(void * const p_data, uint32_t data_size, const rbsp_data_conf_t * const p_data_conf = NULL) {
+        return -1;
+    }
 
     /** Volume control
      *
@@ -63,7 +71,11 @@
      * @return Returns "true" for success, "false" if parameters are out of range
      * Parameters accept a value, where 0.0 <= parameter <= 1.0 (1.0 = default)
      */
-    bool outputVolume(float volume);
+    virtual bool outputVolume(float leftVolumeOut, float rightVolumeOut);
+
+    virtual bool micVolume(float VolumeIn) {
+        return false;
+    }
 
 private:
     #define WRITE_BUFF_SIZE    (1024 * 4)
@@ -72,14 +84,14 @@
     PwmOut   _speaker_l;
     PwmOut   _speaker_r;
     Ticker   _timer;
-    int      _channel;
     int      _length;
     int      _hz_multi;
     int      _data_cnt;
     bool     _playing;
     volatile uint32_t _bottom;
     volatile uint32_t _top;
-    float    _speaker_vol;
+    float    _speaker_vol_l;
+    float    _speaker_vol_r;
     float    _pwm_duty_buf[WRITE_BUFF_SIZE];
 
     void sound_out(void);