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
|
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..<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)
naming_channel := 0
for line in strings.split_lines(file.bext) {
if strings.starts_with(line, "sTRK") {
eq_index := strings.index(line, "=")
file.channel_names[naming_channel] = strings.clone(line[eq_index+1:], allocator=allocator)
naming_channel += 1
}
}
delete(file.buf)
file.buf = nil
return file, true
}
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
}
|