26const quint16 PCMS16MaxAmplitude = 32768;
29qreal pcmToReal(qint16 pcm)
31 return qreal(pcm) / PCMS16MaxAmplitude;
34SWFFTCalc::SWFFTCalc( QObject* parent )
41 _hannWindow.resize( SWNUMSAMPLES );
44 _complexFrame.resize( SWNUMSAMPLES );
47 _spectrum.resize( SWNUMSAMPLES/2 );
56 for(
int i=0; i<SWNUMSAMPLES; i++ )
57 _hannWindow[i] = 0.5 * ( 1 - cos( (2*PI*i) / (SWNUMSAMPLES-1) ) );
92SWFFTCalc::~SWFFTCalc()
128 QAudioFormat format = audiobuffer.format();
130 if( !format.isValid()
131 || format.channelCount() != 2
132 || format.sampleType() == QAudioFormat::Unknown )
144 _complexFrame = CArray( 0.0, SWNUMSAMPLES );
145 const char* ptr = (
char*) audiobuffer.constData();
146 int to = qMin( SWNUMSAMPLES, audiobuffer.frameCount() );
149 for(
int i=0; i<to; ++i )
151 const qint16 pcmSample = *
reinterpret_cast<const qint16*
>( ptr );
154 _complexFrame[i] = Complex( _hannWindow[i] * pcmToReal(pcmSample), 0 );
159 bareFFT( _complexFrame );
163 for(
int i=0; i < SWNUMSAMPLES/2; ++i )
165 const qreal magnitude = abs( _complexFrame[i] );
169 qreal amplitude = 0.2 * qLn( magnitude );
174 _spectrum[i] = qBound( 0.0, amplitude, 1.0 );
180 emit spectrumReady( _spectrum );
216void SWFFTCalc::bareFFT( CArray& x )
227 const size_t N = x.size();
232 CArray even = x[std::slice( 0, N/2, 2 )];
233 CArray odd = x[std::slice( 1, N/2, 2 )];
240 for (
size_t k = 0; k < N/2; ++k)
242 Complex t = std::polar( 1.0, -2 * PI * k / N ) * odd[k];
244 x[k+N/2] = even[k] - t;
void collectFrames(QAudioBuffer &audiobuffer)
Track::getPeakValue.