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

Added/fixed support of pb and at in sendController and addController #626

Merged
merged 4 commits into from
Jan 19, 2025
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
130 changes: 69 additions & 61 deletions hi_scripting/scripting/api/ScriptingApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion hi_scripting/scripting/api/ScriptingApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down