-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathosc.h
291 lines (226 loc) · 8.78 KB
/
osc.h
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
#pragma once
/*
BSD 3-Clause License
Copyright (c) 2023, KORG INC.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//*/
/*
* File: osc.h
*
* Dummy oscillator template instance.
*
*/
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <climits>
#include "unit_osc.h" // Note: Include base definitions for osc units
#include "utils/int_math.h" // for clipminmaxi32()
class Osc {
public:
/*===========================================================================*/
/* Public Data Structures/Types/Enums. */
/*===========================================================================*/
enum {
SHAPE = 0U,
ALT,
PARAM3,
NUM_PARAMS
};
// Note: Make sure that default param values correspond to declarations in header.c
struct Params {
float shape{0.f};
float alt{0.f};
uint32_t param3{1};
void reset() {
shape = 0.f;
alt = 0.f;
param3 = 1;
}
};
enum {
PARAM3_VALUE0 = 0,
PARAM3_VALUE1,
PARAM3_VALUE2,
PARAM3_VALUE3,
NUM_PARAM3_VALUES,
};
/*===========================================================================*/
/* Lifecycle Methods. */
/*===========================================================================*/
Osc(void) {}
~Osc(void) {} // Note: will never actually be called for statically allocated instances
inline int8_t Init(const unit_runtime_desc_t * desc) {
if (!desc)
return k_unit_err_undef;
// Note: make sure the unit is being loaded to the correct platform/module target
if (desc->target != unit_header.target)
return k_unit_err_target;
// Note: check API compatibility with the one this unit was built against
if (!UNIT_API_IS_COMPAT(desc->api))
return k_unit_err_api_version;
// Check compatibility of samplerate with unit, for NTS-1 MKII should be 48000
if (desc->samplerate != 48000)
return k_unit_err_samplerate;
// Check compatibility of frame geometry
// Note: NTS-1 mkII oscillators can make use of the audio input depending on the routing options in global settings, see product documentation for details.
if (desc->input_channels != 2 || desc->output_channels != 1) // should be stereo input / mono output
return k_unit_err_geometry;
// Note: SDRAM is not available from the oscillator runtime environment
// Cache the runtime descriptor for later use
runtime_desc_ = *desc;
// Make sure parameters are reset to default values
params_.reset();
return k_unit_err_none;
}
inline void Teardown() {
// Note: cleanup and release resources if any
}
inline void Reset() {
// Note: Reset effect state, excluding exposed parameter values.
}
inline void Resume() {
// Note: Effect will resume and exit suspend state. Usually means the synth
// was selected and the render callback will be called again
}
inline void Suspend() {
// Note: Effect will enter suspend state. Usually means another effect was
// selected and thus the render callback will not be called
}
/*===========================================================================*/
/* Other Public Methods. */
/*===========================================================================*/
fast_inline void Process(const float * in, float * out, size_t frames) {
const float * __restrict in_p = in;
float * __restrict out_p = out;
const float * out_e = out_p + frames; // assuming mono output
// Caching current parameter values. Consider interpolating sensitive parameters.
// const Params p = params_;
for (; out_p != out_e; in_p += 2, out_p += 1) {
// Process/generate samples here
// Note: this is a dummy unit only to demonstrate APIs, only outputting silence.
*out_p = 0.f; // sample
}
}
inline void setParameter(uint8_t index, int32_t value) {
switch (index) {
case SHAPE:
// 10bit 0-1023 parameter
value = clipminmaxi32(0, value, 1023);
params_.shape = param_10bit_to_f32(value); // 0 .. 1023 -> 0.0 .. 1.0
break;
case ALT:
// 10bit 0-1023 parameter
value = clipminmaxi32(0, value, 1023);
params_.alt = param_10bit_to_f32(value); // 0 .. 1023 -> 0.0 .. 1.0
break;
case PARAM3:
// strings type parameter, receiving index value
value = clipminmaxi32(PARAM3_VALUE0, value, NUM_PARAM3_VALUES-1);
params_.param3 = value;
break;
default:
break;
}
}
inline int32_t getParameterValue(uint8_t index) const {
switch (index) {
case SHAPE:
// 10bit 0-1023 parameter
return param_f32_to_10bit(params_.shape);
break;
case ALT:
// 10bit 0-1023 parameter
return param_f32_to_10bit(params_.alt);
break;
case PARAM3:
// strings type parameter, return index value
return params_.param3;
default:
break;
}
return INT_MIN; // Note: will be handled as invalid
}
inline const char * getParameterStrValue(uint8_t index, int32_t value) const {
// Note: String memory must be accessible even after function returned.
// It can be assumed that caller will have copied or used the string
// before the next call to getParameterStrValue
static const char * param3_strings[NUM_PARAM3_VALUES] = {
"VAL 0",
"VAL 1",
"VAL 2",
"VAL 3",
};
switch (index) {
case PARAM3:
if (value >= PARAM3_VALUE0 && value < NUM_PARAM3_VALUES)
return param3_strings[value];
break;
default:
break;
}
return nullptr;
}
inline void setTempo(uint32_t tempo) {
// const float bpmf = (tempo >> 16) + (tempo & 0xFFFF) / static_cast<float>(0x10000);
(void)tempo;
}
inline void tempo4ppqnTick(uint32_t counter) {
(void)counter;
}
inline void NoteOn(uint8_t note, uint8_t velo) {
(uint8_t)note;
(uint8_t)velo;
}
inline void NoteOff(uint8_t note) {
(uint8_t)note;
}
inline void AllNoteOff() {
}
inline void PitchBend(uint8_t bend) {
(uint8_t)bend;
}
inline void ChannelPressure(uint8_t press) {
(uint8_t)press;
}
inline void AfterTouch(uint8_t note, uint8_t press) {
(uint8_t)note;
(uint8_t)press;
}
/*===========================================================================*/
/* Static Members. */
/*===========================================================================*/
private:
/*===========================================================================*/
/* Private Member Variables. */
/*===========================================================================*/
std::atomic_uint_fast32_t flags_;
unit_runtime_desc_t runtime_desc_;
Params params_;
/*===========================================================================*/
/* Private Methods. */
/*===========================================================================*/
/*===========================================================================*/
/* Constants. */
/*===========================================================================*/
};