-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
171 lines (142 loc) · 4.27 KB
/
main.c
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
#include "userosc.h"
#define POLYPHONY 4
enum modes {
MODE_POLY = 0,
MODE_UNISON,
MODE_OCTAVE,
MODE_FIFTH,
MODE_UNISON_RING,
MODE_POLY_RING
} mode;
static float drve = 0;
static float detune = 0;
static int notes = 0;
static int slot = 0;
static uint8_t note_l;
static uint8_t note[POLYPHONY] = { 0 };
static float nidx[POLYPHONY] = { 0 };
static float phi[POLYPHONY] = { 0.0f };
static float w[POLYPHONY];
void OSC_INIT(uint32_t platform, uint32_t api) {
(void)platform; (void)api;
}
void OSC_CYCLE(const user_osc_param_t * const params,
int32_t *yn,
const uint32_t frames) {
const uint8_t note_c = (params->pitch) >> 8;
const uint8_t mod_c = (params->pitch) & 0x00FF;
switch (mode) {
case MODE_POLY:
case MODE_POLY_RING:
if (!notes || note_c != note_l) {
note_l = note_c;
note[slot] = note_c;
nidx[slot++] = mode == MODE_POLY
? _osc_bl_saw_idx((float)note_c)
: _osc_bl_sqr_idx((float)note_c);
slot %= POLYPHONY;
if (notes < POLYPHONY)
notes++;
}
break;
case MODE_OCTAVE:
case MODE_FIFTH:
slot = 0;
notes = POLYPHONY;
note_l = note_c;
for (int i = 0; i < (POLYPHONY - 1); i += 2) {
note[i] = note_c;
nidx[i] = _osc_bl_saw_idx((float)note[i]);
note[i + 1] = note_c + (mode == MODE_OCTAVE ? 12 : 7);
nidx[i + 1] = _osc_bl_saw_idx((float)note[i + 1]);
}
break;
case MODE_UNISON:
case MODE_UNISON_RING:
slot = 0;
notes = POLYPHONY;
note_l = note_c;
for (int i = 0; i < POLYPHONY; i++) {
note[i] = note_c;
nidx[i] = mode == MODE_UNISON
? _osc_bl_saw_idx((float)note_c)
: _osc_bl_sqr_idx((float)note_c);
}
break;
}
for (int i = 0; i < notes; i++) {
const float d = i - (POLYPHONY / 2) < 0 ? -1.0f * detune: 1.0f * detune;
const float f0 = osc_notehzf(note[i]);
const float f1 = osc_notehzf(note[i] + 1);
const float f = clipmaxf(linintf(mod_c * k_note_mod_fscale + d, f0, f1),
k_note_max_hz);
w[i] = f * k_samplerate_recipf
+ _osc_white() * 0.00001f;
}
const float drve_c = drve / 3;
const float drve_lev = drve * (1.0f / (1.0f - drve_c));
const float drve_1m = 1.0f - drve;
const float sig_init = ((mode == MODE_POLY_RING
|| mode == MODE_UNISON_RING)
? 0.9f
: 0.0f);
q31_t * __restrict y = (q31_t *)yn;
const q31_t * y_e = y + frames;
for ( ; y != y_e; ) {
float sig = sig_init;
for (int i = 0; i < notes; i++) {
if (mode == MODE_POLY_RING || mode == MODE_UNISON_RING)
sig *= osc_bl2_sqrf(phi[i], nidx[i]);
else
sig += (1.0f / POLYPHONY) * osc_bl2_sawf(phi[i], nidx[i]);
phi[i] += w[i];
phi[i] -= (uint32_t)phi[i];
}
sig = drve_lev * osc_softclipf(drve_c, sig) + drve_1m * sig;
*(y++) = f32_to_q31(sig);
}
}
void OSC_NOTEON(const user_osc_param_t * const params) {
(void)params;
/* On the NTS-1 (being designed as a monophonic synth in persistent
* legato mode) OSC_NOTEON will only be called for the first note
* after all previously pressed keys have been released. Also,
* OSC_NOTEOFF will only be called when all keys have been released
* and not for single note offs (with the interesting consequence
* that single notes in a chord can't be released on their own -
* which gives this oscillator implementation just one more quirk
* that contributes to its charm).
*
* So we can reset all previously playing notes here at once when we
* start a new chord/sequence.
*/
notes = slot = 0;
/* Randomize phase offset for all the oscillators.
*/
for (int i = 0; i < notes; i++) {
phi[i] = si_fabsf(_osc_white());
}
}
void OSC_NOTEOFF(const user_osc_param_t * const params) {
(void)params;
/* Reset notes only on OSC_NOTEON (when we start a new chord or
* note sequence) instead of here, to not interrupt the sound
* during the AMP EG release phase.
*/
}
void OSC_PARAM(uint16_t idx, uint16_t val) {
switch (idx) {
case k_user_osc_param_id1:
mode = val;
break;
case k_user_osc_param_id2:
drve = clip01f((float)val / 100.f);
break;
case k_user_osc_param_shape:
detune = param_val_to_f32(val);
break;
case k_user_osc_param_shiftshape:
(void)param_val_to_f32(val);
break;
}
}