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

Support triggering bar phase/clock every 4/8 bars #798

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
46 changes: 43 additions & 3 deletions plugins/Cardinal/src/HostTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ struct HostTime : TerminalModule {
kHostTimeCount
};

enum BarDivisions {
Bars1 = 1,
Bars4 = 4,
Bars8 = 8
};

const CardinalPluginContext* const pcontext;

rack::dsp::PulseGenerator pulseReset, pulseBar, pulseBeat, pulseClock;
float sampleTime = 0.0f;
uint32_t lastProcessCounter = 0;
BarDivisions barDivision = Bars1;
// cached time values
struct {
bool reset = true;
Expand Down Expand Up @@ -122,7 +129,9 @@ struct HostTime : TerminalModule {
{
timeInfo.beat = 1;
++timeInfo.bar;
pulseBar.trigger();

if (timeInfo.bar % barDivision == 1)
pulseBar.trigger();
}
}

Expand All @@ -148,7 +157,8 @@ struct HostTime : TerminalModule {
? tick / pcontext->ticksPerBeat
: 0.0f;
const float barPhase = playingWithBBT && pcontext->beatsPerBar > 0
? ((float) (timeInfo.beat - 1) + beatPhase) / pcontext->beatsPerBar
? ((timeInfo.bar - 1) % barDivision) / (float) barDivision
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic also seems wrong...

if we unfold the parenthesis we have

(
    (timeInfo.bar - 1) % barDivision
) / (float) barDivision
+
(
    (tick / pcontext->ticksPerBeat) / pcontext->beatsPerBar
)

this doesnt match the previous code and is likely to produce wrong results.

there is also another tick / pcontext->ticksPerBeat use that can just be beatPhase instead

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, updated calculation.

+ ((tick / pcontext->ticksPerBeat) / pcontext->beatsPerBar)
: 0.0f;

lights[kHostTimeRolling].setBrightness(playing ? 1.0f : 0.0f);
Expand All @@ -165,11 +175,25 @@ struct HostTime : TerminalModule {
outputs[kHostTimeBeat].setVoltage(hasBeat ? 10.0f : 0.0f);
outputs[kHostTimeClock].setVoltage(hasClock ? 10.0f : 0.0f);
outputs[kHostTimeBarPhase].setVoltage(barPhase * 10.0f);
outputs[kHostTimeBeatPhase].setVoltage(beatPhase * 10.0f);
outputs[kHostTimeBeatPhase].setVoltage((tick / pcontext->ticksPerBeat) * 10.0f);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like a leftover... please restore the original, otherwise for hosts that dont provide time info this results in a division by zero and crash

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, have fixed it.

}

void processTerminalOutput(const ProcessArgs&) override
{}

json_t* dataToJson() override {
json_t* rootJ = json_object();
json_object_set_new(rootJ, "barDivision", json_integer(barDivision));
return rootJ;
}

void dataFromJson(json_t* rootJ) override {
if (json_t* bdJ = json_object_get(rootJ, "barDivision")) {
int value = json_integer_value(bdJ);
if (value == Bars1 || value == Bars4 || value == Bars8)
barDivision = static_cast<BarDivisions>(value);
}
}
};

// --------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -286,6 +310,22 @@ struct HostTimeWidget : ModuleWidgetWith8HP {

ModuleWidget::drawLayer(args, layer);
}

void appendContextMenu(Menu* menu) override {
struct BarDivisionItem : MenuItem {
HostTime* module;
HostTime::BarDivisions value;
void onAction(const event::Action& e) override {
module->barDivision = value;
}
};

menu->addChild(new MenuSeparator);
menu->addChild(construct<MenuLabel>(&MenuLabel::text, "Bar Division"));
menu->addChild(construct<BarDivisionItem>(&BarDivisionItem::text, "Bars/1", &BarDivisionItem::module, module, &BarDivisionItem::value, HostTime::Bars1));
menu->addChild(construct<BarDivisionItem>(&BarDivisionItem::text, "Bars/4", &BarDivisionItem::module, module, &BarDivisionItem::value, HostTime::Bars4));
menu->addChild(construct<BarDivisionItem>(&BarDivisionItem::text, "Bars/8", &BarDivisionItem::module, module, &BarDivisionItem::value, HostTime::Bars8));
}
};
#else
struct HostTimeWidget : ModuleWidget {
Expand Down
Loading