Query of Extraction and process of buffer_data from 4 channel PCM data buffer in STM32 Code

105 Views Asked by At

I am trying to understand one 4 channel mic-array code provided by ST(AMicArray_Microphones_Streaming).

In the code, the PCM buffer data is sent on the USB via this function. In this code, I actually want to do some processing on the received data and then I want to send it on USB. My question is how to extract the raw data and process them?

This topic seems very broad to a beginner person if someone can help me with some start material or guidelines would be appreciated.

//PCMSamples = AUDIO_IN_SAMPLING_FREQUENCY/1000*AUDIO_IN_CHANNELS;

 uint8_t  USBD_AUDIO_Data_Transfer(USBD_HandleTypeDef *pdev, int16_t * audioData, uint16_t PCMSamples)
{
  
  USBD_AUDIO_HandleTypeDef   *haudio;
  haudio = (USBD_AUDIO_HandleTypeDef *)pdev->pClassData;
  
  if(haudioInstance.state==STATE_USB_WAITING_FOR_INIT){    
    return USBD_BUSY;    
  }  
  uint16_t dataAmount = PCMSamples * 2; /*Bytes*/
  uint16_t true_dim = haudio->buffer_length;
  uint16_t current_data_Amount = haudio->dataAmount;
  uint16_t packet_dim = haudio->paketDimension;
  
  if(haudio->state==STATE_USB_REQUESTS_STARTED  || current_data_Amount!=dataAmount){   
    
    /*USB parameters definition, based on the amount of data passed*/
    haudio->dataAmount=dataAmount;                  
    uint16_t wr_rd_offset = (AUDIO_IN_PACKET_NUM/2) * dataAmount / packet_dim; 
    haudio->wr_ptr=wr_rd_offset * packet_dim;
    haudio->rd_ptr = 0;
    haudio->upper_treshold = wr_rd_offset + 1;
    haudio->lower_treshold = wr_rd_offset - 1;
    haudio->buffer_length = (packet_dim * (dataAmount / packet_dim) * AUDIO_IN_PACKET_NUM);
    
    /*Memory allocation for data buffer, depending (also) on data amount passed to the transfer function*/
    if(haudio->buffer != NULL)
    {
      USBD_free(haudio->buffer);      
    }
    haudio->buffer = USBD_malloc(haudio->buffer_length + haudio->dataAmount);
    if(haudio->buffer == NULL)
    {
      return USBD_FAIL;       
    }
    memset(haudio->buffer,0,(haudio->buffer_length + haudio->dataAmount));
    haudio->state=STATE_USB_BUFFER_WRITE_STARTED;
    
    
  }else if(haudio->state==STATE_USB_BUFFER_WRITE_STARTED){
    if(haudio->timeout++==TIMEOUT_VALUE){
      haudio->state=STATE_USB_IDLE;
      ((USBD_AUDIO_ItfTypeDef *)pdev->pUserData)->Stop();   
     haudio->timeout=0;
    }
    memcpy((uint8_t * )&haudio->buffer[haudio->wr_ptr], (uint8_t *)(audioData), dataAmount);    
    haudio->wr_ptr += dataAmount;
    haudio->wr_ptr = haudio->wr_ptr % (true_dim);    
    if((haudio->wr_ptr-dataAmount) == 0){
      memcpy((uint8_t *)(((uint8_t *)haudio->buffer)+true_dim),(uint8_t *)haudio->buffer, dataAmount);
    }
  }
  return USBD_OK;  
}
1

There are 1 best solutions below

0
Thilak On

I am guessing you are using the X-Cube Audio library from ST, if so the

AudioProcess()

function in the Core/Src/audio_application.c file matches your needs.

void AudioProcess(void)
{
  if (CCA02M2_AUDIO_IN_PDMToPCM(CCA02M2_AUDIO_INSTANCE,(uint16_t * )PDM_Buffer,(uint16_t *)PCM_Buffer) != BSP_ERROR_NONE)
  {
    Error_Handler();
  }
  Send_Audio_to_USB((int16_t *)PCM_Buffer, (AUDIO_IN_SAMPLING_FREQUENCY/1000)*AUDIO_IN_CHANNELS * N_MS);
}

The PDM and PCM data are available here for processing.

ST provides video tutorial on Audio acquisition here. You can refer their channel for more information.