From e0676c66a0b463859b7aa2ecc13e8f0a2c4275a5 Mon Sep 17 00:00:00 2001 From: Bruno Herbelin Date: Sun, 13 Mar 2022 23:13:32 +0100 Subject: [PATCH] Metronome fix do not call now() multiple times to avoid time difference between calls. --- ControlManager.cpp | 20 +++++++++++++++++--- Metronome.cpp | 16 +++++++++------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/ControlManager.cpp b/ControlManager.cpp index c4ab487..03996ad 100644 --- a/ControlManager.cpp +++ b/ControlManager.cpp @@ -1,4 +1,4 @@ -/* +/* * This file is part of vimix - video live mixer * * **Copyright** (C) 2019-2022 Bruno Herbelin @@ -403,7 +403,6 @@ bool Control::init() void Control::update() { - // read joystick buttons int num_buttons = 0; const unsigned char *state_buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &num_buttons ); @@ -438,6 +437,21 @@ void Control::update() } } + // draft : react to metronome + // int p = (int) Metronome::manager().phase(); + // static bool bip = false; + // static int t = 2; + // if (!bip) { + // if (p + 1 == t){ + // g_print("bip"); + // bip = true; + // } + // } + // else { + // if (p + 1 != t){ + // bip = false; + // } + // } } void Control::listen() @@ -943,7 +957,7 @@ std::string Control::inputLabel(uint id) } else if ( id >= INPUT_CUSTOM_FIRST && id <= INPUT_CUSTOM_LAST ) { - + label = std::string( "Custom ") + std::to_string(id - INPUT_CUSTOM_FIRST); } return label; diff --git a/Metronome.cpp b/Metronome.cpp index 4706b9f..c196d89 100644 --- a/Metronome.cpp +++ b/Metronome.cpp @@ -59,10 +59,10 @@ namespace ableton return sessionState.beatAtTime(now(), mQuantum); } - std::chrono::microseconds timeNextBeat() const + std::chrono::microseconds timeNextBeat(std::chrono::microseconds now) const { auto sessionState = mLink.captureAppSessionState(); - double beat = ceil(sessionState.beatAtTime(now(), mQuantum)); + double beat = ceil(sessionState.beatAtTime(now, mQuantum)); return sessionState.timeAtBeat(beat, mQuantum); } @@ -72,11 +72,11 @@ namespace ableton return sessionState.phaseAtTime(now(), mQuantum); } - std::chrono::microseconds timeNextPhase() const + std::chrono::microseconds timeNextPhase(std::chrono::microseconds now) const { auto sessionState = mLink.captureAppSessionState(); - double phase = ceil(sessionState.phaseAtTime(now(), mQuantum)); - double beat = ceil(sessionState.beatAtTime(now(), mQuantum)); + double phase = ceil(sessionState.phaseAtTime(now, mQuantum)); + double beat = ceil(sessionState.beatAtTime(now, mQuantum)); return sessionState.timeAtBeat(beat + (mQuantum-phase), mQuantum); } @@ -223,12 +223,14 @@ void Metronome::restart() std::chrono::microseconds Metronome::timeToBeat() { - return engine_.timeNextBeat() - engine_.now(); + std::chrono::microseconds now = engine_.now(); + return engine_.timeNextBeat( now ) - now; } std::chrono::microseconds Metronome::timeToPhase() { - return engine_.timeNextPhase() - engine_.now(); + std::chrono::microseconds now = engine_.now(); + return engine_.timeNextPhase( now ) - now; } void delay(std::function f, std::chrono::microseconds us)