diff --git a/fft.h b/fft.h index d30c456..adec9a7 100644 --- a/fft.h +++ b/fft.h @@ -21,12 +21,15 @@ #include -class FFT { +class FFT +{ public: FFT(int size); ~FFT(); void process(void *dest, void *source); - int getSize() { return fftSize; } + int getSize() { + return fftSize; + } private: int fftSize; diff --git a/grsamplebuffer.cpp b/grsamplebuffer.cpp index 5c62025..5bddce1 100644 --- a/grsamplebuffer.cpp +++ b/grsamplebuffer.cpp @@ -17,15 +17,15 @@ * along with this program. If not, see . */ - #include "grsamplebuffer.h" - - template - void GRSampleBuffer::work(void *input, void *output, int length) - { - mem_source->set_source(input, length); - mem_sink->set_sink(output, length); - tb->run(); - } +#include "grsamplebuffer.h" + +template +void GRSampleBuffer::work(void *input, void *output, int length) +{ + mem_source->set_source(input, length); + mem_sink->set_sink(output, length); + tb->run(); +} template class GRSampleBuffer, std::complex>; template class GRSampleBuffer, float>; diff --git a/grsamplebuffer.h b/grsamplebuffer.h index f013457..585202f 100644 --- a/grsamplebuffer.h +++ b/grsamplebuffer.h @@ -30,12 +30,12 @@ template class GRSampleBuffer : public SampleBuffer { private: - gr::top_block_sptr tb; - gr::blocks::memory_source::sptr mem_source; - gr::blocks::memory_sink::sptr mem_sink; + gr::top_block_sptr tb; + gr::blocks::memory_source::sptr mem_source; + gr::blocks::memory_sink::sptr mem_sink; public: - GRSampleBuffer(SampleSource *src, gr::top_block_sptr tb, gr::blocks::memory_source::sptr mem_source, gr::blocks::memory_sink::sptr mem_sink) - : SampleBuffer(src), tb(tb), mem_source(mem_source), mem_sink(mem_sink) {}; - virtual void work(void *input, void *output, int count); + GRSampleBuffer(SampleSource *src, gr::top_block_sptr tb, gr::blocks::memory_source::sptr mem_source, gr::blocks::memory_sink::sptr mem_sink) + : SampleBuffer(src), tb(tb), mem_source(mem_source), mem_sink(mem_sink) {}; + virtual void work(void *input, void *output, int count); }; diff --git a/inputsource.cpp b/inputsource.cpp index 9761612..1fea732 100644 --- a/inputsource.cpp +++ b/inputsource.cpp @@ -27,7 +27,8 @@ #include -InputSource::InputSource(const char *filename) { +InputSource::InputSource(const char *filename) +{ m_file = fopen(filename, "rb"); if (m_file == nullptr) throw std::runtime_error("Error opening file"); @@ -43,7 +44,8 @@ InputSource::InputSource(const char *filename) { throw std::runtime_error("Error mmapping file"); } -InputSource::~InputSource() { +InputSource::~InputSource() +{ munmap(m_data, m_file_size); fclose(m_file); } diff --git a/inputsource.h b/inputsource.h index 63be033..ea31e1e 100644 --- a/inputsource.h +++ b/inputsource.h @@ -35,5 +35,7 @@ public: ~InputSource(); std::unique_ptr[]> getSamples(off_t start, off_t length); - off_t count() { return sampleCount; }; + off_t count() { + return sampleCount; + }; }; diff --git a/main.cpp b/main.cpp index c922599..01ee997 100644 --- a/main.cpp +++ b/main.cpp @@ -35,27 +35,27 @@ int main(int argc, char *argv[]) // Add options QCommandLineOption rateOption(QStringList() << "r" << "rate", - QCoreApplication::translate("main", "Set sample rate."), - QCoreApplication::translate("main", "Hz")); + QCoreApplication::translate("main", "Set sample rate."), + QCoreApplication::translate("main", "Hz")); parser.addOption(rateOption); // Process the actual command line parser.process(a); - if (parser.isSet(rateOption)){ - bool ok; - // Use toDouble just for scientific notation support - int rate = parser.value(rateOption).toDouble(&ok); - if(!ok){ - fputs("ERROR: could not parse rate\n", stderr); - return 1; - } - mainWin.changeSampleRate(rate); + if (parser.isSet(rateOption)) { + bool ok; + // Use toDouble just for scientific notation support + int rate = parser.value(rateOption).toDouble(&ok); + if(!ok) { + fputs("ERROR: could not parse rate\n", stderr); + return 1; + } + mainWin.changeSampleRate(rate); } const QStringList args = parser.positionalArguments(); if (args.size()>=1) - mainWin.openFile(args.at(0)); + mainWin.openFile(args.at(0)); mainWin.show(); return a.exec(); diff --git a/mainwindow.h b/mainwindow.h index d9ab8f9..400c10b 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -35,16 +35,16 @@ public: public slots: void openFile(QString fileName); - void setSampleRate(QString rate); - void setFFTSize(int size); - void setZoomLevel(int zoom); + void setSampleRate(QString rate); + void setFFTSize(int size); + void setZoomLevel(int zoom); void spectrogramSliderMoved(int value); signals: void viewChanged(off_t firstSample, off_t lastSample); protected: - bool eventFilter(QObject *obj, QEvent *event); + bool eventFilter(QObject *obj, QEvent *event); private: QScrollArea scrollArea; @@ -54,7 +54,7 @@ private: void emitViewChanged(); off_t getTopSample(); - off_t getCenterSample(); + off_t getCenterSample(); off_t getBottomSample(); - int getScrollPos(off_t sample); + int getScrollPos(off_t sample); }; diff --git a/memory_sink.h b/memory_sink.h index 634444f..afcd2cb 100644 --- a/memory_sink.h +++ b/memory_sink.h @@ -23,19 +23,21 @@ #include #include -namespace gr { - namespace blocks { - class memory_sink : virtual public sync_block - { - public: - typedef boost::shared_ptr sptr; +namespace gr +{ +namespace blocks +{ +class memory_sink : virtual public sync_block +{ +public: + typedef boost::shared_ptr sptr; - static sptr make(size_t itemsize); + static sptr make(size_t itemsize); - virtual void set_sink(void *sink, size_t length) = 0; - }; + virtual void set_sink(void *sink, size_t length) = 0; +}; - } /* namespace blocks */ +} /* namespace blocks */ } /* namespace gr */ #endif /* INCLUDED_GR_MEMORY_SINK_H */ \ No newline at end of file diff --git a/memory_sink_impl.h b/memory_sink_impl.h index 4ceb41f..c2fdac5 100644 --- a/memory_sink_impl.h +++ b/memory_sink_impl.h @@ -22,29 +22,31 @@ #include "memory_sink.h" -namespace gr { - namespace blocks { +namespace gr +{ +namespace blocks +{ - class memory_sink_impl : public memory_sink - { - private: - size_t d_itemsize; - void *d_sink; - size_t d_length; - size_t d_ptr = 0; +class memory_sink_impl : public memory_sink +{ +private: + size_t d_itemsize; + void *d_sink; + size_t d_length; + size_t d_ptr = 0; - public: - memory_sink_impl(size_t itemsize); - ~memory_sink_impl(); +public: + memory_sink_impl(size_t itemsize); + ~memory_sink_impl(); - void set_sink(void *sink, size_t length); + void set_sink(void *sink, size_t length); - int work(int noutput_items, - gr_vector_const_void_star &input_items, - gr_vector_void_star &output_items); - }; + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; - } /* namespace blocks */ +} /* namespace blocks */ } /* namespace gr */ #endif /* INCLUDED_GR_MEMORY_SINK_IMPL_H */ \ No newline at end of file diff --git a/memory_source.h b/memory_source.h index f9c75e7..10c3c11 100644 --- a/memory_source.h +++ b/memory_source.h @@ -23,19 +23,21 @@ #include #include -namespace gr { - namespace blocks { - class memory_source : virtual public sync_block - { - public: - typedef boost::shared_ptr sptr; +namespace gr +{ +namespace blocks +{ +class memory_source : virtual public sync_block +{ +public: + typedef boost::shared_ptr sptr; - static sptr make(size_t itemsize); + static sptr make(size_t itemsize); - virtual void set_source(void *source, size_t length) = 0; - }; + virtual void set_source(void *source, size_t length) = 0; +}; - } /* namespace blocks */ +} /* namespace blocks */ } /* namespace gr */ #endif /* INCLUDED_GR_MEMORY_SOURCE_H */ \ No newline at end of file diff --git a/memory_source_impl.h b/memory_source_impl.h index 020946a..7c57a0e 100644 --- a/memory_source_impl.h +++ b/memory_source_impl.h @@ -22,29 +22,31 @@ #include "memory_source.h" -namespace gr { - namespace blocks { +namespace gr +{ +namespace blocks +{ - class memory_source_impl : public memory_source - { - private: - size_t d_itemsize; - void *d_source; - size_t d_length; - size_t d_ptr = 0; +class memory_source_impl : public memory_source +{ +private: + size_t d_itemsize; + void *d_source; + size_t d_length; + size_t d_ptr = 0; - public: - memory_source_impl(size_t itemsize); - ~memory_source_impl(); +public: + memory_source_impl(size_t itemsize); + ~memory_source_impl(); - void set_source(void *source, size_t length); + void set_source(void *source, size_t length); - int work(int noutput_items, - gr_vector_const_void_star &input_items, - gr_vector_void_star &output_items); - }; + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); +}; - } /* namespace blocks */ +} /* namespace blocks */ } /* namespace gr */ #endif /* INCLUDED_GR_MEMORY_SOURCE_IMPL_H */ \ No newline at end of file diff --git a/samplebuffer.cpp b/samplebuffer.cpp index 9cc518d..a34a2f8 100644 --- a/samplebuffer.cpp +++ b/samplebuffer.cpp @@ -22,10 +22,10 @@ template std::unique_ptr SampleBuffer::getSamples(off_t start, off_t length) { - auto samples = src->getSamples(start, length); - std::unique_ptr dest(new Tout[length]); - work(samples.get(), dest.get(), length); - return dest; + auto samples = src->getSamples(start, length); + std::unique_ptr dest(new Tout[length]); + work(samples.get(), dest.get(), length); + return dest; } template class SampleBuffer, std::complex>; diff --git a/samplebuffer.h b/samplebuffer.h index 53c0081..b4b065f 100644 --- a/samplebuffer.h +++ b/samplebuffer.h @@ -27,11 +27,13 @@ template class SampleBuffer : public SampleSource { private: - std::shared_ptr> src; + std::shared_ptr> src; public: - SampleBuffer(SampleSource *src) : src(src) {}; - virtual std::unique_ptr getSamples(off_t start, off_t length); - virtual void work(void *input, void *output, int count) = 0; - virtual off_t count() { return src->count(); }; + SampleBuffer(SampleSource *src) : src(src) {}; + virtual std::unique_ptr getSamples(off_t start, off_t length); + virtual void work(void *input, void *output, int count) = 0; + virtual off_t count() { + return src->count(); + }; }; diff --git a/spectrogram.cpp b/spectrogram.cpp index c23afe5..41e0d19 100644 --- a/spectrogram.cpp +++ b/spectrogram.cpp @@ -30,227 +30,233 @@ Spectrogram::Spectrogram() { - sampleRate = 8000000; - setFFTSize(1024); - zoomLevel = 0; - powerMax = 0.0f; - powerMin = -50.0f; + sampleRate = 8000000; + setFFTSize(1024); + zoomLevel = 0; + powerMax = 0.0f; + powerMin = -50.0f; - for (int i = 0; i < 256; i++) { - float p = (float)i / 256; - colormap[i] = QColor::fromHsvF(p * 0.83f, 1.0, 1.0 - p).rgba(); - } + for (int i = 0; i < 256; i++) { + float p = (float)i / 256; + colormap[i] = QColor::fromHsvF(p * 0.83f, 1.0, 1.0 - p).rgba(); + } } Spectrogram::~Spectrogram() { - delete fft; - delete inputSource; + delete fft; + delete inputSource; } -QSize Spectrogram::sizeHint() const { - return QSize(1024, 2048); +QSize Spectrogram::sizeHint() const +{ + return QSize(1024, 2048); } void Spectrogram::openFile(QString fileName) { - if (fileName != nullptr) { - try { - InputSource *newFile = new InputSource(fileName.toUtf8().constData()); - delete inputSource; - pixmapCache.clear(); - fftCache.clear(); - inputSource = newFile; - resize(fftSize, getHeight()); - } catch (std::runtime_error e) { - // TODO: display error - } - } + if (fileName != nullptr) { + try { + InputSource *newFile = new InputSource(fileName.toUtf8().constData()); + delete inputSource; + pixmapCache.clear(); + fftCache.clear(); + inputSource = newFile; + resize(fftSize, getHeight()); + } catch (std::runtime_error e) { + // TODO: display error + } + } } -template const T& clamp (const T& value, const T& min, const T& max) { +template const T& clamp (const T& value, const T& min, const T& max) +{ return std::min(max, std::max(min, value)); } void Spectrogram::paintEvent(QPaintEvent *event) { - QRect rect = event->rect(); - QPainter painter(this); - painter.fillRect(rect, Qt::black); + QRect rect = event->rect(); + QPainter painter(this); + painter.fillRect(rect, Qt::black); - if (inputSource != nullptr) { - int height = rect.height(); - off_t y = rect.y(); + if (inputSource != nullptr) { + int height = rect.height(); + off_t y = rect.y(); - QImage image(fftSize, height, QImage::Format_RGB32); + QImage image(fftSize, height, QImage::Format_RGB32); - while (height > 0) { - int tileOffset = y % linesPerTile(); // To handle drawing a partial first tile - int drawHeight = std::min(linesPerTile() - tileOffset, height); // Draw rest of first tile, full tile, or partial final tile - off_t tileId = lineToSample(y - tileOffset); - QPixmap *tile = getPixmapTile(tileId); - painter.drawPixmap(QRect(0, y, fftSize, drawHeight), *tile, QRect(0, tileOffset, fftSize, drawHeight)); - y += drawHeight; - height -= drawHeight; - } + while (height > 0) { + int tileOffset = y % linesPerTile(); // To handle drawing a partial first tile + int drawHeight = std::min(linesPerTile() - tileOffset, height); // Draw rest of first tile, full tile, or partial final tile + off_t tileId = lineToSample(y - tileOffset); + QPixmap *tile = getPixmapTile(tileId); + painter.drawPixmap(QRect(0, y, fftSize, drawHeight), *tile, QRect(0, tileOffset, fftSize, drawHeight)); + y += drawHeight; + height -= drawHeight; + } - paintTimeAxis(&painter, rect); - } + paintTimeAxis(&painter, rect); + } } QPixmap* Spectrogram::getPixmapTile(off_t tile) { - QPixmap *obj = pixmapCache.object(TileCacheKey(fftSize, zoomLevel, tile)); - if (obj != 0) - return obj; + QPixmap *obj = pixmapCache.object(TileCacheKey(fftSize, zoomLevel, tile)); + if (obj != 0) + return obj; - float *fftTile = getFFTTile(tile); - obj = new QPixmap(fftSize, linesPerTile()); - QImage image(fftSize, linesPerTile(), QImage::Format_RGB32); - for (int y = 0; y < linesPerTile(); y++) { - float *line = &fftTile[y * fftSize]; - for (int x = 0; x < fftSize; x++) { - float powerRange = std::abs(int(powerMin - powerMax)); - float normPower = (line[x] - powerMax) * -1.0f / powerRange; - normPower = clamp(normPower, 0.0f, 1.0f); + float *fftTile = getFFTTile(tile); + obj = new QPixmap(fftSize, linesPerTile()); + QImage image(fftSize, linesPerTile(), QImage::Format_RGB32); + for (int y = 0; y < linesPerTile(); y++) { + float *line = &fftTile[y * fftSize]; + for (int x = 0; x < fftSize; x++) { + float powerRange = std::abs(int(powerMin - powerMax)); + float normPower = (line[x] - powerMax) * -1.0f / powerRange; + normPower = clamp(normPower, 0.0f, 1.0f); - image.setPixel(x, y, colormap[(uint8_t)(normPower * (256 - 1))]); - } - } - obj->convertFromImage(image); - pixmapCache.insert(TileCacheKey(fftSize, zoomLevel, tile), obj); - return obj; + image.setPixel(x, y, colormap[(uint8_t)(normPower * (256 - 1))]); + } + } + obj->convertFromImage(image); + pixmapCache.insert(TileCacheKey(fftSize, zoomLevel, tile), obj); + return obj; } float* Spectrogram::getFFTTile(off_t tile) { - float *obj = fftCache.object(TileCacheKey(fftSize, zoomLevel, tile)); - if (obj != 0) - return obj; + float *obj = fftCache.object(TileCacheKey(fftSize, zoomLevel, tile)); + if (obj != 0) + return obj; - float *dest = new float[tileSize]; - float *ptr = dest; - off_t sample = tile; - while ((ptr - dest) < tileSize) { - getLine(ptr, sample); - sample += getStride(); - ptr += fftSize; - } - fftCache.insert(TileCacheKey(fftSize, zoomLevel, tile), dest); - return dest; + float *dest = new float[tileSize]; + float *ptr = dest; + off_t sample = tile; + while ((ptr - dest) < tileSize) { + getLine(ptr, sample); + sample += getStride(); + ptr += fftSize; + } + fftCache.insert(TileCacheKey(fftSize, zoomLevel, tile), dest); + return dest; } void Spectrogram::getLine(float *dest, off_t sample) { - if (inputSource && fft) { - auto buffer = inputSource->getSamples(sample, fftSize); + if (inputSource && fft) { + auto buffer = inputSource->getSamples(sample, fftSize); - for (int i = 0; i < fftSize; i++) { - buffer[i].real(buffer[i].real() * window[i]); - buffer[i].imag(buffer[i].imag() * window[i]); - } + for (int i = 0; i < fftSize; i++) { + buffer[i].real(buffer[i].real() * window[i]); + buffer[i].imag(buffer[i].imag() * window[i]); + } - fft->process(buffer.get(), buffer.get()); - for (int i = 0; i < fftSize; i++) { - int k = (i + fftSize / 2) % fftSize; - 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; - dest++; - } - } + fft->process(buffer.get(), buffer.get()); + for (int i = 0; i < fftSize; i++) { + int k = (i + fftSize / 2) % fftSize; + 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; + dest++; + } + } } void Spectrogram::paintTimeAxis(QPainter *painter, QRect rect) { - // Round up for firstLine and round each to nearest linesPerGraduation - int firstLine = ((rect.y() + linesPerGraduation - 1) / linesPerGraduation) * linesPerGraduation; - int lastLine = ((rect.y() + rect.height()) / linesPerGraduation) * linesPerGraduation; + // Round up for firstLine and round each to nearest linesPerGraduation + int firstLine = ((rect.y() + linesPerGraduation - 1) / linesPerGraduation) * linesPerGraduation; + int lastLine = ((rect.y() + rect.height()) / linesPerGraduation) * linesPerGraduation; - painter->save(); - QPen pen(Qt::white, 1, Qt::SolidLine); - painter->setPen(pen); - QFontMetrics fm(painter->font()); - int textOffset = fm.ascent() / 2 - 1; - for (int line = firstLine; line <= lastLine; line += linesPerGraduation) { - painter->drawLine(0, line, 10, line); - painter->drawText(12, line + textOffset, sampleToTime(lineToSample(line))); - } - painter->restore(); + painter->save(); + QPen pen(Qt::white, 1, Qt::SolidLine); + painter->setPen(pen); + QFontMetrics fm(painter->font()); + int textOffset = fm.ascent() / 2 - 1; + for (int line = firstLine; line <= lastLine; line += linesPerGraduation) { + painter->drawLine(0, line, 10, line); + painter->drawText(12, line + textOffset, sampleToTime(lineToSample(line))); + } + painter->restore(); } void Spectrogram::setSampleRate(int rate) { - sampleRate = rate; - update(); + sampleRate = rate; + update(); } void Spectrogram::setFFTSize(int size) { - fftSize = size; - delete fft; - fft = new FFT(fftSize); + fftSize = size; + delete fft; + fft = new FFT(fftSize); - window.reset(new float[fftSize]); - for (int i = 0; i < fftSize; i++) { - window[i] = 0.5f * (1.0f - cos(Tau * i / (fftSize - 1))); - } + window.reset(new float[fftSize]); + for (int i = 0; i < fftSize; i++) { + window[i] = 0.5f * (1.0f - cos(Tau * i / (fftSize - 1))); + } - resize(fftSize, getHeight()); + resize(fftSize, getHeight()); } void Spectrogram::setPowerMax(int power) { - powerMax = power; - pixmapCache.clear(); - update(); + powerMax = power; + pixmapCache.clear(); + update(); } void Spectrogram::setPowerMin(int power) { - powerMin = power; - pixmapCache.clear(); - update(); + powerMin = power; + pixmapCache.clear(); + update(); } void Spectrogram::setZoomLevel(int zoom) { - zoomLevel = clamp(zoom, 0, (int)log2(fftSize)); - resize(fftSize, getHeight()); + zoomLevel = clamp(zoom, 0, (int)log2(fftSize)); + resize(fftSize, getHeight()); } int Spectrogram::getHeight() { - if (!inputSource) - return 0; + if (!inputSource) + return 0; - return inputSource->count() / getStride(); + return inputSource->count() / getStride(); } int Spectrogram::getStride() { - return fftSize / pow(2, zoomLevel); + return fftSize / pow(2, zoomLevel); } -off_t Spectrogram::lineToSample(off_t line) { - return line * getStride(); +off_t Spectrogram::lineToSample(off_t line) +{ + return line * getStride(); } -int Spectrogram::sampleToLine(off_t sample) { - return sample / getStride(); +int Spectrogram::sampleToLine(off_t sample) +{ + return sample / getStride(); } QString Spectrogram::sampleToTime(off_t sample) { - return QString::number((float)sample / sampleRate).append("s"); + return QString::number((float)sample / sampleRate).append("s"); } -int Spectrogram::linesPerTile() { - return tileSize / fftSize; +int Spectrogram::linesPerTile() +{ + return tileSize / fftSize; } -uint qHash(const TileCacheKey &key, uint seed) { - return key.fftSize ^ key.zoomLevel ^ key.sample ^ seed; +uint qHash(const TileCacheKey &key, uint seed) +{ + return key.fftSize ^ key.zoomLevel ^ key.sample ^ seed; } diff --git a/spectrogram.h b/spectrogram.h index 44f278b..301413e 100644 --- a/spectrogram.h +++ b/spectrogram.h @@ -31,72 +31,74 @@ static const double Tau = M_PI * 2.0; class TileCacheKey; -class Spectrogram : public QWidget { - Q_OBJECT +class Spectrogram : public QWidget +{ + Q_OBJECT public: - Spectrogram(); - ~Spectrogram(); - QSize sizeHint() const; - int getHeight(); - int getStride(); + Spectrogram(); + ~Spectrogram(); + QSize sizeHint() const; + int getHeight(); + int getStride(); - InputSource *inputSource = nullptr; + InputSource *inputSource = nullptr; public slots: - void openFile(QString fileName); - void setSampleRate(int rate); - void setFFTSize(int size); - void setPowerMax(int power); - void setPowerMin(int power); - void setZoomLevel(int zoom); + void openFile(QString fileName); + void setSampleRate(int rate); + void setFFTSize(int size); + void setPowerMax(int power); + void setPowerMin(int power); + void setZoomLevel(int zoom); protected: - void paintEvent(QPaintEvent *event); + void paintEvent(QPaintEvent *event); private: - const int linesPerGraduation = 50; - const int tileSize = 65536; // This must be a multiple of the maximum FFT size + const int linesPerGraduation = 50; + const int tileSize = 65536; // This must be a multiple of the maximum FFT size - FFT *fft = nullptr; - std::unique_ptr window; - fftwf_complex *lineBuffer = nullptr; - QCache pixmapCache; - QCache fftCache; - uint colormap[256]; + FFT *fft = nullptr; + std::unique_ptr window; + fftwf_complex *lineBuffer = nullptr; + QCache pixmapCache; + QCache fftCache; + uint colormap[256]; - int sampleRate; - int fftSize; - int zoomLevel; - float powerMax; - float powerMin; + int sampleRate; + int fftSize; + int zoomLevel; + float powerMax; + float powerMin; - QPixmap* getPixmapTile(off_t tile); - float* getFFTTile(off_t tile); - void getLine(float *dest, off_t sample); - void paintTimeAxis(QPainter *painter, QRect rect); - off_t lineToSample(off_t line); - int sampleToLine(off_t sample); - QString sampleToTime(off_t sample); - int linesPerTile(); + QPixmap* getPixmapTile(off_t tile); + float* getFFTTile(off_t tile); + void getLine(float *dest, off_t sample); + void paintTimeAxis(QPainter *painter, QRect rect); + off_t lineToSample(off_t line); + int sampleToLine(off_t sample); + QString sampleToTime(off_t sample); + int linesPerTile(); }; -class TileCacheKey { +class TileCacheKey +{ public: - TileCacheKey(int fftSize, int zoomLevel, off_t sample) { - this->fftSize = fftSize; - this->zoomLevel = zoomLevel; - this->sample = sample; - } + TileCacheKey(int fftSize, int zoomLevel, off_t sample) { + this->fftSize = fftSize; + this->zoomLevel = zoomLevel; + this->sample = sample; + } - bool operator==(const TileCacheKey &k2) const { - return (this->fftSize == k2.fftSize) && - (this->zoomLevel == k2.zoomLevel) && - (this->sample == k2.sample); - } + bool operator==(const TileCacheKey &k2) const { + return (this->fftSize == k2.fftSize) && + (this->zoomLevel == k2.zoomLevel) && + (this->sample == k2.sample); + } - int fftSize; - int zoomLevel; - off_t sample; + int fftSize; + int zoomLevel; + off_t sample; }; diff --git a/spectrogramcontrols.cpp b/spectrogramcontrols.cpp index 132d66d..bfd75fc 100644 --- a/spectrogramcontrols.cpp +++ b/spectrogramcontrols.cpp @@ -24,54 +24,54 @@ #include SpectrogramControls::SpectrogramControls(const QString & title, QWidget * parent) - : QDockWidget::QDockWidget(title, parent) + : QDockWidget::QDockWidget(title, parent) { - widget = new QWidget(this); - layout = new QFormLayout(widget); + widget = new QWidget(this); + layout = new QFormLayout(widget); - fileOpenButton = new QPushButton("Open file...", widget); - layout->addRow(fileOpenButton); + fileOpenButton = new QPushButton("Open file...", widget); + layout->addRow(fileOpenButton); - sampleRate = new QLineEdit("8000000"); - sampleRate->setValidator(new QIntValidator(this)); - layout->addRow(new QLabel(tr("Sample rate:")), sampleRate); + sampleRate = new QLineEdit("8000000"); + sampleRate->setValidator(new QIntValidator(this)); + layout->addRow(new QLabel(tr("Sample rate:")), sampleRate); - fftSizeSlider = new QSlider(Qt::Horizontal, widget); - fftSizeSlider->setRange(7, 13); - fftSizeSlider->setValue(10); - layout->addRow(new QLabel(tr("FFT size:")), fftSizeSlider); + fftSizeSlider = new QSlider(Qt::Horizontal, widget); + fftSizeSlider->setRange(7, 13); + fftSizeSlider->setValue(10); + layout->addRow(new QLabel(tr("FFT size:")), fftSizeSlider); - zoomLevelSlider = new QSlider(Qt::Horizontal, widget); - zoomLevelSlider->setRange(0, 10); - zoomLevelSlider->setValue(0); - layout->addRow(new QLabel(tr("Zoom:")), zoomLevelSlider); + zoomLevelSlider = new QSlider(Qt::Horizontal, widget); + zoomLevelSlider->setRange(0, 10); + zoomLevelSlider->setValue(0); + layout->addRow(new QLabel(tr("Zoom:")), zoomLevelSlider); - powerMaxSlider = new QSlider(Qt::Horizontal, widget); - powerMaxSlider->setRange(-100, 20); - powerMaxSlider->setValue(0); - layout->addRow(new QLabel(tr("Power max:")), powerMaxSlider); + powerMaxSlider = new QSlider(Qt::Horizontal, widget); + powerMaxSlider->setRange(-100, 20); + powerMaxSlider->setValue(0); + layout->addRow(new QLabel(tr("Power max:")), powerMaxSlider); - powerMinSlider = new QSlider(Qt::Horizontal, widget); - powerMinSlider->setRange(-100, 20); - powerMinSlider->setValue(-50); - layout->addRow(new QLabel(tr("Power min:")), powerMinSlider); + powerMinSlider = new QSlider(Qt::Horizontal, widget); + powerMinSlider->setRange(-100, 20); + powerMinSlider->setValue(-50); + layout->addRow(new QLabel(tr("Power min:")), powerMinSlider); - widget->setLayout(layout); - setWidget(widget); + widget->setLayout(layout); + setWidget(widget); - connect(fftSizeSlider, SIGNAL(valueChanged(int)), this, SLOT(fftSizeSliderChanged(int))); - connect(fileOpenButton, SIGNAL(clicked()), this, SLOT(fileOpenButtonClicked())); + connect(fftSizeSlider, SIGNAL(valueChanged(int)), this, SLOT(fftSizeSliderChanged(int))); + connect(fileOpenButton, SIGNAL(clicked()), this, SLOT(fileOpenButtonClicked())); } void SpectrogramControls::fftSizeSliderChanged(int size) { - emit fftSizeChanged((int)pow(2, size)); + emit fftSizeChanged((int)pow(2, size)); } void SpectrogramControls::fileOpenButtonClicked() { - QString fileName = QFileDialog::getOpenFileName( - this, tr("Open File"), "", tr("Sample file (*.cfile *.bin);;All files (*)") - ); - emit openFile(fileName); + QString fileName = QFileDialog::getOpenFileName( + this, tr("Open File"), "", tr("Sample file (*.cfile *.bin);;All files (*)") + ); + emit openFile(fileName); } \ No newline at end of file diff --git a/spectrogramcontrols.h b/spectrogramcontrols.h index 1e03f4c..1979378 100644 --- a/spectrogramcontrols.h +++ b/spectrogramcontrols.h @@ -25,28 +25,29 @@ #include #include -class SpectrogramControls : public QDockWidget { - Q_OBJECT +class SpectrogramControls : public QDockWidget +{ + Q_OBJECT public: - SpectrogramControls(const QString & title, QWidget * parent); + SpectrogramControls(const QString & title, QWidget * parent); signals: - void fftSizeChanged(int size); - void openFile(QString fileName); + void fftSizeChanged(int size); + void openFile(QString fileName); private slots: - void fftSizeSliderChanged(int size); - void fileOpenButtonClicked(); + void fftSizeSliderChanged(int size); + void fileOpenButtonClicked(); private: - QWidget *widget; - QFormLayout *layout; + QWidget *widget; + QFormLayout *layout; public: - QPushButton *fileOpenButton; - QLineEdit *sampleRate; - QSlider *fftSizeSlider; - QSlider *zoomLevelSlider; - QSlider *powerMaxSlider; - QSlider *powerMinSlider; + QPushButton *fileOpenButton; + QLineEdit *sampleRate; + QSlider *fftSizeSlider; + QSlider *zoomLevelSlider; + QSlider *powerMaxSlider; + QSlider *powerMinSlider; }; \ No newline at end of file diff --git a/waveformview.h b/waveformview.h index ab80768..0bc84fc 100644 --- a/waveformview.h +++ b/waveformview.h @@ -23,24 +23,25 @@ #include #include "inputsource.h" -class WaveformView : public QDockWidget { - Q_OBJECT +class WaveformView : public QDockWidget +{ + Q_OBJECT public: - WaveformView(); + WaveformView(); public slots: - void inputSourceChanged(AbstractSampleSource *input); - void viewChanged(off_t firstSample, off_t lastSample); + void inputSourceChanged(AbstractSampleSource *input); + void viewChanged(off_t firstSample, off_t lastSample); protected: - void paintEvent(QPaintEvent *event); + void paintEvent(QPaintEvent *event); private: - std::vector> sampleSources; - off_t firstSample = 0; - off_t lastSample = 0; - QRgb colormap[255]; + std::vector> sampleSources; + off_t firstSample = 0; + off_t lastSample = 0; + QRgb colormap[255]; - void plot(QPainter *painter, QRect &rect, float *samples, off_t count, int step); + void plot(QPainter *painter, QRect &rect, float *samples, off_t count, int step); }; \ No newline at end of file