Il y avait des problèmes dans la libraire...

Dependencies:   ST_FREQUENCY_DIVIDER ST_I2S USBDEVICE

Fork of X_NUCLEO_CCA02M1 by ST

Revision:
2:9f389fd8fb2e
Child:
7:9d6a4a53e640
diff -r 245f83276546 -r 9f389fd8fb2e BSP/PDM2PCMAudio.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BSP/PDM2PCMAudio.cpp	Fri Apr 21 10:33:08 2017 +0200
@@ -0,0 +1,153 @@
+/**
+ ******************************************************************************
+ * @file    PDM2PCMAudio_class.cpp
+ * @author  AST / Software Platforms and Cloud
+ * @version V1.0
+ * @date    November 10th, 2016
+ * @brief   Implementation file for the PDM2PCMAudio conversion library.
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *   1. Redistributions of source code must retain the above copyright notice,
+ *      this list of conditions and the following disclaimer.
+ *   2. Redistributions in binary form must reproduce the above copyright notice,
+ *      this list of conditions and the following disclaimer in the documentation
+ *      and/or other materials provided with the distribution.
+ *   3. Neither the name of STMicroelectronics nor the names of its contributors
+ *      may be used to endorse or promote products derived from this software
+ *      without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ ******************************************************************************
+ */
+
+
+/* Includes ------------------------------------------------------------------*/
+
+#include "PDM2PCMAudio.h"
+
+
+/* Variables -----------------------------------------------------------------*/
+
+/* Demux filter. */
+const uint8_t PDM2PCMAudio::_demux_filter[DEMUX_FILTER_SIZE] =
+{
+    0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x02, 0x03,
+    0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x02, 0x03,
+    0x04, 0x05, 0x04, 0x05, 0x06, 0x07, 0x06, 0x07,
+    0x04, 0x05, 0x04, 0x05, 0x06, 0x07, 0x06, 0x07,
+    0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x02, 0x03,
+    0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x02, 0x03,
+    0x04, 0x05, 0x04, 0x05, 0x06, 0x07, 0x06, 0x07,
+    0x04, 0x05, 0x04, 0x05, 0x06, 0x07, 0x06, 0x07,
+    0x08, 0x09, 0x08, 0x09, 0x0a, 0x0b, 0x0a, 0x0b,
+    0x08, 0x09, 0x08, 0x09, 0x0a, 0x0b, 0x0a, 0x0b,
+    0x0c, 0x0d, 0x0c, 0x0d, 0x0e, 0x0f, 0x0e, 0x0f,
+    0x0c, 0x0d, 0x0c, 0x0d, 0x0e, 0x0f, 0x0e, 0x0f,
+    0x08, 0x09, 0x08, 0x09, 0x0a, 0x0b, 0x0a, 0x0b,
+    0x08, 0x09, 0x08, 0x09, 0x0a, 0x0b, 0x0a, 0x0b,
+    0x0c, 0x0d, 0x0c, 0x0d, 0x0e, 0x0f, 0x0e, 0x0f,
+    0x0c, 0x0d, 0x0c, 0x0d, 0x0e, 0x0f, 0x0e, 0x0f
+};
+
+
+/* Methods -------------------------------------------------------------------*/
+
+/**
+* @brief  Converting audio data from PDM to PCM.
+* @param  output_buffer     Pointer to output PCM buffer data.
+* @param  input_buffer      Pointer to input PDM buffer data.
+* @param  volume            Volume level (it must be in the range [0..64]).
+* @param  decimation_factor Decimation factor (it must be either 64 or 128).
+* @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
+*/
+status_t PDM2PCMAudio::convert(int16_t *output_buffer, uint16_t *input_buffer, uint32_t volume, uint32_t decimation_factor)
+{
+    if (!(volume <= PDM2PCM_MAX_VOLUME))
+        error("Volume level not supported: it must be in the range [0..64].\r\n");
+
+    switch (decimation_factor)
+    {
+        case 64:
+            for (uint32_t index = 0; index < 1/*_channels*/; index++) {
+#ifdef PDM2PCM_AUDIO_DEBUG
+                _pdm2pcm_audio_signal = 1;
+#endif
+#ifdef USE_OPEN_PDM2PCM_LIBRARY
+                Open_PDM_Filter(&((uint8_t *) input_buffer)[index], (uint16_t *) &(output_buffer[index]), volume, (TPDMFilter_InitStruct *) &_PDM2PCM_filter[index]);
+#else                
+                PDM_Filter_64_LSB(&((uint8_t *) input_buffer)[index], (uint16_t *) &(output_buffer[index]), volume, (PDMFilter_InitStruct *) &_PDM2PCM_filter[index]);
+#endif
+#ifdef PDM2PCM_AUDIO_DEBUG
+                _pdm2pcm_audio_signal = 0;
+#endif
+            }
+            break;
+
+        case 128:
+            for (uint32_t index = 0; index < _channels; index++) {
+#ifdef USE_OPEN_PDM2PCM_LIBRARY
+                Open_PDM_Filter(&((uint8_t *) input_buffer)[index], (uint16_t *) &(output_buffer[index]), volume, (TPDMFilter_InitStruct *) &_PDM2PCM_filter[index]);
+#else                
+                PDM_Filter_128_LSB(&((uint8_t *) input_buffer)[index], (uint16_t *) &(output_buffer[index]), volume, (PDMFilter_InitStruct *) &_PDM2PCM_filter[index]);
+#endif
+            }
+            break;
+
+        default:
+            error("Decimation factor not supported: it must be either 64 or 128.\r\n");
+    }
+
+    return COMPONENT_OK;
+}
+
+/**
+ * @brief  Scrambling audio data.
+ * @param  output_buffer Pointer to output PDM buffer data.
+ * @param  input_buffer  Pointer to input PDM buffer data.
+ * @param  size          Size of the buffers (thay has to be equally sized).
+ * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
+ */
+status_t PDM2PCMAudio::scramble(uint16_t *output_buffer, uint16_t *input_buffer, uint32_t size)
+{
+    for (uint32_t index = 0; index < size; index++) {
+        output_buffer[index] = HTONS(input_buffer[index]);
+    }
+
+    return COMPONENT_OK;
+}
+
+/**s
+ * @brief  Demuxing audio data.
+ * @param  output_buffer Pointer to output PDM buffer data.
+ * @param  input_buffer  Pointer to input PDM buffer data.
+ * @param  size          Size of the buffers (thay has to be equally sized).
+ * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
+ */
+status_t PDM2PCMAudio::demux(uint16_t *output_buffer, uint16_t *input_buffer, uint32_t size)
+{
+    for (uint32_t index = 0; index < size; index++) {
+        uint8_t a = ((uint8_t *) input_buffer)[index * 2];
+        uint8_t b = ((uint8_t *) input_buffer)[index * 2 + 1];
+        ((uint8_t *) (output_buffer))[index * 2] = _demux_filter[a & DEMUX_FILTER_MASK] | _demux_filter[b & DEMUX_FILTER_MASK] << 4;
+        ((uint8_t *) (output_buffer))[index * 2 + 1] = _demux_filter[(a >> 1) & DEMUX_FILTER_MASK] | _demux_filter[(b >> 1) & DEMUX_FILTER_MASK] << 4;
+    }
+
+    return COMPONENT_OK;
+}
+
+/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/