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.
Dependencies: ST_FREQUENCY_DIVIDER ST_I2S USBDEVICE
Fork of X_NUCLEO_CCA02M1 by
BSP/PDM2PCMAudio_class.cpp
- Committer:
- davide.aliprandi@st.com
- Date:
- 2017-02-28
- Revision:
- 0:d5552d432108
File content as of revision 0:d5552d432108:
/**
******************************************************************************
* @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>© 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_class.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 < _channels; index++)
PDM_Filter_64_LSB(&((uint8_t *) input_buffer)[index], (uint16_t *) &(output_buffer[index]), volume, (PDMFilter_InitStruct *) &_PDM2PCM_filter[index]);
break;
case 128:
for (uint32_t index = 0; index < _channels; index++)
PDM_Filter_128_LSB(&((uint8_t *) input_buffer)[index], (uint16_t *) &(output_buffer[index]), volume, (PDMFilter_InitStruct *) &_PDM2PCM_filter[index]);
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****/
