forked from VCVRack/pichenettes-eurorack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigital_oscillator.cc
executable file
·262 lines (236 loc) · 8.49 KB
/
digital_oscillator.cc
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
// Copyright 2012 Emilie Gillet.
//
// Author: Emilie Gillet ([email protected])
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
// Digital oscillator generated from a timer.
#include "edges/digital_oscillator.h"
#include "avrlibx/utils/op.h"
#include "edges/audio_buffer.h"
#include "edges/resources.h"
namespace edges {
static const uint8_t kMaxZone = 7;
static const int16_t kOctave = 12 * 128;
static const int16_t kPitchTableStart = 116 * 128;
using namespace avrlibx;
#define UPDATE_PHASE \
phase = U24Add(phase, phase_increment);
#define BEGIN_SAMPLE_LOOP \
uint24_t phase; \
uint24_t phase_increment; \
phase_increment.integral = phase_increment_.integral; \
phase_increment.fractional = phase_increment_.fractional; \
phase.integral = phase_.integral; \
phase.fractional = phase_.fractional; \
uint8_t size = kAudioBlockSize; \
while (size--) {
#define END_SAMPLE_LOOP \
} \
phase_.integral = phase.integral; \
phase_.fractional = phase.fractional;
void DigitalOscillator::ComputePhaseIncrement() {
int16_t ref_pitch = pitch_ - kPitchTableStart;
uint8_t num_shifts = shape_ >= OSC_PITCHED_NOISE ? 0 : 1;
while (ref_pitch < 0) {
ref_pitch += kOctave;
++num_shifts;
}
uint24_t increment;
uint16_t pitch_lookup_index_integral = U16ShiftRight4(ref_pitch);
uint8_t pitch_lookup_index_fractional = U8ShiftLeft4(ref_pitch);
uint16_t increment16 = ResourcesManager::Lookup<uint16_t, uint16_t>(
lut_res_oscillator_increments, pitch_lookup_index_integral);
uint16_t increment16_next = ResourcesManager::Lookup<uint16_t, uint16_t>(
lut_res_oscillator_increments, pitch_lookup_index_integral + 1);
increment.integral = increment16 + U16U8MulShift8(
increment16_next - increment16, pitch_lookup_index_fractional);
increment.fractional = 0;
while (num_shifts--) {
increment = U24ShiftRight(increment);
}
note_ = U15ShiftRight7(pitch_);
if (note_ < 12) {
note_ = 12;
}
phase_increment_ = increment;
}
void DigitalOscillator::Render() {
if (gate_) {
ComputePhaseIncrement();
}
while (audio_buffer.writable() >= kAudioBlockSize) {
if (!gate_) {
RenderSilence();
} else {
RenderFn fn;
ResourcesManager::Load(fn_table_, shape_, &fn);
(this->*fn)();
}
}
}
static inline uint8_t InterpolateSample(
const prog_uint8_t* table,
uint16_t phase) {
uint8_t result;
uint8_t work;
asm(
"movw r30, %A2" "\n\t" // copy base address to r30:r31
"mov %1, %A3" "\n\t" // duplicate phase increment
"add %1, %A3" "\n\t" // duplicate
"adc r30, %B3" "\n\t" // duplicate
"adc r31, r1" "\n\t" // duplicate
"add r30, %B3" "\n\t" // duplicate
"adc r31, r1" "\n\t" // duplicate
"lpm %0, z+" "\n\t" // load sample[n]
"lpm r1, z+" "\n\t" // load sample[n+1]
"mul %1, r1" "\n\t" // multiply second sample by phaseL
"movw r30, r0" "\n\t" // result to accumulator
"com %1" "\n\t" // 255 - phaseL -> phaseL
"mul %1, %0" "\n\t" // multiply first sample by phaseL
"add r30, r0" "\n\t" // accumulate L
"adc r31, r1" "\n\t" // accumulate H
"eor r1, r1" "\n\t" // reset r1 after multiplication
"mov %0, r31" "\n\t" // use sum H as output
: "=r" (result), "=r" (work)
: "r" (table), "r" (phase)
: "r30", "r31"
);
return result;
}
static inline uint16_t InterpolateSample16(
const prog_uint8_t* table,
uint16_t phase) {
uint16_t result;
uint8_t work;
asm(
"movw r30, %A2" "\n\t" // copy base address to r30:r31
"mov %1, %A3" "\n\t" // duplicate phase increment
"add %1, %A3" "\n\t" // duplicate
"adc r30, %B3" "\n\t" // duplicate
"adc r31, r1" "\n\t" // duplicate
"add r30, %B3" "\n\t" // duplicate
"adc r31, r1" "\n\t" // duplicate
"lpm %0, z+" "\n\t" // load sample[n]
"lpm r1, z+" "\n\t" // load sample[n+1]
"mul %1, r1" "\n\t" // multiply second sample by phaseL
"movw r30, r0" "\n\t" // result to accumulator
"com %1" "\n\t" // 255 - phaseL -> phaseL
"mul %1, %0" "\n\t" // multiply first sample by phaseL
"add r30, r0" "\n\t" // accumulate L
"adc r31, r1" "\n\t" // accumulate H
"eor r1, r1" "\n\t" // reset r1 after multiplication
"movw %0, r30" "\n\t" // use sum H as output
: "=r" (result), "=r" (work)
: "r" (table), "r" (phase)
: "r30", "r31"
);
return result;
}
static inline uint16_t InterpolateTwoTables(
const prog_uint8_t* table_a, const prog_uint8_t* table_b,
uint16_t phase, uint8_t gain_a, uint8_t gain_b) {
uint16_t result = 0;
result += U8U8Mul(InterpolateSample(table_a, phase), gain_a);
result += U8U8Mul(InterpolateSample(table_b, phase), gain_b);
return result;
}
void DigitalOscillator::RenderSilence() {
uint8_t size = kAudioBlockSize;
while (size--) {
audio_buffer.Overwrite(2048);
}
}
void DigitalOscillator::RenderSine() {
uint16_t aux_phase_increment = pgm_read_word(
lut_res_bitcrusher_increments + cv_pw_);
BEGIN_SAMPLE_LOOP
UPDATE_PHASE
aux_phase_ += aux_phase_increment;
if (aux_phase_ < aux_phase_increment || !aux_phase_increment) {
sample_ = InterpolateSample16(
wav_res_bandlimited_triangle_6,
phase.integral);
}
audio_buffer.Overwrite(sample_ >> 4);
END_SAMPLE_LOOP
}
void DigitalOscillator::RenderBandlimitedTriangle() {
uint8_t balance_index = U8Swap4(note_ - 12);
uint8_t gain_2 = balance_index & 0xf0;
uint8_t gain_1 = ~gain_2;
uint8_t wave_index = balance_index & 0xf;
uint8_t base_resource_id = (shape_ == OSC_NES_TRIANGLE)
? WAV_RES_BANDLIMITED_NES_TRIANGLE_0
: WAV_RES_BANDLIMITED_TRIANGLE_0;
const prog_uint8_t* wave_1 = waveform_table[base_resource_id + wave_index];
wave_index = U8AddClip(wave_index, 1, kMaxZone);
const prog_uint8_t* wave_2 = waveform_table[base_resource_id + wave_index];
BEGIN_SAMPLE_LOOP
UPDATE_PHASE
uint16_t sample = InterpolateTwoTables(
wave_1, wave_2,
phase.integral, gain_1, gain_2);
audio_buffer.Overwrite(sample >> 4);
END_SAMPLE_LOOP
}
void DigitalOscillator::RenderNoiseNES() {
uint16_t rng_state = rng_state_;
uint16_t sample = sample_;
BEGIN_SAMPLE_LOOP
phase = U24Add(phase, phase_increment);
if (phase.integral < phase_increment.integral) {
uint8_t tap = rng_state >> 1;
if (shape_ == OSC_NES_NOISE_SHORT) {
tap >>= 5;
}
uint8_t random_bit = (rng_state ^ tap) & 1;
rng_state >>= 1;
if (random_bit) {
rng_state |= 0x4000;
sample = 0x0300;
} else {
sample = 0x0cff;
}
}
audio_buffer.Overwrite(sample);
END_SAMPLE_LOOP
rng_state_ = rng_state;
sample_ = sample;
}
void DigitalOscillator::RenderNoise() {
uint16_t rng_state = rng_state_;
uint16_t sample = sample_;
BEGIN_SAMPLE_LOOP
phase = U24Add(phase, phase_increment);
if (phase.integral < phase_increment.integral) {
rng_state = (rng_state >> 1) ^ (-(rng_state & 1) & 0xb400);
sample = rng_state & 0x0fff;
sample = 512 + ((sample * 3) >> 2);
}
audio_buffer.Overwrite(sample);
END_SAMPLE_LOOP
rng_state_ = rng_state;
sample_ = sample;
}
/* static */
const DigitalOscillator::RenderFn DigitalOscillator::fn_table_[] PROGMEM = {
&DigitalOscillator::RenderBandlimitedTriangle,
&DigitalOscillator::RenderBandlimitedTriangle,
&DigitalOscillator::RenderNoise,
&DigitalOscillator::RenderNoiseNES,
&DigitalOscillator::RenderNoiseNES,
&DigitalOscillator::RenderSine,
};
} // namespace shruthi