annotate layer/SpectrumLayer.cpp @ 134:13949a6970ab

* Use peaks instead of means for calculating display values in pixels that cover more than one bin
author Chris Cannam
date Wed, 16 Aug 2006 14:57:47 +0000
parents 9e6b3e239b9d
children aaa3a53dbb10
rev   line source
Chris@133 1
Chris@133 2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@133 3
Chris@133 4 /*
Chris@133 5 Sonic Visualiser
Chris@133 6 An audio file viewer and annotation editor.
Chris@133 7 Centre for Digital Music, Queen Mary, University of London.
Chris@133 8 This file copyright 2006 Chris Cannam.
Chris@133 9
Chris@133 10 This program is free software; you can redistribute it and/or
Chris@133 11 modify it under the terms of the GNU General Public License as
Chris@133 12 published by the Free Software Foundation; either version 2 of the
Chris@133 13 License, or (at your option) any later version. See the file
Chris@133 14 COPYING included with this distribution for more information.
Chris@133 15 */
Chris@133 16
Chris@133 17 #include "SpectrumLayer.h"
Chris@133 18
Chris@133 19 #include "data/model/FFTModel.h"
Chris@133 20 #include "view/View.h"
Chris@133 21
Chris@133 22 #include <QPainter>
Chris@133 23 #include <QPainterPath>
Chris@133 24
Chris@133 25 SpectrumLayer::SpectrumLayer() :
Chris@133 26 m_model(0),
Chris@133 27 m_fft(0),
Chris@133 28 m_colour(Qt::blue)
Chris@133 29 {
Chris@133 30 }
Chris@133 31
Chris@133 32 SpectrumLayer::~SpectrumLayer()
Chris@133 33 {
Chris@133 34 delete m_fft;
Chris@133 35 }
Chris@133 36
Chris@133 37 void
Chris@133 38 SpectrumLayer::setModel(DenseTimeValueModel *model)
Chris@133 39 {
Chris@133 40 m_model = model;
Chris@133 41 delete m_fft;
Chris@133 42 m_fft = new FFTModel(m_model,
Chris@133 43 -1,
Chris@133 44 HanningWindow,
Chris@133 45 1024,
Chris@133 46 256,
Chris@133 47 1024,
Chris@133 48 true);
Chris@133 49 m_fft->resume();
Chris@133 50 }
Chris@133 51
Chris@133 52 void
Chris@133 53 SpectrumLayer::paint(View *v, QPainter &paint, QRect rect) const
Chris@133 54 {
Chris@133 55 if (!m_fft) return;
Chris@133 56
Chris@133 57 int fftSize = 1024; //!!! ...
Chris@133 58 int windowIncrement = 256;
Chris@133 59 int windowSize = 1024;
Chris@133 60
Chris@133 61 size_t f = v->getCentreFrame();
Chris@133 62
Chris@133 63 int w = (v->width() * 2) / 3;
Chris@133 64 int xorigin = (v->width() / 2) - (w / 2);
Chris@133 65
Chris@133 66 int h = (v->height() * 2) / 3;
Chris@133 67 int yorigin = (v->height() / 2) + (h / 2);
Chris@133 68
Chris@133 69 size_t column = f / windowIncrement;
Chris@133 70
Chris@133 71 paint.save();
Chris@133 72 paint.setPen(m_colour);
Chris@133 73 paint.setRenderHint(QPainter::Antialiasing, false);
Chris@133 74
Chris@133 75 QPainterPath path;
Chris@133 76 float thresh = -80.f;
Chris@133 77
Chris@133 78 for (size_t bin = 0; bin < m_fft->getHeight(); ++bin) {
Chris@133 79
Chris@133 80 float mag = m_fft->getMagnitudeAt(column, bin);
Chris@133 81 float db = thresh;
Chris@133 82 if (mag > 0.f) db = 10.f * log10f(mag);
Chris@133 83 if (db < thresh) db = thresh;
Chris@133 84 float val = (db - thresh) / -thresh;
Chris@133 85 float x = xorigin + (float(w) * bin) / m_fft->getHeight();
Chris@133 86 float y = yorigin - (float(h) * val);
Chris@133 87
Chris@133 88 if (bin == 0) {
Chris@133 89 path.moveTo(x, y);
Chris@133 90 } else {
Chris@133 91 path.lineTo(x, y);
Chris@133 92 }
Chris@133 93 }
Chris@133 94
Chris@133 95 paint.drawPath(path);
Chris@133 96 // paint.setRenderHint(QPainter::Antialiasing, false);
Chris@133 97 paint.restore();
Chris@133 98
Chris@133 99 }
Chris@133 100
Chris@133 101 void
Chris@133 102 SpectrumLayer::setProperties(const QXmlAttributes &attr)
Chris@133 103 {
Chris@133 104 }
Chris@133 105
Chris@133 106 bool
Chris@133 107 SpectrumLayer::getValueExtents(float &min, float &max, bool &logarithmic,
Chris@133 108 QString &units) const
Chris@133 109 {
Chris@133 110 return false;
Chris@133 111 }
Chris@133 112