Debian Codegraph @

src/modules/GLGraph.cpp

raw
1 // Copyright 2004 "Gilles Degottex"
2
3 // This file is part of "Music"
4
5 // "Music" is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
9 //
10 // "Music" is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19
20 #include "GLGraph.h"
21
22 #include <iostream>
23 using namespace std;
24 #include <qtooltip.h>
25 #include <qimage.h>
26 #include <qboxlayout.h>
27 #include <qwidgetaction.h>
28 #include <QPainter>
29 #include <Music/Music.h>
30 using namespace Music;
31
32 GLGraph::GLGraph(QWidget* parent)
33 : QOpenGLWidget(parent)
34 , View(tr("Captured Sound"), this)
35 , m_font("Helvetica")
36 , m_skip(1)
37 , m_new_values(false)
38 {
39 // settings
40 setting_show->setIcon(QIcon(":/fmit/ui/images/module_capturedsound.svg"));
41 setting_show->setChecked(true);
42
43 setting_showWaveForm = new QAction(tr("Show Wave Form"), this);
44 setting_showWaveForm->setCheckable(true);
45 connect(setting_showWaveForm, SIGNAL(toggled(bool)), this, SLOT(update()));
46 setting_showWaveForm->setChecked(true);
47 m_popup_menu.addAction(setting_showWaveForm);
48
49 setting_autoScale = new QAction(tr("Auto scale"), this);
50 setting_autoScale->setCheckable(true);
51 connect(setting_autoScale, SIGNAL(toggled(bool)), this, SLOT(update()));
52 setting_autoScale->setChecked(false);
53 m_popup_menu.addAction(setting_autoScale);
54
55 QHBoxLayout* scaleFactorActionLayout = new QHBoxLayout(&m_popup_menu);
56
57 QLabel* scaleFactorActionTitle = new QLabel(tr("Scale factor"), &m_popup_menu);
58 scaleFactorActionLayout->addWidget(scaleFactorActionTitle);
59
60 setting_spinScaleFactor = new QSpinBox(&m_popup_menu);
61 setting_spinScaleFactor->setMinimum(1);
62 setting_spinScaleFactor->setMaximum(1000);
63 setting_spinScaleFactor->setSingleStep(10);
64 setting_spinScaleFactor->setToolTip(tr("Scale factor"));
65 setting_spinScaleFactor->setValue(1000);
66 connect(setting_spinScaleFactor, SIGNAL(valueChanged(int)), this, SLOT(update()));
67 scaleFactorActionLayout->addWidget(setting_spinScaleFactor);
68
69 QWidget* scaleFactorActionWidget = new QWidget(&m_popup_menu);
70 scaleFactorActionWidget->setLayout(scaleFactorActionLayout);
71
72 QWidgetAction* scaleFactorAction = new QWidgetAction(&m_popup_menu);
73 scaleFactorAction->setDefaultWidget(scaleFactorActionWidget);
74 m_popup_menu.addAction(scaleFactorAction);
75
76 QHBoxLayout* durationActionLayout = new QHBoxLayout(&m_popup_menu);
77
78 QLabel* durationActionTitle = new QLabel(tr("Duration"), &m_popup_menu);
79 durationActionLayout->addWidget(durationActionTitle);
80
81 setting_spinDuration = new QSpinBox(&m_popup_menu);
82 setting_spinDuration->setMinimum(20);
83 setting_spinDuration->setMaximum(120000);
84 setting_spinDuration->setSingleStep(100);
85 setting_spinDuration->setToolTip(tr("Duration"));
86 setting_spinDuration->setValue(10000);
87 connect(setting_spinDuration, SIGNAL(valueChanged(int)), this, SLOT(update()));
88 durationActionLayout->addWidget(setting_spinDuration);
89
90 QWidget* durationActionWidget = new QWidget(&m_popup_menu);
91 durationActionWidget->setLayout(durationActionLayout);
92
93 QWidgetAction* durationAction = new QWidgetAction(&m_popup_menu);
94 durationAction->setDefaultWidget(durationActionWidget);
95 m_popup_menu.addAction(durationAction);
96
97 QHBoxLayout* maxHeightActionLayout = new QHBoxLayout(&m_popup_menu);
98
99 QLabel* maxHeightActionTitle = new QLabel(tr("Max height"), &m_popup_menu);
100 maxHeightActionLayout->addWidget(maxHeightActionTitle);
101
102 setting_spinMaxHeight = new QSpinBox(&m_popup_menu);
103 setting_spinMaxHeight->setMinimum(10);
104 setting_spinMaxHeight->setMaximum(100000);
105 setting_spinMaxHeight->setSingleStep(1);
106 setting_spinMaxHeight->setToolTip(tr("Max height"));
107 setting_spinMaxHeight->setValue(50);
108 maxHeightActionLayout->addWidget(setting_spinMaxHeight);
109
110 QWidget* maxHeightActionWidget = new QWidget(&m_popup_menu);
111 maxHeightActionWidget->setLayout(maxHeightActionLayout);
112
113 QWidgetAction* maxHeightAction = new QWidgetAction(&m_popup_menu);
114 maxHeightAction->setDefaultWidget(maxHeightActionWidget);
115 m_popup_menu.addAction(maxHeightAction);
116 }
117
118 void GLGraph::save()
119 {
120 s_settings->setValue("showWaveForm", setting_showWaveForm->isChecked());
121 s_settings->setValue("autoScale", setting_autoScale->isChecked());
122 s_settings->setValue("setting_spinScaleFactor", setting_spinScaleFactor->value());
123 s_settings->setValue("spinDuration", setting_spinDuration->value());
124 s_settings->setValue("spinMaxHeight", setting_spinMaxHeight->value());
125 }
126 void GLGraph::load()
127 {
128 setting_showWaveForm->setChecked(s_settings->value("showWaveForm", setting_showWaveForm->isChecked()).toBool());
129 setting_autoScale->setChecked(s_settings->value("autoScale", setting_autoScale->isChecked()).toBool());
130 setting_spinScaleFactor->setValue(s_settings->value("setting_spinScaleFactor", setting_spinScaleFactor->value()).toInt());
131 setting_spinDuration->setValue(s_settings->value("spinDuration", setting_spinDuration->value()).toInt());
132 setting_spinMaxHeight->setValue(s_settings->value("spinMaxHeight", setting_spinMaxHeight->value()).toInt());
133 }
134 void GLGraph::clearSettings()
135 {
136 // cout << "GLGraph::clearSettings" << endl;
137 s_settings->remove("showWaveForm");
138 s_settings->remove("autoScale");
139 s_settings->remove("setting_spinScaleFactor");
140 s_settings->remove("spinDuration");
141 s_settings->remove("spinMaxHeight");
142 }
143
144 void GLGraph::initializeGL()
145 {
146 // Set the clear color to white
147 glClearColor(1.0, 1.0, 1.0, 0.0);
148
149 // glShadeModel( GL_FLAT );
150 glShadeModel(GL_SMOOTH);
151
152 glEnable(GL_BLEND);
153 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
154
155 glLoadIdentity();
156 }
157
158 void GLGraph::resizeGL( int w, int h )
159 {
160 // Set the new viewport size
161 glViewport(0, 0, (GLint)w, (GLint)h);
162
163 // Choose the projection matrix to be the matrix
164 // manipulated by the following calls
165 glMatrixMode(GL_PROJECTION);
166
167 // Set the projection matrix to be the identity matrix
168 glLoadIdentity();
169
170 // Define the dimensions of the Orthographic Viewing Volume
171 glOrtho(0.0, w, 0.0, h, 0.0, 1.0);
172
173 // Choose the modelview matrix to be the matrix
174 // manipulated by further calls
175 glMatrixMode(GL_MODELVIEW);
176
177 update_maxs();
178 }
179
180 void GLGraph::addValue(double v)
181 {
182 m_pending_queue.push_front(v);
183 m_queue.push_front(v);
184
185 m_new_values = true;
186 }
187 void GLGraph::clearValues()
188 {
189 m_queue.clear();
190 m_maxs.clear();
191 }
192 void GLGraph::refreshGraph()
193 {
194 int max_size = getLength();
195 m_skip = max_size/width();
196 if(m_skip<1) m_skip = 1;
197
198 while(!m_pending_queue.empty() && int(m_pending_queue.size()) >= m_skip)
199 {
200 double smin = m_pending_queue.back();
201 double smax = m_pending_queue.back();
202 for(int i=0; i<m_skip; i++)
203 {
204 m_queue.push_front(m_pending_queue.back());
205 smin = min(smin, m_pending_queue.back());
206 smax = max(smax, m_pending_queue.back());
207 m_pending_queue.pop_back();
208 }
209 m_maxs.push_front(make_pair(smin, smax));
210 }
211
212 // drop unused data
213 while(!m_maxs.empty() && int(m_maxs.size())>=width())
214 m_maxs.pop_back();
215
216 // drop unused data
217 while(!m_queue.empty() && int(m_queue.size())>max_size)
218 m_queue.pop_back();
219
220 m_new_values = false;
221 }
222
223 void GLGraph::update_maxs()
224 {
225 // cout << "GLGraph::update_maxs " << m_sampling_rate << endl;
226
227 if(m_queue.empty()) return;
228
229 m_maxs.clear();
230 int max_size = getLength();
231 m_skip = max_size/width();
232 if(m_skip<1) m_skip = 1;
233
234 // drop unused data
235 while(!m_queue.empty() && int(m_queue.size())>max_size)
236 m_queue.pop_back();
237
238 // computes maxs
239 for(size_t j=0; j+m_skip<=m_queue.size(); j+=m_skip)
240 {
241 double smin, smax;
242 smin = smax = m_queue[j+m_skip];
243 for(size_t i=j; i<j+m_skip && i<m_queue.size(); i++)
244 {
245 smin = min(smin, m_queue[i]);
246 smax = max(smax, m_queue[i]);
247 }
248 m_maxs.push_back(make_pair(smin, smax));
249 }
250
251 update();
252 }
253
254 void GLGraph::base_paint(float graph_gray)
255 {
256 // cout << "GLGraph::base_paint " << m_queue.size() << ":" << m_maxs.size() << endl;
257
258 // cout<<"m_pending_queue="<<m_pending_queue.size()<<" m_queue="<<m_queue.size()<<" m_maxs="<<m_maxs.size()<<endl;
259
260 int width = QOpenGLWidget::width();
261
262 // name
263 glColor3f(0.75,0.75,0.75);
264 QPainter painter(this);
265 m_font.setPixelSize(20);
266 painter.setFont(m_font);
267 painter.drawText(2, 20, tr("Captured Sound"));
268 painter.end();
269
270 if(setting_showWaveForm->isChecked() && !m_queue.empty())
271 {
272 // horiz line
273 glBegin(GL_LINES);
274 float gray = 0.8;
275 glColor3f(gray, gray, gray);
276 glVertex2i(0, height()/2);
277 glVertex2i(width, height()/2);
278 glEnd();
279
280 m_queue_amplitude = 0.0;
281 double scale_factor;
282 if(setting_autoScale->isChecked())
283 {
284 if(m_skip<2)
285 for(size_t i=0; i<m_queue.size(); i++)
286 m_queue_amplitude = max(m_queue_amplitude, abs(m_queue[i]));
287 else
288 for(size_t i=0; i<m_maxs.size(); i++)
289 m_queue_amplitude = max(m_queue_amplitude, max(-m_maxs[i].first,m_maxs[i].second));
290 scale_factor = 1.0/m_queue_amplitude;
291 }
292 else
293 scale_factor = 1000.0/setting_spinScaleFactor->value();
294
295 // paint the noise treshold
296 glBegin(GL_LINES);
297 int y = int((1+m_treshold*scale_factor)*height()/2);
298 glColor3f(1.0,1.0,1.0);
299 glVertex2i(0, y);
300 glColor3f(1.0,0.5,0.5);
301 glVertex2i(width, y);
302 y = int((1-m_treshold*scale_factor)*height()/2);
303 glColor3f(1.0,1.0,1.0);
304 glVertex2i(0, y);
305 glColor3f(1.0,0.5,0.5);
306 glVertex2i(width, y);
307 glEnd();
308
309 // paint the wave form
310 glColor3f(graph_gray, graph_gray, graph_gray);
311 if(m_skip<2)
312 {
313 glBegin(GL_LINE_STRIP);
314
315 glVertex2i(width-1, int((m_queue[0]*scale_factor)*height()/2 + height()/2));
316 float istep = float(width)/m_queue.size();
317 for(size_t i=1; i<m_queue.size(); i++)
318 glVertex2i(width-1-int(i*istep), int((m_queue[i]*scale_factor)*height()/2 + height()/2));
319
320 glEnd();
321 }
322 else
323 {
324 glBegin(GL_LINES);
325 for(int i=0; i<int(m_maxs.size()); i++)
326 {
327 int x = width-1-i;
328 int ymin = int((m_maxs[i].first*scale_factor)*height()/2 + height()/2);
329 int ymax = int((m_maxs[i].second*scale_factor)*height()/2 + height()/2);
330 glVertex2i(x, ymin);
331 glVertex2i(x, ymax);
332 }
333 glEnd();
334 }
335 }
336 }
337
338 void GLGraph::paintGL()
339 {
340 if(m_new_values)
341 refreshGraph();
342
343 glClear(GL_COLOR_BUFFER_BIT);
344
345 base_paint();
346
347 glFlush();
348 }
349