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: BSP_DISCO_F413ZH
main.cpp
00001 #include "mbed.h" 00002 #include "stm32f413h_discovery_audio.h" 00003 00004 static void CopyBuffer(int16_t *pbuffer1, int16_t *pbuffer2, uint16_t BufferSize); 00005 00006 #define SCRATCH_BUFF_SIZE 1024 00007 #define RECORD_BUFFER_SIZE 4096 00008 00009 typedef enum { 00010 BUFFER_OFFSET_NONE = 0, 00011 BUFFER_OFFSET_HALF = 1, 00012 BUFFER_OFFSET_FULL = 2, 00013 } BUFFER_StateTypeDef; 00014 00015 volatile uint32_t audio_rec_buffer_state = BUFFER_OFFSET_NONE; 00016 int32_t Scratch[SCRATCH_BUFF_SIZE]; 00017 00018 /* Buffer containing the PCM samples coming from the microphone */ 00019 int16_t RecordBuffer[RECORD_BUFFER_SIZE]; 00020 00021 /* Buffer used to stream the recorded PCM samples towards the audio codec. */ 00022 int16_t PlaybackBuffer[RECORD_BUFFER_SIZE]; 00023 00024 int main() 00025 { 00026 printf("\n\nAUDIO LOOPBACK EXAMPLE FOR DISCO-F413H START:\n"); 00027 00028 /* Initialize Audio Recorder with 2 microphone of DISCO-F413H to be used */ 00029 if (BSP_AUDIO_IN_Init(DEFAULT_AUDIO_IN_FREQ, DEFAULT_AUDIO_IN_BIT_RESOLUTION, DEFAULT_AUDIO_IN_CHANNEL_NBR) == AUDIO_ERROR) { 00030 printf("BSP_AUDIO_IN_Init error\n"); 00031 } 00032 printf("Audio init done..\n"); 00033 00034 /* Allocate scratch buffers */ 00035 if (BSP_AUDIO_IN_AllocScratch (Scratch, SCRATCH_BUFF_SIZE) == AUDIO_ERROR) { 00036 printf("BSP_AUDIO_IN_AllocScratch error\n"); 00037 } 00038 printf("Audio buffer init done..\n"); 00039 00040 /* Start Recording */ 00041 if (BSP_AUDIO_IN_Record((uint16_t*)&RecordBuffer[0], RECORD_BUFFER_SIZE) == AUDIO_ERROR) { 00042 printf("BSP_AUDIO_IN_Record error\n"); 00043 } 00044 printf("Audio in(Microphone U16, U17) recording start..\n"); 00045 00046 /* Initialize the audio device*/ 00047 if (BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_HEADPHONE, 50, DEFAULT_AUDIO_IN_FREQ) == AUDIO_ERROR) { 00048 printf("BSP_AUDIO_OUT_Init error\n"); 00049 } 00050 printf("Audio out init done..\n"); 00051 00052 /* Play the recorded buffer */ 00053 if (BSP_AUDIO_OUT_Play((uint16_t *) &PlaybackBuffer[0], 2*RECORD_BUFFER_SIZE) == AUDIO_ERROR) { 00054 printf("BSP_AUDIO_OUT_Play error\n"); 00055 } 00056 printf("Start loopback (Mic -> Audio Jack)!!\n"); 00057 00058 audio_rec_buffer_state = BUFFER_OFFSET_NONE; 00059 00060 while (1) { 00061 /* 1st or 2nd half of the record buffer ready for being copied to the playback buffer */ 00062 if(audio_rec_buffer_state != BUFFER_OFFSET_NONE) { 00063 /* Copy half of the record buffer to the playback buffer */ 00064 if(audio_rec_buffer_state == BUFFER_OFFSET_HALF) { 00065 CopyBuffer(&PlaybackBuffer[0], &RecordBuffer[0], RECORD_BUFFER_SIZE/2); 00066 00067 } else { /* if(audio_rec_buffer_state == BUFFER_OFFSET_FULL)*/ 00068 CopyBuffer(&PlaybackBuffer[RECORD_BUFFER_SIZE/2], &RecordBuffer[RECORD_BUFFER_SIZE/2], RECORD_BUFFER_SIZE/2); 00069 } 00070 00071 /* Wait for next data */ 00072 audio_rec_buffer_state = BUFFER_OFFSET_NONE; 00073 } 00074 } 00075 } 00076 00077 /*------------------------------------------------------------------------------------- 00078 Callbacks implementation: 00079 the callbacks API are defined __weak in the stm32f413h_discovery_audio.c file 00080 and their implementation should be done in the user code if they are needed. 00081 Below some examples of callback implementations. 00082 -------------------------------------------------------------------------------------*/ 00083 /** 00084 * @brief Manages the DMA Transfer complete interrupt. 00085 * @param None 00086 * @retval None 00087 */ 00088 void BSP_AUDIO_IN_TransferComplete_CallBack(void) 00089 { 00090 audio_rec_buffer_state = BUFFER_OFFSET_FULL; 00091 } 00092 00093 /** 00094 * @brief Manages the DMA Half Transfer complete interrupt. 00095 * @param None 00096 * @retval None 00097 */ 00098 void BSP_AUDIO_IN_HalfTransfer_CallBack(void) 00099 { 00100 audio_rec_buffer_state = BUFFER_OFFSET_HALF; 00101 } 00102 00103 /** 00104 * @brief Audio IN Error callback function. 00105 * @param None 00106 * @retval None 00107 */ 00108 void BSP_AUDIO_IN_Error_CallBack(void) 00109 { 00110 printf("BSP_AUDIO_IN_Error_CallBack\n"); 00111 } 00112 00113 00114 /** 00115 * @brief Copy content of pbuffer2 to pbuffer1 00116 * @param1 BufferOut 00117 * @param2 BufferIn 00118 * @param3 Size 00119 * @retval None 00120 */ 00121 static void CopyBuffer(int16_t *pbuffer1, int16_t *pbuffer2, uint16_t BufferSize) 00122 { 00123 uint32_t i = 0; 00124 for(i = 0; i < BufferSize; i++) { 00125 pbuffer1[i] = pbuffer2[i]; 00126 } 00127 }
Generated on Wed Jul 13 2022 21:49:50 by
1.7.2