diff options
| author | San Jacobs | 2025-12-12 04:52:35 +0100 |
|---|---|---|
| committer | San Jacobs | 2025-12-12 04:52:35 +0100 |
| commit | e192100ea735fb4f3105468862a978cfbf51938a (patch) | |
| tree | 22779c4be5ae410ed33697d68e13b465302e5191 /src/wav/wav.odin | |
| parent | cb7ef81fd339199c69eccd93105c13d2a1f41f71 (diff) | |
| download | better-report-e192100ea735fb4f3105468862a978cfbf51938a.tar.gz better-report-e192100ea735fb4f3105468862a978cfbf51938a.tar.bz2 better-report-e192100ea735fb4f3105468862a978cfbf51938a.zip | |
I'm so happy with this update that it's version 1.4
Diffstat (limited to 'src/wav/wav.odin')
| -rw-r--r-- | src/wav/wav.odin | 74 |
1 files changed, 45 insertions, 29 deletions
diff --git a/src/wav/wav.odin b/src/wav/wav.odin index 41667b5..af11604 100644 --- a/src/wav/wav.odin +++ b/src/wav/wav.odin @@ -9,12 +9,13 @@ import "xml" Wav :: struct { // Basic data - path : string, - format : Audio_Format, - channels : int, - sample_rate : int, - bit_depth : int, - reported_size : u32, + path : string, + format : Audio_Format, + channels : int, + sample_rate : int, + bit_depth : int, + reported_size : int, + data_size : int, // Internals handle : os.Handle, @@ -22,8 +23,7 @@ Wav :: struct { // Metadata date : Date, channel_names : []string, - samples_since_midnight: u64, - timecode : Timecode, // Derived from samples_since_midnight + samples_since_midnight : int, // Source of timecode tc_framerate : f32, tc_dropframe : bool, ubits : [8]u8, @@ -38,18 +38,12 @@ Audio_Format :: enum { INT = 1, FLOAT = 3, } -Timecode :: struct { - hour : u8, - minute : u8, - second : u8, - frame : f32, -} Date :: struct { year, month, day : int, } VERBOSE :: false -BUFFER_SIZE :: 1<<15 +BUFFER_SIZE :: 1<<18 main :: proc() { // Test @@ -93,7 +87,7 @@ read :: proc(path : string, allocator:=context.allocator) -> (Wav, bool) #option head += 4 // Size - file.reported_size = read_little_endian_u32(temp_buf[head:head+4]) + file.reported_size = int(read_little_endian_u32(temp_buf[head:head+4])) when VERBOSE do fmt.println("Reported size:", file.reported_size) head += 4 @@ -113,10 +107,14 @@ read :: proc(path : string, allocator:=context.allocator) -> (Wav, bool) #option chunk_size := int(read_little_endian_u32(temp_buf[head:head+4])) head += 4 when VERBOSE do fmt.println(chunk_id, chunk_size,"\n-------------------------------------") - data_reached := false next_chunk_start := head + chunk_size - if head+chunk_size < BUFFER_SIZE { + data_reached := false + if chunk_id == "data" { + file.data_size = chunk_size + data_reached = true + } + if next_chunk_start < BUFFER_SIZE && !data_reached { print_data : string data_end := head+chunk_size read_end := min(head+15000, data_end) @@ -150,11 +148,9 @@ read :: proc(path : string, allocator:=context.allocator) -> (Wav, bool) #option head += 2 head = data_end null_chunks = 0 - case "data": - data_reached = true case "\x00\x00\x00\x00": if chunk_size == 0 { - null_chunks += 1 + //null_chunks += 1 } } when VERBOSE do fmt.println(print_data, "\n") @@ -408,15 +404,8 @@ read :: proc(path : string, allocator:=context.allocator) -> (Wav, bool) #option when VERBOSE do fmt.printf("Origination Time: %v\n", string(temp_bext[head:head+8])) head += 8 - file.samples_since_midnight = read_little_endian_u64(temp_bext[head:head+8]) - - seconds_since_midnight := file.samples_since_midnight / u64(file.sample_rate) - file.timecode.hour = u8( seconds_since_midnight / 3600) - file.timecode.minute = u8((seconds_since_midnight % 3600) / 60) - file.timecode.second = u8( seconds_since_midnight % 60) - file.timecode.frame = f32( f64(file.samples_since_midnight % u64(file.sample_rate) ) * f64(file.tc_framerate) / f64(file.sample_rate)) + file.samples_since_midnight = int(read_little_endian_u64(temp_bext[head:head+8])) when VERBOSE do fmt.printf("Time Reference: %v (Samples since midnight, source of timecode)\n", file.samples_since_midnight) - when VERBOSE do fmt.printf(" %v seconds + %v samples\n", seconds_since_midnight, file.samples_since_midnight % u64(file.sample_rate)) head += 8 when VERBOSE do fmt.printf("Version: %v\n", read_little_endian_u16(temp_bext[head:head+2])) @@ -436,6 +425,33 @@ read :: proc(path : string, allocator:=context.allocator) -> (Wav, bool) #option return file, true } +tprint_timecode :: proc(file : Wav) -> string { + return print_timecode(file, allocator=context.temp_allocator) +} +print_timecode :: proc(file : Wav, allocator := context.allocator) -> string { + seconds_since_midnight := file.samples_since_midnight / file.sample_rate + hour := int( seconds_since_midnight / 3600) + minute := int((seconds_since_midnight % 3600) / 60) + second := int( seconds_since_midnight % 60) + frame := int( math.round(f64(file.samples_since_midnight % file.sample_rate ) * f64(file.tc_framerate) / f64(file.sample_rate))) + + return fmt.aprintf("%02d:%02d:%02d:%02d",hour,minute,second,frame, + allocator=allocator) +} + +tprint_duration :: proc(file : Wav) -> string { + return print_duration(file, allocator=context.temp_allocator) +} +print_duration :: proc(file : Wav, allocator := context.allocator) -> string { + samples := file.data_size/file.channels/(file.bit_depth/8) + total_seconds := samples/file.sample_rate + + hours := int( total_seconds / 3600) + minutes := int((total_seconds % 3600) / 60) + seconds := int( total_seconds % 60) + + return fmt.aprintf("%02d:%02d:%02d", hours, minutes, seconds, allocator=allocator) +} read_little_endian_u64 :: proc(x : []u8) -> u64 { |