From 4300592750f0e33876df69debf47b6071a00709a Mon Sep 17 00:00:00 2001 From: dreamer Date: Sun, 13 Oct 2024 17:15:37 +0200 Subject: [PATCH] move host transport events to separate template and allow without midi input --- CHANGELOG.md | 3 +- hvcc/generators/c2dpf/templates/HeavyDPF.cpp | 2 + hvcc/generators/c2dpf/templates/HeavyDPF.hpp | 1 + .../c2dpf/templates/hostTransportEvents.cpp | 66 +++++++++++++++++++ hvcc/generators/c2dpf/templates/midiInput.cpp | 61 ----------------- .../pd2hv/libs/pd/midirealtimein.pd | 8 +-- 6 files changed, 74 insertions(+), 67 deletions(-) create mode 100644 hvcc/generators/c2dpf/templates/hostTransportEvents.cpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 23f1e680..a1ceb868 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,8 @@ Next Release Features: * Migrating to poetry for project management -* Allow modgui on desktop +* DPF: Allow modgui on desktop +* DPF: Allow host transport events without midi input Bugfixes: diff --git a/hvcc/generators/c2dpf/templates/HeavyDPF.cpp b/hvcc/generators/c2dpf/templates/HeavyDPF.cpp index 0d73340c..39844417 100644 --- a/hvcc/generators/c2dpf/templates/HeavyDPF.cpp +++ b/hvcc/generators/c2dpf/templates/HeavyDPF.cpp @@ -193,6 +193,7 @@ void {{class_name}}::setOutputParameter(uint32_t sendHash, const HvMessage *m) #endif {% endif %} +{% include 'hostTransportEvents.cpp' %} // ------------------------------------------------------------------- @@ -206,6 +207,7 @@ void {{class_name}}::run(const float** inputs, float** outputs, uint32_t frames, void {{class_name}}::run(const float** inputs, float** outputs, uint32_t frames) { #endif + hostTransportEvents(frames); {% if meta.denormals is sameas false %} const ScopedDenormalDisable sdd; {% endif %} diff --git a/hvcc/generators/c2dpf/templates/HeavyDPF.hpp b/hvcc/generators/c2dpf/templates/HeavyDPF.hpp index 3ea3aec0..eaeb03d6 100644 --- a/hvcc/generators/c2dpf/templates/HeavyDPF.hpp +++ b/hvcc/generators/c2dpf/templates/HeavyDPF.hpp @@ -47,6 +47,7 @@ class {{class_name}} : public Plugin void handleMidiInput(uint32_t frames, const MidiEvent* midiEvents, uint32_t midiEventCount); void handleMidiSend(uint32_t sendHash, const HvMessage *m); + void hostTransportEvents(uint32_t frames); void setOutputParameter(uint32_t sendHash, const HvMessage *m); protected: diff --git a/hvcc/generators/c2dpf/templates/hostTransportEvents.cpp b/hvcc/generators/c2dpf/templates/hostTransportEvents.cpp new file mode 100644 index 00000000..eabf51c8 --- /dev/null +++ b/hvcc/generators/c2dpf/templates/hostTransportEvents.cpp @@ -0,0 +1,66 @@ +// ------------------------------------------------------------------- +// Host Transport Events handler + +void {{class_name}}::hostTransportEvents(uint32_t frames) +{ + // Realtime events + const TimePosition& timePos(getTimePosition()); + bool reset = false; + + if (timePos.playing) + { + if (timePos.frame == 0) + { + _context->sendMessageToReceiverV(HV_HASH_MIDIREALTIMEIN, 0, + "ff", (float) MIDI_RT_RESET, 0); + reset = true; + } + + if (! this->wasPlaying) + { + if (timePos.frame == 0) + { + _context->sendMessageToReceiverV(HV_HASH_MIDIREALTIMEIN, 0, + "ff", (float) MIDI_RT_START, 0); + } + if (! reset) + { + _context->sendMessageToReceiverV(HV_HASH_MIDIREALTIMEIN, 0, + "ff", (float) MIDI_RT_CONTINUE, 0); + } + } + } + else if (this->wasPlaying) + { + _context->sendMessageToReceiverV(HV_HASH_MIDIREALTIMEIN, 0, + "ff", (float) MIDI_RT_STOP, 0); + } + this->wasPlaying = timePos.playing; + + // sending clock ticks + if (timePos.playing && timePos.bbt.valid) + { + float samplesPerBeat = 60 * getSampleRate() / timePos.bbt.beatsPerMinute; + float samplesPerTick = samplesPerBeat / 24.0; + + /* get state */ + double nextClockTick = this->nextClockTick; + double sampleAtCycleStart = this->sampleAtCycleStart; + double sampleAtCycleEnd = sampleAtCycleStart + frames; + + if (nextClockTick >= 0 && sampleAtCycleStart >= 0 && sampleAtCycleEnd > sampleAtCycleStart) { + while (nextClockTick < sampleAtCycleEnd) { + double delayMs = 1000*(nextClockTick - sampleAtCycleStart)/getSampleRate(); + if (delayMs >= 0.0) { + _context->sendMessageToReceiverV(HV_HASH_MIDIREALTIMEIN, delayMs, + "ff", (float) MIDI_RT_CLOCK, 0); + } + nextClockTick += samplesPerTick; + } + } + + /* save variables for next cycle */ + this->sampleAtCycleStart = sampleAtCycleEnd; + this->nextClockTick = nextClockTick; + } +} diff --git a/hvcc/generators/c2dpf/templates/midiInput.cpp b/hvcc/generators/c2dpf/templates/midiInput.cpp index 0fe3ba4c..537333fc 100644 --- a/hvcc/generators/c2dpf/templates/midiInput.cpp +++ b/hvcc/generators/c2dpf/templates/midiInput.cpp @@ -3,67 +3,6 @@ void {{class_name}}::handleMidiInput(uint32_t frames, const MidiEvent* midiEvents, uint32_t midiEventCount) { - // Realtime events - const TimePosition& timePos(getTimePosition()); - bool reset = false; - - if (timePos.playing) - { - if (timePos.frame == 0) - { - _context->sendMessageToReceiverV(HV_HASH_MIDIREALTIMEIN, 0, - "ff", (float) MIDI_RT_RESET); - reset = true; - } - - if (! this->wasPlaying) - { - if (timePos.frame == 0) - { - _context->sendMessageToReceiverV(HV_HASH_MIDIREALTIMEIN, 0, - "ff", (float) MIDI_RT_START); - } - if (! reset) - { - _context->sendMessageToReceiverV(HV_HASH_MIDIREALTIMEIN, 0, - "ff", (float) MIDI_RT_CONTINUE); - } - } - } - else if (this->wasPlaying) - { - _context->sendMessageToReceiverV(HV_HASH_MIDIREALTIMEIN, 0, - "ff", (float) MIDI_RT_STOP); - } - this->wasPlaying = timePos.playing; - - // sending clock ticks - if (timePos.playing && timePos.bbt.valid) - { - float samplesPerBeat = 60 * getSampleRate() / timePos.bbt.beatsPerMinute; - float samplesPerTick = samplesPerBeat / 24.0; - - /* get state */ - double nextClockTick = this->nextClockTick; - double sampleAtCycleStart = this->sampleAtCycleStart; - double sampleAtCycleEnd = sampleAtCycleStart + frames; - - if (nextClockTick >= 0 && sampleAtCycleStart >= 0 && sampleAtCycleEnd > sampleAtCycleStart) { - while (nextClockTick < sampleAtCycleEnd) { - double delayMs = 1000*(nextClockTick - sampleAtCycleStart)/getSampleRate(); - if (delayMs >= 0.0) { - _context->sendMessageToReceiverV(HV_HASH_MIDIREALTIMEIN, delayMs, - "ff", (float) MIDI_RT_CLOCK); - } - nextClockTick += samplesPerTick; - } - } - - /* save variables for next cycle */ - this->sampleAtCycleStart = sampleAtCycleEnd; - this->nextClockTick = nextClockTick; - } - // Midi events for (uint32_t i=0; i < midiEventCount; ++i) { diff --git a/hvcc/interpreters/pd2hv/libs/pd/midirealtimein.pd b/hvcc/interpreters/pd2hv/libs/pd/midirealtimein.pd index b4579970..6f3d1c68 100644 --- a/hvcc/interpreters/pd2hv/libs/pd/midirealtimein.pd +++ b/hvcc/interpreters/pd2hv/libs/pd/midirealtimein.pd @@ -1,11 +1,9 @@ #N canvas 951 312 204 190 12; -#X obj 87 127 outlet; +#X obj 86 148 outlet; #X obj 20 148 outlet; #X text 23 13 data port; #X obj 20 61 unpack f f; -#X obj 87 94 + 1; #X obj 20 37 r __hv_midirealtimein; #X connect 3 0 1 0; -#X connect 3 1 4 0; -#X connect 4 0 0 0; -#X connect 5 0 3 0; +#X connect 3 1 0 0; +#X connect 4 0 3 0;