From 09ec5f18a0583c94286f1f1f1b462f675d93d94d Mon Sep 17 00:00:00 2001 From: San Jacobs Date: Wed, 12 Nov 2025 05:21:32 +0100 Subject: Starting to read wav metadata directly --- src/wav/wav.odin | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 src/wav/wav.odin (limited to 'src/wav/wav.odin') diff --git a/src/wav/wav.odin b/src/wav/wav.odin new file mode 100644 index 0000000..04cb0cf --- /dev/null +++ b/src/wav/wav.odin @@ -0,0 +1,140 @@ +package wav + +import "core:fmt" +import "core:math" +import "core:strings" +import "core:os" + +Wav :: struct { + path : string, + handle : os.Handle, + buf : []u8, + reported_size : u32, + bext : string, + ixml : string, + format : Audio_Format, + channels : int, + channel_names : []string, + sample_rate : int, + bit_depth : int, +} +Audio_Format :: enum { + PCM = 1, + FLOAT = 3, +} + +BUFFER_SIZE :: 1<<15 + +main :: proc() { + sweden, sweden_ok := read_wav("test/sweden.wav", context.temp_allocator) + fmt.printf("\n\nsweden = %#v\n\n", sweden) + enok, enok_ok := read_wav("test/ENOKS-BIRHTDAYT02.WAV", context.temp_allocator) + fmt.printf("\n\nenok = %#v\n\n", enok) +} + + +read_wav :: proc(path : string, allocator:=context.allocator) -> (Wav, bool) { + file : Wav + + file_ok : os.Error + file.handle, file_ok = os.open(path) + defer os.close(file.handle) + assert(file_ok == os.General_Error.None) + + file.buf = new([BUFFER_SIZE]u8)[:] + defer delete(file.buf) + + os.read(file.handle, file.buf) + + + head : int = 0 + + // RIFF header + fmt.println(string(file.buf[0:4])) + if string(file.buf[0:4]) != "RIFF" do return {}, false + head += 4 + + // Size + file.reported_size = read_little_endian_u32(file.buf[head:head+4]) + fmt.println("Reported size:", file.reported_size) + head += 4 + + // Confirming again that this is a wave file + fmt.println(string(file.buf[head:head+4])) + if string(file.buf[head:head+4]) != "WAVE" do return {}, false + head += 4 + + fmt.println("\nChunks:\n") + + // Looping through chunks + for _ in 0..