ST / X_NUCLEO_CCA02M1

Dependencies:   ST_I2S ST_FREQUENCY_DIVIDER USBDEVICE

Dependents:   HelloWorld_CCA02M1 HelloWorld_CCA02M1_mbedOS HelloWorld_CCA02M1 Karaoke_CCA01M1_CCA02M1_mbedOS

Fork of X_NUCLEO_CCA02M1 by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PDM2PCMAudio.h Source File

PDM2PCMAudio.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    PDM2PCMAudio.h
00004  * @author  AST / Software Platforms and Cloud
00005  * @version V1.0
00006  * @date    November 10th, 2016
00007  * @brief   Class header file for the PDM2PCMAudio conversion library.
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 
00038 
00039 /* Define to prevent recursive inclusion -------------------------------------*/
00040 
00041 #ifndef __PDM2PCM_AUDIO_CLASS_H
00042 #define __PDM2PCM_AUDIO_CLASS_H
00043 
00044 
00045 /* Configuration -------------------------------------------------------------*/
00046 
00047 /**
00048  * Enable to use the OpenPDM2PCM library.
00049  */
00050 #define USE_OPEN_PDM2PCM_LIBRARY
00051 
00052 /**
00053  * Enable to debug the duration of the PDM2PCM library functions through an
00054  * oscilloscope.
00055  */
00056 //#define PDM2PCM_AUDIO_DEBUG
00057 
00058 
00059 /* Includes ------------------------------------------------------------------*/
00060 
00061 #include "mbed.h"
00062 #include "component_def.h"
00063 #ifdef USE_OPEN_PDM2PCM_LIBRARY
00064 #include "OpenPDMFilter.h"
00065 #else
00066 #include "pdm_filter.h"
00067 #endif
00068 
00069 
00070 /* Definitions ---------------------------------------------------------------*/
00071 
00072 #define DEMUX_FILTER_SIZE      128
00073 #define DEMUX_FILTER_MASK     0x55
00074 #define PDM2PCM_FILTER_SIZE      4
00075 #define PDM2PCM_NOGAIN_VOLUME    4
00076 #define PDM2PCM_MAX_VOLUME      64
00077 
00078 
00079 /* Classes -------------------------------------------------------------------*/
00080 
00081 /** PDM2PCMAudio Conversion Library Class.
00082  */
00083 class PDM2PCMAudio
00084 {
00085 public:
00086 
00087     /**
00088      * @brief Constructor.
00089      * @param  frequency Audio sampling frequency.
00090      * @param  channels  Number of audio channels to be recorded.
00091      */
00092     PDM2PCMAudio(uint32_t frequency, uint32_t channels) :
00093         _frequency(frequency),
00094         _channels(channels)
00095 #ifdef PDM2PCM_AUDIO_DEBUG
00096         , _pdm2pcm_audio_signal(D9)
00097 #endif
00098     {
00099         /* Enable CRC peripheral to unlock the PDM2PCMAudio library. */
00100         __CRC_CLK_ENABLE();
00101 
00102         /* Setting configuration. */
00103         switch (_frequency)
00104         {
00105             case I2S_AUDIOFREQ_8K:
00106                 _decimation_factor = 128;
00107                 break;
00108     
00109             case I2S_AUDIOFREQ_16K:
00110             case I2S_AUDIOFREQ_32K:
00111             case I2S_AUDIOFREQ_44K:
00112             case I2S_AUDIOFREQ_48K:
00113             default:
00114                 _decimation_factor = 64;
00115                 break;
00116         }
00117 
00118         /* Initializing PDM2PCMAudio Filter. */
00119         for (uint32_t i = 0; i < _channels; i++)
00120         {
00121             /* Filter LP and HP Init */
00122             _PDM2PCM_filter[i].LP_HZ = _frequency / 2;
00123             _PDM2PCM_filter[i].HP_HZ = 10;
00124             _PDM2PCM_filter[i].Fs = _frequency;
00125             _PDM2PCM_filter[i].Out_MicChannels = _channels;
00126             _PDM2PCM_filter[i].In_MicChannels = _channels;
00127             _PDM2PCM_filter[i].Decimation = _decimation_factor;
00128             _PDM2PCM_filter[i].MaxVolume = PDM2PCM_MAX_VOLUME;
00129 #ifdef USE_OPEN_PDM2PCM_LIBRARY
00130             Open_PDM_Filter_Init((TPDMFilter_InitStruct *) &_PDM2PCM_filter[i]);
00131 #else
00132             PDM_Filter_Init((PDMFilter_InitStruct *) &_PDM2PCM_filter[i]);
00133 #endif
00134         }
00135     };
00136 
00137     /**
00138      * @brief  Getting number of PCM samples from nummber of PDM samples.
00139      * @param  PCM_samples Number of PCM samples.
00140      * @retval Number of equivalent PDM samples.
00141      */
00142     uint32_t pcm2pdm_samples(uint32_t PCM_samples);
00143 
00144     /**
00145      * @brief  Converting audio data from PDM to PCM.
00146      * @param  output_buffer     Pointer to output PCM buffer data.
00147      * @param  input_buffer      Pointer to input PDM buffer data.
00148      * @param  volume            Volume level (it must be in the range [0..PDM2PCM_MAX_VOLUME]).
00149      * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
00150      */
00151     status_t convert(int16_t *output_buffer, uint16_t *input_buffer, uint32_t volume);
00152 
00153     /**
00154      * @brief  Scrambling audio data.
00155      * @param  output_buffer Pointer to output PDM buffer data.
00156      * @param  input_buffer  Pointer to input PDM buffer data.
00157      * @param  size          Size of the buffers (thay has to be equally sized).
00158      * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
00159      */
00160     status_t scramble(uint16_t *output_buffer, uint16_t *input_buffer, uint32_t size);
00161 
00162     /**
00163      * @brief  Demuxing audio data.
00164      * @param  output_buffer Pointer to output PDM buffer data.
00165      * @param  input_buffer  Pointer to input PDM buffer data.
00166      * @param  size          Size of the buffers (thay has to be equally sized).
00167      * @retval COMPONENT_OK in case of success, COMPONENT_ERROR otherwise.
00168      */
00169     status_t demux(uint16_t *output_buffer, uint16_t *input_buffer, uint32_t size);
00170 
00171 
00172 protected:
00173 
00174     uint32_t _frequency;
00175     uint32_t _channels;
00176     uint32_t _decimation_factor;
00177 #ifdef USE_OPEN_PDM2PCM_LIBRARY
00178     TPDMFilter_InitStruct _PDM2PCM_filter[PDM2PCM_FILTER_SIZE];
00179 #else
00180     PDMFilter_InitStruct _PDM2PCM_filter[PDM2PCM_FILTER_SIZE];
00181 #endif
00182     static const uint8_t _demux_filter[DEMUX_FILTER_SIZE];
00183 
00184 #ifdef PDM2PCM_AUDIO_DEBUG
00185     DigitalOut _pdm2pcm_audio_signal;
00186 #endif
00187 };
00188 
00189 #endif /* __PDM2PCM_AUDIO_CLASS_H */
00190 
00191 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/