C / STM32 - 从 .wav 文件读取和复制数据

C / STM32 - Read and copy a data from .wav file

我正在尝试从我的闪存中复制一个 .wav 文件。

#define AUDIO_BUFFER_SIZE          (1024 * 8)       /* Size (in bytes) of the buffer containing the PCM samples */

uint8_t                  Buffer[AUDIO_BUFFER_SIZE];    // Buffer containig the PCM samples to play

...

      /* Fill in the buffer with new data */
  if (f_read(&File, (uint8_t   *)Buffer, AUDIO_BUFFER_SIZE, &bytesRead) != FR_OK)
  {
    Error_Handler();
  }

  if (counter==1){
      HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
      //uint8_t string[20]="Hello, world!";
      //f_write(&OutFile, Buffer, (UINT)sizeof(Buffer),&bytesRead);
      for(int i = 0; i <= sizeof(Buffer); i++){
          f_printf(&OutFile, "%d\n",Buffer[i]);
          osDelay(10);
      }
      counter++;
  }
  else{
      HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_RESET);
      f_close(&OutFile);
  }

当我这样做时,我得到了一个包含类似值的文件(此屏幕截图的右侧部分) Output file

我怎样才能得到正确的值,因为我们可以在屏幕截图的左侧看到它们?

此致

根据这些值,您的样本看起来像是以带符号的 16 位小端格式编码的。

要解码格式,你可以这样做(假设f_printf的格式说明符就像标准的printf):

// 2 bytes per sample, also use < instead of <=
for(int i = 0; i < sizeof(Buffer); i += 2){
    int value = Buffer[i] | (Buffer[i + 1] << 8); // merge the 2 bytes into one integer
    if (value >= 0x8000) value -= 0x10000; // because the samples are signed
    f_printf(&OutFile, "%.4f\n", value / (double)0x8000); // divide with the maximum value
    osDelay(10);
}

如果你不能通过f_printf打印浮点数,你可以打印四舍五入:

  1. 10000因为小数点后有4位
  2. 乘以 2 并根据值的符号加或减 0x8000(用于除法的值)
  3. 除以 0x8000 * 2
  4. 打印小数点前后的数值
// 2 bytes per sample, also use < instead of <=
for(int i = 0; i < sizeof(Buffer); i += 2){
    int v;
    int value = Buffer[i] | (Buffer[i + 1] << 8); // merge the 2 bytes into one integer
    if (value >= 0x8000) value -= 0x10000; // because the samples are signed
    // divide with the maximum value
    v = ((value * 10000) * 2 + (value >= 0 ? 0x8000 : -0x8000)) / (0x8000 * 2);
    f_printf(&OutFile, "%s%d.%04d\n",
      v < 0 && v / 10000 == 0 ? "-" : "", // sign (because typical integers don't have -0)
      v / 10000, // value before the decimal point
      (v < 0 ? -v : v) % 10000); // value after the decimal point
    osDelay(10);
}