Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLAP/VST3 Note Expressions #1341

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions clients/clap-first/scxt-plugin/scxt-plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,21 @@ bool SCXTPlugin::handleEvent(const clap_event_header_t *nextEvent)
handleParamValueEvent(pevt);
}
break;

case CLAP_EVENT_NOTE_EXPRESSION:
{
auto nevt = reinterpret_cast<const clap_event_note_expression *>(nextEvent);
engine->voiceManager.routeNoteExpression(nevt->port_index, nevt->channel, nevt->key,
nevt->note_id, nevt->expression_id,
nevt->value);
}
break;
default:
{
std::cout << __FILE__ << ":" << __LINE__ << " Unhandled event " << nextEvent->type
<< std::endl;
}
break;
}
}
return true;
Expand Down
14 changes: 13 additions & 1 deletion clients/clap-first/scxt-plugin/scxt-plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,23 @@

#include "sst/clap_juce_shim/clap_juce_shim.h"

#include <engine/engine.h>
#include "engine/engine.h"
#include "voice/voice.h"

#include "clap-config.h"
#include "utils.h"

static_assert((int)scxt::voice::Voice::ExpressionIDs::VOLUME == (int)CLAP_NOTE_EXPRESSION_VOLUME);
static_assert((int)scxt::voice::Voice::ExpressionIDs::PAN == (int)CLAP_NOTE_EXPRESSION_PAN);
static_assert((int)scxt::voice::Voice::ExpressionIDs::TUNING == (int)CLAP_NOTE_EXPRESSION_TUNING);
static_assert((int)scxt::voice::Voice::ExpressionIDs::VIBRATO == (int)CLAP_NOTE_EXPRESSION_VIBRATO);
static_assert((int)scxt::voice::Voice::ExpressionIDs::EXPRESSION ==
(int)CLAP_NOTE_EXPRESSION_EXPRESSION);
static_assert((int)scxt::voice::Voice::ExpressionIDs::BRIGHTNESS ==
(int)CLAP_NOTE_EXPRESSION_BRIGHTNESS);
static_assert((int)scxt::voice::Voice::ExpressionIDs::PRESSURE ==
(int)CLAP_NOTE_EXPRESSION_PRESSURE);

