summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2025-01-04 18:27:21 +0100
committerRoland Reichwein <mail@reichwein.it>2025-01-04 18:27:21 +0100
commitdbe72ddb8eb215de6d38cd01ed201704ab315a4f (patch)
tree8fda403192090d8560bca9fe5a2e9f9e4e1a7e41
parent2e793141e5434043205763c70d3a597cf2d78eeb (diff)
Compensate phase
-rw-r--r--InternalClick.cpp11
-rw-r--r--InternalClick.h4
-rw-r--r--MainLoop.cpp6
-rw-r--r--PCM.cpp11
-rw-r--r--PCM.h4
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 <fmt/format.h>
+
InternalClick::InternalClick(Config& config):
m_config{config},
m_timestamp{clock_type::now()}
@@ -12,8 +17,10 @@ void InternalClick::run_cyclic_50ms()
std::chrono::duration<double> duration{60.0 / static_cast<double>(m_config.get_bpm())};
if (now - m_timestamp > duration) {
- m_timestamp = now;
- signal_click(0); // offset
+ std::chrono::duration<double> offset = now - m_timestamp - duration;
+ m_timestamp = now - std::chrono::duration_cast<clock_type::duration>(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 <chrono>
+
#include <boost/signals2.hpp>
#include "config.h"
@@ -15,7 +17,7 @@ public:
InternalClick(Config& config);
// signals
- boost::signals2::signal<void(int64_t)> signal_click;
+ boost::signals2::signal<void(std::chrono::duration<double>)> 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<double> 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<double> 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<int64_t>(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 <libreichwein/file.h>
+#include <chrono>
+
#include <alsa/asoundlib.h>
#include <fmt/format.h>
@@ -21,7 +23,7 @@ public:
PCM();
~PCM();
- void click(int64_t offset_us);
+ void click(std::chrono::duration<double> offset);
// generate 1 buffer size
void generate();