cancel
Showing results for 
Search instead for 
Did you mean: 

Problems when using STM32F407 DISCOVERY BOARD for audio DSP.

jwang.17
Associate

Hi, there. 

Recently I was using STM32F407 DISCOVERY to develop a USB audio device for audio filtering.

The device is defined as a USB audio sound card and the audio data is be played from the PC and can be heard from the headphone plugged in the board.

I was trying to filter the audio file using the library function when the data is transmitted from USB before it being sent to the codec (CS43L22). 

But the result is not right.

I generated an array from wav file at 1kHz and put the int16 data into the buffer and do the same filtering. Then I sent the filtered data to the codec. But then the result was right.

Can anyone tell me what is going wrong? I cannot fix this problem. It has been annooying me for serval weeks.

P.S.The place I try to do the filtering is in the function static uint8_t USBD_AUDIO_DataOut (USBD_HandleTypeDef *pdev, uint8_t epnum) in usbd_audio.c before the mcu begin to receive the next data from USBD_LL_PrepareReceive(). 

The first code script is the original one. The second one is the nonworking one. And the third one is using my own generated wav code. Sampling frequency is 48kHz.

#define AUDIO_OUT_PACKET                              (uint32_t)(((USBD_AUDIO_FREQ * 2 * 2) /1000)) 
#define AUDIO_DEFAULT_VOLUME                          70
    
/* Number of sub-packets in the audio transfer buffer. You can modify this value but always make sure
  that it is an even number and higher than 3 */
#define AUDIO_OUT_PACKET_NUM                          20 //80
/* Total size of the audio transfer buffer */
#define AUDIO_TOTAL_BUF_SIZE                          ((uint32_t)(AUDIO_OUT_PACKET * AUDIO_OUT_PACKET_NUM))
 
 
void  USBD_AUDIO_Sync (USBD_HandleTypeDef *pdev, AUDIO_OffsetTypeDef offset)
{
  int8_t shift = 0;
  USBD_AUDIO_HandleTypeDef   *haudio;
  haudio = (USBD_AUDIO_HandleTypeDef*) pdev->pClassData;
  
  haudio->offset =  offset; 
  
  
  if(haudio->rd_enable == 1)
  {
    haudio->rd_ptr += AUDIO_TOTAL_BUF_SIZE/2;
    
    if (haudio->rd_ptr == AUDIO_TOTAL_BUF_SIZE)
    {
      /* roll back */
      haudio->rd_ptr = 0;
    }
  }
  
  if(haudio->rd_ptr > haudio->wr_ptr)
  {
    if((haudio->rd_ptr - haudio->wr_ptr) < AUDIO_OUT_PACKET)
    {
      shift = -4;
    }
    else if((haudio->rd_ptr - haudio->wr_ptr) > (AUDIO_TOTAL_BUF_SIZE - AUDIO_OUT_PACKET))
    {
      shift = 4;
    }    
 
  }
  else
  {
    if((haudio->wr_ptr - haudio->rd_ptr) < AUDIO_OUT_PACKET)
    {
      shift = 4;
    }
    else if((haudio->wr_ptr - haudio->rd_ptr) > (AUDIO_TOTAL_BUF_SIZE - AUDIO_OUT_PACKET))
    {
      shift = -4;
    }  
  }
 
	
  if(haudio->offset == AUDIO_OFFSET_FULL)
  {
   	
		
		
    ((USBD_AUDIO_ItfTypeDef *)pdev->pUserData)->AudioCmd(&haudio->buffer[0],
                                                         AUDIO_TOTAL_BUF_SIZE/2 - shift,
                                                         AUDIO_CMD_PLAY); 
      haudio->offset = AUDIO_OFFSET_NONE;           
  }	
}
 
