From 83ff745b43ebda7b33c52ac1a80a1408c59bfa70 Mon Sep 17 00:00:00 2001 From: Calvin Morrison Date: Sun, 17 May 2026 09:44:33 -0400 Subject: for the pulse peak streams do nothing if windows aren't open to reduce cpu --- src/model/pulsedevice.cpp | 21 +++++++++++++++++++-- src/model/pulsedevice.h | 2 ++ src/model/pulsemodel.cpp | 14 +++++++++++++- src/model/pulsemodel.h | 5 +++++ src/ui/levelmeter.cpp | 2 +- src/ui/mixerwindow.cpp | 8 ++++++++ src/ui/mixerwindow.h | 1 + src/ui/tmixpopup.cpp | 7 +++++++ src/ui/tmixpopup.h | 1 + 9 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/model/pulsedevice.cpp b/src/model/pulsedevice.cpp index d79ee8e..2f0adb1 100644 --- a/src/model/pulsedevice.cpp +++ b/src/model/pulsedevice.cpp @@ -95,6 +95,22 @@ void PulseDevice::update( const TQString &name, int volume, bool muted, // ---- peak monitoring -------------------------------------------------------- +void PulseDevice::pauseMonitoring() +{ + if ( !m_monitorStream || !m_mainloop ) return; + pa_threaded_mainloop_lock( m_mainloop ); + pa_stream_set_read_callback( m_monitorStream, 0, 0 ); + pa_threaded_mainloop_unlock( m_mainloop ); +} + +void PulseDevice::resumeMonitoring() +{ + if ( !m_monitorStream || !m_mainloop ) return; + pa_threaded_mainloop_lock( m_mainloop ); + pa_stream_set_read_callback( m_monitorStream, monitorReadCb, this ); + pa_threaded_mainloop_unlock( m_mainloop ); +} + void PulseDevice::startMonitoring() { if ( !m_context || !m_mainloop || m_monitorName.isEmpty() || m_monitorStream ) @@ -102,7 +118,7 @@ void PulseDevice::startMonitoring() pa_sample_spec spec; spec.format = PA_SAMPLE_FLOAT32NE; - spec.rate = 25; + spec.rate = 20; spec.channels = 1; pa_threaded_mainloop_lock( m_mainloop ); @@ -112,7 +128,8 @@ void PulseDevice::startMonitoring() m_monitorStream = pa_stream_new_with_proplist( m_context, "tmix-peak", &spec, 0, props ); pa_proplist_free( props ); if ( m_monitorStream ) { - pa_stream_set_read_callback( m_monitorStream, monitorReadCb, this ); + if ( !m_model || m_model->levelsActive() ) + pa_stream_set_read_callback( m_monitorStream, monitorReadCb, this ); // For sink inputs, filter peak detection to just this stream. // Source outputs don't support pa_stream_set_monitor_stream; they connect to the source directly. diff --git a/src/model/pulsedevice.h b/src/model/pulsedevice.h index 27aca02..303bece 100644 --- a/src/model/pulsedevice.h +++ b/src/model/pulsedevice.h @@ -41,6 +41,8 @@ public: void setPAContext( pa_context *ctx, pa_threaded_mainloop *mainloop ); void detach( bool paAlreadyDead = false ); + void pauseMonitoring(); + void resumeMonitoring(); protected: void customEvent( TQCustomEvent *e ); diff --git a/src/model/pulsemodel.cpp b/src/model/pulsemodel.cpp index ad17439..bf9f9ee 100644 --- a/src/model/pulsemodel.cpp +++ b/src/model/pulsemodel.cpp @@ -176,7 +176,7 @@ static void applyCardEvent( const PACardEvent *ev, PulseCardInfo &info ) // ---- ctor / dtor ------------------------------------------------------------ PulseModel::PulseModel( TQObject *parent ) - : TQObject(parent), m_mainloop(0), m_context(0) + : TQObject(parent), m_mainloop(0), m_context(0), m_levelViewers(0) { m_sinks.setAutoDelete( true ); m_sources.setAutoDelete( true ); @@ -686,6 +686,18 @@ AudioDevice *PulseModel::defaultInput() const return 0; } +void PulseModel::addLevelViewer() +{ + if ( ++m_levelViewers == 1 ) + forEachDevice( [](PulseDevice *dev) { dev->resumeMonitoring(); } ); +} + +void PulseModel::removeLevelViewer() +{ + if ( m_levelViewers > 0 && --m_levelViewers == 0 ) + forEachDevice( [](PulseDevice *dev) { dev->pauseMonitoring(); } ); +} + void PulseModel::paOp( std::function fn ) { pa_threaded_mainloop_lock( m_mainloop ); diff --git a/src/model/pulsemodel.h b/src/model/pulsemodel.h index b7200e3..2adfee7 100644 --- a/src/model/pulsemodel.h +++ b/src/model/pulsemodel.h @@ -85,6 +85,10 @@ public: void setSinkPort( uint32_t sinkPaIndex, const TQString &portName ); void setSourcePort( uint32_t sourcePaIndex, const TQString &portName ); + void addLevelViewer(); + void removeLevelViewer(); + bool levelsActive() const { return m_levelViewers > 0; } + protected: void customEvent( TQCustomEvent *e ); @@ -124,6 +128,7 @@ private: pa_threaded_mainloop *m_mainloop; pa_context *m_context; + int m_levelViewers; TQPtrList m_sinks; TQPtrList m_sources; diff --git a/src/ui/levelmeter.cpp b/src/ui/levelmeter.cpp index 52412de..906ef20 100644 --- a/src/ui/levelmeter.cpp +++ b/src/ui/levelmeter.cpp @@ -29,7 +29,7 @@ void LevelMeter::setLevel( float level ) if ( display > m_level ) m_level = m_level * 0.3f + display * 0.7f; // fast attack, not instant else { - m_level *= 0.94f; // slow decay ~1.5s to floor + m_level *= 0.925f; // slow decay ~1.5s to floor at 20Hz if ( m_level < 0.001f ) m_level = 0.0f; } update(); diff --git a/src/ui/mixerwindow.cpp b/src/ui/mixerwindow.cpp index 53113e6..d8463f1 100644 --- a/src/ui/mixerwindow.cpp +++ b/src/ui/mixerwindow.cpp @@ -448,6 +448,14 @@ void MixerWindow::showEvent( TQShowEvent *e ) c->setGroup("MainWindow"); if ( c->hasKey("XPos") && c->hasKey("YPos") ) move( c->readNumEntry("XPos"), c->readNumEntry("YPos") ); + + m_model->addLevelViewer(); +} + +void MixerWindow::hideEvent( TQHideEvent *e ) +{ + TDEMainWindow::hideEvent( e ); + m_model->removeLevelViewer(); } #include "mixerwindow.moc" diff --git a/src/ui/mixerwindow.h b/src/ui/mixerwindow.h index fa613cd..8dc740d 100644 --- a/src/ui/mixerwindow.h +++ b/src/ui/mixerwindow.h @@ -32,6 +32,7 @@ protected: bool queryClose(); bool eventFilter( TQObject *obj, TQEvent *e ); void showEvent( TQShowEvent *e ); + void hideEvent( TQHideEvent *e ); public slots: void bringToFront(); diff --git a/src/ui/tmixpopup.cpp b/src/ui/tmixpopup.cpp index 85a0c54..2cdd16a 100644 --- a/src/ui/tmixpopup.cpp +++ b/src/ui/tmixpopup.cpp @@ -169,10 +169,17 @@ bool TmixPopup::justHidden() const return m_hideTime.isValid() && m_hideTime.elapsed() < 100; } +void TmixPopup::showEvent( TQShowEvent *e ) +{ + TQWidget::showEvent( e ); + m_model->addLevelViewer(); +} + void TmixPopup::hideEvent( TQHideEvent *e ) { TQWidget::hideEvent( e ); m_hideTime.start(); + m_model->removeLevelViewer(); } void TmixPopup::wheelEvent( TQWheelEvent *e ) diff --git a/src/ui/tmixpopup.h b/src/ui/tmixpopup.h index 527f7f7..2f8e463 100644 --- a/src/ui/tmixpopup.h +++ b/src/ui/tmixpopup.h @@ -24,6 +24,7 @@ public: bool justHidden() const; protected: + void showEvent( TQShowEvent *e ); void hideEvent( TQHideEvent *e ); void wheelEvent( TQWheelEvent *e ); -- cgit v1.2.3