diff --git a/inputsource.cpp b/inputsource.cpp index ca03fd3..2cb1c31 100644 --- a/inputsource.cpp +++ b/inputsource.cpp @@ -36,9 +36,9 @@ InputSource::InputSource(const char *filename) { if (fstat(fileno(m_file), &sb) != 0) throw std::runtime_error("Error fstating file"); m_file_size = sb.st_size; - sampleCount = m_file_size / sizeof(fftwf_complex); + sampleCount = m_file_size / sizeof(std::complex); - m_data = (fftwf_complex*)mmap(NULL, m_file_size, PROT_READ, MAP_SHARED, fileno(m_file), 0); + m_data = (std::complex*)mmap(NULL, m_file_size, PROT_READ, MAP_SHARED, fileno(m_file), 0); if (m_data == 0) throw std::runtime_error("Error mmapping file"); } @@ -48,7 +48,7 @@ InputSource::~InputSource() { fclose(m_file); } -bool InputSource::getSamples(fftwf_complex *dest, off_t start, int length) +bool InputSource::getSamples(std::complex *dest, off_t start, off_t length) { if(start < 0 || length < 0) return false; @@ -56,6 +56,6 @@ bool InputSource::getSamples(fftwf_complex *dest, off_t start, int length) if (start + length >= sampleCount) return false; - memcpy(dest, &m_data[start], length * sizeof(fftwf_complex)); + memcpy(dest, &m_data[start], length * sizeof(std::complex)); return true; } diff --git a/inputsource.h b/inputsource.h index ad310f2..d3e3bd6 100644 --- a/inputsource.h +++ b/inputsource.h @@ -19,22 +19,21 @@ #pragma once -#include "fft.h" -#include -#include +#include +#include "samplesource.h" -class InputSource +class InputSource : public SampleSource { private: FILE *m_file; off_t m_file_size; off_t sampleCount; - fftwf_complex *m_data; + std::complex *m_data; public: InputSource(const char *filename); ~InputSource(); - bool getSamples(fftwf_complex *dest, off_t start, int length); - off_t getSampleCount() { return sampleCount; }; + bool getSamples(std::complex *dest, off_t start, off_t length); + off_t count() { return sampleCount; }; }; diff --git a/samplesource.h b/samplesource.h index 1682337..523a43f 100644 --- a/samplesource.h +++ b/samplesource.h @@ -27,6 +27,6 @@ class SampleSource public: virtual ~SampleSource() {}; - virtual bool getSamples(std::complex *dest, off_t start, off_t length); + virtual bool getSamples(std::complex *dest, off_t start, off_t length) = 0; virtual off_t count() = 0; }; diff --git a/spectrogram.cpp b/spectrogram.cpp index c70f201..f756473 100644 --- a/spectrogram.cpp +++ b/spectrogram.cpp @@ -143,19 +143,19 @@ float* Spectrogram::getFFTTile(off_t tile) void Spectrogram::getLine(float *dest, off_t sample) { if (inputSource && fft) { - fftwf_complex buffer[fftSize]; + std::complex buffer[fftSize]; inputSource->getSamples(buffer, sample, fftSize); for (int i = 0; i < fftSize; i++) { - buffer[i][0] *= window[i]; - buffer[i][1] *= window[i]; + buffer[i].real(buffer[i].real() * window[i]); + buffer[i].imag(buffer[i].imag() * window[i]); } fft->process(buffer, buffer); for (int i = 0; i < fftSize; i++) { int k = (i + fftSize / 2) % fftSize; - float re = buffer[k][0]; - float im = buffer[k][1]; + float re = buffer[k].real(); + float im = buffer[k].imag(); float mag = sqrt(re * re + im * im) / fftSize; float magdb = 10 * log2(mag) / log2(10); *dest = magdb; @@ -227,7 +227,7 @@ int Spectrogram::getHeight() if (!inputSource) return 0; - return inputSource->getSampleCount() / getStride(); + return inputSource->count() / getStride(); } int Spectrogram::getStride() diff --git a/spectrogram.h b/spectrogram.h index bc8bc4e..44f278b 100644 --- a/spectrogram.h +++ b/spectrogram.h @@ -24,6 +24,7 @@ #include "fft.h" #include "inputsource.h" +#include #include static const double Tau = M_PI * 2.0; diff --git a/waveformview.cpp b/waveformview.cpp index 3be2e04..a677867 100644 --- a/waveformview.cpp +++ b/waveformview.cpp @@ -55,7 +55,7 @@ void WaveformView::paintEvent(QPaintEvent *event) painter.fillRect(rect, Qt::black); off_t length = lastSample - firstSample; - fftwf_complex *samples = new fftwf_complex[length]; + std::complex *samples = new std::complex[length]; inputSource->getSamples(samples, firstSample, length); for (int iq = 0; iq < 2; iq++) { switch (iq) { @@ -70,7 +70,7 @@ void WaveformView::paintEvent(QPaintEvent *event) int yprev = 0; for (off_t i = 0; i < length; i++) { int x = (float)i / length * rect.width(); - int y = (samples[i][iq] * 50 * rect.height()/2) + rect.height()/2; + int y = (samples[i].real() * 50 * rect.height()/2) + rect.height()/2; if (x < 0) x = 0; if (y < 0) y = 0;