File: DistrhoPluginNekobi.cpp

package info (click to toggle)
dpf-plugins 1.6%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 145,284 kB
  • sloc: cpp: 299,382; ansic: 61,509; objc: 2,715; makefile: 2,076; xml: 618; sh: 462; java: 211; python: 184
file content (423 lines) | stat: -rw-r--r-- 13,635 bytes parent folder | download
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * DISTRHO Nekobi Plugin, based on Nekobee by Sean Bolton and others.
 * Copyright (C) 2004 Sean Bolton and others
 * Copyright (C) 2013-2022 Filipe Coelho <falktx@falktx.com>
 *
 * 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 2 of
 * the License, or 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.
 *
 * For a full copy of the GNU General Public License see the LICENSE file.
 */

#include "DistrhoPluginNekobi.hpp"

extern "C" {

#include "nekobee-src/nekobee_synth.c"
#include "nekobee-src/nekobee_voice.c"
#include "nekobee-src/nekobee_voice_render.c"
#include "nekobee-src/minblep_tables.c"

// -----------------------------------------------------------------------
// mutual exclusion

bool dssp_voicelist_mutex_trylock(nekobee_synth_t* const synth)
{
    /* Attempt the mutex lock */
    if (pthread_mutex_trylock(&synth->voicelist_mutex) != 0)
    {
        synth->voicelist_mutex_grab_failed = 1;
        return false;
    }

    /* Clean up if a previous mutex grab failed */
    if (synth->voicelist_mutex_grab_failed)
    {
        nekobee_synth_all_voices_off(synth);
        synth->voicelist_mutex_grab_failed = 0;
    }

    return true;
}

bool dssp_voicelist_mutex_unlock(nekobee_synth_t* const synth)
{
    return (pthread_mutex_unlock(&synth->voicelist_mutex) == 0);
}

// -----------------------------------------------------------------------
// nekobee_handle_raw_event

void nekobee_handle_raw_event(nekobee_synth_t* const synth, const uint8_t size, const uint8_t* const data)
{
    if (size != 3)
        return;

    switch (data[0] & 0xf0)
    {
    case 0x80:
        nekobee_synth_note_off(synth, data[1], data[2]);
        break;
    case 0x90:
        if (data[2] > 0)
            nekobee_synth_note_on(synth, data[1], data[2]);
        else
            nekobee_synth_note_off(synth, data[1], 64); /* shouldn't happen, but... */
        break;
    case 0xB0:
        nekobee_synth_control_change(synth, data[1], data[2]);
        break;
    default:
        break;
    }
}

} /* extern "C" */

START_NAMESPACE_DISTRHO

// -----------------------------------------------------------------------

DistrhoPluginNekobi::DistrhoPluginNekobi()
    : Plugin(paramCount, 0, 0) // 0 programs, 0 states
{
    nekobee_init_tables();

    // init synth
    fSynth.sample_rate = getSampleRate();
    fSynth.deltat = 1.0f / (float)getSampleRate();
    fSynth.nugget_remains = 0;

    fSynth.note_id = 0;
    fSynth.polyphony = XSYNTH_DEFAULT_POLYPHONY;
    fSynth.voices = XSYNTH_DEFAULT_POLYPHONY;
    fSynth.monophonic = XSYNTH_MONO_MODE_ONCE;
    fSynth.glide = 0;
    fSynth.last_noteon_pitch = 0.0f;
    fSynth.vcf_accent = 0.0f;
    fSynth.vca_accent = 0.0f;

    for (int i=0; i<8; ++i)
        fSynth.held_keys[i] = -1;

    fSynth.voice = nekobee_voice_new();
    fSynth.voicelist_mutex_grab_failed = 0;
    pthread_mutex_init(&fSynth.voicelist_mutex, nullptr);

    fSynth.channel_pressure = 0;
    fSynth.pitch_wheel_sensitivity = 0;
    fSynth.pitch_wheel = 0;

    for (int i=0; i<128; ++i)
    {
        fSynth.key_pressure[i] = 0;
        fSynth.cc[i] = 0;
    }
    fSynth.cc[7] = 127; // full volume

    fSynth.mod_wheel  = 1.0f;
    fSynth.pitch_bend = 1.0f;
    fSynth.cc_volume  = 1.0f;

    // Default values
    fParams.waveform = 0.0f;
    fParams.tuning = 0.0f;
    fParams.cutoff = 25.0f;
    fParams.resonance = 25.0f;
    fParams.envMod = 50.0f;
    fParams.decay  = 75.0f;
    fParams.accent = 25.0f;
    fParams.volume = 75.0f;

    // Internal stuff
    fSynth.waveform  = 0.0f;
    fSynth.tuning    = 1.0f;
    fSynth.cutoff    = 5.0f;
    fSynth.resonance = 0.8f;
    fSynth.envmod    = 0.3f;
    fSynth.decay     = 0.0002f;
    fSynth.accent    = 0.3f;
    fSynth.volume    = 0.75f;

    // reset
    deactivate();
}

