comparison layer/SpectrumLayer.cpp @ 133:9e6b3e239b9d

* Add zoom thumbwheels to Pane. Implement horizontal thumbwheel, and vertical depending on layer type (supported for waveform and spectrogram, though wrong for log-scale spectrogram at the moment). * Add bare bones of a spectrum layer. * Add window icon * Add shortcut for "insert time instant" on laptops without keypad enter (";") * Delete FFT processing thread when it exits (at least, next time we're asked for something interesting) * Get audio file extensions from the file readers, and thus from libsndfile for the wave file reader -- leads to rather a wide combo box in file dialog though * Better refresh order for spectrogram (redraw centre section first)
author Chris Cannam
date Fri, 04 Aug 2006 17:01:37 +0000
parents
children aaa3a53dbb10
comparison
equal deleted inserted replaced
132:5d3a483856ff 133:9e6b3e239b9d
1
2 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
3
4 /*
5 Sonic Visualiser
6 An audio file viewer and annotation editor.
7 Centre for Digital Music, Queen Mary, University of London.
8 This file copyright 2006 Chris Cannam.
9
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version. See the file
14 COPYING included with this distribution for more information.
15 */
16
17 #include "SpectrumLayer.h"
18
19 #include "data/model/FFTModel.h"
20 #include "view/View.h"
21
22 #include <QPainter>
23 #include <QPainterPath>
24
25 SpectrumLayer::SpectrumLayer() :
26 m_model(0),
27 m_fft(0),
28 m_colour(Qt::blue)
29 {
30 }
31
32 SpectrumLayer::~SpectrumLayer()
33 {
34 delete m_fft;
35 }
36
37 void
38 SpectrumLayer::setModel(DenseTimeValueModel *model)
39 {
40 m_model = model;
41 delete m_fft;
42 m_fft = new FFTModel(m_model,
43 -1,
44 HanningWindow,
45 1024,
46 256,
47 1024,
48 true);
49 m_fft->resume();
50 }
51
52 void
53 SpectrumLayer::paint(View *v, QPainter &paint, QRect rect) const
54 {
55 if (!m_fft) return;
56
57 int fftSize = 1024; //!!! ...
58 int windowIncrement = 256;
59 int windowSize = 1024;
60
61 size_t f = v->getCentreFrame();
62
63 int w = (v->width() * 2) / 3;
64 int xorigin = (v->width() / 2) - (w / 2);
65
66 int h = (v->height() * 2) / 3;
67 int yorigin = (v->height() / 2) + (h / 2);
68
69 size_t column = f / windowIncrement;
70
71 paint.save();
72 paint.setPen(m_colour);
73 paint.setRenderHint(QPainter::Antialiasing, false);
74
75 QPainterPath path;
76 float thresh = -80.f;
77
78 for (size_t bin = 0; bin < m_fft->getHeight(); ++bin) {
79
80 float mag = m_fft->getMagnitudeAt(column, bin);
81 float db = thresh;
82 if (mag > 0.f) db = 10.f * log10f(mag);
83 if (db < thresh) db = thresh;
84 float val = (db - thresh) / -thresh;
85 float x = xorigin + (float(w) * bin) / m_fft->getHeight();
86 float y = yorigin - (float(h) * val);
87
88 if (bin == 0) {
89 path.moveTo(x, y);
90 } else {
91 path.lineTo(x, y);
92 }
93 }
94
95 paint.drawPath(path);
96 // paint.setRenderHint(QPainter::Antialiasing, false);
97 paint.restore();
98
99 }
100
101 void
102 SpectrumLayer::setProperties(const QXmlAttributes &attr)
103 {
104 }
105
106 bool
107 SpectrumLayer::getValueExtents(float &min, float &max, bool &logarithmic,
108 QString &units) const
109 {
110 return false;
111 }
112