static uint8_t  USBD_AUDIO_DataOut (USBD_HandleTypeDef *pdev, 
                              uint8_t epnum)
{
  USBD_AUDIO_HandleTypeDef   *haudio;
  haudio = (USBD_AUDIO_HandleTypeDef*) pdev->pClassData;
  
  if (epnum == AUDIO_OUT_EP)
  {
    /* Increment the Buffer pointer or roll it back when all buffers are full */
    
    haudio->wr_ptr += AUDIO_OUT_PACKET;
    
    if (haudio->wr_ptr == AUDIO_TOTAL_BUF_SIZE)
    {/* All buffers are full: roll back */
      haudio->wr_ptr = 0;
      
      if(haudio->offset == AUDIO_OFFSET_UNKNOWN)
      {
        ((USBD_AUDIO_ItfTypeDef *)pdev->pUserData)->AudioCmd(&haudio->buffer[0],
                                                             AUDIO_TOTAL_BUF_SIZE/2,
                                                             AUDIO_CMD_START);
          haudio->offset = AUDIO_OFFSET_NONE;
      }
    }
    
    if(haudio->rd_enable == 0)
    {
      if (haudio->wr_ptr == (AUDIO_TOTAL_BUF_SIZE / 2))
      {
        haudio->rd_enable = 1; 
      }
    }
    
16 = (int16_t*)&haudio->buffer[AUDIO_TOTAL_BUF_SIZE - 
    /* Prepare Out endpoint to receive next audio packet */
    USBD_LL_PrepareReceive(pdev,
                           AUDIO_OUT_EP,
                           &haudio->buffer[haudio->wr_ptr], 
                           AUDIO_OUT_PACKET);  
      
  }
const int16_t dacLUT[96] = {  0, 6056, 9208, 8792, 6875, 6605, 9723, 15170, // 0-7
19897, 21226, 18931, 15398, 13750, 15398, 18931, 21226, // 8-15
19897, 15170, 9723, 6605, 6875, 8792, 9208, 6056, // 16-23
  0, -6056, -9208, -8792, -6875, -6605, -9723, -15170, // 24-31
-19897, -21226, -18931, -15398, -13750, -15398, -18931, -21226, // 32-39
-19897, -15170, -9723, -6605, -6875, -8792, -9208, -6056, // 40-47
  0, 6056, 9208, 8792, 6875, 6605, 9723, 15170, // 48-55
19897, 21226, 18931, 15398, 13750, 15398, 18931, 21226, // 56-63
19897, 15170, 9723, 6605, 6875, 8792, 9208, 6056, // 64-71
  0, -6056, -9208, -8792, -6875, -6605, -9723, -15170, // 72-79
-19897, -21226, -18931, -15398, -13750, -15398, -18931, -21226, // 80-87
-19897, -15170, -9723, -6605, -6875, -8792, -9208, -6056, // 88-95
	};   
 
static uint8_t  USBD_AUDIO_DataOut (USBD_HandleTypeDef *pdev, 
                              uint8_t epnum)
{
  USBD_AUDIO_HandleTypeDef   *haudio;
  haudio = (USBD_AUDIO_HandleTypeDef*) pdev->pClassData;
  
  if (epnum == AUDIO_OUT_EP)
  {
    /* Increment the Buffer pointer or roll it back when all buffers are full */
    
    haudio->wr_ptr += AUDIO_OUT_PACKET;
    
    if (haudio->wr_ptr == AUDIO_TOTAL_BUF_SIZE)
    {/* All buffers are full: roll back */
      haudio->wr_ptr = 0;
      
      if(haudio->offset == AUDIO_OFFSET_UNKNOWN)
      {
        ((USBD_AUDIO_ItfTypeDef *)pdev->pUserData)->AudioCmd(&haudio->buffer[0],
                                                             AUDIO_TOTAL_BUF_SIZE/2,
                                                             AUDIO_CMD_START);
          haudio->offset = AUDIO_OFFSET_NONE;
      }
    }
    
    if(haudio->rd_enable == 0)
    {
      if (haudio->wr_ptr == (AUDIO_TOTAL_BUF_SIZE / 2))
      {
        haudio->rd_enable = 1; 
      }
    }
		if (haudio->wr_ptr != 0)
		{
			fir_full_q15((int16_t*)&dacLUT[0],outputData,192/2);
			memcpy((uint8_t*)&haudio->buffer[haudio->wr_ptr-AUDIO_OUT_PACKET], &outputData[0] ,96*sizeof(dacLUT[0]));
		}
		else
			{
				fir_full_q15((int16_t*)&dacLUT[0],outputData,192/2);
				memcpy((uint8_t*)&haudio->buffer[AUDIO_TOTAL_BUF_SIZE - AUDIO_OUT_PACKET], &outputData[0] ,96*sizeof(dacLUT[0]));
			}
 
    /* Prepare Out endpoint to receive next audio packet */
    USBD_LL_PrepareReceive(pdev,
                           AUDIO_OUT_EP,
                           &haudio->buffer[haudio->wr_ptr], 
                           AUDIO_OUT_PACKET);  
      
  }

static uint8_t  USBD_AUDIO_DataOut (USBD_HandleTypeDef *pdev, 
                              uint8_t epnum)
{
  USBD_AUDIO_HandleTypeDef   *haudio;
  haudio = (USBD_AUDIO_HandleTypeDef*) pdev->pClassData;
  
  if (epnum == AUDIO_OUT_EP)
  {
    /* Increment the Buffer pointer or roll it back when all buffers are full */
    
    haudio->wr_ptr += AUDIO_OUT_PACKET;
    
    if (haudio->wr_ptr == AUDIO_TOTAL_BUF_SIZE)
    {/* All buffers are full: roll back */
      haudio->wr_ptr = 0;
      
      if(haudio->offset == AUDIO_OFFSET_UNKNOWN)
      {
        ((USBD_AUDIO_ItfTypeDef *)pdev->pUserData)->AudioCmd(&haudio->buffer[0],
                                                             AUDIO_TOTAL_BUF_SIZE/2,
                                                             AUDIO_CMD_START);
          haudio->offset = AUDIO_OFFSET_NONE;
      }
    }
    
    if(haudio->rd_enable == 0)
    {
      if (haudio->wr_ptr == (AUDIO_TOTAL_BUF_SIZE / 2))
      {
        haudio->rd_enable = 1; 
      }
    }
    
		
		dataout_wr_ptr = haudio->wr_ptr;
		if (haudio->wr_ptr == 0)
		{
			test_pointer16 = (int16_t*)&haudio->buffer[AUDIO_TOTAL_BUF_SIZE - AUDIO_OUT_PACKET];
			fir_16(test_pointer16, TEST_LENGTH_SAMPLES_16);
			copy_int16_to_short(test_pointer16, &haudio->buffer[AUDIO_TOTAL_BUF_SIZE - AUDIO_OUT_PACKET], TEST_LENGTH_SAMPLES_16);
		}
		else
		{
			test_pointer16 = (int16_t*)&haudio->buffer[haudio->wr_ptr-AUDIO_OUT_PACKET];
			fir_16(test_pointer16, TEST_LENGTH_SAMPLES_16);
			
AUDIO_OUT_PACKET], TEST_LENGTH_SAMPLES_16);
		}
 
		
 
    /* Prepare Out endpoint to receive next audio packet */
    USBD_LL_PrepareReceive(pdev,
                           AUDIO_OUT_EP,
                           &haudio->buffer[haudio->wr_ptr], 
                           AUDIO_OUT_PACKET);  
      
  }

0 REPLIES 0