From dbe72ddb8eb215de6d38cd01ed201704ab315a4f Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 4 Jan 2025 18:27:21 +0100 Subject: Compensate phase --- InternalClick.cpp | 11 +++++++++-- InternalClick.h | 4 +++- MainLoop.cpp | 6 +++--- PCM.cpp | 11 +++++++++-- PCM.h | 4 +++- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/InternalClick.cpp b/InternalClick.cpp index 33654fd..e0c757e 100644 --- a/InternalClick.cpp +++ b/InternalClick.cpp @@ -1,5 +1,10 @@ #include "InternalClick.h" +#include "log.h" +#include "debug.h" + +#include + InternalClick::InternalClick(Config& config): m_config{config}, m_timestamp{clock_type::now()} @@ -12,8 +17,10 @@ void InternalClick::run_cyclic_50ms() std::chrono::duration duration{60.0 / static_cast(m_config.get_bpm())}; if (now - m_timestamp > duration) { - m_timestamp = now; - signal_click(0); // offset + std::chrono::duration offset = now - m_timestamp - duration; + m_timestamp = now - std::chrono::duration_cast(offset); + signal_click(offset); + debug_cout << fmt::format("offset={}", offset.count()) << std::endl; } } diff --git a/InternalClick.h b/InternalClick.h index 1fffb6c..3fb04d4 100644 --- a/InternalClick.h +++ b/InternalClick.h @@ -1,5 +1,7 @@ #pragma once +#include + #include #include "config.h" @@ -15,7 +17,7 @@ public: InternalClick(Config& config); // signals - boost::signals2::signal signal_click; + boost::signals2::signal)> signal_click; // offset in s // slots void run_cyclic_50ms(); diff --git a/MainLoop.cpp b/MainLoop.cpp index 1bd0e1d..9db8c9a 100644 --- a/MainLoop.cpp +++ b/MainLoop.cpp @@ -48,11 +48,11 @@ void MainLoop::reconfigure_mode() { int mode = m_config.get_mode(); if (mode == 0) { - m_click_connection = m_note_click.signal_click.connect([&](){m_pcm.click(0);}); + m_click_connection = m_note_click.signal_click.connect([&](){m_pcm.click({});}); } else if (mode == 1) { - m_click_connection = m_clock_click.signal_click.connect([&](){m_pcm.click(0);}); + m_click_connection = m_clock_click.signal_click.connect([&](){m_pcm.click({});}); } else if (mode == 2) { - m_click_connection = m_internal_click.signal_click.connect([&](int64_t offset){m_pcm.click(offset);}); + m_click_connection = m_internal_click.signal_click.connect([&](std::chrono::duration offset){m_pcm.click(offset);}); } else { log_cout << fmt::format("Error: Unknown mode: {}", mode) << std::endl; } diff --git a/PCM.cpp b/PCM.cpp index 218ba59..8c0e01f 100644 --- a/PCM.cpp +++ b/PCM.cpp @@ -59,13 +59,20 @@ PCM::~PCM() free(pfd); } -void PCM::click(int64_t offset_us) +// offset > 0: play earlier +// offset < 0: play later +void PCM::click(std::chrono::duration offset) { snd_pcm_sframes_t delay; if (0 > snd_pcm_delay(handle, &delay)) { } - m_phase = - click_latency_frames + delay + offset_us; + int64_t offset_frames{static_cast(offset.count() * f_sample)}; + m_phase = - click_latency_frames + delay + offset_frames; + + if (m_phase > 0) { + log_cout << fmt::format("phase = {}", m_phase) << std::endl; + } } // generate 1 buffer size diff --git a/PCM.h b/PCM.h index 90abc3d..e7c7a5e 100644 --- a/PCM.h +++ b/PCM.h @@ -6,6 +6,8 @@ #include +#include + #include #include @@ -21,7 +23,7 @@ public: PCM(); ~PCM(); - void click(int64_t offset_us); + void click(std::chrono::duration offset); // generate 1 buffer size void generate(); -- cgit v1.2.3