Debian Codegraph @

src/context/applets/analyzer/plugin/AnalyzerBase.cpp

raw
1 /****************************************************************************************
2 * Copyright (c) 2003 Max Howell <[email protected]> *
3 * Copyright (c) 2009 Martin Sandsmark <[email protected]> *
4 * Copyright (c) 2013 Mark Kretschmann <[email protected]> *
5 * Copyright (c) 2017 Malte Veerman <[email protected]> *
6 * *
7 * This program is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU General Public License as published by the Free Software *
9 * Foundation; either version 2 of the License, or (at your option) any later *
10 * version. *
11 * *
12 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
14 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License along with *
17 * this program. If not, see <http://www.gnu.org/licenses/>. *
18 ****************************************************************************************/
19
20 #include "AnalyzerBase.h"
21 #include "AnalyzerWorker.h"
22
23 #include "core/support/Amarok.h"
24 #include "core/support/Debug.h"
25 #include "EngineController.h"
26 #include "ContextDock.h"
27 #include "MainWindow.h"
28
29 #include <cmath>
30
31 #include <QQuickWindow>
32 #include <QTimer>
33
34
35 // INSTRUCTIONS
36 // 1. Reimplement QQuickFramebufferObject::createRenderer().
37 // 2. Reimplement Analyzer::Base::createWorker().
38 // 3. Set your preferred scope width with setScopeSize().
39
40
41 Analyzer::Base::Base( QQuickItem *parent )
42 : QQuickFramebufferObject( parent )
43 , m_sampleRate( 44100 )
44 , m_scopeSize( 0 )
45 , m_worker( nullptr )
46 {
47 DEBUG_BLOCK
48
49 qRegisterMetaType<WindowFunction>("WindowFunction");
50
51 m_minFreq = config().readEntry( "minFreq", 50.0 );
52 m_maxFreq = config().readEntry( "maxFreq", 15000.0 );
53
54 connect( The::engineController(), &EngineController::trackChanged, this, &Base::refreshSampleRate );
55 connect( The::engineController(), &EngineController::trackMetadataChanged, this, &Base::refreshSampleRate );
56
57 QTimer::singleShot( 0, this, &Base::connectSignals );
58
59 // Possibly wait for The::mainWindow to finish initialization first
60 QTimer::singleShot( The::mainWindow() ? 0 : 200, [this](){
61 connect( The::mainWindow(), &MainWindow::drawNeedChanged, this, &Base::drawNeedChanged );
62 connect( The::mainWindow()->contextDock(), &ContextDock::visibilityChanged, this, &Base::drawNeedChanged );
63 } );
64 }
65
66 Analyzer::Base::~Base()
67 {
68 DEBUG_BLOCK
69
70 if( m_worker )
71 {
72 m_worker->deleteLater();
73 m_worker = nullptr;
74 }
75
76 m_workerThread.quit();
77 m_workerThread.wait();
78 }
79
80 void
81 Analyzer::Base::connectSignals()
82 {
83 DEBUG_BLOCK
84
85 if( !m_worker )
86 {
87 m_worker = createWorker();
88 m_worker->setSampleSize( sampleSize() );
89 m_worker->setScopeSize( m_scopeSize );
90 m_worker->setWindowFunction( windowFunction() );
91 m_worker->moveToThread( &m_workerThread );
92 m_workerThread.start();
93
94 connect( this, &Base::calculateExpFactorNeeded, m_worker, &Worker::calculateExpFactor );
95 connect( this, &Base::windowFunctionChanged, m_worker, &Worker::setWindowFunction );
96 connect( this, &Base::sampleSizeChanged, m_worker, &Worker::setSampleSize );
97 connect( this, &Base::scopeSizeChanged, m_worker, &Worker::setScopeSize );
98 connect( The::engineController(), &EngineController::playbackStateChanged, m_worker, &Worker::playbackStateChanged );
99
100 setSampleSize( config().readEntry( "sampleSize", 4096 ) );
101 setWindowFunction( (WindowFunction) config().readEntry( "windowFunction", (int)Hann ) );
102 Q_EMIT calculateExpFactorNeeded( m_minFreq, m_maxFreq, m_sampleRate);
103 }
104 if( m_worker )
105 connect( The::engineController(), &EngineController::audioDataReady, m_worker, &Worker::receiveData,
106 static_cast<Qt::ConnectionType>( Qt::DirectConnection | Qt::UniqueConnection ) );
107 }
108
109 void
110 Analyzer::Base::disconnectSignals()
111 {
112 DEBUG_BLOCK
113
114 if( m_worker )
115 disconnect( The::engineController(), &EngineController::audioDataReady, m_worker, &Worker::receiveData );
116 }
117
118 void
119 Analyzer::Base::drawNeedChanged( const bool drawingNeeded )
120 {
121 // Optimization for at least X11/Linux desktops:
122 // Don't update the analyzer if Amarok is not on the active virtual desktop or otherwise not visible.
123
124 if( drawingNeeded )
125 {
126 connectSignals();
127 QTimer::singleShot( 0, m_worker, [=]() { m_worker->setHibernate( false ); } );
128 }
129 else
130 {
131 disconnectSignals();
132 QTimer::singleShot( 0, m_worker, [=]() { m_worker->setHibernate( true ); } );
133 }
134 }
135
136 void
137 Analyzer::Base::refreshSampleRate()
138 {
139 const auto currentTrack = The::engineController()->currentTrack();
140 int sampleRate = currentTrack ? currentTrack->sampleRate() : 44100;
141
142 if( m_sampleRate == sampleRate )
143 return;
144
145 m_sampleRate = sampleRate;
146
147 Q_EMIT calculateExpFactorNeeded( m_minFreq, m_maxFreq, m_sampleRate );
148 }
149
150 KConfigGroup
151 Analyzer::Base::config() const
152 {
153 return Amarok::config( QStringLiteral( "Context" ) ).group( QStringLiteral("Analyzer") );
154 }
155
156 void
157 Analyzer::Base::setScopeSize( int scopeSize )
158 {
159 if( scopeSize <= 0 )
160 {
161 debug() << "Scope size must be greater than zero";
162 return;
163 }
164
165 if( m_scopeSize == scopeSize )
166 return;
167
168 m_scopeSize = scopeSize;
169 Q_EMIT scopeSizeChanged( scopeSize );
170 Q_EMIT calculateExpFactorNeeded( m_minFreq, m_maxFreq, m_sampleRate );
171 }
172
173 void
174 Analyzer::Base::setMaxFreq( qreal maxFreq )
175 {
176 DEBUG_BLOCK
177
178 debug() << "Set maximum frequency to:" << maxFreq;
179
180 if( m_maxFreq == maxFreq || maxFreq < 0.0 )
181 return;
182
183 config().writeEntry( "maxFreq", maxFreq );
184 m_maxFreq = maxFreq;
185 Q_EMIT maxFreqChanged();
186 Q_EMIT calculateExpFactorNeeded( m_minFreq, m_maxFreq, m_sampleRate );
187 }
188
189 void
190 Analyzer::Base::setMinFreq( qreal minFreq )
191 {
192 DEBUG_BLOCK
193
194 debug() << "Set minimum frequency to:" << minFreq;
195
196 if( m_minFreq == minFreq || minFreq <= 0.0 )
197 return;
198
199 config().writeEntry( "minFreq", minFreq );
200 m_minFreq = minFreq;
201 Q_EMIT minFreqChanged();
202 Q_EMIT calculateExpFactorNeeded( m_minFreq, m_maxFreq, m_sampleRate );
203 }
204
205 Analyzer::Base::WindowFunction
206 Analyzer::Base::windowFunction() const
207 {
208 return (WindowFunction)config().readEntry( "windowFunction", (int)Hann );
209 }
210
211 void
212 Analyzer::Base::setWindowFunction( WindowFunction windowFunction )
213 {
214 DEBUG_BLOCK
215
216 debug() << "Set window function to:" << windowFunction;
217
218 config().writeEntry( "windowFunction", (int)windowFunction );
219 Q_EMIT windowFunctionChanged( windowFunction );
220 }
221
222 int Analyzer::Base::sampleSize() const
223 {
224 return config().readEntry( "sampleSize", 2048 );
225 }
226
227 void
228 Analyzer::Base::setSampleSize( uint sampleSize )
229 {
230 DEBUG_BLOCK
231
232 debug() << "Set sample size to:" << sampleSize;
233
234 if( sampleSize < (int) EngineController::DATAOUTPUT_DATA_SIZE )
235 {
236 warning() << "Sample size must be at least" << EngineController::DATAOUTPUT_DATA_SIZE;
237 sampleSize = EngineController::DATAOUTPUT_DATA_SIZE;
238 }
239
240 config().writeEntry( "sampleSize", sampleSize );
241 Q_EMIT sampleSizeChanged( sampleSize );
242 Q_EMIT calculateExpFactorNeeded( m_minFreq, m_maxFreq, m_sampleRate );
243 }
244
245 const Analyzer::Worker *
246 Analyzer::Base::worker() const
247 {
248 return const_cast<const Worker*>( m_worker );
249 }
250