lab 1 code

Dependencies:   CMSIS-DSP_for_STM32F746G BSP_DISCO_F746NG

Revision:
14:18f159d48340
Parent:
13:61131aac4031
Child:
15:83e40ccc1b1f
--- a/main.cpp	Mon Dec 30 17:27:57 2019 +0000
+++ b/main.cpp	Mon Dec 30 18:34:21 2019 +0000
@@ -26,7 +26,7 @@
 } BUFFER_StateTypeDef;
 
 
-#define HALF_AUDIO_BLOCK_SIZE           ((uint32_t)128)         // Number of samples @ Frequency
+#define HALF_AUDIO_BLOCK_SIZE           ((uint32_t)128)         // Number of samples (L and R) @ Frequency
 #define AUDIO_BLOCK_SIZE                ((uint32_t)256)
 
 #define SDRAM_DEVICE_ADDR_AUDIO_MEM     ((uint32_t)0xC0400000)
@@ -44,7 +44,17 @@
 volatile uint32_t  audio_rec_buffer_state = BUFFER_OFFSET_NONE;
 static void Erase_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t Length);
 static void Draw_Trace(uint16_t Xpos, uint16_t L_Ypos, uint16_t R_Ypos, uint16_t* Mem_start, uint16_t Length);
+static void Audio_to_Float(uint16_t* buffer_in, uint16_t* L_out, uint16_t * R_out, uint16_t Length);
+static void Float_to_Audio(uint16_t* L_in, uint16_t * R_in, uint16_t* buffer_out, uint16_t Length);
 
+/* To do converstion to float */
+float       L_channel_float[HALF_AUDIO_BLOCK_SIZE];
+float       R_channel_float[HALF_AUDIO_BLOCK_SIZE];
+
+/* Back conversion to integer */
+int32_t     Processed_audio[HALF_AUDIO_BLOCK_SIZE];
+
+/* Useful variables during looping */
 uint32_t counter = 0;
 char buf[40];
 int first_half_time = 0;
@@ -101,6 +111,11 @@
         Erase_Trace(OSC_START_X_POS, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, HALF_AUDIO_BLOCK_SIZE);
         Draw_Trace(OSC_START_X_POS, L_CHANNEL_Y_POS, R_CHANNEL_Y_POS, (uint16_t *) AUDIO_BUFFER_IN, HALF_AUDIO_BLOCK_SIZE);
 
+        /* Convert data to floating point representation for processing */
+        Audio_to_Float((uint16_t *) AUDIO_BUFFER_IN, (uint16_t *) L_channel_float, (uint16_t *) R_channel_float, HALF_AUDIO_BLOCK_SIZE);
+        
+        Float_to_Audio((uint16_t *) L_channel_float, (uint16_t *) R_channel_float, (uint16_t *) Processed_audio, HALF_AUDIO_BLOCK_SIZE);
+
         /* Copy recorded 1st half block into the audio buffer that goes out */
         memcpy((uint16_t *)(AUDIO_BUFFER_OUT), (uint16_t *)(AUDIO_BUFFER_IN), AUDIO_BLOCK_SIZE);
 
@@ -221,9 +236,73 @@
    }
    
 }
+
+/**
+  * @brief  Converts audio data in buffer to floating point representation.
+  * @param  buffer_in: Pointer to Audio buffer start location
+  * @param  L_out: Pointer to Left channel out data (float)
+  * @param  R_out: Pointer to Right channel out data (float)
+  * @param  Length: length of data to convert
+  * @retval None
+  */
+void Audio_to_Float(uint16_t* buffer_in, uint16_t* L_out, uint16_t * R_out, uint16_t Length)
+{
+    uint16_t i;
+    uint32_t data_value;
+    uint16_t* audio_mem_address;
+    uint16_t* L_chan_mem_address;
+    uint16_t* R_chan_mem_address;
+    float L_audio_value;
+    float R_audio_value;
     
-/* Read data value from SDRAM memory */
-//    ret = *(__IO uint32_t*) (hLtdcHandler.LayerCfg[ActiveLayer].FBStartAdress + (4*(Ypos*BSP_LCD_GetXSize() + Xpos)));
+    for (i=0; i<Length; i++)
+   {
+        audio_mem_address = (uint16_t*) buffer_in + i;
+        L_chan_mem_address = (uint16_t*) L_out + i;
+        R_chan_mem_address = (uint16_t*) R_out + i;
+        data_value = *((uint16_t*) audio_mem_address);
+        L_audio_value = (float) ((int16_t) ((data_value >> 16) & 0xFFFF));
+        R_audio_value = (float) ((int16_t) (data_value & 0xFFFF));
+                
+        *L_chan_mem_address = L_audio_value;
+        *R_chan_mem_address = R_audio_value;
+   }
+}
+
+/**
+  * @brief  Converts audio data in buffer to floating point representation.
+  * @param  L_out: Pointer to Left channel in data (float)
+  * @param  R_out: Pointer to Right channel in data (float)
+  * @param  buffer_out: Pointer to combined 32 bit (two 16-bit int samples)
+  * @param  Length: length of data to convert
+  * @retval None
+  */
+void Float_to_Audio(uint16_t* L_in, uint16_t * R_in, uint16_t* buffer_out, uint16_t Length)
+{
+    uint16_t i;
+    uint32_t data_value;
+    uint16_t* audio_mem_address;
+    uint16_t* L_chan_mem_address;
+    uint16_t* R_chan_mem_address;
+    float L_audio_value;
+    float R_audio_value;
+    
+    for (i=0; i<Length; i++)
+   {
+        L_chan_mem_address = (uint16_t*) L_in + i;
+        R_chan_mem_address = (uint16_t*) R_in + i;
+        audio_mem_address = (uint16_t*) buffer_out + i;
+        
+        L_audio_value = *((uint16_t*) L_chan_mem_address);
+        R_audio_value = *((uint16_t*) R_chan_mem_address);
+                
+        data_value = (((uint32_t) ((int16_t) L_audio_value)) << 16) | ((uint32_t) ((int16_t) R_audio_value));
+        *audio_mem_address = data_value;
+   }
+}
+
+
+
 
 
 /*-------------------------------------------------------------------------------------