From 9e7a3049090e6943a3fe561601f59678056b474a Mon Sep 17 00:00:00 2001 From: parking_turkeys Date: Fri, 7 Aug 2026 17:34:37 -0400 Subject: [PATCH] wave header --- readwave.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ wavtool.h | 2 ++ 2 files changed, 68 insertions(+) diff --git a/readwave.c b/readwave.c index 0c6d2cd..691a1d2 100644 --- a/readwave.c +++ b/readwave.c @@ -32,6 +32,72 @@ int readWave(char *filename, int16_t buffer[]) //if buffer is NULL it only retur error_msg = "input wave file not good :<"; goto error; }; + uint32_t riff_length = 0; //length of the rest of the file + ReadFile(fp, &riff_length, 4, &bytes_read, NULL); + if (bytes_read != 4) { + error_msg = "file ended before the 8th byte?? wha"; + goto error; + }; + //now the WAVE chunk id + ReadFile(fp, chunk_id, 4, &bytes_read, NULL); + if (strcmp("WAVE", chunk_id)) { + error_msg = "not a WAVE file :<"; + goto error; + }; + //now the 'fmt ' chunk id + ReadFile(fp, chunk_id, 4, &bytes_read, NULL); + if (strcmp("fmt ", chunk_id)) { + error_msg = "fmt chunk not first chunk :("; + goto error; + }; + //length + uint32_t fmt_length = 0; + ReadFile(fp, &fmt_length, 4, &bytes_read, NULL); + if (bytes_read != 4) { + error_msg = "couldn't read length of 'fmt ' chunk in input wave file"; + goto error; + }; + //now the format tag + uint16_t format_tag = 0; + ReadFile(fp, &format_tag, 2, &bytes_read, NULL); + if (format_tag != 1) { + error_msg = "not the format i like..."; + goto error; + }; + uint16_t channel_count = 0; + ReadFile(fp, &channel_count, 2, &bytes_read, NULL); + if (channel_count != 1) { + error_msg = "input wave file not mono"; + goto error; + }; + uint32_t sampling_rate = 0; + ReadFile(fp, &sampling_rate, 4, &bytes_read, NULL); + if (sampling_rate != SAMPLING_RATE) { + error_msg = "incorrect sampling rate (should be 44100)"; + goto error; + }; + uint8_t boring[6] = {0}; + ReadFile(fp, boring, 6, &bytes_read, NULL); + if (bytes_read != 6) { + error_msg = "input file ended unexpectedly in 'fmt ' chunk"; + goto error; + }; + uint16_t bitsPerSample = 0; + ReadFile(fp, &bitsPerSample, 2, &bytes_read, NULL); + if (bitsPerSample != 16) { + error_msg = "input file has non-16-bit samples"; + goto error; + }; + ReadFile(fp, &chunk_id, 4, &bytes_read, NULL); + if (strcmp(chunk_id, "data")) { + error_msg = "input file does not have data chunk right after fmt chunk"; + goto error; + }; + + + + + return 0; diff --git a/wavtool.h b/wavtool.h index ab7a293..a56c134 100644 --- a/wavtool.h +++ b/wavtool.h @@ -9,6 +9,8 @@ #include #include +#define SAMPLING_RATE 44100 + struct riff_chunk { char ckID[4];//NOT ZERO TERMINATED uint32_t ckSize; -- 2.47.3