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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#![deny(missing_docs)]
pub extern crate bincode;
pub extern crate chrono;
pub extern crate conv;
pub extern crate log;
pub extern crate peg;
pub extern crate rand;
pub use either::Either;
#[macro_use]
mod macros;
pub mod time;
pub mod time_stats;
pub mod uid;
pub mod prelude;
pub mod error_chain {
pub use error_chain::*;
}
pub mod err;
use prelude::serde::*;
#[inline]
pub fn convert<In, Out>(n: In, from: &'static str) -> Out
where
In: std::convert::TryInto<Out> + std::fmt::Display + Copy,
In::Error: std::fmt::Display,
{
match n.try_into() {
Ok(res) => res,
Err(e) => panic!("[fatal] while converting {} ({}): {}", n, from, e),
}
}
pub fn identity<T>(t: T) -> T {
t
}
pub fn destroy<T>(_: T) {}
pub type SVec8<T> = smallvec::SmallVec<[T; 8]>;
pub type SVec16<T> = smallvec::SmallVec<[T; 16]>;
pub type SVec32<T> = smallvec::SmallVec<[T; 32]>;
pub type SVec64<T> = smallvec::SmallVec<[T; 64]>;
#[macro_use]
pub mod client {
#[macro_export]
macro_rules! client_wasm_build_dir_for {
(release) => {
"libs/client/target/client.wasm/release/"
};
(debug) => {
"libs/client/target/client.wasm/debug/"
};
($profile:tt bins) => {
vec![
concat!(
$crate::client_wasm_build_dir_for!($profile),
"client_bg.wasm"
),
concat!($crate::client_wasm_build_dir_for!($profile), "client.js"),
]
};
}
#[macro_export]
#[cfg(debug_assertions)]
macro_rules! client_wasm_build_dir {
() => {
$crate::client_wasm_build_dir_for!(debug)
};
}
#[macro_export]
#[cfg(not(debug_assertions))]
macro_rules! client_wasm_build_dir {
() => {
$crate::client_wasm_build_dir_for!(release)
};
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SampleRate {
pub sample_rate: f64,
pub factor: u32,
pub factor_is_approx: bool,
pub word_size_bytes: u32,
}
impl SampleRate {
pub fn new(sample_rate: f64, word_size: u32) -> Self {
use conv::*;
let inv = 1. / sample_rate;
let factor = inv.trunc();
let factor_is_approx = factor == inv;
let factor: u32 = factor.approx().expect("error while handling sample rate");
Self {
sample_rate,
factor,
factor_is_approx,
word_size_bytes: word_size / 8,
}
}
pub fn real_size_of(&self, nsamples: u32) -> u32 {
nsamples * self.word_size_bytes * self.factor
}
}
cfg_item! {
cfg(client) {
#[derive(Debug, Clone)]
pub struct Memory<T> {
current: T,
reference: T,
}
} {
impl<T> Default for Memory<T>
where
T: Default + Clone,
{
fn default() -> Self {
Self::new(T::default())
}
}
} {
impl<T> Memory<T> {
pub fn new(reference: T) -> Self
where
T: Clone,
{
Self {
current: reference.clone(),
reference,
}
}
pub fn get(&self) -> &T {
&self.current
}
pub fn set(&mut self, current: T) {
self.current = current
}
pub fn get_mut(&mut self) -> &mut T {
&mut self.current
}
pub fn reference(&self) -> &T {
&self.reference
}
pub fn has_changed(&self) -> bool
where
T: PartialEq,
{
self.current != self.reference
}
pub fn set_both(&mut self, reference: T)
where
T: Clone,
{
self.current = reference.clone();
self.reference = reference;
}
pub fn do_both(&mut self, mut action: impl FnMut(&mut T)) {
action(&mut self.current);
action(&mut self.reference)
}
pub fn reset(&mut self)
where
T: Clone,
{
self.current = self.reference.clone()
}
pub fn overwrite_reference(&mut self)
where
T: Clone,
{
self.reference = self.current.clone()
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
pub enum RangeCmp {
Below,
Inside,
Above,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Deserialize, Serialize)]
pub struct Range<T> {
pub lbound: T,
pub ubound: T,
}
impl<T> Range<T> {
pub const fn new(lbound: T, ubound: T) -> Self {
Self { lbound, ubound }
}
pub fn map<U>(self, mut action: impl FnMut(T) -> U) -> Range<U> {
Range::new(action(self.lbound), action(self.ubound))
}
pub fn as_ref(&self) -> Range<&T> {
Range::new(&self.lbound, &self.ubound)
}
pub fn spread<Out>(self) -> Out
where
T: std::ops::Sub<T, Output = Out>,
{
self.ubound - self.lbound
}
}
impl<T> Range<T>
where
T: PartialOrd,
{
pub fn cmp(&self, val: impl prelude::Borrow<T>) -> RangeCmp {
let val = val.borrow();
if val < &self.lbound {
RangeCmp::Below
} else if &self.ubound < val {
RangeCmp::Above
} else {
debug_assert!(&self.lbound <= val);
debug_assert!(val <= &self.ubound);
RangeCmp::Inside
}
}
pub fn contains(&self, val: impl prelude::Borrow<T>) -> bool {
match self.cmp(val) {
RangeCmp::Inside => true,
RangeCmp::Below | RangeCmp::Above => false,
}
}
pub fn is_empty(&self) -> bool {
self.lbound > self.ubound
}
pub fn intersection(self, other: Self) -> Self
where
T: Ord,
{
Self::new(
std::cmp::max(self.lbound, other.lbound),
std::cmp::min(self.ubound, other.ubound),
)
}
}
impl Range<Option<time::SinceStart>> {
pub fn to_time_window(
self,
ubound: impl FnOnce() -> time::SinceStart,
) -> Range<time::SinceStart> {
self.unwrap_or_else(time::SinceStart::zero, ubound)
}
}
impl<T> Range<Option<T>> {
pub fn unwrap(self) -> Range<T> {
Range::new(
self.lbound.expect("while unwrapping range lower bound"),
self.ubound.expect("while unwrapping range upper bound"),
)
}
pub fn unwrap_or(self, lbound: T, ubound: T) -> Range<T> {
Range::new(self.lbound.unwrap_or(lbound), self.ubound.unwrap_or(ubound))
}
pub fn unwrap_or_else(
self,
lbound: impl FnOnce() -> T,
ubound: impl FnOnce() -> T,
) -> Range<T> {
Range::new(
self.lbound.unwrap_or_else(lbound),
self.ubound.unwrap_or_else(ubound),
)
}
}
implement! {
impl Range<T>, with (T: PartialOrd) {
From {
from (T, T) => |(lbound, ubound)| Self::new(lbound, ubound)
}
}
impl Range<T>, with (T: std::fmt::Display) {
Display {
|&self, fmt| write!(fmt, "[{}, {}]", self.lbound, self.ubound),
}
}
}