DistrhoPluginNekobi::~DistrhoPluginNekobi()
{
    std::free(fSynth.voice);
}

// -----------------------------------------------------------------------
// Init

void DistrhoPluginNekobi::initAudioPort(bool input, uint32_t index, AudioPort& port)
{
    port.groupId = kPortGroupMono;

    Plugin::initAudioPort(input, index, port);
}

void DistrhoPluginNekobi::initParameter(uint32_t index, Parameter& parameter)
{
    switch (index)
    {
    case paramWaveform:
        parameter.hints      = kParameterIsAutomatable|kParameterIsInteger;
        parameter.name       = "Waveform";
        parameter.symbol     = "waveform";
        parameter.ranges.def = 0.0f;
        parameter.ranges.min = 0.0f;
        parameter.ranges.max = 1.0f;
        parameter.enumValues.count = 2;
        parameter.enumValues.restrictedMode = true;
        parameter.midiCC = 70; //Sound Variation
        {
            ParameterEnumerationValue* const enumValues = new ParameterEnumerationValue[2];
            enumValues[0].value = 0.0f;
            enumValues[0].label = "Square";
            enumValues[1].value = 1.0f;
            enumValues[1].label = "Triangle";
            parameter.enumValues.values = enumValues;
        }
        break;
    case paramTuning:
        parameter.hints      = kParameterIsAutomatable; // was 0.5 <-> 2.0, log
        parameter.name       = "Tuning";
        parameter.symbol     = "tuning";
        parameter.ranges.def = 0.0f;
        parameter.ranges.min = -12.0f;
        parameter.ranges.max = 12.0f;
        parameter.midiCC = 75;
        break;
    case paramCutoff:
        parameter.hints      = kParameterIsAutomatable; // modified x2.5
        parameter.name       = "Cutoff";
        parameter.symbol     = "cutoff";
        parameter.unit       = "%";
        parameter.ranges.def = 25.0f;
        parameter.ranges.min = 0.0f;
        parameter.ranges.max = 100.0f;
        parameter.midiCC = 74;
        break;
    case paramResonance:
        parameter.hints      = kParameterIsAutomatable; // modified x100
        parameter.name       = "VCF Resonance";
        parameter.symbol     = "resonance";
        parameter.unit       = "%";
        parameter.ranges.def = 25.0f;
        parameter.ranges.min = 0.0f;
        parameter.ranges.max = 95.0f;
        parameter.midiCC = 71;
        break;
    case paramEnvMod:
        parameter.hints      = kParameterIsAutomatable; // modified x100
        parameter.name       = "Env Mod";
        parameter.symbol     = "env_mod";
        parameter.unit       = "%";
        parameter.ranges.def = 50.0f;
        parameter.ranges.min = 0.0f;
        parameter.ranges.max = 100.0f;
        parameter.midiCC = 1; //Mod Wheel
        break;
    case paramDecay:
        parameter.hints      = kParameterIsAutomatable; // was 0.000009 <-> 0.0005, log
        parameter.name       = "Decay";
        parameter.symbol     = "decay";
        parameter.unit       = "%";
        parameter.ranges.def = 75.0f;
        parameter.ranges.min = 0.0f;
        parameter.ranges.max = 100.0f;
        parameter.midiCC = 72;
        break;
    case paramAccent:
        parameter.hints      = kParameterIsAutomatable; // modified x100
        parameter.name       = "Accent";
        parameter.symbol     = "accent";
        parameter.unit       = "%";
        parameter.ranges.def = 25.0f;
        parameter.ranges.min = 0.0f;
        parameter.ranges.max = 100.0f;
        parameter.midiCC = 76;
        break;
    case paramVolume:
        parameter.hints      = kParameterIsAutomatable; // modified x100
        parameter.name       = "Volume";
        parameter.symbol     = "volume";
        parameter.unit       = "%";
        parameter.ranges.def = 75.0f;
        parameter.ranges.min = 0.0f;
        parameter.ranges.max = 100.0f;
        parameter.midiCC = 7; //Volume
        break;
    }
}

// -----------------------------------------------------------------------
// Internal data

float DistrhoPluginNekobi::getParameterValue(uint32_t index) const
{
    switch (index)
    {
    case paramWaveform:
        return fParams.waveform;
    case paramTuning:
        return fParams.tuning;
    case paramCutoff:
        return fParams.cutoff;
    case paramResonance:
        return fParams.resonance;
    case paramEnvMod:
        return fParams.envMod;
    case paramDecay:
        return fParams.decay;
    case paramAccent:
        return fParams.accent;
    case paramVolume:
        return fParams.volume;
    }

    return 0.0f;
}

