AudioRecord
Dependencies: STM32L4xx_HAL_Driver CMSIS_DSP_401
Diff: audio_record.c
- Revision:
- 1:1ae4b642c533
- Parent:
- 0:d4e5ad7ad71c
- Child:
- 3:ec7e3c37fe80
diff -r d4e5ad7ad71c -r 1ae4b642c533 audio_record.c --- a/audio_record.c Mon Nov 23 19:37:13 2015 +0000 +++ b/audio_record.c Wed Nov 25 17:31:28 2015 +0000 @@ -1,5 +1,3 @@ - - /* Includes ------------------------------------------------------------------*/ #include "main.h" @@ -17,25 +15,19 @@ /* Private define ------------------------------------------------------------*/ /* Size (in bytes) of the buffer containing the recorded PCM samples */ -#define RECORD_BUFFER_SIZE (uint32_t)1024 +#define RECORD_BUFFER_SIZE (uint32_t)1024 //BUFFER SIZE IS THE SAME FOR BOTH RECORDINGS -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ /* Buffer containing the PCM samples coming from the microphone */ -static uint16_t RecordBuffer[RECORD_BUFFER_SIZE]; +static uint32_t RecordBuffer[RECORD_BUFFER_SIZE]; //RECORDING BUFFER FOR SAMPLE #0 +static uint32_t RecordBuffer1[RECORD_BUFFER_SIZE];//RECORDING BUFFER FOR SAMPLE #1 /* Buffer used to stream the recorded PCM samples towards the audio codec. */ -static uint16_t PlaybackBuffer[RECORD_BUFFER_SIZE*2]; +static uint16_t PlaybackBuffer[RECORD_BUFFER_SIZE*2]; //PLAYBACK BUFFER FOR SAMPLE#0 +static uint16_t PlaybackBuffer1[RECORD_BUFFER_SIZE*2];//PLAYBACK BUFFER FOR SAMPLE#1 #define NB_SAMPLES_RATE 7 -static uint32_t SamplesRates[NB_SAMPLES_RATE] = {BSP_AUDIO_FREQUENCY_48K, - BSP_AUDIO_FREQUENCY_44K, - BSP_AUDIO_FREQUENCY_32K, - BSP_AUDIO_FREQUENCY_22K, - BSP_AUDIO_FREQUENCY_16K, - BSP_AUDIO_FREQUENCY_11K, - BSP_AUDIO_FREQUENCY_8K}; +static uint32_t SamplesRates[NB_SAMPLES_RATE] = {BSP_AUDIO_FREQUENCY_48K}; //SAMPLE RATE IS THE SAME /* Information indicating which part of the recorded buffer is ready for audio loopback */ static RecordBufferOffset_Typedef RecordBufferOffset = BUFFER_OFFSET_NONE; @@ -64,9 +56,9 @@ /* Indicates which part of the payback buffer must be updated */ uint8_t transfer_range = 0; - /* Flag indicating whether the audio codec has already been initialized */ + /* Flag indicating whether the audio codec has already been initialized */ uint32_t audio_loop_back_init = RESET ; - + /* Audio record demo is running */ AudioDemo = AUDIO_DEMO_RECORD; @@ -78,7 +70,7 @@ /* Select the audio record sample rate */ SampleRateSelection(); - + //INITIALIZE RECORDING #0 /* Initialize audio input */ if (BSP_AUDIO_IN_Init(SamplesRates[SampleRateIndex], DEFAULT_AUDIO_IN_BIT_RESOLUTION, @@ -99,10 +91,19 @@ { Error_Handler(); } - + uint32_t counter=0; //COUNTER IS REGISTER R4 /* PCM samples recording loop */ while (AudioRecordExit != SET) { + + if (counter==10000000) //Approximately 3 seconds of record time + { + AudioRecordExit=1; + } + else + { + counter=counter++; + } /* 1st or 2nd half of the record buffer ready for being copied to the playback buffer */ if(RecordBufferOffset != BUFFER_OFFSET_NONE) @@ -130,28 +131,118 @@ } + /* Stop audio input */ + if (BSP_AUDIO_IN_Stop() != AUDIO_OK) + { + Error_Handler(); + } + /* De-initialize audio input */ + if (BSP_AUDIO_IN_DeInit() != AUDIO_OK) + { + Error_Handler(); + } + //END OF RECORDING #0 + //INITIALIZE RECORDING #1 + transfer_range = 0; + + /* Flag indicating whether the audio codec has already been initialized */ + audio_loop_back_init = RESET ; + + /* Turn off green LED5 */ + BSP_LED_Off(LED5); - + /* Initialize record buffer offset */ + RecordBufferOffset = BUFFER_OFFSET_NONE; + /* Initialize audio input */ + if (BSP_AUDIO_IN_Init(SamplesRates[SampleRateIndex], + DEFAULT_AUDIO_IN_BIT_RESOLUTION, + DEFAULT_AUDIO_IN_CHANNEL_NBR) != AUDIO_OK) + { + /* Record Error */ + Error_Handler(); + } + + /* Set Callback function pointers */ + BSP_AUDIO_IN_RegisterCallbacks(AudioRecord_Error_CallBack, + AudioRecord_HalfTransfer_CallBack, + AudioRecord_TransferComplete_CallBack); + + /* Start the audio record */ + if (BSP_AUDIO_IN_Record((uint16_t*)&RecordBuffer1[0], + RECORD_BUFFER_SIZE) != AUDIO_OK) + { + Error_Handler(); + } + + AudioRecordExit = 0; //RESET AUDIO RECORD FLAG + counter=0; //RESET COUNTER + /* PCM samples recording loop */ //COPIED FROM ABOVE USE '1' WHEN REFERENCING VARIABLES + while (AudioRecordExit != SET) + { + + if (counter==10000000) //Approximately 3 seconds of record time + { + AudioRecordExit=1; //FLAG IS REGISTER R5 + } + else + { + counter=counter++; + } + /* 1st or 2nd half of the record buffer ready for being copied + to the playback buffer */ + if(RecordBufferOffset != BUFFER_OFFSET_NONE) + { + /* Copy half of the record buffer to the playback buffer */ + if(RecordBufferOffset == BUFFER_OFFSET_HALF) + { + memcpy(&PlaybackBuffer1[(RECORD_BUFFER_SIZE/2) * transfer_range], + RecordBuffer1, + RECORD_BUFFER_SIZE); + + + } + else /* if(RecordBufferOffset == BUFFER_OFFSET_FULL)*/ + { + memcpy(&PlaybackBuffer1[(RECORD_BUFFER_SIZE/2) * transfer_range], + &RecordBuffer1[RECORD_BUFFER_SIZE/2], + RECORD_BUFFER_SIZE); + } + + /* Wait for next data */ + RecordBufferOffset = BUFFER_OFFSET_NONE; + transfer_range = (transfer_range+1) % 4; + } + } + /* Stop audio input */ if (BSP_AUDIO_IN_Stop() != AUDIO_OK) { Error_Handler(); } - + /* De-initialize audio input */ if (BSP_AUDIO_IN_DeInit() != AUDIO_OK) { Error_Handler(); } - + //END OF RECORDING #1 /* Turn OFF LED5: stop record */ BSP_LED_Off(LED5); - /* Reset AudioRecordExit flag */ AudioRecordExit = 0; - + +//COMPARISON ALGORITHM START + +/*arm_cfft_f32 ( const arm_cfft_instance_f32 * S, + float32_t * p1, + uint8_t ifftFlag, + uint8_t bitReverseFlag +) + + */ + /* Turn off green led */ AudioDemo = AUDIO_DEMO_NONE; } @@ -172,29 +263,8 @@ case BSP_AUDIO_FREQUENCY_48K: BSP_LCD_GLASS_DisplayString((uint8_t *)"48000"); break; - case BSP_AUDIO_FREQUENCY_44K: - BSP_LCD_GLASS_DisplayString((uint8_t *)"44000"); - break; - case BSP_AUDIO_FREQUENCY_32K: - BSP_LCD_GLASS_DisplayString((uint8_t *)"32000"); - break; - case BSP_AUDIO_FREQUENCY_22K: - BSP_LCD_GLASS_DisplayString((uint8_t *)"22000"); - break; - case BSP_AUDIO_FREQUENCY_16K: - BSP_LCD_GLASS_DisplayString((uint8_t *)"16000"); - break; - case BSP_AUDIO_FREQUENCY_11K: - BSP_LCD_GLASS_DisplayString((uint8_t *)"11000"); - break; - case BSP_AUDIO_FREQUENCY_8K: - BSP_LCD_GLASS_DisplayString((uint8_t *)"8000"); - break; - default: - break; - } -} - + } + } /** * @brief Audio record sample rate selection * @param None