Audio Demo with DISCO Board, takes control samples, waits for user input, samples regularly.
Dependencies: CMSIS_DSP_401 STM32L4xx_HAL_Driver mbed-src_DISO_AUDIO_DEMO
audio_if.cpp@0:3eee9435dd17, 2015-12-13 (annotated)
- Committer:
- EricLew
- Date:
- Sun Dec 13 19:12:11 2015 +0000
- Revision:
- 0:3eee9435dd17
Audio Demo using DISCO Board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
EricLew | 0:3eee9435dd17 | 1 | /** |
EricLew | 0:3eee9435dd17 | 2 | ****************************************************************************** |
EricLew | 0:3eee9435dd17 | 3 | * @file /Src/audio_if.c |
EricLew | 0:3eee9435dd17 | 4 | * @author Central Labs |
EricLew | 0:3eee9435dd17 | 5 | * @version V1.0.0 |
EricLew | 0:3eee9435dd17 | 6 | * @date 7-May-2015 |
EricLew | 0:3eee9435dd17 | 7 | * @brief USB Device Audio interface file. |
EricLew | 0:3eee9435dd17 | 8 | ****************************************************************************** |
EricLew | 0:3eee9435dd17 | 9 | @attention |
EricLew | 0:3eee9435dd17 | 10 | * |
EricLew | 0:3eee9435dd17 | 11 | * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> |
EricLew | 0:3eee9435dd17 | 12 | * |
EricLew | 0:3eee9435dd17 | 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); |
EricLew | 0:3eee9435dd17 | 14 | * You may not use this file except in compliance with the License. |
EricLew | 0:3eee9435dd17 | 15 | * You may obtain a copy of the License at: |
EricLew | 0:3eee9435dd17 | 16 | * |
EricLew | 0:3eee9435dd17 | 17 | * http://www.st.com/software_license_agreement_liberty_v2 |
EricLew | 0:3eee9435dd17 | 18 | * |
EricLew | 0:3eee9435dd17 | 19 | * Redistribution and use in source and binary forms, with or without modification, |
EricLew | 0:3eee9435dd17 | 20 | * are permitted provided that the following conditions are met: |
EricLew | 0:3eee9435dd17 | 21 | * 1. Redistributions of source code must retain the above copyright notice, |
EricLew | 0:3eee9435dd17 | 22 | * this list of conditions and the following disclaimer. |
EricLew | 0:3eee9435dd17 | 23 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
EricLew | 0:3eee9435dd17 | 24 | * this list of conditions and the following disclaimer in the documentation |
EricLew | 0:3eee9435dd17 | 25 | * and/or other materials provided with the distribution. |
EricLew | 0:3eee9435dd17 | 26 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
EricLew | 0:3eee9435dd17 | 27 | * may be used to endorse or promote products derived from this software |
EricLew | 0:3eee9435dd17 | 28 | * without specific prior written permission. |
EricLew | 0:3eee9435dd17 | 29 | * |
EricLew | 0:3eee9435dd17 | 30 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
EricLew | 0:3eee9435dd17 | 31 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
EricLew | 0:3eee9435dd17 | 32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
EricLew | 0:3eee9435dd17 | 33 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
EricLew | 0:3eee9435dd17 | 34 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
EricLew | 0:3eee9435dd17 | 35 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
EricLew | 0:3eee9435dd17 | 36 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
EricLew | 0:3eee9435dd17 | 37 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
EricLew | 0:3eee9435dd17 | 38 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
EricLew | 0:3eee9435dd17 | 39 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
EricLew | 0:3eee9435dd17 | 40 | * |
EricLew | 0:3eee9435dd17 | 41 | ****************************************************************************** |
EricLew | 0:3eee9435dd17 | 42 | */ |
EricLew | 0:3eee9435dd17 | 43 | |
EricLew | 0:3eee9435dd17 | 44 | /* Includes ------------------------------------------------------------------*/ |
EricLew | 0:3eee9435dd17 | 45 | #include "audio_if.h" |
EricLew | 0:3eee9435dd17 | 46 | |
EricLew | 0:3eee9435dd17 | 47 | //uint16_t PDM_Buffer[AUDIO_CHANNELS*AUDIO_SAMPLING_FREQUENCY/1000*64/8]; |
EricLew | 0:3eee9435dd17 | 48 | //static uint16_t PCM_Buffer[AUDIO_CHANNELS*AUDIO_SAMPLING_FREQUENCY/1000]; |
EricLew | 0:3eee9435dd17 | 49 | |
EricLew | 0:3eee9435dd17 | 50 | /* Private typedef -----------------------------------------------------------*/ |
EricLew | 0:3eee9435dd17 | 51 | /* Private define ------------------------------------------------------------*/ |
EricLew | 0:3eee9435dd17 | 52 | /* Private macro -------------------------------------------------------------*/ |
EricLew | 0:3eee9435dd17 | 53 | /* Private function prototypes -----------------------------------------------*/ |
EricLew | 0:3eee9435dd17 | 54 | /* Private variables ---------------------------------------------------------*/ |
EricLew | 0:3eee9435dd17 | 55 | |
EricLew | 0:3eee9435dd17 | 56 | |
EricLew | 0:3eee9435dd17 | 57 | /* Private functions ---------------------------------------------------------*/ |
EricLew | 0:3eee9435dd17 | 58 | /* This table maps the audio device class setting in 1/256 dB to a |
EricLew | 0:3eee9435dd17 | 59 | * linear 0-64 scaling used in pdm_filter.c. It is computed as |
EricLew | 0:3eee9435dd17 | 60 | * 256*20*log10(index/64). */ |
EricLew | 0:3eee9435dd17 | 61 | //const int16_t vol_table[65] = |
EricLew | 0:3eee9435dd17 | 62 | //{ 0x8000, 0xDBE0, 0xE1E6, 0xE56B, 0xE7EB, 0xE9DB, 0xEB70, 0xECC7, |
EricLew | 0:3eee9435dd17 | 63 | //0xEDF0, 0xEEF6, 0xEFE0, 0xF0B4, 0xF176, 0xF228, 0xF2CD, 0xF366, |
EricLew | 0:3eee9435dd17 | 64 | //0xF3F5, 0xF47C, 0xF4FB, 0xF574, 0xF5E6, 0xF652, 0xF6BA, 0xF71C, |
EricLew | 0:3eee9435dd17 | 65 | //0xF778, 0xF7D6, 0xF82D, 0xF881, 0xF8D2, 0xF920, 0xF96B, 0xF9B4, |
EricLew | 0:3eee9435dd17 | 66 | //0xF9FB, 0xFA3F, 0xFA82, 0xFAC2, 0xFB01, 0xFB3E, 0xFB79, 0xFBB3, |
EricLew | 0:3eee9435dd17 | 67 | //0xFBEB, 0xFC22, 0xFC57, 0xFC8C, 0xFCBF, 0xFCF1, 0xFD22, 0xFD51, |
EricLew | 0:3eee9435dd17 | 68 | //0xFD80, 0xFDAE, 0xFDDB, 0xFE07, 0xFE32, 0xFE5D, 0xFE86, 0xFEAF, |
EricLew | 0:3eee9435dd17 | 69 | //0xFED7, 0xFF00, 0xFF25, 0xFF4B, 0xFF70, 0xFF95, 0xFFB9, 0xFFD0, |
EricLew | 0:3eee9435dd17 | 70 | //0x0000 }; |
EricLew | 0:3eee9435dd17 | 71 | |
EricLew | 0:3eee9435dd17 | 72 | //volatile uint8_t VolumeSetting=64; |
EricLew | 0:3eee9435dd17 | 73 | |
EricLew | 0:3eee9435dd17 | 74 | /** |
EricLew | 0:3eee9435dd17 | 75 | * @brief Initializes the AUDIO media low layer. |
EricLew | 0:3eee9435dd17 | 76 | * @param AudioFreq: Audio frequency used to play the audio stream. |
EricLew | 0:3eee9435dd17 | 77 | * @param BitRes: desired bit resolution |
EricLew | 0:3eee9435dd17 | 78 | * @param ChnlNbr: number of channel to be configured |
EricLew | 0:3eee9435dd17 | 79 | * @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise |
EricLew | 0:3eee9435dd17 | 80 | */ |
EricLew | 0:3eee9435dd17 | 81 | int8_t Audio_Init(void) |
EricLew | 0:3eee9435dd17 | 82 | { |
EricLew | 0:3eee9435dd17 | 83 | return BSP_AUDIO_IN_Init(48000, 16, 1); |
EricLew | 0:3eee9435dd17 | 84 | } |
EricLew | 0:3eee9435dd17 | 85 | |
EricLew | 0:3eee9435dd17 | 86 | |
EricLew | 0:3eee9435dd17 | 87 | |
EricLew | 0:3eee9435dd17 | 88 | /** |
EricLew | 0:3eee9435dd17 | 89 | * @brief Start audio recording engine |
EricLew | 0:3eee9435dd17 | 90 | * @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise |
EricLew | 0:3eee9435dd17 | 91 | */ |
EricLew | 0:3eee9435dd17 | 92 | int8_t Audio_Record(uint16_t * recbuff) |
EricLew | 0:3eee9435dd17 | 93 | { |
EricLew | 0:3eee9435dd17 | 94 | int8_t res = BSP_AUDIO_IN_Record(recbuff, 2048); |
EricLew | 0:3eee9435dd17 | 95 | // AudioProcess(pbuf, recbuff); |
EricLew | 0:3eee9435dd17 | 96 | return res; |
EricLew | 0:3eee9435dd17 | 97 | } |
EricLew | 0:3eee9435dd17 | 98 | |
EricLew | 0:3eee9435dd17 | 99 | /** |
EricLew | 0:3eee9435dd17 | 100 | * @brief Controls AUDIO Volume. |
EricLew | 0:3eee9435dd17 | 101 | * @param vol: Volume level |
EricLew | 0:3eee9435dd17 | 102 | * @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise |
EricLew | 0:3eee9435dd17 | 103 | */ |
EricLew | 0:3eee9435dd17 | 104 | //int8_t Audio_VolumeCtl(int16_t Volume) |
EricLew | 0:3eee9435dd17 | 105 | //{ |
EricLew | 0:3eee9435dd17 | 106 | // /* Call low layer volume setting function */ |
EricLew | 0:3eee9435dd17 | 107 | // int j; |
EricLew | 0:3eee9435dd17 | 108 | // |
EricLew | 0:3eee9435dd17 | 109 | // j = 0; |
EricLew | 0:3eee9435dd17 | 110 | // /* Find the setting nearest to the desired setting */ |
EricLew | 0:3eee9435dd17 | 111 | // while(j<64 && |
EricLew | 0:3eee9435dd17 | 112 | // abs(Volume-vol_table[j]) > abs(Volume-vol_table[j+1])) { |
EricLew | 0:3eee9435dd17 | 113 | // j++; |
EricLew | 0:3eee9435dd17 | 114 | // } |
EricLew | 0:3eee9435dd17 | 115 | // /* Now do the volume adjustment */ |
EricLew | 0:3eee9435dd17 | 116 | // VolumeSetting = (uint8_t)j; |
EricLew | 0:3eee9435dd17 | 117 | // return BSP_AUDIO_IN_SetVolume(VolumeSetting); |
EricLew | 0:3eee9435dd17 | 118 | // |
EricLew | 0:3eee9435dd17 | 119 | // |
EricLew | 0:3eee9435dd17 | 120 | //} |
EricLew | 0:3eee9435dd17 | 121 | |
EricLew | 0:3eee9435dd17 | 122 | /** |
EricLew | 0:3eee9435dd17 | 123 | * @brief Controls AUDIO Mute. |
EricLew | 0:3eee9435dd17 | 124 | * @param cmd: Command opcode |
EricLew | 0:3eee9435dd17 | 125 | * @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise |
EricLew | 0:3eee9435dd17 | 126 | */ |
EricLew | 0:3eee9435dd17 | 127 | //int8_t Audio_MuteCtl(uint8_t cmd) |
EricLew | 0:3eee9435dd17 | 128 | //{ |
EricLew | 0:3eee9435dd17 | 129 | // return AUDIO_OK; |
EricLew | 0:3eee9435dd17 | 130 | //} |
EricLew | 0:3eee9435dd17 | 131 | |
EricLew | 0:3eee9435dd17 | 132 | |
EricLew | 0:3eee9435dd17 | 133 | /** |
EricLew | 0:3eee9435dd17 | 134 | * @brief Stops audio acquisition |
EricLew | 0:3eee9435dd17 | 135 | * @param none |
EricLew | 0:3eee9435dd17 | 136 | * @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise |
EricLew | 0:3eee9435dd17 | 137 | */ |
EricLew | 0:3eee9435dd17 | 138 | int8_t Audio_Stop(void) |
EricLew | 0:3eee9435dd17 | 139 | { |
EricLew | 0:3eee9435dd17 | 140 | return BSP_AUDIO_IN_Stop(); |
EricLew | 0:3eee9435dd17 | 141 | |
EricLew | 0:3eee9435dd17 | 142 | } |
EricLew | 0:3eee9435dd17 | 143 | |
EricLew | 0:3eee9435dd17 | 144 | /** |
EricLew | 0:3eee9435dd17 | 145 | * @brief Pauses audio acquisition |
EricLew | 0:3eee9435dd17 | 146 | * @param none |
EricLew | 0:3eee9435dd17 | 147 | * @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise |
EricLew | 0:3eee9435dd17 | 148 | */ |
EricLew | 0:3eee9435dd17 | 149 | // |
EricLew | 0:3eee9435dd17 | 150 | //int8_t Audio_Pause(void) |
EricLew | 0:3eee9435dd17 | 151 | //{ |
EricLew | 0:3eee9435dd17 | 152 | // return 0; |
EricLew | 0:3eee9435dd17 | 153 | //} |
EricLew | 0:3eee9435dd17 | 154 | |
EricLew | 0:3eee9435dd17 | 155 | |
EricLew | 0:3eee9435dd17 | 156 | /** |
EricLew | 0:3eee9435dd17 | 157 | * @brief Resumes audio acquisition |
EricLew | 0:3eee9435dd17 | 158 | * @param none |
EricLew | 0:3eee9435dd17 | 159 | * @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise |
EricLew | 0:3eee9435dd17 | 160 | */ |
EricLew | 0:3eee9435dd17 | 161 | //int8_t Audio_Resume(void) |
EricLew | 0:3eee9435dd17 | 162 | //{ |
EricLew | 0:3eee9435dd17 | 163 | // return AUDIO_OK; |
EricLew | 0:3eee9435dd17 | 164 | //} |
EricLew | 0:3eee9435dd17 | 165 | |
EricLew | 0:3eee9435dd17 | 166 | /** |
EricLew | 0:3eee9435dd17 | 167 | * @brief Manages command from usb |
EricLew | 0:3eee9435dd17 | 168 | * @param None |
EricLew | 0:3eee9435dd17 | 169 | * @retval AUDIO_OK in case of success, AUDIO_ERROR otherwise |
EricLew | 0:3eee9435dd17 | 170 | */ |
EricLew | 0:3eee9435dd17 | 171 | // |
EricLew | 0:3eee9435dd17 | 172 | //int8_t Audio_CommandMgr(uint8_t cmd) |
EricLew | 0:3eee9435dd17 | 173 | //{ |
EricLew | 0:3eee9435dd17 | 174 | // return AUDIO_OK; |
EricLew | 0:3eee9435dd17 | 175 | //} |
EricLew | 0:3eee9435dd17 | 176 | // |
EricLew | 0:3eee9435dd17 | 177 | //void AudioProcess(uint16_t * pbuf, uint16_t * recbuff) |
EricLew | 0:3eee9435dd17 | 178 | //{ |
EricLew | 0:3eee9435dd17 | 179 | // BSP_AUDIO_IN_PDMToPCM(pbuf,recbuff); |
EricLew | 0:3eee9435dd17 | 180 | // // need to copy recbuff to final output buff (in chunks) |
EricLew | 0:3eee9435dd17 | 181 | //} |
EricLew | 0:3eee9435dd17 | 182 | |
EricLew | 0:3eee9435dd17 | 183 | |
EricLew | 0:3eee9435dd17 | 184 | |
EricLew | 0:3eee9435dd17 | 185 | |
EricLew | 0:3eee9435dd17 | 186 | |
EricLew | 0:3eee9435dd17 | 187 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
EricLew | 0:3eee9435dd17 | 188 |