-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwave.mbt
More file actions
174 lines (152 loc) · 4.16 KB
/
wave.mbt
File metadata and controls
174 lines (152 loc) · 4.16 KB
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
///|
/// Wave type, wrapping the internal FFI wave resource.
struct Wave(@ffi.Wave)
///|
/// Check if wave data is valid (data loaded and parameters).
#as_free_fn(is_wave_valid)
pub fn Wave::is_valid(self : Wave) -> Bool {
@ffi.is_wave_valid(self.0)
}
///|
/// Load sound from wave data.
#as_free_fn(load_sound_from_wave)
pub fn Wave::load_sound(self : Wave) -> Sound {
@ffi.load_sound_from_wave(self.0)
}
///|
/// Unload wave data.
#as_free_fn(unload_wave)
pub fn Wave::unload(self : Wave) -> Unit {
@ffi.unload_wave(self.0)
}
///|
/// Copy a wave to a new wave.
#as_free_fn(wave_copy)
pub fn Wave::copy(self : Wave) -> Wave {
@ffi.wave_copy(self.0)
}
///|
/// Crop a wave to defined frames range.
#as_free_fn(wave_crop)
pub fn Wave::crop(self : Wave, init_frame : Int, final_frame : Int) -> Unit {
@ffi.wave_crop(self.0, init_frame, final_frame)
}
///|
/// Convert wave data to desired format.
#as_free_fn(wave_format)
pub fn Wave::format(
self : Wave,
sample_rate : Int,
sample_size : Int,
channels : Int,
) -> Unit {
@ffi.wave_format(self.0, sample_rate, sample_size, channels)
}
///|
/// Wave samples data, wrapping a float array.
pub struct WaveSamples {
priv samples : FloatArray
}
///|
/// Get the number of samples.
pub fn WaveSamples::length(self : WaveSamples) -> Int {
self.samples.length()
}
///|
/// Get sample value at index.
pub fn WaveSamples::op_get(self : WaveSamples, index : Int) -> Float {
self.samples[index]
}
///|
/// Set sample value at index.
pub fn WaveSamples::op_set(
self : WaveSamples,
index : Int,
value : Float,
) -> Unit {
self.samples[index] = value
}
///|
/// Unload samples data loaded with load_wave_samples.
#as_free_fn(unload_wave_samples)
pub fn WaveSamples::unload(self : WaveSamples) -> Unit {
@ffi.unload_wave_samples(self.samples.0)
}
///|
/// Load samples data from wave as a 32-bit float data array.
#as_free_fn(load_wave_samples)
pub fn Wave::load_samples(self : Wave) -> WaveSamples {
let count = self.frame_count() * self.channels()
{ samples: FloatArray(@ffi.load_wave_samples(self.0), count) }
}
///|
pub enum WaveData {
Float(FloatArray)
Short(ShortArray)
UByte(UByteArray)
}
///|
/// Get wave data as a zero-copy view into the internal buffer.
/// Returns `Float`, `Short`, or `UByte` depending on `sample_size()` (32, 16, or 8).
#as_free_fn(get_wave_data)
pub fn Wave::data(self : Wave) -> WaveData {
let count = self.frame_count() * self.channels()
match self.sample_size() {
32 => Float(FloatArray(@ffi.get_wave_data_float(self.0), count))
16 => Short(ShortArray(@ffi.get_wave_data_short(self.0), count))
8 => UByte(UByteArray(@ffi.get_wave_data_ubyte(self.0), count))
_ => abort("Wave::data(): unexpected sample_size")
}
}
///|
/// Get wave frame count.
#as_free_fn(get_wave_frame_count)
pub fn Wave::frame_count(self : Wave) -> Int {
@ffi.get_wave_frame_count(self.0)
}
///|
/// Get wave sample rate.
#as_free_fn(get_wave_sample_rate)
pub fn Wave::sample_rate(self : Wave) -> Int {
@ffi.get_wave_sample_rate(self.0)
}
///|
/// Get wave sample size.
#as_free_fn(get_wave_sample_size)
pub fn Wave::sample_size(self : Wave) -> Int {
@ffi.get_wave_sample_size(self.0)
}
///|
/// Get wave channels count.
#as_free_fn(get_wave_channels)
pub fn Wave::channels(self : Wave) -> Int {
@ffi.get_wave_channels(self.0)
}
///|
/// Load wave data from file.
#as_free_fn(load_wave)
pub fn Wave::load(file_name : String) -> Wave {
@ffi.load_wave(@utf8.encode(file_name))
}
///|
/// Load wave from memory buffer, file_type refers to extension (e.g. ".wav").
#as_free_fn(load_wave_from_memory)
pub fn Wave::load_from_memory(
file_type : String,
file_data : Bytes,
data_size : Int,
) -> Wave {
@ffi.load_wave_from_memory(@utf8.encode(file_type), file_data, data_size)
}
///|
/// Export wave data to file, returns true on success.
#as_free_fn(export_wave)
pub fn Wave::export_(self : Wave, file_name : String) -> Bool {
@ffi.export_wave(self.0, @utf8.encode(file_name))
}
///|
/// Export wave sample data to code (.h), returns true on success.
#as_free_fn(export_wave_as_code)
pub fn Wave::export_as_code(self : Wave, file_name : String) -> Bool {
@ffi.export_wave_as_code(self.0, @utf8.encode(file_name))
}