aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.schreiber2
-rw-r--r--wav/wav.odin319
-rw-r--r--wav/xml/tokenizer.odin6
-rw-r--r--wav/xml/xml_reader.odin4
4 files changed, 185 insertions, 146 deletions
diff --git a/.schreiber b/.schreiber
index e4a5c90..215e6cd 100644
--- a/.schreiber
+++ b/.schreiber
@@ -59,7 +59,7 @@ release: odin build . -o:speed -out:wav.exe
# collection: lib = lib
root: D:/Progs/odin
check: .
-check_flags: -vet
+check_flags: -vet-unused-variables -vet-cast -vet-semicolon -vet-style
# The C and C++ code of the project: the compile_commands.json of the build, which gives the include folders and the defines
# of each file. With no such line, Schreiber takes the newest one in this folder or in a folder in it. A define line gives a
diff --git a/wav/wav.odin b/wav/wav.odin
index dfa7fdb..51d54e2 100644
--- a/wav/wav.odin
+++ b/wav/wav.odin
@@ -56,7 +56,7 @@ Date :: struct {
year, month, day : int,
}
Timecode :: struct {
- hour, minute, second, frame : int
+ hour, minute, second, frame : int,
}
Speakers :: enum u32 {
Front_Left = 0,
@@ -84,7 +84,9 @@ VERBOSE :: false
BUFFER_SIZE :: 1<<18
/*
+Comfy.
Reads in the wav file metadata, without loading the sound data into ram.
+Makes and deletes its own temporary work buffer.
*/
read :: proc(path : string, allocator := context.allocator) -> (Wav, bool) #optional_ok {
handle, load_err := os.open(path)
@@ -92,39 +94,54 @@ read :: proc(path : string, allocator := context.allocator) -> (Wav, bool) #opti
fmt.eprintfln("ERROR %v: Unable to load file \"%v\"", load_err, path)
return {}, false
}
- output_wav, ok := read_by_handle(handle, path, allocator)
+ temp_buf := make([]u8, BUFFER_SIZE)
+
+ output_wav, ok := read_by_handle(handle, path, temp_buf, allocator)
+
+ delete(temp_buf)
os.close(handle)
return output_wav, ok
}
-read_by_handle :: proc(handle : ^os.File, file_path : string, allocator := context.allocator) -> (Wav, bool) #optional_ok {
+
+/*
+Reads in the wav file metadata, without loading the sound data into memory.
+*/
+read_by_handle :: proc(handle : ^os.File, file_path : string, work_buf : []u8, allocator := context.allocator) -> (Wav, bool) #optional_ok {
+ /*
+ TODO: Add ability to read beyond the initial work_buf area.
+ This is needed because it is technically possible to put stuff after data.
+ Yes, this is cursed. We could replace it all with seeking and reading each
+ section one by one, but that would be like 15 individual reads every file,
+ potentially making things slow if you have big latency to storage. Much
+ nicer to grab 256 KB right away, which covers all you need in basically
+ every case, and then seek if you need to go beyond that.
+ */
the_wav : Wav
the_wav.path = strings.clone(file_path, allocator)
the_wav.take = -1
the_wav.tc_framerate = -1
the_wav.take = -1
- temp_buf := make([]u8, BUFFER_SIZE)
- defer delete(temp_buf)
temp_bext : []u8
temp_ixml : string
- os.read(handle, temp_buf)
+ os.read(handle, work_buf)
head : int = 0
// RIFF header
- when VERBOSE do fmt.println(string(temp_buf[0:4]))
- if string(temp_buf[0:4]) != "RIFF" do return {}, false
+ when VERBOSE do fmt.println(string(work_buf[0:4]))
+ if string(work_buf[0:4]) != "RIFF" do return {}, false
head += 4
// Size
- the_wav.reported_size = int(little_endian_u32(temp_buf[head:head+4]))
+ the_wav.reported_size = int(little_endian_u32(work_buf[head:head+4]))
when VERBOSE do fmt.println("Reported size:", the_wav.reported_size)
head += 4
// Confirming again that this is a wave file
- when VERBOSE do fmt.println(string(temp_buf[head:head+4]))
- if string(temp_buf[head:head+4]) != "WAVE" do return {}, false
+ when VERBOSE do fmt.println(string(work_buf[head:head+4]))
+ if string(work_buf[head:head+4]) != "WAVE" do return {}, false
head += 4
when VERBOSE do fmt.println("\nChunks:\n")
@@ -132,10 +149,10 @@ read_by_handle :: proc(handle : ^os.File, file_path : string, allocator := conte
// Looping through chunks
null_chunks := 0
for _ in 0..<BUFFER_SIZE {
- chunk_id_raw := temp_buf[head:head+4]
+ chunk_id_raw := work_buf[head:head+4]
chunk_id := string(chunk_id_raw)
head += 4
- chunk_size := int(little_endian_u32(temp_buf[head:head+4]))
+ chunk_size := int(little_endian_u32(work_buf[head:head+4]))
head += 4
when VERBOSE do fmt.println(chunk_id, chunk_size,"\n-------------------------------------")
// Chunks are padded to an even size. The pad byte is not included in chunk_size.
@@ -148,20 +165,20 @@ read_by_handle :: proc(handle : ^os.File, file_path : string, allocator := conte
data_reached = true
}
if next_chunk_start < BUFFER_SIZE && !data_reached {
- print_data : string
+ // print_data : string
data_end := head+chunk_size
- read_end := min(head+15000, data_end)
+ // read_end := min(head+15000, data_end)
switch chunk_id {
case "iXML":
- temp_ixml = string(temp_buf[head:data_end])
+ temp_ixml = string(work_buf[head:data_end])
//print_data = temp_ixml
null_chunks = 0
case "bext":
- temp_bext = temp_buf[head:data_end]
+ temp_bext = work_buf[head:data_end]
//print_data = string(temp_bext)
null_chunks = 0
case "fmt ":
- raw_format_field := little_endian_u16(temp_buf[head:])
+ raw_format_field := little_endian_u16(work_buf[head:])
wave_format_extensible := false
switch raw_format_field {
case 1..=3:
@@ -172,30 +189,30 @@ read_by_handle :: proc(handle : ^os.File, file_path : string, allocator := conte
when VERBOSE do fmt.println("Format: wave_format_extensible")
}
head += 2
- the_wav.channels = int(little_endian_u16(temp_buf[head:]))
+ the_wav.channels = int(little_endian_u16(work_buf[head:]))
when VERBOSE do fmt.println("Channels:", the_wav.channels)
head += 2
- the_wav.sample_rate = int(little_endian_u32(temp_buf[head:]))
+ the_wav.sample_rate = int(little_endian_u32(work_buf[head:]))
when VERBOSE do fmt.println("Sample rate:", the_wav.sample_rate)
head += 4
// Skipping byte rate. It's redundant.
head += 4
-
+
// Block align is the size of one frame (one sample for every channel).
// It's the most reliable source of the sample stride, e.g. 20-bit files
// have a bit depth of 20, but each sample still takes up 3 bytes.
- block_align := int(little_endian_u16(temp_buf[head:]))
+ block_align := int(little_endian_u16(work_buf[head:]))
when VERBOSE do fmt.println("Block align:", block_align)
head += 2
-
+
// The bit depth. In the wave_format_extensible case, this is the container size,
// and the actual bit depth is in valid_bits_pr_sample further down.
- bits_pr_sample := int(little_endian_u16(temp_buf[head:]))
+ bits_pr_sample := int(little_endian_u16(work_buf[head:]))
the_wav.bit_depth = bits_pr_sample
when VERBOSE do fmt.println("Bit depth:", the_wav.bit_depth)
head += 2
-
+
if the_wav.channels > 0 && block_align > 0 && block_align % the_wav.channels == 0 {
the_wav.sample_size = block_align / the_wav.channels * 8
} else {
@@ -203,28 +220,28 @@ read_by_handle :: proc(handle : ^os.File, file_path : string, allocator := conte
the_wav.sample_size = (bits_pr_sample + 7) / 8 * 8
}
when VERBOSE do fmt.println("Sample size/stride:", the_wav.sample_size)
-
+
if wave_format_extensible && chunk_size>=40 {
-
+
when VERBOSE do fmt.println(" - wave_format_extensible -")
- when VERBOSE do fmt.println("Size of extension:", little_endian_u16(temp_buf[head:]))
+ when VERBOSE do fmt.println("Size of extension:", little_endian_u16(work_buf[head:]))
head += 2
// Valid bits pr sample (aka is every 24-bit sample padded to be 32 bit aligned?)
// OR this field can be samples pr block, if this is a compressed format...
- // ...sigh... yes, you can cram a compressed format into a wav file. I know.
- valid_bits_pr_sample := int(little_endian_u16(temp_buf[head:]))
+ // ...sigh... yes, you can cram a compressed format into a wav file... I know.
+ valid_bits_pr_sample := int(little_endian_u16(work_buf[head:]))
head += 2
// Channel mask (Which speaker channels are in the file)
- the_wav.channel_mask = transmute(Speaker_Set)little_endian_u32(temp_buf[head:])
- when VERBOSE do fmt.println("Channel Mask (RAW):", temp_buf[head:head+4])
+ the_wav.channel_mask = transmute(Speaker_Set)little_endian_u32(work_buf[head:])
+ when VERBOSE do fmt.println("Channel Mask (RAW):", work_buf[head:head+4])
when VERBOSE do fmt.println("Channel Mask (Speakers):", the_wav.channel_mask)
head += 4
// 16 bytes of SubFormat GUID, with the first two bytes being the format
- when VERBOSE do fmt.println("GUID:", temp_buf[head:head+16])
- sub_format := little_endian_u16(temp_buf[head:])
+ when VERBOSE do fmt.println("GUID:", work_buf[head:head+16])
+ sub_format := little_endian_u16(work_buf[head:])
if sub_format >= 1 && sub_format <= 3 {
the_wav.format = Audio_Format(sub_format)
}
@@ -232,8 +249,8 @@ read_by_handle :: proc(handle : ^os.File, file_path : string, allocator := conte
head += 2
// Other GUID stuff
head += 14
-
-
+
+
if the_wav.format == .ADPCM {
// TODO: Support ADPCM (Very low priority)
when VERBOSE do fmt.println("Samples pr chunk (In valid bits field because it's unsupported):", valid_bits_pr_sample)
@@ -281,7 +298,7 @@ read_by_handle :: proc(handle : ^os.File, file_path : string, allocator := conte
fmt.eprintfln("ERROR: Missing or broken fmt chunk in \"%v\"", file_path)
return {}, false
}
-
+
the_wav.channel_names = make([]string, the_wav.channels, allocator=allocator)
the_wav.audio = make([][]f32, the_wav.channels, allocator=allocator)
@@ -317,10 +334,10 @@ read_by_handle :: proc(handle : ^os.File, file_path : string, allocator := conte
fmt.printf("\t")
}
}
-
+
when VERBOSE do fmt.printf("\n")
when VERBOSE do tab(indent)
-
+
element := doc.elements[element_id]
if element.kind == .Element {
@@ -433,10 +450,11 @@ read_by_handle :: proc(handle : ^os.File, file_path : string, allocator := conte
xml_recurse(doc, v, the_wav, naming_channel, interleave_set, allocator, indent + 1)
}
}
-
- for attr in element.attribs {
- when VERBOSE do tab(indent + 1)
- when VERBOSE do fmt.printf("[Attr] %v: %v\n", attr.key, attr.val)
+ when VERBOSE {
+ for attr in element.attribs {
+ tab(indent + 1)
+ fmt.printf("[Attr] %v: %v\n", attr.key, attr.val)
+ }
}
} else if element.kind == .Comment {
when VERBOSE do fmt.printf("[COMMENT] %v\n", element.value)
@@ -555,40 +573,108 @@ read_by_handle :: proc(handle : ^os.File, file_path : string, allocator := conte
/*
-Fills the list of buffers you provide with audio from channels you list, from the sample index you provide.
-It returns the number of samples loaded (per requested channel, not in total).
+Comfy.
+Loads the full-length audio of the wav file into memory (allocated by allocator) by path.
+Pass in a slice of ints to select only specific channels to load.
+This opens and closes the file handle on its own, and makes/deletes it's own work_buffer.
+Consider the load a success if samples_loaded == get_sample_count(the_wav)
+
+This will just allocate and load again if you call it twice.
*/
-load_at :: proc(handle : ^os.File, the_wav : ^Wav, start_index : int, output_bufs : [][]f32, work_buf : []u8, channels : []int = {}) -> (samples_loaded : int = 0) {
- decode, decode_ok := get_decoder(the_wav^)
+load :: proc(the_wav : ^Wav, channels : []int = {}, allocator:=context.allocator) -> (samples_loaded : int = 0) {
+ handle, load_err := os.open(the_wav.path)
+ if load_err != nil {
+ fmt.eprintfln("ERROR %v: Unable to load file \"%v\"", load_err, the_wav.path)
+ return
+ }
+
+ all_channels := len(channels) == 0
+ channel_count := all_channels ? the_wav.channels : len(channels)
+
+ err_quit := false
+ if !all_channels {
+ count := 0
+ for a in channels {
+ count = 0
+ for b in channels {
+ if a==b do count+=1
+ }
+ if count > 1 {
+ fmt.eprintfln("ERROR: Channel {} was requested to load {} times! Channels: {}", a, count, channels)
+ err_quit = true
+ }
+ }
+ }
+
+ _, decode_ok := get_decoder(the_wav^)
if !decode_ok {
fmt.eprintfln("ERROR: Unsupported format: %v, %v bit samples", the_wav.format, the_wav.sample_size)
return samples_loaded
}
-
- channels := channels
- all_channels := false
- if len(channels) == 0 {
- all_channels = true
- channels = make([]int, the_wav.channels)
- for c, i in channels {
- channels[i] = i
+
+ for i in 0..<channel_count {
+ c := all_channels ? i : channels[i]
+ if c < 0 || c >= the_wav.channels {
+ fmt.eprintfln("ERROR: Requested channel ({}) doesn't exist in file, which has {} channels!", c, the_wav.channels)
+ err_quit = true
}
}
- defer if all_channels do delete(channels)
+
+ if err_quit do return
+
+ // Allocating for the work
+ work_buffer := allocate_appropriate_work_buffer(the_wav)
+ output_buffers := make([][]f32, channel_count)
+
+ for i in 0..<channel_count {
+ c := all_channels ? i : channels[i]
+ the_wav.audio[c] = make([]f32, get_sample_count(the_wav^), allocator)
+ output_buffers[i] = the_wav.audio[c]
+ }
+
+ // The work
+ samples_loaded = load_at(handle, the_wav, 0, output_buffers, work_buffer, channels)
+
+ delete(work_buffer)
+ delete(output_buffers)
+ os.close(handle)
+ return samples_loaded
+}
+/*
+Fills the list of buffers you provide with audio from channels you list, from the sample index you provide.
+It returns the number of samples loaded (per requested channel, not in total).
+*/
+load_at :: proc(handle : ^os.File,
+ the_wav : ^Wav,
+ start_index : int,
+ output_bufs : [][]f32,
+ work_buf : []u8,
+ channels : []int = {}) -> (samples_loaded : int = 0) {
+
+ all_channels := len(channels) == 0
+ channel_count := all_channels ? the_wav.channels : len(channels)
+
+ decode, decode_ok := get_decoder(the_wav^)
+ if !decode_ok {
+ fmt.eprintfln("ERROR: Unsupported format: %v, %v bit samples", the_wav.format, the_wav.sample_size)
+ return samples_loaded
+ }
+
err_quit := false
- for c in channels {
+ for i in 0..<channel_count {
+ c := all_channels ? i : channels[i]
if c < 0 || c >= the_wav.channels {
fmt.eprintfln("ERROR: Requested channel ({}) doesn't exist in file, which has {} channels!", c, the_wav.channels)
err_quit = true
}
}
- if len(output_bufs) != len(channels) {
- fmt.eprintfln("ERROR: Number of channels requrested ({}) does not match number of buffers to put them in ({})!", len(channels), len(output_bufs))
+ if len(output_bufs) != channel_count {
+ fmt.eprintfln("ERROR: Number of channels requrested ({}) does not match number of buffers to put them in ({})!", channel_count, len(output_bufs))
err_quit = true
}
if err_quit do return samples_loaded
-
+
samples_pr_channel := len(output_bufs[0])
for buf, i in output_bufs {
if len(buf) != samples_pr_channel {
@@ -598,6 +684,13 @@ load_at :: proc(handle : ^os.File, the_wav : ^Wav, start_index : int, output_buf
}
if err_quit do return samples_loaded
+ frame_size := the_wav.sample_size/8 * the_wav.channels
+ if len(work_buf) < frame_size {
+ fmt.eprintfln("ERROR: Work buffer can't even hold a single frame of samples! len(work_buf)={} < frame_size={}!", len(work_buf), frame_size)
+ return samples_loaded
+ }
+
+
// The main work part
file_length := get_sample_count(the_wav^)
@@ -606,15 +699,16 @@ load_at :: proc(handle : ^os.File, the_wav : ^Wav, start_index : int, output_buf
samples_in_buffer := int(len(work_buf)/bytes_pr_sample/the_wav.channels)
buffer_bytes := i64(samples_in_buffer*bytes_pr_sample*the_wav.channels)
work_buf := work_buf[:buffer_bytes]
-
+
head : i64 = i64(the_wav.data_start) + i64(start_index * the_wav.channels * bytes_pr_sample)
absolute_sample : u64 = u64(start_index)
relative_sample : int = 0
for {
os.read_at(handle, work_buf, head)
for local_sample in 0..<samples_in_buffer {
- for c, i in channels {
- offset := (local_sample*the_wav.channels+c)*bytes_pr_sample
+ for i in 0..<channel_count {
+ c := all_channels ? i : channels[i]
+ offset := (local_sample * the_wav.channels + c) * bytes_pr_sample
output_bufs[i][relative_sample] = decode(work_buf[offset:])
}
relative_sample += 1
@@ -629,94 +723,34 @@ load_at :: proc(handle : ^os.File, the_wav : ^Wav, start_index : int, output_buf
}
head += buffer_bytes
}
-
+
return samples_loaded
}
/*
-Loads the full-length audio of the wav file into memory by path.
-Pass in a slice of ints to select only specific channels to load.
-This opens and closes the file handle on its own, and calls load_by_handle().
+Use this to de-allocate the audio loaded into wav.
+Of course, you're probably smart enough to have allocated it on an arena,
+so you won't need this, but in case you had to allocate it with a random heap allocator
+Here's a convenient way to de-allocate it.
*/
-load :: proc(the_wav : ^Wav, channels : []int = {}, allocator:=context.allocator) -> bool #optional_ok {
- handle, load_err := os.open(the_wav.path)
- if load_err != nil {
- fmt.eprintfln("ERROR %v: Unable to load file \"%v\"", load_err, the_wav.path)
- return false
+unload :: proc(the_wav : ^Wav, allocator := context.allocator) {
+ for &each_channel in the_wav.audio {
+ delete(each_channel, allocator)
+ each_channel = nil
}
- ok := load_by_handle(handle, the_wav, channels, allocator)
- os.close(handle)
- return ok
}
-/*
-Loads the full-length audio of the wav file into memory.
-Pass in a slice of ints to select only specific channels to load.
-This does not aquire its own handle, you must give it one.
-*/
-load_by_handle :: proc(handle : ^os.File, the_wav : ^Wav, channels : []int = {}, allocator:=context.allocator) -> bool #optional_ok {
- decode, decode_ok := get_decoder(the_wav^)
- if !decode_ok {
- fmt.eprintfln("ERROR: Unsupported format: %v, %v bit samples", the_wav.format, the_wav.sample_size)
- return false
- }
- channels := channels
- all_channels := false
- if len(channels) == 0 {
- all_channels = true
- channels = make([]int, the_wav.channels)
- for c, i in channels {
- channels[i] = i
- }
- }
- defer if all_channels do delete(channels)
- err_quit := false
- for c in channels {
- if c < 0 || c >= the_wav.channels {
- fmt.eprintfln("ERROR: Requested channel ({}) doesn't exist in file, which has {} channels!", c, the_wav.channels)
- err_quit = true
- }
- }
- if err_quit do return false
- // The main work part
- length := get_sample_count(the_wav^)
- for c in channels {
- the_wav.audio[c] = make([]f32, length, allocator=allocator)
- }
- samples_in_buffer :: 1024
- bytes_pr_sample := the_wav.sample_size/8
- buffer_bytes : i64 = i64(bytes_pr_sample*the_wav.channels*samples_in_buffer)
- buffer := make([]u8, buffer_bytes)
- defer delete(buffer)
- head : i64 = i64(the_wav.data_start)
- absolute_sample : u64 = 0
- for {
- os.read_at(handle, buffer, head)
- //fmt.println(buffer)
- for local_sample in 0..<samples_in_buffer {
- for c in channels {
- offset := (local_sample*the_wav.channels+c)*bytes_pr_sample
- the_wav.audio[c][absolute_sample] = decode(buffer[offset:])
- }
- absolute_sample += 1
- if absolute_sample >= u64(length) {
- break
- }
- }
- if absolute_sample >= u64(length) {
- break
- }
- head += buffer_bytes
- }
- return true
-}
+
+
+
+
tprint_timecode :: proc(the_wav : Wav) -> string {
return print_timecode(the_wav, allocator=context.temp_allocator)
}
@@ -795,10 +829,15 @@ bar_print :: proc(x : f32, one_side_width : int = 64, sign := '#', square := tru
}
-allocate_appropriate_work_buffer :: proc(the_wav : Wav, sample_count : int, allocator:=context.allocator) -> []u8 {
- // Bytes pr sample
- size := (the_wav.sample_size/8) * sample_count * the_wav.channels
- return make([]u8, size)
+/*
+Allocates a work buffer of at most max_bytes, rounded down to whole frames of the wav.
+Always at least one frame. Defaults to right under a Megabyte.
+(A frame is sample*channel_count)
+*/
+allocate_appropriate_work_buffer :: proc(the_wav : ^Wav, max_bytes : int = 1<<20, allocator := context.allocator) -> []u8 {
+ frame_size := the_wav.sample_size/8 * the_wav.channels
+ frames := max(max_bytes / frame_size, 1)
+ return make([]u8, frames * frame_size, allocator)
}
diff --git a/wav/xml/tokenizer.odin b/wav/xml/tokenizer.odin
index f4c9c8a..9390ae8 100644
--- a/wav/xml/tokenizer.odin
+++ b/wav/xml/tokenizer.odin
@@ -221,7 +221,7 @@ scan_identifier :: proc(t: ^Tokenizer) -> string {
if t.ch == ':' {
// A namespaced attr can have at most two parts, `namespace:ident`.
if namespaced {
- break
+ break
}
namespaced = true
}
@@ -274,7 +274,7 @@ skip_cdata :: proc(t: ^Tokenizer) -> (err: Error) {
}
t.read_offset += len(CDATA_START)
- offset := t.offset
+ // offset := t.offset
cdata_scan: for {
advance_rune(t)
@@ -321,7 +321,7 @@ scan_string :: proc(t: ^Tokenizer, offset: int, close: rune = '<', consume_close
//error(t, offset, string(t.src[offset : t.offset]))
//error(t, offset, "[scan_string] Not terminated\n")
err = .Invalid_Tag_Value
- break loop
+ break loop
}
}
diff --git a/wav/xml/xml_reader.odin b/wav/xml/xml_reader.odin
index 5fd3dc8..8954998 100644
--- a/wav/xml/xml_reader.odin
+++ b/wav/xml/xml_reader.odin
@@ -457,7 +457,7 @@ check_duplicate_attributes :: proc(t: ^Tokenizer, attribs: Attributes, attr: Att
return .Duplicate_Attribute
}
}
- return .None
+ return .None
}
parse_attributes :: proc(doc: ^Document, attribs: ^Attributes) -> (err: Error) {
@@ -479,7 +479,7 @@ parse_prologue :: proc(doc: ^Document) -> (err: Error) {
context.allocator = doc.allocator
t := doc.tokenizer
- offset := t.offset
+ // offset := t.offset
parse_attributes(doc, &doc.prologue) or_return
for attr in doc.prologue {