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
use super::Float;
use super::WavetableRef;
use log::{info, trace, warn};
const NUM_SAMPLES_PER_TABLE: usize = 2048;
const NUM_VALUES_PER_TABLE: usize = NUM_SAMPLES_PER_TABLE + 1;
pub struct WtOsc {
pub sample_rate: Float,
last_update: i64,
last_sample: Float,
last_pos: Float,
wave: WavetableRef,
}
impl WtOsc {
pub fn new(sample_rate: Float, wave: WavetableRef) -> WtOsc {
let last_update = 0;
let last_sample = 0.0;
let last_pos = 0.0;
WtOsc{sample_rate,
last_update,
last_sample,
last_pos,
wave}
}
pub fn set_wavetable(&mut self, wavetable: WavetableRef) {
self.wave = wavetable;
}
fn interpolate(val_a: Float, val_b: Float, ratio: Float) -> Float {
val_a + ((val_b - val_a) * ratio)
}
fn calc_sample(table: &[Float], table_index: usize, position: Float) -> Float {
let floor_pos = position as usize;
let frac = position - floor_pos as Float;
let position = floor_pos + table_index * NUM_VALUES_PER_TABLE;
if frac > 0.9 {
table[position + 1]
} else if frac < 0.1 {
table[position]
} else {
let value_left = table[position];
let value_right = table[position + 1];
WtOsc::interpolate(value_left, value_right, frac)
}
}
fn get_table_index(num_octaves: usize, freq: Float) -> usize {
let two: Float = 2.0;
let mut compare_freq = (440.0 / 32.0) * (two.powf((-9.0) / 12.0));
for i in 0..num_octaves {
if freq < compare_freq * 2.0 {
return i;
}
compare_freq *= 2.0;
}
0
}
pub fn get_sample(&mut self, frequency: Float, sample_clock: i64, wave_index: Float, reset: bool) -> Float {
if reset {
self.reset(sample_clock - 1);
}
if sample_clock == self.last_update {
return self.last_sample;
}
let dt = sample_clock - self.last_update;
let dt_f = dt as Float;
let freq_speed = frequency * (NUM_SAMPLES_PER_TABLE as Float / self.sample_rate);
let diff = freq_speed * dt_f;
self.last_pos += diff;
if self.last_pos > (NUM_SAMPLES_PER_TABLE as Float) {
self.last_pos -= NUM_SAMPLES_PER_TABLE as Float;
}
let translated_index = (self.wave.table.len() - 1) as Float * wave_index;
let lower_wave = translated_index as usize;
let lower_wave_float = lower_wave as Float;
let lower_fract: Float = 1.0 - (translated_index - lower_wave_float);
let upper_fract: Float = if lower_fract != 1.0 { 1.0 - lower_fract } else { 0.0 };
let table_index = WtOsc::get_table_index(self.wave.num_octaves, frequency);
let mut result = WtOsc::calc_sample(&self.wave.table[lower_wave], table_index, self.last_pos) * lower_fract;
if upper_fract > 0.0 {
result += WtOsc::calc_sample(&self.wave.table[lower_wave + 1], table_index, self.last_pos) * upper_fract;
}
self.last_update += dt;
self.last_sample = result;
result
}
fn reset(&mut self, sample_clock: i64) {
self.last_pos = 0.0;
self.last_update = sample_clock;
}
}