aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--wav/readme.md7
-rw-r--r--wav/wav.odin850
-rw-r--r--wav/xml/debug_print.odin86
-rw-r--r--wav/xml/doc.odin23
-rw-r--r--wav/xml/helpers.odin52
-rw-r--r--wav/xml/tokenizer.odin415
-rw-r--r--wav/xml/xml_reader.odin628
7 files changed, 2061 insertions, 0 deletions
diff --git a/wav/readme.md b/wav/readme.md
new file mode 100644
index 0000000..6611814
--- /dev/null
+++ b/wav/readme.md
@@ -0,0 +1,7 @@
+# My wav lib for Odin
+
+This is my wav lib for Odin.
+Eventually, I think this will becomoe the most complete wav lib available for Odin, but right now it's not at all.
+I'm only adding features and coverage as I find I need it.
+Don't use it yet.
+It's built mainly for dealing with obscure metadata extensions like BEXT and iXML.
diff --git a/wav/wav.odin b/wav/wav.odin
new file mode 100644
index 0000000..ea6f5b7
--- /dev/null
+++ b/wav/wav.odin
@@ -0,0 +1,850 @@
+package wav
+
+import "core:fmt"
+import "core:math"
+import "core:strings"
+import "core:strconv"
+import "core:os"
+import "xml"
+
+/*
+TODO: Support RF64
+TODO: Test on files from SD788t
+TODO: Support music metadata
+*/
+
+Wav :: struct {
+ // Basic data
+ path : string,
+ format : Audio_Format,
+ channels : int,
+ sample_rate : int,
+ bit_depth : int,
+ sample_size : int, // Samples can be bigger than bit_depth for alignment
+ reported_size : int,
+ data_size : int,
+ data_start : int,
+
+ // The actual audio data
+ audio : [][]f32,
+
+ // Internals
+ handle : ^os.File,
+ load_head : int,
+
+ // Metadata
+ date : Date,
+ channel_names : []string,
+ channel_mask : Speaker_Set,
+ samples_since_midnight : int, // Source of timecode
+ tc_framerate : f32,
+ tc_dropframe : bool,
+ ubits : [8]u8,
+ take : int,
+ scene : string,
+ note : string,
+ project : string,
+ tape : string,
+ circled : bool,
+}
+Audio_Format :: enum {
+ INT = 1,
+ ADPCM = 2, // Compressed format. Not yet handled.
+ FLOAT = 3,
+}
+Date :: struct {
+ year, month, day : int,
+}
+Timecode :: struct {
+ hour, minute, second, frame : int
+}
+Speakers :: enum u32 {
+ Front_Left = 0,
+ Front_Right = 1,
+ Front_Center = 2,
+ LFE = 3,
+ Back_Left = 4,
+ Back_Right = 5,
+ Front_Left_of_Center = 6,
+ Front_Right_of_Center = 7,
+ Back_Center = 8,
+ Side_Left = 9,
+ Side_Right = 10,
+ Top_Center = 11,
+ Top_Front_Left = 12,
+ Top_Front_Center = 13,
+ Top_Front_Right = 14,
+ Top_Back_Left = 15,
+ Top_Back_Center = 16,
+ Top_Back_Right = 17,
+}
+Speaker_Set :: bit_set[Speakers; u32]
+
+VERBOSE :: false
+BUFFER_SIZE :: 1<<18
+
+/*
+Reads in the wav file metadata, without loading the sound data into ram.
+*/
+read :: proc(path : string, allocator := context.allocator) -> (Wav, bool) #optional_ok {
+ file : Wav
+ file.path = path
+ file.take = -1
+ file.tc_framerate = -1
+ file.take = -1
+
+ load_err : os.Error
+ file.handle, load_err = os.open(path)
+ defer os.close(file.handle)
+ defer file.handle = nil
+ if load_err != nil {
+ fmt.eprintfln("ERROR %v: Unable to load file \"%v\"", load_err, path)
+ return {}, false
+ }
+
+ temp_buf := make([]u8, BUFFER_SIZE)
+ defer delete(temp_buf)
+ temp_bext : []u8
+ temp_ixml : string
+
+ os.read(file.handle, temp_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
+ head += 4
+
+ // Size
+ file.reported_size = int(little_endian_u32(temp_buf[head:head+4]))
+ when VERBOSE do fmt.println("Reported size:", file.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
+ head += 4
+
+ when VERBOSE do fmt.println("\nChunks:\n")
+
+ // Looping through chunks
+ null_chunks := 0
+ for _ in 0..<BUFFER_SIZE {
+ chunk_id_raw := temp_buf[head:head+4]
+ chunk_id := string(chunk_id_raw)
+ head += 4
+ chunk_size := int(little_endian_u32(temp_buf[head:head+4]))
+ head += 4
+ when VERBOSE do fmt.println(chunk_id, chunk_size,"\n-------------------------------------")
+ current_chunk_start := head
+ next_chunk_start := head + chunk_size
+
+ data_reached := false
+ if chunk_id == "data" {
+ file.data_size = chunk_size
+ file.data_start = head
+ 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)
+ switch chunk_id {
+ case "iXML":
+ temp_ixml = string(temp_buf[head:data_end])
+ //print_data = temp_ixml
+ null_chunks = 0
+ case "bext":
+ temp_bext = temp_buf[head:data_end]
+ //print_data = string(temp_bext)
+ null_chunks = 0
+ case "fmt ":
+ raw_format_field := little_endian_u16(temp_buf[head:])
+ wave_format_extensible := false
+ switch raw_format_field {
+ case 1..=3:
+ file.format = Audio_Format(raw_format_field)
+ when VERBOSE do fmt.println("Format:", file.format)
+ case 65534:
+ wave_format_extensible = true
+ when VERBOSE do fmt.println("Format: wave_format_extensible")
+ }
+ head += 2
+ file.channels = int(little_endian_u16(temp_buf[head:]))
+ when VERBOSE do fmt.println("Channels:", file.channels)
+ head += 2
+ file.sample_rate = int(little_endian_u32(temp_buf[head:]))
+ when VERBOSE do fmt.println("Sample rate:", file.sample_rate)
+ head += 4
+
+ // Skipping byte rate and block align.
+ // These two legacy fields are only ever redundant or wrong,
+ // and are implied by the other fields.
+ head += 4 + 2
+
+ if !wave_format_extensible {
+ file.bit_depth = int(little_endian_u16(temp_buf[head:]))
+ file.sample_size = file.bit_depth
+ when VERBOSE do fmt.println("Bit depth:", file.bit_depth)
+ head += 2
+ } else if chunk_size>=40 {
+
+ // This is what would normally be the bit depth field
+ // But in the wave_format_extensible case, it essentailly tells you the
+ // stride between each sample, aka "sample size" in this lib's API.
+ bits_pr_sample := int(little_endian_u16(temp_buf[head:]))
+ head += 2
+
+ when VERBOSE do fmt.println(" - wave_format_extensible -")
+ when VERBOSE do fmt.println("Size of extension:", little_endian_u16(temp_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:]))
+ head += 2
+
+ // Channel mask (Which speaker channels are in the file)
+ file.channel_mask = transmute(Speaker_Set)little_endian_u32(temp_buf[head:])
+ when VERBOSE do fmt.println("Channel Mask (RAW):", temp_buf[head:head+4])
+ when VERBOSE do fmt.println("Channel Mask (Speakers):", file.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])
+ file.format = Audio_Format(little_endian_u16(temp_buf[head:]))
+ when VERBOSE do fmt.println("Format:", file.format)
+ head += 2
+ // Other GUID stuff
+ head += 14
+
+
+ if file.format == .ADPCM {
+ // TODO: Support ADPCM (Very low priority)
+ when VERBOSE do fmt.println("Samples pr chunk (In bit-depth field because it's unsupported):", file.bit_depth)
+ } else {
+ if valid_bits_pr_sample < bits_pr_sample {
+ when VERBOSE do fmt.printfln("Warning: Bad metadata. valid_bits_pr_sample ({}) < bits_pr_sample ({}). Ignoring valid_bits_pr_sample. Using bits_pr_sample for bit_depth and sample_size.", valid_bits_pr_sample, bits_pr_sample)
+ file.bit_depth = bits_pr_sample
+ file.sample_size = bits_pr_sample
+ when VERBOSE do fmt.println("Bit depth & sample size:", bits_pr_sample)
+ } else {
+ // This is what is supposed to happen with wave_format_extensible
+ file.bit_depth = valid_bits_pr_sample
+ file.sample_size = bits_pr_sample
+ when VERBOSE do fmt.println("Bit depth:", file.bit_depth)
+ when VERBOSE do fmt.println("Sample size/stride:", file.sample_size)
+ }
+ }
+ }
+
+ if file.format == .ADPCM {
+ fmt.eprintln("WARNING! ADPCM file submitted. This is NOT SUPPORTED.")
+ }
+
+ head = data_end
+ null_chunks = 0
+ case "\x00\x00\x00\x00":
+ if chunk_size == 0 {
+ //null_chunks += 1
+ }
+ }
+ when VERBOSE do fmt.println(print_data, "\n")
+ } else {
+ when VERBOSE do fmt.println("End of buffer reached.")
+ break
+ }
+
+
+ head = next_chunk_start
+
+ if null_chunks > 3 {
+ when VERBOSE do fmt.println("Got more than 3 null chunks in a row. Quitting parse.")
+ break
+ }
+ if data_reached {
+ when VERBOSE do fmt.println("Data reached.")
+ }
+ }
+
+ file.channel_names = make([]string, file.channels, allocator=allocator)
+ file.audio = make([][]f32, file.channels, allocator=allocator)
+
+
+ // iXML Parsing
+ if temp_ixml != "" {
+
+ // Stripping null padding
+ end := len(temp_ixml) - 1
+ for end >= 0 && temp_ixml[end] == 0 {
+ end -= 1
+ }
+ temp_ixml = temp_ixml[:end]
+
+
+ naming_channel := 0
+ /*
+ interleave_set is here because we don't want to overwrite naming_channel
+ with a number from CHANNEL_INDEX, if we've already set it with INTERLEAVE_INDEX.
+ INTERLEAVE_INDEX is the number that actually tells you which channel the name
+ belongs to. CHANNEL_INDEX is to be treated as a backup.
+ */
+ interleave_set := false
+
+ xml_recurse :: proc(doc: ^xml.Document, element_id: xml.Element_ID, file: ^Wav, naming_channel: ^int, interleave_set: ^bool, allocator:type_of(context.allocator), indent := 0) {
+
+ naming_channel := naming_channel
+ interleave_set := interleave_set
+
+
+ tab :: proc(indent: int) {
+ for _ in 0..=indent {
+ fmt.printf("\t")
+ }
+ }
+
+ when VERBOSE do fmt.printf("\n")
+ when VERBOSE do tab(indent)
+
+ element := doc.elements[element_id]
+
+ if element.kind == .Element {
+ when VERBOSE do fmt.printf("<%v>", element.ident)
+
+ if len(element.value) > 0 {
+ value := element.value[0]
+ switch element.ident {
+ case "TRACK":
+ interleave_set^ = false
+ case "CHANNEL_INDEX":
+ if !interleave_set^ {
+ switch v in value {
+ case string:
+ naming_channel^, _ = strconv.parse_int(v)
+ case u32:
+ naming_channel^ = int(v)
+ }
+ }
+ naming_channel^ -= 1
+ case "INTERLEAVE_INDEX":
+ interleave_set^ = true
+ switch v in value {
+ case string:
+ naming_channel^, _ = strconv.parse_int(v)
+ case u32:
+ naming_channel^ = int(v)
+ }
+ naming_channel^ -= 1
+ case "NAME":
+ #partial switch v in value {
+ case string:
+ file.channel_names[naming_channel^] = strings.clone(v, allocator=allocator)
+ }
+ case "UBITS":
+ #partial switch v in value {
+ case string:
+ for r, i in v {
+ file.ubits[i] = u8(r) - u8('0')
+ }
+ }
+ case "TAKE":
+ #partial switch v in value {
+ case string:
+ file.take, _ = strconv.parse_int(v)
+ }
+ case "SCENE":
+ #partial switch v in value {
+ case string:
+ file.scene = strings.clone(v, allocator=allocator)
+ }
+ case "PROJECT":
+ #partial switch v in value {
+ case string:
+ file.project = strings.clone(v, allocator=allocator)
+ }
+ case "TAPE":
+ #partial switch v in value {
+ case string:
+ file.tape = strings.clone(v, allocator=allocator)
+ }
+ case "CIRCLED":
+ #partial switch v in value {
+ case string:
+ file.circled = v == "TRUE"
+ }
+ case "TIMECODE_FLAG":
+ #partial switch v in value {
+ case string:
+ file.tc_dropframe = v != "NDF"
+ }
+ case "TIMECODE_RATE":
+ #partial switch v in value {
+ case string:
+ end : int
+ for r, i in v {
+ if r == '/' {
+ end = i
+ break
+ }
+ }
+ file.tc_framerate, _ = strconv.parse_f32(v[:end])
+ }
+ case "NOTE":
+ #partial switch v in value {
+ case string:
+ if v != "" {
+ file.note = strings.clone(v, allocator=allocator)
+ }
+ }
+ }
+ }
+
+ for value in element.value {
+ switch v in value {
+ case string:
+ when VERBOSE do fmt.printf(": %v", v)
+ case xml.Element_ID:
+ xml_recurse(doc, v, file, 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)
+ }
+ } else if element.kind == .Comment {
+ when VERBOSE do fmt.printf("[COMMENT] %v\n", element.value)
+ }
+
+ return
+ }
+
+
+ parsed_ixml : ^xml.Document
+ prev_alloc := context.allocator
+ defer context.allocator = prev_alloc
+ context.allocator = context.temp_allocator
+ parsed_ixml, _ = xml.parse(temp_ixml, xml.Options{
+ flags={.Ignore_Unsupported},
+ expected_doctype = "",
+ })
+ xml_recurse(parsed_ixml, 0, &file, &naming_channel, &interleave_set, allocator)
+ }
+
+ // bext parsing
+ if len(temp_bext) > 0 {
+
+ // Stripping null padding
+ /*end := len(temp_bext) - 1
+ for end >= 0 && temp_bext[end] == 0 {
+ end -= 1
+ }
+ temp_bext = temp_bext[:end]*/
+
+ naming_channel := 0
+
+ description := string(temp_bext[:256])
+ for line in strings.split_lines_iterator(&description) {
+ if len(line)<1 do continue
+ if file.channel_names[naming_channel] == "" &&
+ (strings.starts_with(line, "sTRK") || strings.starts_with(line, "zTRK")) {
+ eq_index := strings.index(line, "=")
+ file.channel_names[naming_channel] = strings.clone(line[eq_index+1:], allocator=allocator)
+ naming_channel += 1
+ }
+ if strings.starts_with(line[1:], "TAKE=") {
+ file.take, _ = strconv.parse_int(line[6:])
+ }
+ if strings.starts_with(line[1:], "CIRCLED=") {
+ file.circled = line[9:] == "TRUE"
+ }
+ if strings.starts_with(line[1:], "SPEED=") {
+ value := line[7:]
+ num_end : int
+ type_start : int
+ for r, i in value {
+ if !strings.contains_rune("1234567890.", r) && num_end == 0 {
+ num_end = i
+ }
+ if r=='N' || r=='D' {
+ type_start = i
+ break
+ }
+ }
+ file.tc_framerate, _ = strconv.parse_f32(value[:num_end])
+ file.tc_dropframe = value[type_start:] != "ND"
+
+ }
+ // Only if ixml doesn't exist, so we don't allocate the note string twice.
+ if file.note == "" {
+ if strings.starts_with(line[1:], "NOTE=") {
+ v := line[6:]
+ if v != "" {
+ file.note = strings.clone(v, allocator=allocator)
+ }
+ }
+ }
+ }
+ head := 0
+ when VERBOSE do fmt.printf("Description: \n%v\n", string(temp_bext[head:256]))
+ head += 256
+ when VERBOSE do fmt.printf("Originator: %v\n", string(temp_bext[head:head+32]))
+ head += 32
+ when VERBOSE do fmt.printf("Originator Reference: %v\n", string(temp_bext[head:head+32]))
+ head += 32
+ date := string(temp_bext[head:head+10])
+ when VERBOSE do fmt.printf("Origination Date: %v\n", date)
+ date_splits := strings.split(date, "-")
+ file.date.year, _ = strconv.parse_int(date_splits[0])
+ file.date.month, _ = strconv.parse_int(date_splits[1])
+ file.date.day, _ = strconv.parse_int(date_splits[2])
+ delete(date_splits)
+ head += 10
+ when VERBOSE do fmt.printf("Origination Time: %v\n", string(temp_bext[head:head+8]))
+ head += 8
+
+ file.samples_since_midnight = int(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)
+ head += 8
+
+ when VERBOSE do fmt.printf("Version: %v\n", little_endian_u16(temp_bext[head:head+2]))
+ head += 2
+ when VERBOSE do fmt.printf("UMID Skipped.\n")
+ head += 64
+ when VERBOSE do fmt.printf("Skipped reserved nothingness.\n")
+ head += 190
+ when VERBOSE do fmt.printf("Coding history:\n%v\n", string(temp_bext[head:]))
+ }
+ when VERBOSE do fmt.printfln("%#v", file)
+ when VERBOSE do fmt.println()
+
+ // just here to make some printing prettier
+ temp_bext = nil
+
+ return file, true
+}
+
+
+
+/*
+Fills the list of buffer 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).
+Make sure you've run open_handle() on the Wav first, and run close_handle() afterwards.
+*/
+load_at :: proc(file : ^Wav, start_index : int, output_bufs : [][]f32, work_buf : []u8, channels : []int = {}) -> (samples_loaded : int = 0) {
+ channels := channels
+ all_channels := false
+ if len(channels) == 0 {
+ all_channels = true
+ channels = make([]int, file.channels)
+ for c, i in channels {
+ channels[i] = i
+ }
+ }
+ err_quit := false
+ for c in channels {
+ if c >= file.channels {
+ fmt.eprintfln("ERROR: Requested higher channel ({}) to be loaded than exists in file ({})!", c, file.channels)
+ }
+ }
+ 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))
+ err_quit = true
+ }
+ samples_pr_channel := len(output_bufs[0])
+ for buf, i in output_bufs {
+ if len(buf) != samples_pr_channel {
+ fmt.eprintfln("ERROR: Uneven number of samples in output buffers! len([0])={} versus len([{}])={}!", samples_pr_channel, i, len(buf))
+ err_quit = true
+ }
+ }
+ if err_quit do return samples_loaded
+
+ // The main work part
+ file_length := get_sample_count(file^)
+
+ // Cropping work_buf to make sure it aligns with the sample_size
+ bytes_pr_sample := file.sample_size/8
+ samples_in_buffer := int(len(work_buf)/bytes_pr_sample/file.channels)
+ buffer_bytes := i64(samples_in_buffer*bytes_pr_sample*file.channels)
+ work_buf := work_buf[:buffer_bytes]
+
+ decode := decode_24
+ switch file.format {
+ case .INT:
+ switch file.bit_depth {
+ case 16:
+ decode = decode_16
+ case 24:
+ decode = decode_24
+ case 32:
+ decode = decode_32
+ }
+ case .FLOAT:
+ switch file.bit_depth {
+ case 32:
+ decode = decode_f32
+ }
+ case .ADPCM:
+ fmt.eprintln("ERROR: ADPCM is not supported!")
+ return samples_loaded
+ }
+
+ head : i64 = i64(file.data_start) + i64(start_index * file.channels * bytes_pr_sample)
+ absolute_sample : u64 = u64(start_index)
+ relative_sample : int = 0
+ for {
+ os.read_at(file.handle, work_buf, head)
+ for local_sample in 0..<samples_in_buffer {
+ for c, i in channels {
+ offset := (local_sample*file.channels+c)*bytes_pr_sample
+ output_bufs[i][relative_sample] = decode(work_buf[offset:])
+ }
+ relative_sample += 1
+ absolute_sample += 1
+ samples_loaded += 1 // TODO: BUG: This is broken. It returns 1 when finished and processing zero samples.
+ if relative_sample >= samples_pr_channel || absolute_sample >= u64(file_length) {
+ break
+ }
+ }
+ if relative_sample >= samples_pr_channel || absolute_sample >= u64(file_length) {
+ break
+ }
+ head += buffer_bytes
+ }
+
+ if all_channels do delete(channels)
+ return samples_loaded
+}
+
+/*
+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 opens and closes the file handle on its own.
+Don't call open_handle() and close_handle().
+*/
+load :: proc(file : ^Wav, channels : []int = {}, allocator:=context.allocator) {
+ channels := channels
+ all_channels := false
+ if len(channels) == 0 {
+ all_channels = true
+ channels = make([]int, file.channels)
+ for c, i in channels {
+ channels[i] = i
+ }
+ }
+ err_quit := false
+ for c in channels {
+ if c >= file.channels {
+ fmt.eprintfln("ERROR: Requested higher channel ({}) to be loaded than exists in file ({})!", c, file.channels)
+ }
+ }
+ if err_quit do return
+
+
+ // The main work part
+ length := get_sample_count(file^)
+
+ for c in channels {
+ file.audio[c] = make([]f32, length, allocator=allocator)
+ }
+
+ samples_in_buffer :: 1024
+ bytes_pr_sample := file.sample_size/8
+ buffer_bytes : i64 = i64(bytes_pr_sample*file.channels*samples_in_buffer)
+ buffer := make([]u8, buffer_bytes)
+
+ decode := decode_24
+ switch file.format {
+ case .INT:
+ switch file.bit_depth {
+ case 16:
+ decode = decode_16
+ case 24:
+ decode = decode_24
+ case 32:
+ decode = decode_32
+ }
+ case .FLOAT:
+ switch file.bit_depth {
+ case 32:
+ decode = decode_f32
+ }
+ case .ADPCM:
+ fmt.eprintln("ERROR: ADPCM is not supported!")
+ return
+ }
+
+ head : i64 = i64(file.data_start)
+ absolute_sample : u64 = 0
+ file.handle, _ = os.open(file.path)
+ for {
+ os.read_at(file.handle, buffer, head)
+ //fmt.println(buffer)
+ for local_sample in 0..<samples_in_buffer {
+ for c in channels {
+ offset := (local_sample*file.channels+c)*bytes_pr_sample
+ file.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
+ }
+ os.close(file.handle)
+ delete(buffer)
+
+
+ if all_channels do delete(channels)
+ return
+}
+tprint_timecode :: proc(file : Wav) -> string {
+ return print_timecode(file, allocator=context.temp_allocator)
+}
+print_timecode :: proc(file : Wav, allocator := context.allocator) -> string {
+ tc := get_timecode(file)
+ return fmt.aprintf("%02d:%02d:%02d:%02d", tc.hour, tc.minute, tc.second, tc.frame,
+ allocator=allocator)
+}
+get_timecode :: proc(file : Wav) -> (output:Timecode) {
+ seconds_since_midnight := file.samples_since_midnight / file.sample_rate
+ output.hour = int( seconds_since_midnight / 3600)
+ output.minute = int((seconds_since_midnight % 3600) / 60)
+ output.second = int( seconds_since_midnight % 60)
+ output.frame = int( math.round(f64(file.samples_since_midnight % file.sample_rate ) * f64(file.tc_framerate) / f64(file.sample_rate)))
+ return
+}
+
+tprint_duration :: proc(file : Wav) -> string {
+ return print_duration(file, allocator=context.temp_allocator)
+}
+print_duration :: proc(file : Wav, allocator := context.allocator) -> string {
+ total_seconds := int(math.round(get_duration(file)))
+ 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)
+}
+/* Returns duration in seconds */
+get_duration :: proc(file : Wav) -> f64 {
+ return f64(get_sample_count(file))/f64(file.sample_rate)
+}
+get_sample_count :: proc(file : Wav) -> int {
+ return file.data_size/file.channels/(file.sample_size/8)
+}
+
+open_handle :: proc(w : ^Wav) -> bool {
+ handle, ok := os.open(w.path)
+ w.handle = handle
+ return ok == nil
+}
+close_handle :: proc(w : ^Wav) {
+ os.close(w.handle)
+ w.handle = nil
+}
+
+wave_print :: proc(wave : []f32) {
+ for sample, i in wave {
+ fmt.printf("[%012d] ", i)
+ bar_print(sample)
+ fmt.printf(" %+01.04f\n", sample)
+ }
+}
+bar_print :: proc(x : f32, one_side_width : int = 64, sign := '#', square := true) {
+ x := x
+ positive := x>0
+ if square {
+ x = math.sqrt(abs(x))
+ if !positive do x *= -1
+ }
+ bar_length := int(math.round(abs(min(x, 1))*f32(one_side_width)))
+ spaces := one_side_width-bar_length
+ if positive {
+ for _ in 0..<one_side_width {
+ fmt.print(" ")
+ }
+ fmt.print("|")
+ for _ in 0..<bar_length {
+ fmt.print(sign)
+ }
+ for _ in 0..<spaces {
+ fmt.print(" ")
+ }
+ } else {
+ for _ in 0..<spaces {
+ fmt.print(" ")
+ }
+ for _ in 0..<bar_length {
+ fmt.print(sign)
+ }
+ fmt.print("|")
+ for _ in 0..<one_side_width {
+ fmt.print(" ")
+ }
+ }
+}
+
+
+allocate_appropriate_work_buffer :: proc(file : Wav, sample_count : int, allocator:=context.allocator) -> []u8 {
+ // Bytes pr sample
+ size := (file.sample_size/8) * sample_count * file.channels
+ return make([]u8, size)
+}
+
+
+decode_f32 :: proc(x : []u8) -> f32 {
+ return (^f32)(&x[0])^
+}
+decode_32 :: proc(x : []u8) -> f32 {
+ integer := i32(x[0]) |
+ i32(x[1]) << 8 |
+ i32(x[2]) << 16 |
+ i32(x[3]) << 24
+ return f32(integer) / f32(1 << 31)
+}
+decode_24 :: proc(x : []u8) -> f32 {
+ integer := i32(x[0]) |
+ i32(x[1]) << 8 |
+ i32(x[2]) << 16
+
+ // Cheeky sign extension
+ integer = (integer << 8) >> 8
+
+ return f32(integer) / f32(1 << 23)
+}
+decode_16 :: proc(x : []u8) -> f32 {
+ integer := i16(x[0]) |
+ i16(x[1]) << 8
+ return f32(integer) / f32(1 << 15)
+}
+
+little_endian_u64 :: proc(x : []u8) -> u64 {
+ return u64(x[0]) |
+ u64(x[1]) << 8 |
+ u64(x[2]) << 16 |
+ u64(x[3]) << 24 |
+ u64(x[4]) << 32 |
+ u64(x[5]) << 40 |
+ u64(x[6]) << 48 |
+ u64(x[7]) << 56
+}
+
+little_endian_u32 :: proc(x : []u8) -> u32 {
+ return u32(x[0]) |
+ u32(x[1]) << 8 |
+ u32(x[2]) << 16 |
+ u32(x[3]) << 24
+}
+
+little_endian_u16 :: proc(x : []u8) -> u16 {
+ return u16(x[0]) |
+ u16(x[1]) << 8
+} \ No newline at end of file
diff --git a/wav/xml/debug_print.odin b/wav/xml/debug_print.odin
new file mode 100644
index 0000000..9c47e79
--- /dev/null
+++ b/wav/xml/debug_print.odin
@@ -0,0 +1,86 @@
+package encoding_xml
+
+/*
+ An XML 1.0 / 1.1 parser
+
+ Copyright 2021-2022 Jeroen van Rijn <nom@duclavier.com>.
+ Made available under Odin's license.
+
+ A from-scratch XML implementation, loosely modeled on the [spec](https://www.w3.org/TR/2006/REC-xml11-20060816).
+
+ List of contributors:
+ Jeroen van Rijn: Initial implementation.
+*/
+
+
+import "core:io"
+import "core:fmt"
+
+/*
+ Just for debug purposes.
+*/
+print :: proc(writer: io.Writer, doc: ^Document) -> (written: int, err: io.Error) {
+ if doc == nil { return }
+ written += fmt.wprintf(writer, "[XML Prolog]\n")
+
+ for attr in doc.prologue {
+ written += fmt.wprintf(writer, "\t%v: %v\n", attr.key, attr.val)
+ }
+
+ written += fmt.wprintf(writer, "[Encoding] %v\n", doc.encoding)
+
+ if len(doc.doctype.ident) > 0 {
+ written += fmt.wprintf(writer, "[DOCTYPE] %v\n", doc.doctype.ident)
+
+ if len(doc.doctype.rest) > 0 {
+ fmt.wprintf(writer, "\t%v\n", doc.doctype.rest)
+ }
+ }
+
+ for comment in doc.comments {
+ written += fmt.wprintf(writer, "[Pre-root comment] %v\n", comment)
+ }
+
+ if len(doc.elements) > 0 {
+ fmt.wprintln(writer, " --- ")
+ print_element(writer, doc, 0)
+ fmt.wprintln(writer, " --- ")
+ }
+
+ return written, .None
+}
+
+print_element :: proc(writer: io.Writer, doc: ^Document, element_id: Element_ID, indent := 0) -> (written: int, err: io.Error) {
+ tab :: proc(writer: io.Writer, indent: int) {
+ for _ in 0..=indent {
+ fmt.wprintf(writer, "\t")
+ }
+ }
+
+ tab(writer, indent)
+
+ element := doc.elements[element_id]
+
+ if element.kind == .Element {
+ fmt.wprintf(writer, "<%v>\n", element.ident)
+
+ for value in element.value {
+ switch v in value {
+ case string:
+ tab(writer, indent + 1)
+ fmt.wprintf(writer, "[Value] %v\n", v)
+ case Element_ID:
+ print_element(writer, doc, v, indent + 1)
+ }
+ }
+
+ for attr in element.attribs {
+ tab(writer, indent + 1)
+ fmt.wprintf(writer, "[Attr] %v: %v\n", attr.key, attr.val)
+ }
+ } else if element.kind == .Comment {
+ fmt.wprintf(writer, "[COMMENT] %v\n", element.value)
+ }
+
+ return written, .None
+}
diff --git a/wav/xml/doc.odin b/wav/xml/doc.odin
new file mode 100644
index 0000000..9030cd4
--- /dev/null
+++ b/wav/xml/doc.odin
@@ -0,0 +1,23 @@
+/*
+A parser for a useful subset of the `XML` specification.
+
+A from-scratch `XML` implementation, loosely modelled on the [[ spec; https://www.w3.org/TR/2006/REC-xml11-20060816 ]].
+
+Features:
+- Supports enough of the XML 1.0/1.1 spec to handle the 99.9% of XML documents in common current usage.
+- Simple to understand and use. Small.
+
+Caveats:
+- We do NOT support HTML in this package, as that may or may not be valid XML.
+ If it works, great. If it doesn't, that's not considered a bug.
+
+- We do NOT support `UTF-16`. If you have a `UTF-16` XML file, please convert it to `UTF-8` first. Also, our condolences.
+- `<[!ELEMENT` and `<[!ATTLIST` are not supported, and will be either ignored or return an error depending on the parser options.
+
+MAYBE:
+- XML writer?
+- Serialize/deserialize Odin types?
+
+For a full example, see: [[ core/encoding/xml/example; https://github.com/odin-lang/Odin/tree/master/core/encoding/xml/example ]]
+*/
+package encoding_xml
diff --git a/wav/xml/helpers.odin b/wav/xml/helpers.odin
new file mode 100644
index 0000000..79f2d72
--- /dev/null
+++ b/wav/xml/helpers.odin
@@ -0,0 +1,52 @@
+package encoding_xml
+
+/*
+ An XML 1.0 / 1.1 parser
+
+ Copyright 2021-2022 Jeroen van Rijn <nom@duclavier.com>.
+ Made available under Odin's license.
+
+ This file contains helper functions.
+*/
+
+
+// Find parent's nth child with a given ident.
+find_child_by_ident :: proc(doc: ^Document, parent_id: Element_ID, ident: string, nth := 0) -> (res: Element_ID, found: bool) {
+ tag := doc.elements[parent_id]
+
+ count := 0
+ for v in tag.value {
+ switch child_id in v {
+ case string: continue
+ case Element_ID:
+ child := doc.elements[child_id]
+ /*
+ Skip commments. They have no name.
+ */
+ if child.kind != .Element { continue }
+
+ /*
+ If the ident matches and it's the nth such child, return it.
+ */
+ if child.ident == ident {
+ if count == nth { return child_id, true }
+ count += 1
+ }
+ }
+
+ }
+ return 0, false
+}
+
+// Find an attribute by key.
+find_attribute_val_by_key :: proc(doc: ^Document, parent_id: Element_ID, key: string) -> (val: string, found: bool) {
+ tag := doc.elements[parent_id]
+
+ for attr in tag.attribs {
+ /*
+ If the ident matches, we're done. There can only ever be one attribute with the same name.
+ */
+ if attr.key == key { return attr.val, true }
+ }
+ return "", false
+}
diff --git a/wav/xml/tokenizer.odin b/wav/xml/tokenizer.odin
new file mode 100644
index 0000000..f4c9c8a
--- /dev/null
+++ b/wav/xml/tokenizer.odin
@@ -0,0 +1,415 @@
+package encoding_xml
+
+/*
+ An XML 1.0 / 1.1 parser
+
+ Copyright 2021-2022 Jeroen van Rijn <nom@duclavier.com>.
+ Made available under Odin's license.
+
+ A from-scratch XML implementation, loosely modeled on the [spec](https://www.w3.org/TR/2006/REC-xml11-20060816).
+
+ List of contributors:
+ Jeroen van Rijn: Initial implementation.
+*/
+
+
+import "core:fmt"
+import "core:unicode"
+import "core:unicode/utf8"
+import "core:strings"
+
+Error_Handler :: #type proc(pos: Pos, fmt: string, args: ..any)
+
+Token :: struct {
+ kind: Token_Kind,
+ text: string,
+ pos: Pos,
+}
+
+Pos :: struct {
+ file: string,
+ offset: int, // starting at 0
+ line: int, // starting at 1
+ column: int, // starting at 1
+}
+
+Token_Kind :: enum {
+ Invalid,
+
+ Ident,
+ Literal,
+ Rune,
+ String,
+
+ Double_Quote, // "
+ Single_Quote, // '
+ Colon, // :
+
+ Eq, // =
+ Lt, // <
+ Gt, // >
+ Exclaim, // !
+ Question, // ?
+ Hash, // #
+ Slash, // /
+ Dash, // -
+
+ Open_Bracket, // [
+ Close_Bracket, // ]
+
+ EOF,
+}
+
+CDATA_START :: "<![CDATA["
+CDATA_END :: "]]>"
+
+COMMENT_START :: "<!--"
+COMMENT_END :: "-->"
+
+Tokenizer :: struct {
+ // Immutable data
+ path: string,
+ src: string,
+ err: Error_Handler,
+
+ // Tokenizing state
+ ch: rune,
+ offset: int,
+ read_offset: int,
+ line_offset: int,
+ line_count: int,
+
+ // Mutable data
+ error_count: int,
+}
+
+init :: proc(t: ^Tokenizer, src: string, path: string, err: Error_Handler = default_error_handler) {
+ t.src = src
+ t.err = err
+ t.ch = ' '
+ t.offset = 0
+ t.read_offset = 0
+ t.line_offset = 0
+ t.line_count = len(src) > 0 ? 1 : 0
+ t.error_count = 0
+ t.path = path
+
+ advance_rune(t)
+ if t.ch == utf8.RUNE_BOM {
+ advance_rune(t)
+ }
+}
+
+@(private)
+offset_to_pos :: proc(t: ^Tokenizer, offset: int) -> Pos {
+ line := t.line_count
+ column := offset - t.line_offset + 1
+
+ return Pos {
+ file = t.path,
+ offset = offset,
+ line = line,
+ column = column,
+ }
+}
+
+default_error_handler :: proc(pos: Pos, msg: string, args: ..any) {
+ fmt.eprintf("%s(%d:%d) ", pos.file, pos.line, pos.column)
+ fmt.eprintf(msg, ..args)
+ fmt.eprintf("\n")
+}
+
+error :: proc(t: ^Tokenizer, offset: int, msg: string, args: ..any) {
+ pos := offset_to_pos(t, offset)
+ if t.err != nil {
+ t.err(pos=pos, fmt=msg, args=args)
+ }
+ t.error_count += 1
+}
+
+@(optimization_mode="favor_size")
+advance_rune :: proc(t: ^Tokenizer) {
+ #no_bounds_check {
+ /*
+ Already bounds-checked here.
+ */
+ if t.read_offset < len(t.src) {
+ t.offset = t.read_offset
+ if t.ch == '\n' {
+ t.line_offset = t.offset
+ t.line_count += 1
+ }
+ r, w := rune(t.src[t.read_offset]), 1
+ switch {
+ case r == 0:
+ //error(t, t.offset, "illegal character NUL")
+ case r >= utf8.RUNE_SELF:
+ r, w = #force_inline utf8.decode_rune_in_string(t.src[t.read_offset:])
+ if r == utf8.RUNE_ERROR && w == 1 {
+ //error(t, t.offset, "illegal UTF-8 encoding")
+ } else if r == utf8.RUNE_BOM && t.offset > 0 {
+ //error(t, t.offset, "illegal byte order mark")
+ }
+ }
+ t.read_offset += w
+ t.ch = r
+ } else {
+ t.offset = len(t.src)
+ if t.ch == '\n' {
+ t.line_offset = t.offset
+ t.line_count += 1
+ }
+ t.ch = -1
+ }
+ }
+}
+
+peek_byte :: proc(t: ^Tokenizer, offset := 0) -> byte {
+ if t.read_offset+offset < len(t.src) {
+ #no_bounds_check return t.src[t.read_offset+offset]
+ }
+ return 0
+}
+
+@(optimization_mode="favor_size")
+skip_whitespace :: proc(t: ^Tokenizer) {
+ for {
+ switch t.ch {
+ case ' ', '\t', '\r', '\n':
+ advance_rune(t)
+ case:
+ return
+ }
+ }
+}
+
+@(optimization_mode="favor_size")
+is_letter :: proc(r: rune) -> bool {
+ if r < utf8.RUNE_SELF {
+ switch r {
+ case '_':
+ return true
+ case 'A'..='Z', 'a'..='z':
+ return true
+ }
+ }
+ return unicode.is_letter(r)
+}
+
+is_valid_identifier_rune :: proc(r: rune) -> bool {
+ if r < utf8.RUNE_SELF {
+ switch r {
+ case '_', '-', ':': return true
+ case 'A'..='Z', 'a'..='z': return true
+ case '0'..='9': return true
+ case -1: return false
+ }
+ }
+
+ if unicode.is_letter(r) || unicode.is_digit(r) {
+ return true
+ }
+ return false
+}
+
+scan_identifier :: proc(t: ^Tokenizer) -> string {
+ offset := t.offset
+ namespaced := false
+
+ for is_valid_identifier_rune(t.ch) {
+ advance_rune(t)
+ if t.ch == ':' {
+ // A namespaced attr can have at most two parts, `namespace:ident`.
+ if namespaced {
+ break
+ }
+ namespaced = true
+ }
+ }
+ return string(t.src[offset : t.offset])
+}
+
+/*
+ A comment ends when we see -->, preceded by a character that's not a dash.
+ "For compatibility, the string "--" (double-hyphen) must not occur within comments."
+
+ See: https://www.w3.org/TR/2006/REC-xml11-20060816/#dt-comment
+
+ Thanks to the length (4) of the comment start, we also have enough lookback,
+ and the peek at the next byte asserts that there's at least one more character
+ that's a `>`.
+*/
+scan_comment :: proc(t: ^Tokenizer) -> (comment: string, err: Error) {
+ offset := t.offset
+
+ for {
+ advance_rune(t)
+ ch := t.ch
+
+ if ch < 0 {
+ //error(t, offset, "[parse] Comment was not terminated\n")
+ return "", .Unclosed_Comment
+ }
+
+ if string(t.src[t.offset - 1:][:2]) == "--" {
+ if peek_byte(t) == '>' {
+ break
+ } else {
+ //error(t, t.offset - 1, "Invalid -- sequence in comment.\n")
+ return "", .Invalid_Sequence_In_Comment
+ }
+ }
+ }
+
+ expect(t, .Dash)
+ expect(t, .Gt)
+
+ return string(t.src[offset : t.offset - 1]), .None
+}
+
+// Skip CDATA
+skip_cdata :: proc(t: ^Tokenizer) -> (err: Error) {
+ if s := string(t.src[t.offset:]); !strings.has_prefix(s, CDATA_START) {
+ return .None
+ }
+
+ t.read_offset += len(CDATA_START)
+ offset := t.offset
+
+ cdata_scan: for {
+ advance_rune(t)
+ if t.ch < 0 {
+ //error(t, offset, "[scan_string] CDATA was not terminated\n")
+ return .Premature_EOF
+ }
+
+ // Scan until the end of a CDATA tag.
+ if s := string(t.src[t.read_offset:]); strings.has_prefix(s, CDATA_END) {
+ t.read_offset += len(CDATA_END)
+ break cdata_scan
+ }
+ }
+ return .None
+}
+
+@(optimization_mode="favor_size")
+scan_string :: proc(t: ^Tokenizer, offset: int, close: rune = '<', consume_close := false, multiline := true) -> (value: string, err: Error) {
+ err = .None
+
+ loop: for {
+ ch := t.ch
+
+ switch ch {
+ case -1:
+ //error(t, t.offset, "[scan_string] Premature end of file.\n")
+ return "", .Premature_EOF
+
+ case '<':
+ if peek_byte(t) == '!' {
+ if peek_byte(t, 1) == '[' {
+ // Might be the start of a CDATA tag.
+ skip_cdata(t) or_return
+ } else if peek_byte(t, 1) == '-' && peek_byte(t, 2) == '-' {
+ // Comment start. Eat comment.
+ t.read_offset += 3
+ _ = scan_comment(t) or_return
+ }
+ }
+
+ case '\n':
+ if !multiline {
+ //error(t, offset, string(t.src[offset : t.offset]))
+ //error(t, offset, "[scan_string] Not terminated\n")
+ err = .Invalid_Tag_Value
+ break loop
+ }
+ }
+
+ if t.ch == close {
+ // If it's not a CDATA or comment, it's the end of this body.
+ break loop
+ }
+ advance_rune(t)
+ }
+
+ // Strip trailing whitespace.
+ lit := string(t.src[offset : t.offset])
+
+ end := len(lit)
+ eat: for ; end > 0; end -= 1 {
+ ch := lit[end - 1]
+ switch ch {
+ case ' ', '\t', '\r', '\n':
+ case:
+ break eat
+ }
+ }
+ lit = lit[:end]
+
+ if consume_close {
+ advance_rune(t)
+ }
+ return lit, err
+}
+
+peek :: proc(t: ^Tokenizer) -> (token: Token) {
+ old := t^
+ token = scan(t)
+ t^ = old
+ return token
+}
+
+scan :: proc(t: ^Tokenizer, multiline_string := false) -> Token {
+ skip_whitespace(t)
+
+ offset := t.offset
+
+ kind: Token_Kind
+ err: Error
+ lit: string
+ pos := offset_to_pos(t, offset)
+
+ switch ch := t.ch; true {
+ case is_letter(ch):
+ lit = scan_identifier(t)
+ kind = .Ident
+
+ case:
+ advance_rune(t)
+ switch ch {
+ case -1:
+ kind = .EOF
+
+ case '<': kind = .Lt
+ case '>': kind = .Gt
+ case '!': kind = .Exclaim
+ case '?': kind = .Question
+ case '=': kind = .Eq
+ case '#': kind = .Hash
+ case '/': kind = .Slash
+ case '-': kind = .Dash
+ case ':': kind = .Colon
+ case '[': kind = .Open_Bracket
+ case ']': kind = .Close_Bracket
+
+ case '"', '\'':
+ kind = .Invalid
+
+ lit, err = scan_string(t, t.offset, ch, true, multiline_string)
+ if err == .None {
+ kind = .String
+ }
+
+ case '\n':
+ lit = "\n"
+
+ case:
+ kind = .Invalid
+ }
+ }
+
+ if kind != .String && lit == "" {
+ lit = string(t.src[offset : t.offset])
+ }
+ return Token{kind, lit, pos}
+}
diff --git a/wav/xml/xml_reader.odin b/wav/xml/xml_reader.odin
new file mode 100644
index 0000000..5fd3dc8
--- /dev/null
+++ b/wav/xml/xml_reader.odin
@@ -0,0 +1,628 @@
+package encoding_xml
+/*
+ An XML 1.0 / 1.1 parser
+
+ 2021-2022 Jeroen van Rijn <nom@duclavier.com>.
+ available under Odin's license.
+
+ List of contributors:
+ - Jeroen van Rijn: Initial implementation.
+*/
+
+import "core:bytes"
+import "core:encoding/entity"
+import "base:intrinsics"
+import "core:mem"
+import "core:os"
+import "core:strings"
+import "base:runtime"
+
+likely :: intrinsics.expect
+
+DEFAULT_OPTIONS :: Options{
+ flags = {.Ignore_Unsupported},
+ expected_doctype = "",
+}
+
+Option_Flag :: enum {
+ // If the caller says that input may be modified, we can perform in-situ parsing.
+ // If this flag isn't provided, the XML parser first duplicates the input so that it can.
+ Input_May_Be_Modified,
+
+ // Document MUST start with `<?xml` prologue.
+ Must_Have_Prolog,
+
+ // Document MUST have a `<!DOCTYPE`.
+ Must_Have_DocType,
+
+ // By default we skip comments. Use this option to intern a comment on a parented Element.
+ Intern_Comments,
+
+ // How to handle unsupported parts of the specification, like <! other than <!DOCTYPE and <![CDATA[
+ Error_on_Unsupported,
+ Ignore_Unsupported,
+
+ // By default CDATA tags are passed-through as-is.
+ // This option unwraps them when encountered.
+ Unbox_CDATA,
+
+ // By default SGML entities like `&gt;`, `&#32;` and `&#x20;` are passed-through as-is.
+ // This option decodes them when encountered.
+ Decode_SGML_Entities,
+
+ // If a tag body has a comment, it will be stripped unless this option is given.
+ Keep_Tag_Body_Comments,
+}
+Option_Flags :: bit_set[Option_Flag; u16]
+
+Document :: struct {
+ elements: [dynamic]Element `fmt:"v,element_count"`,
+ element_count: Element_ID,
+
+ prologue: Attributes,
+ encoding: Encoding,
+
+ doctype: struct {
+ // We only scan the <!DOCTYPE IDENT part and skip the rest.
+ ident: string,
+ rest: string,
+ },
+
+ // If we encounter comments before the root node, and the option to intern comments is given, this is where they'll live.
+ // Otherwise they'll be in the element tree.
+ comments: [dynamic]string `fmt:"-"`,
+
+ // Internal
+ tokenizer: ^Tokenizer `fmt:"-"`,
+ allocator: mem.Allocator `fmt:"-"`,
+
+ // Input. Either the original buffer, or a copy if `.Input_May_Be_Modified` isn't specified.
+ input: []u8 `fmt:"-"`,
+ strings_to_free: [dynamic]string `fmt:"-"`,
+}
+
+Element :: struct {
+ ident: string,
+ value: [dynamic]Value,
+ attribs: Attributes,
+
+ kind: enum {
+ Element = 0,
+ Comment,
+ },
+ parent: Element_ID,
+}
+
+Value :: union {
+ string,
+ Element_ID,
+}
+
+Attribute :: struct {
+ key: string,
+ val: string,
+}
+
+Attributes :: [dynamic]Attribute
+
+Options :: struct {
+ flags: Option_Flags,
+ expected_doctype: string,
+}
+
+Encoding :: enum {
+ Unknown,
+
+ UTF_8,
+ ISO_8859_1,
+
+ // Aliases
+ LATIN_1 = ISO_8859_1,
+}
+
+Error :: enum {
+ // General return values.
+ None = 0,
+ General_Error,
+ Unexpected_Token,
+ Invalid_Token,
+
+ // Couldn't find, open or read file.
+ File_Error,
+
+ // File too short.
+ Premature_EOF,
+
+ // XML-specific errors.
+ No_Prolog,
+ Invalid_Prolog,
+ Too_Many_Prologs,
+
+ No_DocType,
+ Too_Many_DocTypes,
+ DocType_Must_Preceed_Elements,
+
+ // If a DOCTYPE is present _or_ the caller
+ // asked for a specific DOCTYPE and the DOCTYPE
+ // and root tag don't match, we return `.Invalid_DocType`.
+ Invalid_DocType,
+
+ Invalid_Tag_Value,
+ Mismatched_Closing_Tag,
+
+ Unclosed_Comment,
+ Comment_Before_Root_Element,
+ Invalid_Sequence_In_Comment,
+
+ Unsupported_Version,
+ Unsupported_Encoding,
+
+ // <!FOO are usually skipped.
+ Unhandled_Bang,
+
+ Duplicate_Attribute,
+ Conflicting_Options,
+}
+
+parse_bytes :: proc(data: []u8, options := DEFAULT_OPTIONS, path := "", error_handler := default_error_handler, allocator := context.allocator) -> (doc: ^Document, err: Error) {
+ data := data
+ context.allocator = allocator
+
+ opts := validate_options(options) or_return
+
+ // If `.Input_May_Be_Modified` is not specified, we duplicate the input so that we can modify it in-place.
+ if .Input_May_Be_Modified not_in opts.flags {
+ data = bytes.clone(data)
+ }
+
+ t := new(Tokenizer)
+ init(t, string(data), path, error_handler)
+
+ doc = new(Document)
+ doc.allocator = allocator
+ doc.tokenizer = t
+ doc.input = data
+
+ doc.elements = make([dynamic]Element, 1024, 1024, allocator)
+
+ err = .Unexpected_Token
+ element, parent: Element_ID
+ open: Token
+
+ // If a DOCTYPE is present, the root tag has to match.
+ // If an expected DOCTYPE is given in options (i.e. it's non-empty), the DOCTYPE (if present) and root tag have to match.
+ expected_doctype := options.expected_doctype
+
+ loop: for {
+ skip_whitespace(t)
+ switch t.ch {
+ case '<':
+ // Consume peeked `<`
+ advance_rune(t)
+
+ open = scan(t)
+ // NOTE(Jeroen): We're not using a switch because this if-else chain ordered by likelihood is 2.5% faster at -o:size and -o:speed.
+ if likely(open.kind, Token_Kind.Ident) == .Ident {
+ // e.g. <odin - Start of new element.
+ element = new_element(doc)
+ if element == 0 { // First Element
+ parent = element
+ } else {
+ append(&doc.elements[parent].value, element)
+ }
+
+ doc.elements[element].parent = parent
+ doc.elements[element].ident = open.text
+
+ parse_attributes(doc, &doc.elements[element].attribs) or_return
+
+ // If a DOCTYPE is present _or_ the caller
+ // asked for a specific DOCTYPE and the DOCTYPE
+ // and root tag don't match, we return .Invalid_Root_Tag.
+ if element == 0 { // Root tag?
+ if len(expected_doctype) > 0 && expected_doctype != open.text {
+ //error(t, t.offset, "Root Tag doesn't match DOCTYPE. Expected: %v, got: %v\n", expected_doctype, open.text)
+ return doc, .Invalid_DocType
+ }
+ }
+
+ // One of these should follow:
+ // - `>`, which means we've just opened this tag and expect a later element to close it.
+ // - `/>`, which means this is an 'empty' or self-closing tag.
+ end_token := scan(t)
+ #partial switch end_token.kind {
+ case .Gt:
+ // We're now the new parent.
+ parent = element
+
+ case .Slash:
+ // Empty tag. Close it.
+ expect(t, .Gt) or_return
+ parent = doc.elements[element].parent
+ element = parent
+
+ case:
+ //error(t, t.offset, "Expected close tag, got: %#v\n", end_token)
+ return
+ }
+
+ } else if open.kind == .Slash {
+ // Close tag.
+ ident := expect(t, .Ident) or_return
+ _ = expect(t, .Gt) or_return
+
+ if doc.elements[element].ident != ident.text {
+ //error(t, t.offset, "Mismatched Closing Tag. Expected %v, got %v\n", doc.elements[element].ident, ident.text)
+ return doc, .Mismatched_Closing_Tag
+ }
+ parent = doc.elements[element].parent
+ element = parent
+
+ } else if open.kind == .Exclaim {
+ // <!
+ next := scan(t)
+ #partial switch next.kind {
+ case .Ident:
+ switch next.text {
+ case "DOCTYPE":
+ if len(doc.doctype.ident) > 0 {
+ return doc, .Too_Many_DocTypes
+ }
+ if doc.element_count > 0 {
+ return doc, .DocType_Must_Preceed_Elements
+ }
+ parse_doctype(doc) or_return
+
+ if len(expected_doctype) > 0 && expected_doctype != doc.doctype.ident {
+ //error(t, t.offset, "Invalid DOCTYPE. Expected: %v, got: %v\n", expected_doctype, doc.doctype.ident)
+ return doc, .Invalid_DocType
+ }
+ expected_doctype = doc.doctype.ident
+
+ case:
+ if .Error_on_Unsupported in opts.flags {
+ //error(t, t.offset, "Unhandled: <!%v\n", next.text)
+ return doc, .Unhandled_Bang
+ }
+ skip_element(t) or_return
+ }
+
+ case .Dash:
+ // Comment: <!-- -->.
+ // The grammar does not allow a comment to end in --->
+ expect(t, .Dash)
+ comment := scan_comment(t) or_return
+
+ if .Intern_Comments in opts.flags {
+ if len(doc.elements) == 0 {
+ append(&doc.comments, comment)
+ } else {
+ el := new_element(doc)
+ doc.elements[el].parent = element
+ doc.elements[el].kind = .Comment
+ append(&doc.elements[el].value, comment)
+ append(&doc.elements[element].value, el)
+ }
+ }
+
+ case .Open_Bracket:
+ // This could be a CDATA tag part of a tag's body. Unread the `<![`
+ t.offset -= 3
+
+ // Instead of calling `parse_body` here, we could also `continue loop`
+ // and fall through to the `case:` at the bottom of the outer loop.
+ // This makes the intent clearer.
+ parse_body(doc, element, opts) or_return
+
+ case:
+ //error(t, t.offset, "Unexpected Token after <!: %#v", next)
+ }
+
+ } else if open.kind == .Question {
+ // <?xml
+ next := scan(t)
+ #partial switch next.kind {
+ case .Ident:
+ if len(next.text) == 3 && strings.equal_fold(next.text, "xml") {
+ parse_prologue(doc) or_return
+ } else if len(doc.prologue) > 0 {
+ // We've already seen a prologue.
+ return doc, .Too_Many_Prologs
+ } else {
+ // Could be `<?xml-stylesheet`, etc. Ignore it.
+ skip_element(t) or_return
+ }
+ case:
+ //error(t, t.offset, "Expected \"<?xml\", got \"<?%v\".", next.text)
+ return
+ }
+
+ } else {
+ //error(t, t.offset, "Invalid Token after <: %#v\n", open)
+ return
+ }
+
+ case -1:
+ // End of file.
+ break loop
+
+ case:
+ // This should be a tag's body text.
+ parse_body(doc, element, opts) or_return
+ }
+ }
+
+ if .Must_Have_Prolog in opts.flags && len(doc.prologue) == 0 {
+ return doc, .No_Prolog
+ }
+
+ if .Must_Have_DocType in opts.flags && len(doc.doctype.ident) == 0 {
+ return doc, .No_DocType
+ }
+
+ resize(&doc.elements, int(doc.element_count))
+ return doc, .None
+}
+
+parse_string :: proc(data: string, options := DEFAULT_OPTIONS, path := "", error_handler := default_error_handler, allocator := context.allocator) -> (doc: ^Document, err: Error) {
+ _data := transmute([]u8)data
+
+ return parse_bytes(_data, options, path, error_handler, allocator)
+}
+
+parse :: proc { parse_string, parse_bytes }
+
+// Load an XML file
+load_from_file :: proc(filename: string, options := DEFAULT_OPTIONS, error_handler := default_error_handler, allocator := context.allocator) -> (doc: ^Document, err: Error) {
+ context.allocator = allocator
+ options := options
+
+ data, read_err := os.read_entire_file_from_path(filename, context.allocator)
+ if read_err != nil { return {}, .File_Error }
+
+ options.flags += { .Input_May_Be_Modified }
+
+ return parse_bytes(data, options, filename, error_handler, allocator)
+}
+
+destroy :: proc(doc: ^Document) {
+ if doc == nil { return }
+
+ for el in doc.elements {
+ delete(el.attribs)
+ delete(el.value)
+ }
+ delete(doc.elements)
+
+ delete(doc.prologue)
+ delete(doc.comments)
+ delete(doc.input)
+
+ for s in doc.strings_to_free {
+ delete(s)
+ }
+ delete(doc.strings_to_free)
+
+ free(doc.tokenizer)
+ free(doc)
+}
+
+/*
+ Helpers.
+*/
+
+validate_options :: proc(options: Options) -> (validated: Options, err: Error) {
+ validated = options
+
+ if .Error_on_Unsupported in validated.flags && .Ignore_Unsupported in validated.flags {
+ return options, .Conflicting_Options
+ }
+ return validated, .None
+}
+
+expect :: proc(t: ^Tokenizer, kind: Token_Kind, multiline_string := false) -> (tok: Token, err: Error) {
+ tok = scan(t, multiline_string=multiline_string)
+ if tok.kind == kind { return tok, .None }
+
+ //error(t, t.offset, "Expected \"%v\", got \"%v\".", kind, tok.kind)
+ return tok, .Unexpected_Token
+}
+
+parse_attribute :: proc(doc: ^Document) -> (attr: Attribute, offset: int, err: Error) {
+ assert(doc != nil)
+ context.allocator = doc.allocator
+ t := doc.tokenizer
+
+ key := expect(t, .Ident) or_return
+ _ = expect(t, .Eq) or_return
+ value := expect(t, .String, multiline_string=true) or_return
+
+ normalized, normalize_err := entity.decode_xml(value.text, {.Normalize_Whitespace}, doc.allocator)
+ if normalize_err == .None {
+ append(&doc.strings_to_free, normalized)
+ value.text = normalized
+ }
+
+ attr.key = key.text
+ attr.val = value.text
+
+ err = .None
+ return
+}
+
+check_duplicate_attributes :: proc(t: ^Tokenizer, attribs: Attributes, attr: Attribute, offset: int) -> (err: Error) {
+ for a in attribs {
+ if attr.key == a.key {
+ //error(t, offset, "Duplicate attribute: %v\n", attr.key)
+ return .Duplicate_Attribute
+ }
+ }
+ return .None
+}
+
+parse_attributes :: proc(doc: ^Document, attribs: ^Attributes) -> (err: Error) {
+ assert(doc != nil)
+ context.allocator = doc.allocator
+ t := doc.tokenizer
+
+ for peek(t).kind == .Ident {
+ attr, offset := parse_attribute(doc) or_return
+ check_duplicate_attributes(t, attribs^, attr, offset) or_return
+ append(attribs, attr)
+ }
+ skip_whitespace(t)
+ return .None
+}
+
+parse_prologue :: proc(doc: ^Document) -> (err: Error) {
+ assert(doc != nil)
+ context.allocator = doc.allocator
+ t := doc.tokenizer
+
+ offset := t.offset
+ parse_attributes(doc, &doc.prologue) or_return
+
+ for attr in doc.prologue {
+ switch attr.key {
+ case "version":
+ switch attr.val {
+ case "1.0", "1.1":
+ case:
+ //error(t, offset, "[parse_prologue] Warning: Unhandled XML version: %v\n", attr.val)
+ }
+
+ case "encoding":
+ runtime.DEFAULT_TEMP_ALLOCATOR_TEMP_GUARD()
+ switch strings.to_lower(attr.val, context.temp_allocator) {
+ case "utf-8", "utf8":
+ doc.encoding = .UTF_8
+
+ case "latin-1", "latin1", "iso-8859-1":
+ doc.encoding = .LATIN_1
+
+ case:
+ // Unrecognized encoding, assume UTF-8.
+ //error(t, offset, "[parse_prologue] Warning: Unrecognized encoding: %v\n", attr.val)
+ }
+
+ case:
+ // Ignored.
+ }
+ }
+
+ _ = expect(t, .Question) or_return
+ _ = expect(t, .Gt) or_return
+
+ return .None
+}
+
+skip_element :: proc(t: ^Tokenizer) -> (err: Error) {
+ close := 1
+
+ loop: for {
+ tok := scan(t)
+ #partial switch tok.kind {
+ case .EOF:
+ //error(t, t.offset, "[skip_element] Premature EOF\n")
+ return .Premature_EOF
+
+ case .Lt:
+ close += 1
+
+ case .Gt:
+ close -= 1
+ if close == 0 {
+ break loop
+ }
+
+ case:
+
+ }
+ }
+ return .None
+}
+
+parse_doctype :: proc(doc: ^Document) -> (err: Error) {
+ /*
+ <!DOCTYPE greeting SYSTEM "hello.dtd">
+
+ <!DOCTYPE greeting [
+ <!ELEMENT greeting (#PCDATA)>
+ ]>
+ */
+ assert(doc != nil)
+ context.allocator = doc.allocator
+ t := doc.tokenizer
+
+ tok := expect(t, .Ident) or_return
+ doc.doctype.ident = tok.text
+
+ skip_whitespace(t)
+ offset := t.offset
+ skip_element(t) or_return
+
+ // -1 because the current offset is that of the closing tag, so the rest of the DOCTYPE tag ends just before it.
+ doc.doctype.rest = string(t.src[offset : t.offset - 1])
+ return .None
+}
+
+parse_body :: proc(doc: ^Document, element: Element_ID, opts: Options) -> (err: Error) {
+ assert(doc != nil)
+ context.allocator = doc.allocator
+ t := doc.tokenizer
+
+ body_text := scan_string(t, t.offset) or_return
+ needs_processing := .Unbox_CDATA in opts.flags
+ needs_processing |= .Decode_SGML_Entities in opts.flags
+
+ if !needs_processing {
+ append(&doc.elements[element].value, body_text)
+ return
+ }
+
+ decode_opts := entity.XML_Decode_Options{}
+ if .Keep_Tag_Body_Comments not_in opts.flags {
+ decode_opts += { .Comment_Strip }
+ }
+
+ if .Decode_SGML_Entities not_in opts.flags {
+ decode_opts += { .No_Entity_Decode }
+ }
+
+ if .Unbox_CDATA in opts.flags {
+ decode_opts += { .Unbox_CDATA }
+ if .Decode_SGML_Entities in opts.flags {
+ decode_opts += { .Decode_CDATA }
+ }
+ }
+
+ decoded, decode_err := entity.decode_xml(body_text, decode_opts)
+ if decode_err == .None {
+ append(&doc.elements[element].value, decoded)
+ append(&doc.strings_to_free, decoded)
+ } else {
+ append(&doc.elements[element].value, body_text)
+ }
+
+ return
+}
+
+Element_ID :: u32
+
+new_element :: proc(doc: ^Document) -> (id: Element_ID) {
+ element_space := len(doc.elements)
+
+ // Need to resize
+ if int(doc.element_count) + 1 > element_space {
+ if element_space < 65536 {
+ element_space *= 2
+ } else {
+ element_space += 65536
+ }
+ resize(&doc.elements, element_space)
+ }
+
+ cur := doc.element_count
+ doc.element_count += 1
+ return cur
+} \ No newline at end of file