1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
package wav
import "core:fmt"
import "core:math"
import "core:strings"
import "core:os"
import "core:encoding/xml"
Wav :: struct {
// Basic data
path : string,
handle : os.Handle,
format : Audio_Format,
channels : int,
sample_rate : int,
bit_depth : int,
reported_size : u32,
// Metadata
buf : []u8,
channel_names : []string,
bext : string,
ixml : string,
}
Audio_Format :: enum {
PCM = 1,
FLOAT = 3,
}
BUFFER_SIZE :: 1<<15
main :: proc() {
enok, enok_ok := read_wav("test/ENOKS-BIRHTDAYT02.WAV", context.temp_allocator)
fmt.printf("\n\nenok = %#v\n\n", enok)
f8, f8_ok := read_wav("test/F8-SL098-T001.WAV", context.temp_allocator)
fmt.printf("\n\nf8 = %#v\n\n", f8)
}
// TODO: Maybe split reading metadata into its own function call.
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..<BUFFER_SIZE {
chunk_id := string(file.buf[head:head+4])
head += 4
chunk_size := int(read_little_endian_u32(file.buf[head:head+4]))
head += 4
fmt.println(chunk_id,"\n-------------------------------------")
if head+chunk_size < BUFFER_SIZE {
print_data : string
data_end := head+chunk_size
read_end := min(head+15000, data_end)
switch chunk_id {
case "iXML":
print_data = string(file.buf[head:read_end])
file.ixml = strings.clone(string(file.buf[head:data_end]), allocator=allocator)
case "bext":
print_data = string(file.buf[head:read_end])
file.bext = strings.clone(string(file.buf[head:data_end]), allocator=allocator)
case "fmt ":
file.format = Audio_Format(read_little_endian_u16(file.buf[head:]))
head += 2
file.channels = int(read_little_endian_u16(file.buf[head:]))
head += 2
file.sample_rate = int(read_little_endian_u32(file.buf[head:]))
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
file.bit_depth = int(read_little_endian_u16(file.buf[head:]))
head += 2
}
fmt.println(print_data, "\n")
} else {
break
}
head += chunk_size
}
file.channel_names = make([]string, file.channels, allocator=allocator)
naming_channel := 0
for line in strings.split_lines(file.bext) {
if 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
}
}
// iXML Parsing
parsed_ixml : ^xml.Document
if file.ixml != "" {
parsed_ixml, _ = xml.parse(file.ixml, xml.Options{
flags={.Ignore_Unsupported},
expected_doctype = "",
})
}
xml_recurse(parsed_ixml, 0)
delete(file.buf)
file.buf = nil
return file, true
}
xml_recurse :: proc(doc: ^xml.Document, element_id: xml.Element_ID, indent := 0) {
tab :: proc(indent: int) {
for _ in 0..=indent {
fmt.printf("\t")
}
}
tab(indent)
element := doc.elements[element_id]
if element.kind == .Element {
fmt.printf("<%v>\n", element.ident)
for value in element.value {
switch v in value {
case string:
tab(indent + 1)
fmt.printf("[Value] %v\n", v)
case xml.Element_ID:
xml_recurse(doc, v, indent + 1)
}
}
for attr in element.attribs {
tab(indent + 1)
fmt.printf("[Attr] %v: %v\n", attr.key, attr.val)
}
} else if element.kind == .Comment {
fmt.printf("[COMMENT] %v\n", element.value)
}
return
}
read_little_endian_u32 :: proc(x : []u8) -> u32 {
return u32(x[0]) |
u32(x[1]) << 8 |
u32(x[2]) << 16 |
u32(x[3]) << 24
}
read_little_endian_u16 :: proc(x : []u8) -> u16 {
return u16(x[0]) |
u16(x[1]) << 8
}
|