From aa9a5d26f4b4a86659bf947072eeba13d75d3f0f Mon Sep 17 00:00:00 2001 From: Jean Pierre Cimalando Date: Fri, 6 Mar 2020 11:46:49 +0100 Subject: [PATCH] Add the polyphony parameter --- vst/SfizzVstController.cpp | 29 +++++++++++++++++++++++++---- vst/SfizzVstEditor.cpp | 32 +++++++++++++++++++++----------- vst/SfizzVstEditor.h | 2 ++ vst/SfizzVstProcessor.cpp | 23 +++++++++++++++++++++++ vst/SfizzVstState.cpp | 6 ++++++ vst/SfizzVstState.h | 3 +++ 6 files changed, 80 insertions(+), 15 deletions(-) diff --git a/vst/SfizzVstController.cpp b/vst/SfizzVstController.cpp index fc7cfe5e..eba85ca2 100644 --- a/vst/SfizzVstController.cpp +++ b/vst/SfizzVstController.cpp @@ -22,6 +22,10 @@ tresult PLUGIN_API SfizzVstControllerNoUi::initialize(FUnknown* context) kParamVolumeRange.createParameter( Steinberg::String("Volume"), pid++, Steinberg::String("dB"), 0, Vst::ParameterInfo::kCanAutomate, Vst::kRootUnitId)); + parameters.addParameter( + kParamNumVoicesRange.createParameter( + Steinberg::String("Polyphony"), pid++, nullptr, + 0, Vst::ParameterInfo::kNoFlags, Vst::kRootUnitId)); // MIDI controllers for (unsigned i = 0; i < kNumControllerParams; ++i) { @@ -85,19 +89,35 @@ tresult PLUGIN_API SfizzVstController::setParamNormalized(Vst::ParamID tag, Vst: if (r != kResultTrue) return r; - float *slot = nullptr; + float *slotF32 = nullptr; + int32 *slotI32 = nullptr; float value = 0; switch (tag) { case kPidVolume: { - slot = &_state.volume; + slotF32 = &_state.volume; value = kParamVolumeRange.denormalize(normValue); break; } + case kPidNumVoices: { + slotI32 = &_state.numVoices; + value = kParamNumVoicesRange.denormalize(normValue); + break; + } } - if (slot && *slot != value) { - *slot = value; + bool update = false; + + if (slotF32 && *slotF32 != value) { + *slotF32 = value; + update = true; + } + else if (slotI32 && *slotI32 != (int32)value) { + *slotI32 = (int32)value; + update = true; + } + + if (update) { for (StateListener* listener : _stateListeners) listener->onStateChanged(); } @@ -137,6 +157,7 @@ tresult PLUGIN_API SfizzVstController::setComponentState(IBStream* state) _state = s; setParamNormalized(kPidVolume, kParamVolumeRange.normalize(s.volume)); + setParamNormalized(kPidNumVoices, kParamNumVoicesRange.normalize(s.numVoices)); for (StateListener* listener : _stateListeners) listener->onStateChanged(); diff --git a/vst/SfizzVstEditor.cpp b/vst/SfizzVstEditor.cpp index 9b197331..d3079626 100644 --- a/vst/SfizzVstEditor.cpp +++ b/vst/SfizzVstEditor.cpp @@ -76,6 +76,11 @@ void SfizzVstEditor::valueChanged(CControl* ctl) controller->performEdit(kPidVolume, valueNorm); break; + case kTagSetNumVoices: + controller->setParamNormalized(kPidNumVoices, valueNorm); + controller->performEdit(kPidNumVoices, valueNorm); + break; + default: if (tag >= kTagFirstChangePanel && tag <= kTagLastChangePanel) setActivePanel(tag - kTagFirstChangePanel); @@ -90,6 +95,7 @@ void SfizzVstEditor::enterOrLeaveEdit(CControl* ctl, bool enter) switch (tag) { case kTagSetVolume: id = kPidVolume; break; + case kTagSetNumVoices: id = kPidNumVoices; break; default: return; } @@ -231,21 +237,23 @@ void SfizzVstEditor::createFrameContents() label->setHoriAlign(kLeftText); panel->addView(label); slider = new SimpleSlider(rightSide(), this, kTagSetVolume); - adjustMinMaxToRangeParam(slider, kPidVolume); panel->addView(slider); + adjustMinMaxToRangeParam(slider, kPidVolume); _volumeSlider = slider; - // row.top += interRow; - // row.bottom += interRow; + row.top += interRow; + row.bottom += interRow; - // label = new CTextLabel(leftSide(), "Polyphony"); - // label->setFontColor(CColor(0x00, 0x00, 0x00)); - // label->setFrameColor(CColor(0x00, 0x00, 0x00, 0x00)); - // label->setBackColor(CColor(0x00, 0x00, 0x00, 0x00)); - // label->setHoriAlign(kLeftText); - // panel->addView(label); - // slider = new SimpleSlider(rightSide(), this, -1); - // panel->addView(slider); + label = new CTextLabel(leftSide(), "Polyphony"); + label->setFontColor(CColor(0x00, 0x00, 0x00)); + label->setFrameColor(CColor(0x00, 0x00, 0x00, 0x00)); + label->setBackColor(CColor(0x00, 0x00, 0x00, 0x00)); + label->setHoriAlign(kLeftText); + panel->addView(label); + slider = new SimpleSlider(rightSide(), this, kTagSetNumVoices); + panel->addView(slider); + adjustMinMaxToRangeParam(slider, kPidNumVoices); + _numVoicesSlider = slider; // row.top += interRow; // row.bottom += interRow; @@ -331,6 +339,8 @@ void SfizzVstEditor::updateStateDisplay() _fileLabel->setText(("File: " + state.sfzFile).c_str()); if (_volumeSlider) _volumeSlider->setValue(state.volume); + if (_numVoicesSlider) + _numVoicesSlider->setValue(state.numVoices); setActivePanel(uiState.activePanel); } diff --git a/vst/SfizzVstEditor.h b/vst/SfizzVstEditor.h index 1acd7492..e71bd284 100644 --- a/vst/SfizzVstEditor.h +++ b/vst/SfizzVstEditor.h @@ -62,6 +62,7 @@ private: enum { kTagLoadSfzFile, kTagSetVolume, + kTagSetNumVoices, kTagFirstChangePanel, kTagLastChangePanel = kTagFirstChangePanel + kNumPanels - 1, }; @@ -69,4 +70,5 @@ private: CBitmap _logo; CTextLabel* _fileLabel = nullptr; CSliderBase *_volumeSlider = nullptr; + CSliderBase *_numVoicesSlider = nullptr; }; diff --git a/vst/SfizzVstProcessor.cpp b/vst/SfizzVstProcessor.cpp index 6a7160df..5cdafaf9 100644 --- a/vst/SfizzVstProcessor.cpp +++ b/vst/SfizzVstProcessor.cpp @@ -80,6 +80,7 @@ void SfizzVstProcessor::syncStateToSynth() synth->loadSfzFile(_state.sfzFile); synth->setVolume(_state.volume); + synth->setNumVoices(_state.numVoices); } tresult PLUGIN_API SfizzVstProcessor::canProcessSampleSize(int32 symbolicSampleSize) @@ -173,6 +174,21 @@ void SfizzVstProcessor::processParameterChanges(Vst::IParameterChanges& pc) if (pointCount > 0 && vq->getPoint(pointCount - 1, sampleOffset, value) == kResultTrue) _state.volume = kParamVolumeRange.denormalize(value); break; + case kPidNumVoices: + if (pointCount > 0 && vq->getPoint(pointCount - 1, sampleOffset, value) == kResultTrue) { + Vst::IMessage* msg = allocateMessage(); + if (!msg) + break; + msg->setMessageID("SetNumVoices"); + Vst::IAttributeList* attr = msg->getAttributes(); + attr->setInt("NumVoices", kParamNumVoicesRange.denormalize(value)); + if (!_fifoToWorker.push(msg)) { + msg->release(); + break; + } + _semaToWorker.post(); + } + break; } } } @@ -296,6 +312,13 @@ void SfizzVstProcessor::doBackgroundWork() _synth->loadSfzFile(_state.sfzFile); } } + else if (!std::strcmp(id, "SetNumVoices")) { + int64 value; + if (attr->getInt("NumVoices", value) == kResultTrue) { + _state.numVoices = value; + _synth->setNumVoices(value); + } + } msg->release(); } diff --git a/vst/SfizzVstState.cpp b/vst/SfizzVstState.cpp index fffeaae2..6c951314 100644 --- a/vst/SfizzVstState.cpp +++ b/vst/SfizzVstState.cpp @@ -24,6 +24,9 @@ tresult SfizzVstState::load(IBStream* state) if (!s.readFloat(volume)) return kResultFalse; + if (!s.readInt32(numVoices)) + return kResultFalse; + return kResultTrue; } @@ -40,6 +43,9 @@ tresult SfizzVstState::store(IBStream* state) const if (!s.writeFloat(volume)) return kResultFalse; + if (!s.writeInt32(numVoices)) + return kResultFalse; + return kResultTrue; } diff --git a/vst/SfizzVstState.h b/vst/SfizzVstState.h index f1cfc563..f68412f6 100644 --- a/vst/SfizzVstState.h +++ b/vst/SfizzVstState.h @@ -19,6 +19,7 @@ enum { // parameters enum { kPidVolume, + kPidNumVoices, kPidMidiCC0, kPidMidiCCLast = kPidMidiCC0 + kNumControllerParams - 1, kPidMidiAftertouch, @@ -30,6 +31,7 @@ class SfizzVstState { public: std::string sfzFile; float volume = 0; + int numVoices = 64; static constexpr uint64 currentStateVersion = 0; @@ -72,3 +74,4 @@ struct SfizzParameterRange { }; static constexpr SfizzParameterRange kParamVolumeRange(0.0, -60.0, +6.0); +static constexpr SfizzParameterRange kParamNumVoicesRange(64.0, 1.0, 256.0);