| 1 |
/* |
| 2 |
* Copyright 2018 Malte Veerman <[email protected]> |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU General Public License as |
| 6 |
* published by the Free Software Foundation; either version 2 of |
| 7 |
* the License or (at your option) version 3 or any later version |
| 8 |
* accepted by the membership of KDE e.V. (or its successor approved |
| 9 |
* by the membership of KDE e.V.), which shall act as a proxy |
| 10 |
* defined in Section 14 of version 3 of the license. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "AnalyzerWorker.h" |
| 22 |
|
| 23 |
#include "core/support/Debug.h" |
| 24 |
#include "EngineController.h" |
| 25 |
|
| 26 |
#include <QThread> |
| 27 |
#include <QTimer> |
| 28 |
|
| 29 |
|
| 30 |
Analyzer::Worker::Worker() |
| 31 |
: m_currentScope( QVector<double>( 1, 0.0 ) ) |
| 32 |
, m_size( 0 ) |
| 33 |
, m_windowFunction( Base::Hann ) |
| 34 |
, m_expectedDataTime( 20 ) |
| 35 |
, m_demoT( 201 ) |
| 36 |
, m_lastUpdate() |
| 37 |
, m_demoTimer( new QTimer( this ) ) |
| 38 |
, m_processTimer( new QTimer( this ) ) |
| 39 |
, m_hibernate( false ) |
| 40 |
{ |
| 41 |
m_in = (double*) fftw_malloc( m_size * sizeof( double ) ); |
| 42 |
m_out = (std::complex<double>*) fftw_malloc( ( m_size / 2 + 1 ) * sizeof( std::complex<double> ) ); |
| 43 |
m_plan = fftw_plan_dft_r2c_1d( m_size, m_in, reinterpret_cast<fftw_complex*>( m_out ), FFTW_ESTIMATE ); |
| 44 |
|
| 45 |
m_demoTimer->setInterval( Analyzer::Base::DEMO_INTERVAL ); |
| 46 |
m_processTimer->setInterval( PROCESSING_INTERVAL ); |
| 47 |
if( EngineController::instance()->isPlaying() ) |
| 48 |
m_processTimer->start(); |
| 49 |
else |
| 50 |
m_demoTimer->start(); |
| 51 |
m_lastUpdate.start(); |
| 52 |
|
| 53 |
connect( m_demoTimer, &QTimer::timeout, this, &Worker::demo ); |
| 54 |
connect( m_processTimer, &QTimer::timeout, this, &Worker::processData ); |
| 55 |
} |
| 56 |
|
| 57 |
Analyzer::Worker::~Worker() |
| 58 |
{ |
| 59 |
fftw_destroy_plan( m_plan ); |
| 60 |
fftw_free( m_in ); |
| 61 |
fftw_free( m_out ); |
| 62 |
} |
| 63 |
|
| 64 |
void Analyzer::Worker::stopTimers() |
| 65 |
{ |
| 66 |
m_demoTimer->stop(); |
| 67 |
m_processTimer->stop(); |
| 68 |
} |
| 69 |
|
| 70 |
void Analyzer::Worker::setHibernate( bool state ) |
| 71 |
{ |
| 72 |
m_hibernate = state; |
| 73 |
if( m_hibernate ) |
| 74 |
stopTimers(); |
| 75 |
else |
| 76 |
playbackStateChanged(); |
| 77 |
} |
| 78 |
|
| 79 |
void Analyzer::Worker::receiveData( const QMap<int, QVector<qint16> > &newData ) |
| 80 |
{ |
| 81 |
const int newDataSize = EngineController::DATAOUTPUT_DATA_SIZE; |
| 82 |
|
| 83 |
if( newData.isEmpty() || newData[0].size() != newDataSize ) |
| 84 |
return; |
| 85 |
|
| 86 |
m_rawInMutex.lock(); |
| 87 |
|
| 88 |
for( int x = 0; x < newDataSize; x++ ) |
| 89 |
{ |
| 90 |
if( newData.size() == 1 ) // Mono |
| 91 |
{ |
| 92 |
m_rawIn << double( newData[0][x] ); |
| 93 |
} |
| 94 |
else // Anything > Mono is treated as Stereo |
| 95 |
{ |
| 96 |
m_rawIn << ( double( newData[0][x] ) |
| 97 |
+ double( newData[1][x] ) ) |
| 98 |
/ 2; // Average between the channels |
| 99 |
} |
| 100 |
m_rawIn.last() /= ( 1 << 15 ); // Scale to [0, 1] |
| 101 |
} |
| 102 |
|
| 103 |
m_rawInMutex.unlock(); |
| 104 |
} |
| 105 |
|
| 106 |
void Analyzer::Worker::processData() |
| 107 |
{ |
| 108 |
int timeElapsed = m_lastUpdate.elapsed(); |
| 109 |
|
| 110 |
// Delay if processing is too fast |
| 111 |
if( timeElapsed < m_expectedDataTime - 1 ) |
| 112 |
QThread::currentThread()->msleep( m_expectedDataTime - timeElapsed - 1 ); |
| 113 |
|
| 114 |
applyWindowFunction(); |
| 115 |
} |
| 116 |
|
| 117 |
void Analyzer::Worker::applyWindowFunction() |
| 118 |
{ |
| 119 |
m_rawInMutex.lock(); |
| 120 |
|
| 121 |
if( m_rawIn.size() < (int)m_size ) |
| 122 |
{ |
| 123 |
m_rawInMutex.unlock(); |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
const int newDataSize = EngineController::DATAOUTPUT_DATA_SIZE; |
| 128 |
|
| 129 |
while( m_rawIn.size() > (int)m_size + DATA_BUFFER_SIZE * newDataSize ) |
| 130 |
m_rawIn.removeFirst(); |
| 131 |
|
| 132 |
// Apply window function |
| 133 |
for( uint i = 0; i < m_size; i++ ) |
| 134 |
{ |
| 135 |
double windowFactor = 1.0; |
| 136 |
switch( m_windowFunction ) |
| 137 |
{ |
| 138 |
case Base::Rectangular: |
| 139 |
{ |
| 140 |
windowFactor = 1.0; |
| 141 |
break; |
| 142 |
} |
| 143 |
case Base::Hann: |
| 144 |
{ |
| 145 |
windowFactor = ( 1.0 - cos( 2.0 * M_PI * i / ( m_size - 1 ) ) ) / 2.0; |
| 146 |
break; |
| 147 |
} |
| 148 |
case Base::Nuttall: |
| 149 |
{ |
| 150 |
const double a = 0.355768; |
| 151 |
const double b = 0.487396 * cos( 2 * M_PI * i / ( m_size - 1 ) ); |
| 152 |
const double c = 0.144232 * cos( 4 * M_PI * i / ( m_size - 1 ) ); |
| 153 |
const double d = 0.012604 * cos( 6 * M_PI * i / ( m_size - 1 ) ); |
| 154 |
windowFactor = a - b + c - d; |
| 155 |
break; |
| 156 |
} |
| 157 |
case Base::Lanczos: |
| 158 |
{ |
| 159 |
const double x = 2.0 * i / ( m_size - 1 ) - 1; |
| 160 |
windowFactor = sin( M_PI * x ) / M_PI / x; |
| 161 |
break; |
| 162 |
} |
| 163 |
case Base::Sine: |
| 164 |
{ |
| 165 |
windowFactor = ( M_PI * i ) / ( m_size - 1 ); |
| 166 |
break; |
| 167 |
} |
| 168 |
}; |
| 169 |
|
| 170 |
if( i < newDataSize ) |
| 171 |
m_in[i] = m_rawIn.takeFirst() * windowFactor; |
| 172 |
else |
| 173 |
m_in[i] = m_rawIn.at( i - newDataSize ) * windowFactor; |
| 174 |
} |
| 175 |
|
| 176 |
m_rawInMutex.unlock(); |
| 177 |
|
| 178 |
fftw_execute( m_plan ); |
| 179 |
makeScope(); |
| 180 |
} |
| 181 |
|
| 182 |
void Analyzer::Worker::makeScope() |
| 183 |
{ |
| 184 |
for( const auto& band : m_notInterpolatedScopeBands ) |
| 185 |
{ |
| 186 |
m_currentScope[band.scopeIndex] = 0.0; |
| 187 |
uint numValues = 0; |
| 188 |
for( long k = std::lround( std::ceil( band.lowerK ) ); k <= std::lround( std::floor( band.upperK ) ); k++ ) |
| 189 |
{ |
| 190 |
m_currentScope[band.scopeIndex] += std::abs( m_out[k] ) * sqrt( k ); |
| 191 |
numValues++; |
| 192 |
} |
| 193 |
m_currentScope[band.scopeIndex] /= numValues; |
| 194 |
m_currentScope[band.scopeIndex] /= m_size / 2; |
| 195 |
} |
| 196 |
|
| 197 |
// monotone cubic interpolation |
| 198 |
if( !m_interpolatedScopeBands.isEmpty() ) |
| 199 |
{ |
| 200 |
QVector<QPointF> data; |
| 201 |
for( uint k = 0; k < m_size / 2 + 1 && k <= m_interpolatedScopeBands.last().midK; k++ ) |
| 202 |
{ |
| 203 |
data << QPointF( k, std::abs( m_out[k] ) * sqrt( k ) / m_size * 2 ); |
| 204 |
} |
| 205 |
// Get consecutive differences and slopes |
| 206 |
QVector<double> dys, dxs, ms; |
| 207 |
for( int i = 0; i < data.size() - 1; i++ ) |
| 208 |
{ |
| 209 |
double dx = data[i + 1].x() - data[i].x(); |
| 210 |
double dy = data[i + 1].y() - data[i].y(); |
| 211 |
dxs << dx; |
| 212 |
dys << dy; |
| 213 |
ms << dy / dx; |
| 214 |
} |
| 215 |
if( ms.size() == 0 ) // make code quality check happy |
| 216 |
ms << 0; |
| 217 |
// Get degree-1 coefficients |
| 218 |
QVector<double> c1s = QVector<double>() << ms[0]; |
| 219 |
for( int i = 0; i < dxs.size() - 1; i++) |
| 220 |
{ |
| 221 |
double m = ms[i], mNext = ms[i + 1]; |
| 222 |
if( m * mNext <= 0 ) |
| 223 |
c1s << 0.0; |
| 224 |
else |
| 225 |
{ |
| 226 |
double dx_ = dxs[i], dxNext = dxs[i + 1], common = dx_ + dxNext; |
| 227 |
c1s << ( 3 * common / ( ( common + dxNext ) / m + ( common + dx_ ) / mNext ) ); |
| 228 |
} |
| 229 |
} |
| 230 |
c1s << ms.last(); |
| 231 |
// Get degree-2 and degree-3 coefficients |
| 232 |
QVector<double> c2s, c3s; |
| 233 |
for( int i = 0; i < c1s.size() - 1; i++ ) |
| 234 |
{ |
| 235 |
double c1 = c1s[i], m_ = ms[i], invDx = 1 / dxs[i], common_ = c1 + c1s[i + 1] - m_ - m_; |
| 236 |
c2s << ( m_ - c1 - common_ ) * invDx; |
| 237 |
c3s << common_ * invDx * invDx; |
| 238 |
} |
| 239 |
// write interpolated data to scope |
| 240 |
for( auto &band : m_interpolatedScopeBands ) |
| 241 |
{ |
| 242 |
const double x = band.midK; |
| 243 |
auto &scope = m_currentScope[band.scopeIndex]; |
| 244 |
|
| 245 |
// Search for the interval x is in, returning the corresponding y if x is one of the original xs |
| 246 |
if( c3s.size() == 0 ) // make code quality check happy |
| 247 |
c3s << 0; |
| 248 |
int low = 0, mid, high = c3s.size() - 1; |
| 249 |
while ( low <= high ) |
| 250 |
{ |
| 251 |
mid = std::floor( 0.5 * ( low + high ) ); |
| 252 |
double xHere = data[mid].x(); |
| 253 |
if( xHere < x ) |
| 254 |
low = mid + 1; |
| 255 |
else if( xHere > x ) |
| 256 |
high = mid - 1; |
| 257 |
else |
| 258 |
scope = data[mid].y(); |
| 259 |
} |
| 260 |
int i = qMax( 0, high ); |
| 261 |
|
| 262 |
// Interpolate |
| 263 |
double diff = x - data[i].x(), diffSq = diff * diff; |
| 264 |
scope = qMax( 0.0, data[i].y() + c1s[i] * diff + c2s[i] * diffSq + c3s[i] * diff * diffSq ); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
analyze(); |
| 269 |
} |
| 270 |
|
| 271 |
void Analyzer::Worker::setSampleSize( uint size ) |
| 272 |
{ |
| 273 |
if( m_size == size ) |
| 274 |
return; |
| 275 |
|
| 276 |
m_size = size; |
| 277 |
|
| 278 |
fftw_destroy_plan( m_plan ); |
| 279 |
fftw_free( m_in ); |
| 280 |
fftw_free( m_out ); |
| 281 |
|
| 282 |
m_in = (double*) fftw_malloc( m_size * sizeof( double ) ); |
| 283 |
m_out = (std::complex<double>*) fftw_malloc( ( m_size / 2 + 1 ) * sizeof( std::complex<double> ) ); |
| 284 |
m_plan = fftw_plan_dft_r2c_1d( m_size, m_in, reinterpret_cast<fftw_complex*>( m_out ), FFTW_ESTIMATE ); |
| 285 |
} |
| 286 |
|
| 287 |
void Analyzer::Worker::setWindowFunction( Base::WindowFunction windowFunction ) |
| 288 |
{ |
| 289 |
if( m_windowFunction == windowFunction ) |
| 290 |
return; |
| 291 |
|
| 292 |
m_windowFunction = windowFunction; |
| 293 |
} |
| 294 |
|
| 295 |
void Analyzer::Worker::setScopeSize( int size ) |
| 296 |
{ |
| 297 |
m_currentScope.resize( size ); |
| 298 |
} |
| 299 |
|
| 300 |
void Analyzer::Worker::calculateExpFactor( qreal minFreq, qreal maxFreq, int sampleRate ) |
| 301 |
{ |
| 302 |
DEBUG_BLOCK |
| 303 |
|
| 304 |
if( minFreq <= 0.0 ) |
| 305 |
{ |
| 306 |
warning() << "Minimum frequency must be greater than zero!"; |
| 307 |
minFreq = 1.0; |
| 308 |
} |
| 309 |
|
| 310 |
if( minFreq >= maxFreq ) |
| 311 |
{ |
| 312 |
warning() << "Minimum frequency must be smaller than maximum frequency!"; |
| 313 |
maxFreq = minFreq + 1.0; |
| 314 |
} |
| 315 |
|
| 316 |
if( sampleRate == 0 ) |
| 317 |
{ |
| 318 |
debug() << "Reported impossible sample rate of zero. Assuming 44.1KHz."; |
| 319 |
sampleRate = 44100; |
| 320 |
} |
| 321 |
|
| 322 |
m_expFactor = pow( maxFreq / minFreq, 1.0 / m_currentScope.size() ); |
| 323 |
m_expectedDataTime = std::floor( (qreal)EngineController::DATAOUTPUT_DATA_SIZE * 1000.0 / sampleRate ); |
| 324 |
|
| 325 |
m_interpolatedScopeBands.clear(); |
| 326 |
m_notInterpolatedScopeBands.clear(); |
| 327 |
const uint outputSize = m_size / 2 + 1; |
| 328 |
|
| 329 |
for( int scopeIndex = 0; scopeIndex < m_currentScope.size(); scopeIndex++ ) |
| 330 |
{ |
| 331 |
BandInfo newBandInfo; |
| 332 |
newBandInfo.lowerFreq = minFreq * pow( m_expFactor, double( scopeIndex ) - 0.5 ); |
| 333 |
newBandInfo.midFreq = minFreq * pow( m_expFactor, scopeIndex ); |
| 334 |
newBandInfo.upperFreq = minFreq * pow( m_expFactor, double( scopeIndex ) + 0.5 ); |
| 335 |
newBandInfo.lowerK = newBandInfo.lowerFreq / ( sampleRate / 2 ) * outputSize; |
| 336 |
newBandInfo.midK = newBandInfo.midFreq / ( sampleRate / 2 ) * outputSize; |
| 337 |
newBandInfo.upperK = newBandInfo.upperFreq / ( sampleRate / 2 ) * outputSize; |
| 338 |
newBandInfo.scopeIndex = scopeIndex; |
| 339 |
|
| 340 |
if ( std::floor( newBandInfo.upperK ) >= std::ceil( newBandInfo.lowerK ) ) |
| 341 |
m_notInterpolatedScopeBands << newBandInfo; |
| 342 |
else |
| 343 |
m_interpolatedScopeBands << newBandInfo; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
void Analyzer::Worker::demo() |
| 348 |
{ |
| 349 |
if( m_demoT > 300 ) |
| 350 |
m_demoT = 1; //0 = wasted calculations |
| 351 |
|
| 352 |
if( m_demoT < 201 ) |
| 353 |
{ |
| 354 |
const double dt = double( m_demoT ) / 200; |
| 355 |
for( int i = 0; i < m_currentScope.size(); ++i ) |
| 356 |
{ |
| 357 |
m_currentScope[i] = dt * ( sin( M_PI + ( i * M_PI ) / m_currentScope.size() ) + 1.0 ); |
| 358 |
} |
| 359 |
} |
| 360 |
else |
| 361 |
{ |
| 362 |
for( int i = 0; i < m_currentScope.size(); ++i ) |
| 363 |
{ |
| 364 |
m_currentScope[i] = 0.0; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
++m_demoT; |
| 369 |
|
| 370 |
int timeElapsed = m_lastUpdate.elapsed(); |
| 371 |
|
| 372 |
// Delay if interval is too low |
| 373 |
if( timeElapsed < Analyzer::Base::DEMO_INTERVAL - 1 ) |
| 374 |
QThread::currentThread()->msleep( Analyzer::Base::DEMO_INTERVAL - 1 - timeElapsed ); |
| 375 |
|
| 376 |
m_lastUpdate.restart(); |
| 377 |
|
| 378 |
analyze(); |
| 379 |
} |
| 380 |
|
| 381 |
void Analyzer::Worker::playbackStateChanged() |
| 382 |
{ |
| 383 |
if( m_hibernate ) |
| 384 |
return; |
| 385 |
bool playing = EngineController::instance()->isPlaying(); |
| 386 |
playing ? m_demoTimer->stop() : m_demoTimer->start(); |
| 387 |
playing ? m_processTimer->start() : m_processTimer->stop(); |
| 388 |
resetDemo(); |
| 389 |
} |