From 633bac9e82aef192a803666aaf3d3dcff920ac0c Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Wed, 13 Jul 2016 11:25:59 +0200 Subject: Use the latency reported by the engine in the jack and wavfile output modules. --- drumgizmo/output/jackaudio.cc | 36 ++++++++++++++++++++++++++++++++++++ drumgizmo/output/jackaudio.h | 4 ++++ drumgizmo/output/wavfile.cc | 35 +++++++++++++++++++++++++++++------ drumgizmo/output/wavfile.h | 14 +++++++++----- 4 files changed, 78 insertions(+), 11 deletions(-) (limited to 'drumgizmo/output') diff --git a/drumgizmo/output/jackaudio.cc b/drumgizmo/output/jackaudio.cc index abd7d4f..75766d1 100644 --- a/drumgizmo/output/jackaudio.cc +++ b/drumgizmo/output/jackaudio.cc @@ -33,6 +33,7 @@ JackAudioOutputEngine::JackAudioOutputEngine(JackClient& client) : AudioOutputEngine{} , client(client) , channels{} + , latency{0} { client.add(*this); } @@ -122,6 +123,11 @@ size_t JackAudioOutputEngine::getSamplerate() const return client.getSampleRate(); } +void JackAudioOutputEngine::onLatencyChange(std::size_t latency) +{ + this->latency = latency; +} + JackAudioOutputEngine::Channel::Channel(JackClient& client, const std::string& name, std::size_t buffer_size) @@ -130,3 +136,33 @@ JackAudioOutputEngine::Channel::Channel(JackClient& client, { samples.resize(buffer_size); } + +void JackAudioOutputEngine::jackLatencyCallback(jack_latency_callback_mode_t mode) +{ + jack_latency_range_t range; + switch(mode) + { + case JackCaptureLatency: + // We do not have any audio input ports. Use this for when we do... +// jack_port_get_latency_range(port_feeding_input_port, +// JackPlaybackLatency, +// &range); +// range.min += 0; +// range.max += 0; +// jack_port_set_latency_range(input_port, JackPlaybackLatency, &range); + break; + case JackPlaybackLatency: + for(auto& channel : channels) + { + jack_port_get_latency_range(channel.port.port, + JackPlaybackLatency, + &range); + range.min += latency; + range.max += latency; + jack_port_set_latency_range(channel.port.port, + JackPlaybackLatency, + &range); + } + break; + } +} diff --git a/drumgizmo/output/jackaudio.h b/drumgizmo/output/jackaudio.h index 324e95a..b8fbcab 100644 --- a/drumgizmo/output/jackaudio.h +++ b/drumgizmo/output/jackaudio.h @@ -49,11 +49,14 @@ public: void post(size_t nsamples) override; size_t getBufferSize() const override; size_t getSamplerate() const override; + void onLatencyChange(std::size_t latency) override; // based on JackProcess void process(jack_nframes_t num_frames) override; + void jackLatencyCallback(jack_latency_callback_mode_t mode) override; private: + struct Channel { JackPort port; @@ -66,4 +69,5 @@ private: JackClient& client; std::vector channels; Semaphore sema; + std::size_t latency{0}; }; diff --git a/drumgizmo/output/wavfile.cc b/drumgizmo/output/wavfile.cc index a9d8b0b..20b63bd 100644 --- a/drumgizmo/output/wavfile.cc +++ b/drumgizmo/output/wavfile.cc @@ -33,6 +33,7 @@ WavfileOutputEngine::WavfileOutputEngine() , info{} , channels{} , file{"output"} + , latency{0} { info.samplerate = 44100; info.channels = 1; @@ -69,7 +70,8 @@ bool WavfileOutputEngine::init(const Channels& data) return true; } -void WavfileOutputEngine::setParm(const std::string& parm, const std::string& value) +void WavfileOutputEngine::setParm(const std::string& parm, + const std::string& value) { if(parm == "file") { @@ -105,11 +107,11 @@ void WavfileOutputEngine::stop() { } -void WavfileOutputEngine::pre(size_t nsamples) +void WavfileOutputEngine::pre(std::size_t nsamples) { } -void WavfileOutputEngine::run(int ch, sample_t* samples, size_t nsamples) +void WavfileOutputEngine::run(int ch, sample_t* samples, std::size_t nsamples) { if(static_cast(ch) >= channels.size()) { @@ -118,14 +120,35 @@ void WavfileOutputEngine::run(int ch, sample_t* samples, size_t nsamples) return; } - sf_writef_float(channels[ch], samples, nsamples); + // Skip the initial 'latency' samples. + if(nsamples <= latency) + { + return; + } + + nsamples -= latency; + + sf_writef_float(channels[ch], samples + latency, nsamples); } -void WavfileOutputEngine::post(size_t nsamples) +void WavfileOutputEngine::post(std::size_t nsamples) { + if(latency > nsamples) + { + latency -= nsamples; + } + else + { + latency = 0; + } } -size_t WavfileOutputEngine::getSamplerate() const +std::size_t WavfileOutputEngine::getSamplerate() const { return info.samplerate; } + +void WavfileOutputEngine::onLatencyChange(std::size_t latency) +{ + this->latency = latency; +} diff --git a/drumgizmo/output/wavfile.h b/drumgizmo/output/wavfile.h index dfe422c..656c0cd 100644 --- a/drumgizmo/output/wavfile.h +++ b/drumgizmo/output/wavfile.h @@ -25,6 +25,8 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #pragma once + +#include #include #include @@ -44,15 +46,17 @@ public: void setParm(const std::string& parm, const std::string& value) override; bool start() override; void stop() override; - void pre(size_t nsamples) override; - void run(int ch, sample_t* samples, size_t nsamples) override; - void post(size_t nsamples) override; - size_t getSamplerate() const override; + void pre(std::size_t nsamples) override; + void run(int ch, sample_t* samples, std::size_t nsamples) override; + void post(std::size_t nsamples) override; + std::size_t getSamplerate() const override; + void onLatencyChange(std::size_t latency) override; private: SF_INFO info; std::vector channels; - size_t num_channels; + std::size_t num_channels; std::string file; + std::size_t latency; }; -- cgit v1.2.3