diff --git a/hi_scripting/scripting/api/ScriptingApi.cpp b/hi_scripting/scripting/api/ScriptingApi.cpp index 751b488e9..8364f93be 100644 --- a/hi_scripting/scripting/api/ScriptingApi.cpp +++ b/hi_scripting/scripting/api/ScriptingApi.cpp @@ -5688,43 +5688,42 @@ double ScriptingApi::Synth::getTimerInterval() const } } -void ScriptingApi::Synth::sendController(int controllerNumber, int controllerValue) +void ScriptingApi::Synth::sendController(int number, int value) { - if (parentMidiProcessor != nullptr) - { - if (controllerNumber > 0) - { - if (controllerValue >= 0) - { - HiseEvent e; - - if(controllerNumber == HiseEvent::PitchWheelCCNumber) - { - e = HiseEvent(HiseEvent::Type::PitchBend, 0, 0); - e.setPitchWheelValue(controllerValue); - } - else if (controllerNumber == HiseEvent::AfterTouchCCNumber) - { - e = HiseEvent(HiseEvent::Type::Aftertouch, 0, controllerValue); - } - else - { - e = HiseEvent(HiseEvent::Type::Controller, (uint8)controllerNumber, (uint8)controllerValue); - } + if (parentMidiProcessor == nullptr) + return reportScriptError("Only valid in MidiProcessors"); + + if (number < 0) + return reportScriptError("CC number must be positive"); + bool isPitchBend = number == HiseEvent::PitchWheelCCNumber; + + if (!isPitchBend && (value < 0 || value > 127)) + return reportScriptError("CC value must be between 0 and 127"); - if (const HiseEvent* current = parentMidiProcessor->getCurrentHiseEvent()) - { - e.setTimeStamp((int)current->getTimeStamp()); - } + if (isPitchBend && (value < 0 || value > 16383)) + return reportScriptError("CC value must be between 0 and 16383"); + + HiseEvent e; - parentMidiProcessor->addHiseEventToBuffer(e); - } - else reportScriptError("CC value must be positive"); - } - else reportScriptError("CC number must be positive"); + if (isPitchBend) + { + e = HiseEvent(HiseEvent::Type::PitchBend, 0, 0); + e.setPitchWheelValue(value); } - else reportScriptError("Only valid in MidiProcessors"); + else if (number == HiseEvent::AfterTouchCCNumber) + { + e = HiseEvent(HiseEvent::Type::Aftertouch, 0, value); + } + else + { + e = HiseEvent(HiseEvent::Type::Controller, (uint8)number, (uint8)value); + } + + if (auto ce = parentMidiProcessor->getCurrentHiseEvent()) + e.setTimeStamp((int)ce->getTimeStamp()); + + parentMidiProcessor->addHiseEventToBuffer(e); }; void ScriptingApi::Synth::sendControllerToChildSynths(int controllerNumber, int controllerValue) @@ -6296,40 +6295,49 @@ void ScriptingApi::Synth::addNoteOff(int channel, int noteNumber, int timeStampS void ScriptingApi::Synth::addController(int channel, int number, int value, int timeStampSamples) { - if (channel > 0 && channel <= 16) - { - if (number >= 0 && number <= 127) - { - if (value >= 0 && value <= 127) - { - if (timeStampSamples >= 0) - { - if (parentMidiProcessor != nullptr) - { - HiseEvent m = HiseEvent(HiseEvent::Type::Controller, (uint8)number, (uint8)value, (uint8)channel); + if (channel <= 0 || channel > 16) + return reportScriptError("Channel must be between 1 and 16."); - if (auto ce = parentMidiProcessor->getCurrentHiseEvent()) - { - m.setTimeStamp((int)ce->getTimeStamp() + timeStampSamples); - } - else - { - m.setTimeStamp(timeStampSamples); - } + if (parentMidiProcessor == nullptr) + return reportScriptError("Only valid in MidiProcessors"); - m.setArtificial(); + if (number < 0) + return reportScriptError("CC number must be positive"); - parentMidiProcessor->addHiseEventToBuffer(m); - } + bool isPitchBend = number == HiseEvent::PitchWheelCCNumber; + + if (!isPitchBend && (value < 0 || value > 127)) + return reportScriptError("CC value must be between 0 and 127"); - } - else reportScriptError("Timestamp must be > 0"); - } - else reportScriptError("CC Value must be between 0 and 127"); - } - else reportScriptError("CC number must be between 0 and 127"); + if (isPitchBend && (value < 0 || value > 16383)) + return reportScriptError("CC value must be between 0 and 16383"); + + if (timeStampSamples < 0) + return reportScriptError("Timestamp must be > 0"); + + HiseEvent e; + + if (isPitchBend) + { + e = HiseEvent(HiseEvent::Type::PitchBend, 0, 0, (uint8)channel); + e.setPitchWheelValue(value); } - else reportScriptError("Channel must be between 1 and 16."); + else if (number == HiseEvent::AfterTouchCCNumber) + { + e = HiseEvent(HiseEvent::Type::Aftertouch, 0, value, (uint8)channel); + } + else + { + e = HiseEvent(HiseEvent::Type::Controller, (uint8)number, (uint8)value, (uint8)channel); + } + + if (auto ce = parentMidiProcessor->getCurrentHiseEvent()) + e.setTimeStamp((int)ce->getTimeStamp() + timeStampSamples); + else + e.setTimeStamp(timeStampSamples); + + e.setArtificial(); + parentMidiProcessor->addHiseEventToBuffer(e); } void ScriptingApi::Synth::setClockSpeed(int clockSpeed) diff --git a/hi_scripting/scripting/api/ScriptingApi.h b/hi_scripting/scripting/api/ScriptingApi.h index 202ed9c05..13c1cc49b 100644 --- a/hi_scripting/scripting/api/ScriptingApi.h +++ b/hi_scripting/scripting/api/ScriptingApi.h @@ -1181,7 +1181,7 @@ class ScriptingApi /** Sends a controller event to the synth. */ - void sendController(int controllerNumber, int controllerValue); + void sendController(int number, int value); /** The same as sendController (for backwards compatibility) */ void sendControllerToChildSynths(int controllerNumber, int controllerValue);