-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
vave.v
145 lines (118 loc) · 3.09 KB
/
vave.v
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
module vave
import os
struct C.FILE
fn C.feof(f &FILE) int
fn C.ferror() int
struct WavFile {
filename string
mut:
chunk WavMasterChunk
fp &C.FILE
mode string
}
//open opens a WAV file for read/write in the specified mode
pub fn open(path, mode string) &WavFile {
os.tmpdir() //hack to include os import
mut wav := &WavFile{
fp: C.NULL
}
match mode {
"rb", "r" {wav.mode = "rb"}
"r+", "rb+", "r+b" {wav.mode = "rb+"}
"w", "wb" {wav.mode = "wb"}
"w+", "wb+", "w+b" {wav.mode = "wb+"}
"wx", "wbx" {wav.mode = "wbx"}
"w+x", "wb+x", "w+bx" {wav.mode = "wb+x"}
"a", "ab" {wav.mode = "ab"}
"a+","ab+","ab+b" {wav.mode = "ab+"}
else {
panic("init: wrong 'mode' given.")
}
}
$if windows {
wav.fp = C._wfopen(path.replace('/', '\\').to_wide(), wav.mode.to_wide())
} $if linux {
wav.fp = C.fopen(path.replace('\\', '/').str, wav.mode.str)
}
if wav.fp == C.NULL {
panic("Couldn't open file: ${path}. Make sure it exists.")
}
if wav.mode[0] == `r` {
wav.parse_header()
} else if wav.mode[0] == `a` {
if !wav.parse_header() {
C.rewind(wav.fp)
}
}
return wav
}
pub fn (w mut WavFile) close() int {
if !w.finalize() {
panic("Failed to close the file.")
}
unsafe {
free(w)
}
return 0
}
pub fn (w &WavFile) bytes_per_sample() u16 {
return w.chunk.format_chunk.bits_per_sample / u16(8)
}
pub fn (w &WavFile) total_samples() u32 {
return w.chunk.data_chunk.size / u32(w.chunk.format_chunk.block_align)
}
pub fn (w &WavFile) sample_rate() u32 {
return w.chunk.format_chunk.sample_rate
}
pub fn (w &WavFile) channel_mask() u32 {
return w.chunk.format_chunk.channel_mask
}
pub fn (w &WavFile) sub_format() u16 {
return w.chunk.format_chunk.sub_format.format_code
}
pub fn (w &WavFile) sample_size() u16 {
return w.chunk.format_chunk.block_align / w.chunk.format_chunk.n_channels
}
pub fn (w &WavFile) format() Formats {
return Formats(w.chunk.format_chunk.format_tag)
}
pub fn (w &WavFile) num_channels() u16 {
return w.chunk.format_chunk.n_channels
}
pub fn (w &WavFile) valid_bits_per_sample() u16 {
if w.chunk.format_chunk.format_tag != u16(Formats.extensible) {
return w.chunk.format_chunk.bits_per_sample
} else {
return w.chunk.format_chunk.valid_bits_per_sample
}
}
pub fn (w &WavFile) duration() u32 {
return w.total_samples() / w.sample_rate()
}
pub fn (w &WavFile) data_len() int {
return int(w.chunk.data_chunk.size)
}
// Private
fn (w mut WavFile) finalize () bool {
if w.fp == C.NULL {
return false
}
ret := C.fclose(w.fp)
if (ret != 0) {
panic("Couldn't close the file properly.")
}
return true
}
/* fn (w mut WavFile) reopen(path, mode string) &WavFile {
w.finalize()
return init(path, mode)
} */
fn (w &WavFile) get_header_size() u32 {
mut header_size := WAV_RIFF_HEADER_SIZE + u32(4) +
WAV_RIFF_HEADER_SIZE + w.chunk.format_chunk.size +
WAV_RIFF_HEADER_SIZE
if compare(&w.chunk.fact_chunk.id, WAV_FACT_CHUNK_ID) {
header_size += WAV_RIFF_HEADER_SIZE + w.chunk.fact_chunk.size
}
return header_size
}