From 9b634cf001697af3826ad988b198ac1997fd300a Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Thu, 12 Sep 2013 00:07:18 -0700 Subject: [PATCH] Tweak latency reporting/compensation. --- mixer/src/Chain.C | 43 +++++++++++++++++++++++---- mixer/src/Module.C | 59 +++++++++++++++++++------------------ mixer/src/Module.H | 4 +-- mixer/src/Plugin_Module.C | 9 ++---- mixer/src/Plugin_Module.H | 4 +-- timeline/src/Engine/Track.C | 18 +++++++---- 6 files changed, 88 insertions(+), 49 deletions(-) diff --git a/mixer/src/Chain.C b/mixer/src/Chain.C index f2a4073..7bbdad3 100644 --- a/mixer/src/Chain.C +++ b/mixer/src/Chain.C @@ -399,24 +399,57 @@ Chain::configure_ports ( void ) void Chain::set_latency ( JACK::Port::direction_e dir ) { - nframes_t total_latency = 0; + nframes_t tmax = 0; + nframes_t tmin = 0; + nframes_t added = 0; if ( dir == JACK::Port::Input ) { for ( int i = 0; i < modules(); ++i ) { Module *m = module( i ); - total_latency += m->get_latency( dir ); - m->set_latency( dir, total_latency ); + nframes_t min,max; + + /* added = m->get_latency( JACK::Port::Input, &min, &max ); */ + added += m->get_latency( JACK::Port::Input, &min, &max ); + + min += added; + max += added; + + if ( min > tmin ) + tmin = min; + if ( max > tmax ) + tmax = max; + + m->set_latency( dir, tmin, tmax ); } } else { + tmin = JACK_MAX_FRAMES >> 1; + for ( int i = modules(); i--; ) { Module *m = module( i ); - total_latency += m->get_latency( dir ); - m->set_latency( dir, total_latency ); + + nframes_t min,max; + + added += m->get_latency( JACK::Port::Output, &min, &max ); + + min += added; + max += added; + + if ( min < tmin ) + tmin = min; + if ( max > tmax ) + tmax = max; + + DMESSAGE( "Chain %s/%s: setting %s latency minimum to %lu", name(), + m->name(), + dir == JACK::Port::Input ? "Capture" : "Playback", + (unsigned long)tmin); + + m->set_latency( dir, tmin, tmax ); } } } diff --git a/mixer/src/Module.C b/mixer/src/Module.C index e05559d..d216f4e 100644 --- a/mixer/src/Module.C +++ b/mixer/src/Module.C @@ -1157,9 +1157,9 @@ Module::auto_disconnect_outputs ( void ) } nframes_t -Module::get_latency ( JACK::Port::direction_e dir ) const +Module::get_latency ( JACK::Port::direction_e dir, nframes_t *min, nframes_t *max ) const { - nframes_t tmin = 0; + nframes_t tmin = JACK_MAX_FRAMES >> 1; nframes_t tmax = 0; if ( dir == JACK::Port::Input ) @@ -1168,21 +1168,23 @@ Module::get_latency ( JACK::Port::direction_e dir ) const { for ( unsigned int i = 0; i < aux_audio_input.size(); i++ ) { + /* if ( ! aux_audio_input[i].jack_port()->connected() ) */ + /* continue; */ + nframes_t min,max; aux_audio_input[i].jack_port()->get_latency( dir, &min, &max ); - tmin += min; - tmax += max; + if ( min < tmin ) + tmin = min; + if ( max > tmax ) + tmax = max; } - - tmin /= aux_audio_input.size(); - tmax /= aux_audio_input.size(); } - - return tmin; - /* for ( unsigned int i = 0; i < aux_audio_output.size(); i++ ) */ - /* aux_audio_output[i].set_latency( dir, tmin, tmax ); */ + else + { + tmin = 0; + } } else { @@ -1190,38 +1192,39 @@ Module::get_latency ( JACK::Port::direction_e dir ) const { for ( unsigned int i = 0; i < aux_audio_output.size(); i++ ) { + /* if ( ! aux_audio_output[i].jack_port()->connected() ) */ + /* continue; */ + nframes_t min,max; aux_audio_output[i].jack_port()->get_latency( dir, &min, &max ); - tmin += min; - tmax += max; + if ( min < tmin ) + tmin = min; + if ( max > tmax ) + tmax = max; } - - tmin /= aux_audio_output.size(); - tmax /= aux_audio_output.size(); } - - return tmin; - - - /* for ( unsigned int i = 0; i < aux_audio_output.size(); i++ ) */ - /* aux_audio_output[i].set_latency( dir, tmin, tmax ); */ } + + *min = tmin; + *max = tmax; + + return 0; } void -Module::set_latency ( JACK::Port::direction_e dir, nframes_t latency ) +Module::set_latency ( JACK::Port::direction_e dir, nframes_t min, nframes_t max ) { - if ( dir == JACK::Port::Input ) + if ( dir == JACK::Port::Output ) { - for ( unsigned int i = 0; i < aux_audio_output.size(); i++ ) - aux_audio_output[i].jack_port()->set_latency( dir, latency, latency ); + for ( unsigned int i = 0; i < aux_audio_input.size(); i++ ) + aux_audio_input[i].jack_port()->set_latency( dir, min, max ); } else { - for ( unsigned int i = 0; i < aux_audio_input.size(); i++ ) - aux_audio_input[i].jack_port()->set_latency( dir, latency, latency ); + for ( unsigned int i = 0; i < aux_audio_output.size(); i++ ) + aux_audio_output[i].jack_port()->set_latency( dir, min, max ); } } diff --git a/mixer/src/Module.H b/mixer/src/Module.H index 99281c9..8553760 100644 --- a/mixer/src/Module.H +++ b/mixer/src/Module.H @@ -72,8 +72,8 @@ protected: public: - virtual nframes_t get_latency ( JACK::Port::direction_e dir ) const; - virtual void set_latency ( JACK::Port::direction_e dir, nframes_t latency ); + virtual nframes_t get_latency ( JACK::Port::direction_e dir, nframes_t *min, nframes_t *max ) const; + virtual void set_latency ( JACK::Port::direction_e dir, nframes_t min, nframes_t max ); /* true if this module was added by default and not under normal user control */ bool is_default ( void ) const { return _is_default; } diff --git a/mixer/src/Plugin_Module.C b/mixer/src/Plugin_Module.C index 4d41a25..a202418 100644 --- a/mixer/src/Plugin_Module.C +++ b/mixer/src/Plugin_Module.C @@ -420,15 +420,12 @@ Plugin_Module::get_plugin_latency ( void ) const return 0; } - nframes_t -Plugin_Module::get_latency ( JACK::Port::direction_e dir ) const +Plugin_Module::get_latency ( JACK::Port::direction_e dir, nframes_t *min, nframes_t *max ) const { - nframes_t latency = Module::get_latency( dir ); + Module::get_latency( dir, min, max ); - latency += get_plugin_latency(); - - return latency; + return get_plugin_latency(); } bool diff --git a/mixer/src/Plugin_Module.H b/mixer/src/Plugin_Module.H index d7c420c..aa51ab9 100644 --- a/mixer/src/Plugin_Module.H +++ b/mixer/src/Plugin_Module.H @@ -67,7 +67,7 @@ private: volatile nframes_t _latency; nframes_t _last_latency; - + nframes_t get_plugin_latency ( void ) const; void init ( void ); @@ -112,7 +112,7 @@ private: public: virtual void update ( void ); - virtual nframes_t get_latency ( JACK::Port::direction_e dir ) const; + virtual nframes_t get_latency ( JACK::Port::direction_e dir, nframes_t *min, nframes_t *max ) const; static std::list get_all_plugins ( void ); diff --git a/timeline/src/Engine/Track.C b/timeline/src/Engine/Track.C index 0273ad5..cc0b502 100644 --- a/timeline/src/Engine/Track.C +++ b/timeline/src/Engine/Track.C @@ -312,18 +312,20 @@ Track::record ( Capture *c, nframes_t frame ) { /* in freewheeling mode, assume we're bouncing and only * compensate for capture latency */ - _capture_offset = min; + _capture_offset = max; } else { /* not freewheeling, so assume we're overdubbing and need to * compensate for both capture and playback latency */ - _capture_offset = min; + _capture_offset = max; /* since the track output might not be connected to * anything, just get the playback latency */ - - _capture_offset += engine->playback_latency(); + /* When playback latency compensation is enabled, this will + * have already been done. */ + if ( ! Timeline::playback_latency_compensation ) + _capture_offset += engine->playback_latency(); } } @@ -371,9 +373,13 @@ Track::compute_latency_compensation ( void ) nframes_t min,max; output[0].get_latency( JACK::Port::Output, &min, &max ); - DMESSAGE( "Track %s, setting undelay to %lu", name(), (unsigned long)min); + DMESSAGE( "Track %s, setting undelay to %lu", name(), (unsigned long)max); + char s[256]; + snprintf( s, sizeof(s), "Latency Comp: -%lu", (unsigned long)max); + + copy_tooltip(s); - undelay( min ); + undelay( max ); } else {