Reputation: 3
I am trying to copy a .wav file from my flash RAM memory.
#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);
}
When i do this, I get a file with some values like that (right part of this screenshot) Output file
How can i get the correct values as we can see them on the left part of my screenshot ?
Regards
Upvotes: 0
Views: 984
Reputation: 75062
Based on the values, your samples looks like being encoded in signed 16-bit little endian format.
To decode the format, you can do like this (assuming that the format specifier of f_printf
is just like the standard 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);
}
If you cannot print floating-point number via f_printf
, you can print with rounding by doing:
10000
because there are 4 digits after the decimal point0x8000
(the value used for division) based of the sign of the value0x8000 * 2
// 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);
}
Upvotes: 1