namespace scxt::clap_first::scxt_plugin
{

Expand Down
2 changes: 1 addition & 1 deletion src/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ struct Engine : MoveableOnly<Engine>, SampleRateSupport
{
}

void setNoteExpression(voice::Voice *v, int32_t expression, double value) {}
void setNoteExpression(voice::Voice *v, int32_t expression, double value);
void setPolyphonicAftertouch(voice::Voice *v, int8_t pat) {}
void setChannelPressure(voice::Voice *v, int8_t pres) {}
void allSoundsOff() { engine.releaseAllVoices(); }
Expand Down
15 changes: 15 additions & 0 deletions src/engine/engine_voice_responder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,19 @@ void Engine::VoiceManagerResponder::setMIDI1CC(voice::Voice *v, int8_t controlle
auto part = v->zone->parentGroup->parentPart;
part->midiCCSmoothers[controller].setTarget(fv);
}

void Engine::VoiceManagerResponder::setNoteExpression(voice::Voice *v, int32_t expression,
double value)
{
if (expression >= 0 && expression < voice::Voice::noteExpressionCount)
{
// bitwig shows timbre/brightness as bipolar. Correctly?
if (expression == (int)voice::Voice::ExpressionIDs::BRIGHTNESS)
{
value = value * 2 - 1;
}
v->noteExpressions[expression] = value;
}
}

} // namespace scxt::engine
12 changes: 12 additions & 0 deletions src/modulation/voice_matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ void MatrixEndpoints::Sources::bind(scxt::voice::modulation::Matrix &m, engine::
m.bindSourceValue(midiSources.velocitySource, v.velocity);
m.bindSourceValue(midiSources.keytrackSource, v.keytrackPerOct);

m.bindSourceValue(noteExpressions.volume, v.noteExpressions[(int)Voice::ExpressionIDs::VOLUME]);
m.bindSourceValue(noteExpressions.pan, v.noteExpressions[(int)Voice::ExpressionIDs::PAN]);
m.bindSourceValue(noteExpressions.tuning, v.noteExpressions[(int)Voice::ExpressionIDs::TUNING]);
m.bindSourceValue(noteExpressions.vibrato,
v.noteExpressions[(int)Voice::ExpressionIDs::VIBRATO]);
m.bindSourceValue(noteExpressions.expression,
v.noteExpressions[(int)Voice::ExpressionIDs::EXPRESSION]);
m.bindSourceValue(noteExpressions.brightness,
v.noteExpressions[(int)Voice::ExpressionIDs::BRIGHTNESS]);
m.bindSourceValue(noteExpressions.pressure,
v.noteExpressions[(int)Voice::ExpressionIDs::PRESSURE]);

for (int i = 0; i < scxt::numTransportPhasors; ++i)
{
m.bindSourceValue(transportSources.phasors[i], z.getEngine()->transportPhasors[i]);
Expand Down
20 changes: 19 additions & 1 deletion src/modulation/voice_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ struct MatrixEndpoints
struct Sources
{
Sources(engine::Engine *e)
: lfoSources(e), midiSources(e), aegSource{'zneg', 'aeg ', 0},
: lfoSources(e), midiSources(e), noteExpressions(e), aegSource{'zneg', 'aeg ', 0},
eg2Source{'zneg', 'eg2 ', 0}, transportSources(e), rngSources(e), macroSources(e)
{
registerVoiceModSource(e, aegSource, "", "AEG");
Expand All @@ -245,6 +245,24 @@ struct MatrixEndpoints
SR modWheelSource, velocitySource, keytrackSource;
} midiSources;

struct NoteExpressionSources
{
NoteExpressionSources(engine::Engine *e)
: volume{'znte', 'volu'}, pan{'znte', 'pan '}, tuning{'znte', 'tuni'},
vibrato{'znte', 'vibr'}, expression{'znte', 'expr'}, brightness{'znte', 'brit'},
pressure{'znte', 'pres'}
{
registerVoiceModSource(e, volume, "Note Expressions", "Volume");
registerVoiceModSource(e, pan, "Note Expressions", "Pan");
registerVoiceModSource(e, tuning, "Note Expressions", "Tuning");
registerVoiceModSource(e, vibrato, "Note Expressions", "Vibrato");
registerVoiceModSource(e, expression, "Note Expressions", "Expression");
registerVoiceModSource(e, brightness, "Note Expressions", "Brightness");
registerVoiceModSource(e, pressure, "Note Expressions", "Pressure");
}
SR volume, pan, tuning, vibrato, expression, brightness, pressure;
} noteExpressions;

TransportSourceBase<SR, 'ztsp', true, registerVoiceModSource> transportSources;

struct RNGSources
Expand Down
17 changes: 14 additions & 3 deletions src/voice/voice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* https://github.com/surge-synthesizer/shortcircuit-xt
*/

#include "voice.h"
#include "voice.h"
#include "tuning/equal.h"
#include <cassert>
Expand Down Expand Up @@ -82,6 +83,13 @@ void Voice::cleanupVoice()

void Voice::voiceStarted()
{
noteExpressions[(int)ExpressionIDs::VOLUME] = 1.0;
noteExpressions[(int)ExpressionIDs::PAN] = 0.5;
for (int i = (int)ExpressionIDs::TUNING; i <= (int)ExpressionIDs::PRESSURE; ++i)
{
noteExpressions[i] = 0.0;
}

forceOversample = zone->parentGroup->outputInfo.oversample;

lfosActive = zone->lfosActive;
Expand Down Expand Up @@ -467,7 +475,9 @@ template <bool OS> bool Voice::processWithOS()
/*
* Implement output pan
*/
auto pvo = *endpoints->outputTarget.panP;
auto pvo = std::clamp(*endpoints->outputTarget.panP +
2.0 * (noteExpressions[(int)ExpressionIDs::PAN] - 0.5),
-1., 1.);
if (pvo != 0.f)
{
outputPan.set_target(pvo);
Expand All @@ -486,7 +496,7 @@ template <bool OS> bool Voice::processWithOS()

if constexpr (OS)
{
outputAmpOS.set_target(pao * pao * pao);
outputAmpOS.set_target(pao * pao * pao * noteExpressions[(int)ExpressionIDs::VOLUME]);
if (chainIsMono)
{
outputAmpOS.multiply_block(output[0]);
Expand All @@ -498,7 +508,7 @@ template <bool OS> bool Voice::processWithOS()
}
else
{
outputAmp.set_target(pao * pao * pao);
outputAmp.set_target(pao * pao * pao * noteExpressions[(int)ExpressionIDs::VOLUME]);
if (chainIsMono)
{
outputAmp.multiply_block(output[0]);
Expand Down Expand Up @@ -665,6 +675,7 @@ float Voice::calculateVoicePitch()
zone->getEngine()->midikeyRetuner.retuneRemappedKey(channel, key, originalMidiKey);

fpitch += retuner;
fpitch += noteExpressions[(int)ExpressionIDs::TUNING];

keytrackPerOct = (key + retuner - zone->mapping.rootKey) / 12.0;

Expand Down
14 changes: 14 additions & 0 deletions src/voice/voice.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ struct alignas(16) Voice : MoveableOnly<Voice>,
float velocity{1.f};
float velKeyFade{1.f};
float keytrackPerOct{0.f}; // resolvee key - pitch cnter / 12
static constexpr size_t noteExpressionCount{7};
float noteExpressions[noteExpressionCount]{};
// These are the same as teh CLAP expression IDs but I dont want to include
// clap.h here
enum struct ExpressionIDs
{
VOLUME = 0,
PAN = 1,
TUNING = 2,
VIBRATO = 3,
EXPRESSION = 4,
BRIGHTNESS = 5,
PRESSURE = 6
};

scxt::voice::modulation::Matrix modMatrix;
std::unique_ptr<modulation::MatrixEndpoints> endpoints;
Expand Down
Loading