1

我想用 STM32 实现一个 WAV/MP3 播放器(现在我们只说 WAV),它用 FATFS 从 SD 读取它,然后用 DMA 将它传输到 I2S 缓冲区。

问题是,当我插入扬声器时,它会以正确的音高(音调)播放歌曲,但速度很慢,而且还会发出奇怪的重复滴答声,就像缓冲区被缓慢填充,或者播放缓慢,但使用了正确的频率。

我使用 STM32_F4VE(STM32F407VET6) 和 PCM5102(只需要 DATA,16 位的 32xfs 的 BCK 和 fs 的 LRCK)

这里我附上几个代码部分:main.c

    if(res = f_mount(&SDFatFS, SDPath, 1) == FR_OK){                        //If SD card is correctly mounted
      HAL_UART_Transmit_IT(&huart1, "SD montada correctamente\n\r", strlen("SD montada correctamente\n\r"));
      if(wavPlayer_fileSelect("test.wav")  == 0){                   //If the wav file wasn't correstly opened
          HAL_UART_Transmit_IT(&huart1, "Error al abrir el WAV\n\r", strlen("Error al abrir el WAV\n\r"));
      }
      else
      {
          HAL_UART_Transmit_IT(&huart1, "WAV abierto\n\r", strlen("WAV abierto\n\r"));
          wavPlayer_play();
          HAL_UART_Transmit_IT(&huart1, "WAV PLAY\n\r", strlen("WAV PLAY\n\r"));
          isPlaying = true;
      }
  }
  else
  {
      HAL_UART_Transmit_IT(&huart1, "Error al montar la SD\n\r", strlen("Error al montar la SD\n\r"));
  }

播放器.c

    /**
 * @brief Select WAV file to play
 * @retval returns true when file is found in USB Drive
 */
bool wavPlayer_fileSelect(const char* filePath)
{
  UINT readBytes = 0;
  //Open WAV file
  if(f_open(&wavFile, filePath, FA_READ) != FR_OK)
  {
    return false;
  }
  //Read WAV file Header
  f_read(&wavFile, &wavHeader, sizeof(wavHeader), &readBytes);
  sprintf(UART_buff, "Tamaño del archivo: %d\n\rFrecuencia de muestreo: %d\n\r", wavHeader.FileSize, wavHeader.SampleRate);
  HAL_UART_Transmit_IT(&huart1, UART_buff, strlen(UART_buff));//TX Function
  end_of_file_reached = false;
  return true;
}

/**
 * @brief WAV File Play
 */
void wavPlayer_play(void)
{
  isFinished = false;
  //Read Audio data from USB Disk
  f_lseek(&wavFile, 0);
  f_read (&wavFile, &audioBuffer[0], AUDIO_BUFFER_SIZE, &playerReadBytes);
  audioRemainSize = wavHeader.FileSize - playerReadBytes;
  //Start playing the WAV
  HAL_I2S_Transmit_DMA(&hi2s2, (uint16_t *)&audioBuffer[0], AUDIO_BUFFER_SIZE);
}

    
/**
 * @brief Half/Full transfer Audio callback for buffer management
 */
void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
{
  if(hi2s->Instance == SPI2)
  {
      if(end_of_file_reached){
                return;
        }

        res = f_read (&wavFile, &audioBuffer[AUDIO_BUFFER_SIZE/2], AUDIO_BUFFER_SIZE/2, &playerReadBytes);

        if(audioRemainSize > (AUDIO_BUFFER_SIZE / 2))
        {
          audioRemainSize -= playerReadBytes;
        }
        else
        {
          audioRemainSize = 0;
          end_of_file_reached = true;
        }
  }
}

void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
{
  if(hi2s->Instance == SPI2)
  {
      if(end_of_file_reached){
          return;
      }

      res = f_read (&wavFile, &audioBuffer[0], AUDIO_BUFFER_SIZE/2, &playerReadBytes);

      if(audioRemainSize > (AUDIO_BUFFER_SIZE / 2))
      {
          audioRemainSize -= playerReadBytes;
      }
      else
      {
          audioRemainSize = 0;
          end_of_file_reached = true;
      }
  }
}
4

0 回答 0