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
use crate::Interpolator;
use core::f64::consts::PI;
use dasp_frame::Frame;
use dasp_ring_buffer as ring_buffer;
use dasp_sample::{Duplex, Sample};
use ops::f64::{cos, sin};
mod ops;
pub struct Sinc<S> {
frames: ring_buffer::Fixed<S>,
idx: usize,
}
impl<S> Sinc<S> {
pub fn new(frames: ring_buffer::Fixed<S>) -> Self
where
S: ring_buffer::SliceMut,
S::Element: Frame,
{
assert!(frames.len() % 2 == 0);
Sinc {
frames: frames,
idx: 0,
}
}
fn depth(&self) -> usize
where
S: ring_buffer::Slice,
{
self.frames.len() / 2
}
}
impl<S> Interpolator for Sinc<S>
where
S: ring_buffer::SliceMut,
S::Element: Frame,
<S::Element as Frame>::Sample: Duplex<f64>,
{
type Frame = S::Element;
fn interpolate(&self, x: f64) -> Self::Frame {
let phil = x;
let phir = 1.0 - x;
let nl = self.idx;
let nr = self.idx + 1;
let depth = self.depth();
let rightmost = nl + depth;
let leftmost = nr as isize - depth as isize;
let max_depth = if rightmost >= self.frames.len() {
self.frames.len() - depth
} else if leftmost < 0 {
(depth as isize + leftmost) as usize
} else {
depth
};
(0..max_depth).fold(Self::Frame::EQUILIBRIUM, |mut v, n| {
v = {
let a = PI * (phil + n as f64);
let first = if a == 0.0 { 1.0 } else { sin(a) / a };
let second = 0.5 + 0.5 * cos(a / (phil + max_depth as f64));
v.zip_map(self.frames[nr - n], |vs, r_lag| {
vs.add_amp(
(first * second * r_lag.to_sample::<f64>())
.to_sample::<<Self::Frame as Frame>::Sample>()
.to_signed_sample(),
)
})
};
let a = PI * (phir + n as f64);
let first = if a == 0.0 { 1.0 } else { sin(a) / a };
let second = 0.5 + 0.5 * cos(a / (phir + max_depth as f64));
v.zip_map(self.frames[nl + n], |vs, r_lag| {
vs.add_amp(
(first * second * r_lag.to_sample::<f64>())
.to_sample::<<Self::Frame as Frame>::Sample>()
.to_signed_sample(),
)
})
})
}
fn next_source_frame(&mut self, source_frame: Self::Frame) {
let _old_frame = self.frames.push(source_frame);
if self.idx < self.depth() {
self.idx += 1;
}
}
}