void DistrhoPluginNekobi::setParameterValue(uint32_t index, float value)
{
    switch (index)
    {
    case paramWaveform:
        fParams.waveform = value;
        fSynth.waveform = value;
        DISTRHO_SAFE_ASSERT(fSynth.waveform == 0.0f || fSynth.waveform == 1.0f);
        break;
    case paramTuning:
        fParams.tuning = value;
        fSynth.tuning = (value+12.0f)/24.0f * 1.5 + 0.5f; // FIXME: log?
        DISTRHO_SAFE_ASSERT(fSynth.tuning >= 0.5f && fSynth.tuning <= 2.0f);
        break;
    case paramCutoff:
        fParams.cutoff = value;
        fSynth.cutoff = value/2.5f;
        DISTRHO_SAFE_ASSERT(fSynth.cutoff >= 0.0f && fSynth.cutoff <= 40.0f);
        break;
    case paramResonance:
        fParams.resonance = value;
        fSynth.resonance = value/100.0f;
        DISTRHO_SAFE_ASSERT(fSynth.resonance >= 0.0f && fSynth.resonance <= 0.95f);
        break;
    case paramEnvMod:
        fParams.envMod = value;
        fSynth.envmod = value/100.0f;
        DISTRHO_SAFE_ASSERT(fSynth.envmod >= 0.0f && fSynth.envmod <= 1.0f);
        break;
    case paramDecay:
        fParams.decay = value;
        fSynth.decay = value/100.0f * 0.000491f + 0.000009f; // FIXME: log?
        DISTRHO_SAFE_ASSERT(fSynth.decay >= 0.000009f && fSynth.decay <= 0.0005f);
        break;
    case paramAccent:
        fParams.accent = value;
        fSynth.accent = value/100.0f;
        DISTRHO_SAFE_ASSERT(fSynth.accent >= 0.0f && fSynth.accent <= 1.0f);
        break;
    case paramVolume:
        fParams.volume = value;
        fSynth.volume = value/100.0f;
        DISTRHO_SAFE_ASSERT(fSynth.volume >= 0.0f && fSynth.volume <= 1.0f);
        break;
    }
}

// -----------------------------------------------------------------------
// Process

void DistrhoPluginNekobi::activate()
{
    fSynth.nugget_remains = 0;
    fSynth.note_id = 0;

    if (fSynth.voice != nullptr)
        nekobee_synth_all_voices_off(&fSynth);
}

void DistrhoPluginNekobi::deactivate()
{
    if (fSynth.voice != nullptr)
        nekobee_synth_all_voices_off(&fSynth);
}

void DistrhoPluginNekobi::run(const float**, float** outputs, uint32_t frames, const MidiEvent* midiEvents, uint32_t midiEventCount)
{
    uint32_t framesDone = 0;
    uint32_t curEventIndex = 0;
    uint32_t burstSize;

    float* out = outputs[0];

    if (fSynth.voice == nullptr || ! dssp_voicelist_mutex_trylock(&fSynth))
    {
        std::memset(out, 0, sizeof(float)*frames);
        return;
    }

    while (framesDone < frames)
    {
        if (fSynth.nugget_remains == 0)
            fSynth.nugget_remains = XSYNTH_NUGGET_SIZE;

        /* process any ready events */
        for (; curEventIndex < midiEventCount && framesDone == midiEvents[curEventIndex].frame; ++curEventIndex)
        {
            if (midiEvents[curEventIndex].size > MidiEvent::kDataSize)
                continue;

            nekobee_handle_raw_event(&fSynth, midiEvents[curEventIndex].size, midiEvents[curEventIndex].data);
        }

        /* calculate the sample count (burstSize) for the next nekobee_voice_render() call to be the smallest of:
         * - control calculation quantization size (XSYNTH_NUGGET_SIZE, in samples)
         * - the number of samples remaining in an already-begun nugget (synth->nugget_remains)
         * - the number of samples until the next event is ready
         * - the number of samples left in this run
         */
        burstSize = XSYNTH_NUGGET_SIZE;

        /* we're still in the middle of a nugget, so reduce the burst size
         * to end when the nugget ends */
        if (fSynth.nugget_remains < burstSize)
            burstSize = fSynth.nugget_remains;

        /* reduce burst size to end when next event is ready */
        if (curEventIndex < midiEventCount && midiEvents[curEventIndex].frame - framesDone < burstSize)
            burstSize = midiEvents[curEventIndex].frame - framesDone;

        /* reduce burst size to end at end of this run */
        if (frames - framesDone < burstSize)
            burstSize = frames - framesDone;

        /* render the burst */
        nekobee_synth_render_voices(&fSynth, out + framesDone, burstSize, (burstSize == fSynth.nugget_remains));
        framesDone += burstSize;
        fSynth.nugget_remains -= burstSize;
    }

    dssp_voicelist_mutex_unlock(&fSynth);
}

// -----------------------------------------------------------------------

Plugin* createPlugin()
{
    return new DistrhoPluginNekobi();
}

// -----------------------------------------------------------------------

END_NAMESPACE_DISTRHO