Make internal plugin context use const; Fix host time

Signed-off-by: falkTX <falktx@falktx.com>
This commit is contained in:
falkTX 2022-01-14 15:45:36 +00:00
parent ec2f455826
commit 1a2c64309b
No known key found for this signature in database
GPG key ID: CDBAA37ABC74FBA0
8 changed files with 63 additions and 39 deletions

View file

@ -76,7 +76,7 @@ struct CarlaModule : Module {
NUM_LIGHTS
};
CardinalPluginContext* const pcontext;
const CardinalPluginContext* const pcontext;
const NativePluginDescriptor* fCarlaPluginDescriptor = nullptr;
NativePluginHandle fCarlaPluginHandle = nullptr;
@ -398,7 +398,7 @@ static uint32_t host_get_buffer_size(const NativeHostHandle handle)
static double host_get_sample_rate(const NativeHostHandle handle)
{
CardinalPluginContext* const pcontext = static_cast<CarlaModule*>(handle)->pcontext;
const CardinalPluginContext* const pcontext = static_cast<CarlaModule*>(handle)->pcontext;
DISTRHO_SAFE_ASSERT_RETURN(pcontext != nullptr, 48000.0);
return pcontext->sampleRate;
}
@ -516,7 +516,7 @@ struct CarlaModuleWidget : ModuleWidget, IdleCallback {
return;
const CarlaHostHandle handle = module->fCarlaHostHandle;
CardinalPluginContext* const pcontext = module->pcontext;
const CardinalPluginContext* const pcontext = module->pcontext;
char winIdStr[24];
std::snprintf(winIdStr, sizeof(winIdStr), "%llx", (ulonglong)pcontext->nativeWindowId);
@ -539,7 +539,7 @@ struct CarlaModuleWidget : ModuleWidget, IdleCallback {
return;
const CarlaHostHandle handle = module->fCarlaHostHandle;
CardinalPluginContext* const pcontext = module->pcontext;
const CardinalPluginContext* const pcontext = module->pcontext;
module->fUI = nullptr;

View file

@ -42,7 +42,7 @@ struct HostParameters : Module {
{
config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS);
CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP);
const CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP);
if (pcontext == nullptr)
throw rack::Exception("Plugin context is null.");
@ -57,7 +57,7 @@ struct HostParameters : Module {
void process(const ProcessArgs&) override
{
if (CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP))
if (const CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP))
{
for (uint32_t i=0; i<kModuleParameters; ++i)
outputs[i].setVoltage(parameters[i].process(sampleTime, pcontext->parameters[i]));
@ -66,7 +66,7 @@ struct HostParameters : Module {
void onSampleRateChange(const SampleRateChangeEvent& e) override
{
if (CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP))
if (const CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP))
{
const double fall = 1.0 / (double(pcontext->bufferSize) / e.sampleRate);
@ -84,12 +84,12 @@ struct HostParameters : Module {
#ifndef HEADLESS
struct CardinalParameterPJ301MPort : PJ301MPort {
void onDragStart(const DragStartEvent& e) override {
if (CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP))
if (const CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP))
handleHostParameterDrag(pcontext, portId, true);
PJ301MPort::onDragStart(e);
}
void onDragEnd(const DragEndEvent& e) override {
if (CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP))
if (const CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP))
handleHostParameterDrag(pcontext, portId, false);
PJ301MPort::onDragEnd(e);
}

View file

@ -37,12 +37,21 @@ struct HostTime : Module {
rack::dsp::PulseGenerator pulseReset, pulseBar, pulseBeat, pulseClock;
float sampleTime = 0.0f;
int64_t lastBlockFrame = -1;
// cached time values
struct {
bool reset = true;
int32_t bar = 0;
int32_t beat = 0;
double tick = 0.0;
double tickClock = 0.0;
} timeInfo;
HostTime()
{
config(NUM_PARAMS, NUM_INPUTS, kHostTimeCount, kHostTimeCount);
CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP);
const CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP);
if (pcontext == nullptr)
throw rack::Exception("Plugin context is null.");
@ -50,44 +59,57 @@ struct HostTime : Module {
void process(const ProcessArgs& args) override
{
if (CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP))
if (const CardinalPluginContext* const pcontext = static_cast<CardinalPluginContext*>(APP))
{
const int64_t blockFrame = pcontext->engine->getBlockFrame();
// Update time position if running a new audio block
if (lastBlockFrame != blockFrame)
{
lastBlockFrame = blockFrame;
timeInfo.reset = pcontext->reset;
timeInfo.bar = pcontext->bar;
timeInfo.beat = pcontext->beat;
timeInfo.tick = pcontext->tick;
timeInfo.tickClock = pcontext->tickClock;
}
const bool playing = pcontext->playing;
const bool playingWithBBT = playing && pcontext->bbtValid;
if (playingWithBBT)
{
if (pcontext->tick == 0.0)
if (timeInfo.tick == 0.0)
{
pulseReset.trigger();
pulseClock.trigger();
pulseBeat.trigger();
if (pcontext->beat == 1)
if (timeInfo.beat == 1)
pulseBar.trigger();
}
if (pcontext->reset)
if (timeInfo.reset)
{
pcontext->reset = false;
timeInfo.reset = false;
pulseReset.trigger();
}
if ((pcontext->tick += pcontext->ticksPerFrame) >= pcontext->ticksPerBeat)
if ((timeInfo.tick += pcontext->ticksPerFrame) >= pcontext->ticksPerBeat)
{
pcontext->tick -= pcontext->ticksPerBeat;
timeInfo.tick -= pcontext->ticksPerBeat;
pulseBeat.trigger();
if (++pcontext->beat > pcontext->beatsPerBar)
if (++timeInfo.beat > pcontext->beatsPerBar)
{
pcontext->beat = 1;
++pcontext->bar;
timeInfo.beat = 1;
++timeInfo.bar;
pulseBar.trigger();
}
}
if ((pcontext->tickClock += pcontext->ticksPerFrame) >= pcontext->ticksPerClock)
if ((timeInfo.tickClock += pcontext->ticksPerFrame) >= pcontext->ticksPerClock)
{
pcontext->tickClock -= pcontext->ticksPerClock;
timeInfo.tickClock -= pcontext->ticksPerClock;
pulseClock.trigger();
}
}
@ -97,10 +119,10 @@ struct HostTime : Module {
const bool hasBeat = pulseBeat.process(args.sampleTime);
const bool hasClock = pulseClock.process(args.sampleTime);
const float beatPhase = playingWithBBT && pcontext->ticksPerBeat > 0.0
? pcontext->tick / pcontext->ticksPerBeat
? timeInfo.tick / pcontext->ticksPerBeat
: 0.0f;
const float barPhase = playingWithBBT && pcontext->beatsPerBar > 0
? ((float) (pcontext->beat - 1) + beatPhase) / pcontext->beatsPerBar
? ((float) (timeInfo.beat - 1) + beatPhase) / pcontext->beatsPerBar
: 0.0f;
lights[kHostTimeRolling].setBrightness(playing ? 1.0f : 0.0f);

View file

@ -248,7 +248,7 @@ struct IldaeilModule : Module {
SharedResourcePointer<JuceInitializer> juceInitializer;
#endif
CardinalPluginContext* const pcontext;
const CardinalPluginContext* const pcontext;
const NativePluginDescriptor* fCarlaPluginDescriptor = nullptr;
NativePluginHandle fCarlaPluginHandle = nullptr;
@ -588,7 +588,7 @@ static uint32_t host_get_buffer_size(const NativeHostHandle handle)
static double host_get_sample_rate(const NativeHostHandle handle)
{
CardinalPluginContext* const pcontext = static_cast<IldaeilModule*>(handle)->pcontext;
const CardinalPluginContext* const pcontext = static_cast<IldaeilModule*>(handle)->pcontext;
DISTRHO_SAFE_ASSERT_RETURN(pcontext != nullptr, 48000.0);
return pcontext->sampleRate;
}
@ -837,7 +837,7 @@ struct IldaeilWidget : ImGuiWidget, IdleCallback, Thread {
DISTRHO_SAFE_ASSERT_RETURN(idleCallbackActive,);
DISTRHO_SAFE_ASSERT_RETURN(fPluginType == PLUGIN_INTERNAL || fPluginType == PLUGIN_LV2,);
CardinalPluginContext* const pcontext = module->pcontext;
const CardinalPluginContext* const pcontext = module->pcontext;
// FIXME isEmbed
FileBrowserOptions opts;
@ -1004,7 +1004,7 @@ struct IldaeilWidget : ImGuiWidget, IdleCallback, Thread {
{
if (const CarlaHostHandle handle = module->fCarlaHostHandle)
{
CardinalPluginContext* const pcontext = module->pcontext;
const CardinalPluginContext* const pcontext = module->pcontext;
char winIdStr[24];
std::snprintf(winIdStr, sizeof(winIdStr), "%llx", (ulonglong)pcontext->nativeWindowId);
@ -1026,7 +1026,7 @@ struct IldaeilWidget : ImGuiWidget, IdleCallback, Thread {
{
if (const CarlaHostHandle handle = module->fCarlaHostHandle)
{
CardinalPluginContext* const pcontext = module->pcontext;
const CardinalPluginContext* const pcontext = module->pcontext;
module->fCarlaHostDescriptor.uiParentId = 0;
carla_set_engine_option(handle, ENGINE_OPTION_FRONTEND_WIN_ID, 0, "0");

View file

@ -55,13 +55,13 @@ struct CardinalPluginContext : rack::Context {
#endif
CardinalPluginContext(Plugin* const p);
#ifndef HEADLESS
bool addIdleCallback(IdleCallback* cb);
void removeIdleCallback(IdleCallback* cb);
bool addIdleCallback(IdleCallback* cb) const;
void removeIdleCallback(IdleCallback* cb) const;
#endif
};
#ifndef HEADLESS
void handleHostParameterDrag(CardinalPluginContext* pcontext, uint index, bool started);
void handleHostParameterDrag(const CardinalPluginContext* pcontext, uint index, bool started);
#endif
END_NAMESPACE_DISTRHO

View file

@ -930,12 +930,13 @@ protected:
{
const TimePosition& timePos(getTimePosition());
bool reset = false;
MidiEvent singleTimeMidiEvent = { 0, 1, { 0, 0, 0, 0 }, nullptr };
if (timePos.playing)
{
if (timePos.frame == 0 || fPreviousFrame + frames != timePos.frame)
context->reset = true;
reset = true;
if (! context->playing)
{
@ -977,6 +978,7 @@ protected:
context->tickClock = std::fmod(timePos.bbt.tick, context->ticksPerClock);
}
context->reset = reset;
fPreviousFrame = timePos.frame;
}

View file

@ -193,7 +193,7 @@ START_NAMESPACE_DISTRHO
// -----------------------------------------------------------------------------------------------------------
bool CardinalPluginContext::addIdleCallback(IdleCallback* const cb)
bool CardinalPluginContext::addIdleCallback(IdleCallback* const cb) const
{
if (ui == nullptr)
return false;
@ -202,7 +202,7 @@ bool CardinalPluginContext::addIdleCallback(IdleCallback* const cb)
return true;
}
void CardinalPluginContext::removeIdleCallback(IdleCallback* const cb)
void CardinalPluginContext::removeIdleCallback(IdleCallback* const cb) const
{
if (ui == nullptr)
return;
@ -210,7 +210,7 @@ void CardinalPluginContext::removeIdleCallback(IdleCallback* const cb)
ui->removeIdleCallback(cb);
}
void handleHostParameterDrag(CardinalPluginContext* pcontext, uint index, bool started)
void handleHostParameterDrag(const CardinalPluginContext* pcontext, uint index, bool started)
{
DISTRHO_SAFE_ASSERT_RETURN(pcontext->ui != nullptr,);

View file

@ -91,13 +91,13 @@ struct CardinalPluginContext : rack::Context {
}
#ifndef HEADLESS
bool addIdleCallback(IdleCallback* cb);
void removeIdleCallback(IdleCallback* cb);
bool addIdleCallback(IdleCallback* cb) const;
void removeIdleCallback(IdleCallback* cb) const;
#endif
};
#ifndef HEADLESS
void handleHostParameterDrag(CardinalPluginContext* pcontext, uint index, bool started);
void handleHostParameterDrag(const CardinalPluginContext* pcontext, uint index, bool started);
#endif
// -----------------------------------------------------------------------------------------------------------