Skip to content

Commit c4fbf1d

Browse files
committed
fix wav file header for recorded files
1 parent dc1960a commit c4fbf1d

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

main/hardware/audio.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,26 @@ void AUDIO_Record(void *pvParameters)
228228

229229
ESP_LOGI(TAG, "File opened");
230230

231+
// Determines how many samples we want to save
232+
const size_t target_samples_written = param->duration_sec * AUDIO_INPUT_SAMPLE_FREQ;
233+
size_t bytes_received = 0;
234+
size_t samples_written = 0;
235+
231236
// write wav header
232237
wav_header_t wav_header = {
233-
.Subchunk1ID = "fmt",
234-
.Subchunk2ID = "data",
235-
.SampleRate = AUDIO_INPUT_SAMPLE_FREQ,
238+
.ChunkID = "RIFF",
239+
.ChunkSize = (2 * target_samples_written) - 8,
240+
.Format = "WAVE",
241+
.Subchunk1ID = "fmt ",
242+
.Subchunk1Size = 16,
243+
.AudioFormat = 1,
236244
.NumChannels = 1,
237-
.BitsPerSample = 16};
245+
.SampleRate = AUDIO_INPUT_SAMPLE_FREQ,
246+
.ByteRate = 64000,
247+
.BlockAlign = 2,
248+
.BitsPerSample = 16,
249+
.Subchunk2ID = "data",
250+
.Subchunk2Size = target_samples_written * 2};
238251

239252
int len = fwrite(&wav_header, 1, sizeof(wav_header_t), fd);
240253

@@ -243,11 +256,6 @@ void AUDIO_Record(void *pvParameters)
243256
ESP_LOGE(TAG, "Read wav header failed");
244257
}
245258

246-
// Determines how many samples we want to save
247-
const size_t target_samples_written = param->duration_sec * AUDIO_INPUT_SAMPLE_FREQ;
248-
size_t bytes_received = 0;
249-
size_t samples_written = 0;
250-
251259
ESP_LOGI(TAG, "Waiting for squelch to open");
252260

253261
while (1)

0 commit comments

Comments
 (0)