Debian Codegraph @

src/qutescope.cpp

raw symbols SCIP
1 /*
2 Copyright (C) 2008, 2009 Andres Cabrera
3 [email protected]
4
5 This file is part of CsoundQt.
6
7 CsoundQt is free software; you can redistribute it
8 and/or modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 CsoundQt is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with Csound; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA
21 */
22
23 #include "qutescope.h"
24 #include <cmath>
25 #include "types.h" //necessary for the userdata struct
26 #include "qutecsound.h" //necessary for the userdata struct
27
28
29 QuteScope::QuteScope(QWidget *parent) : QuteWidget(parent)
30 {
31 QGraphicsScene *m_scene = new QGraphicsScene(this);
32 // m_scene->setBackgroundBrush(QBrush(Qt::black));
33 m_scene->setBackgroundBrush(QBrush(QColor("#161616")));
34 m_widget = new ScopeWidget(this);
35 m_widget->show();
36 m_widget->setAutoFillBackground(true);
37 m_widget->setContextMenuPolicy(Qt::NoContextMenu);
38 // Necessary to pass mouse tracking to widget panel for _MouseX channels
39 m_widget->setMouseTracking(true);
40 // m_widget->setWindowFlags(Qt::WindowStaysOnTopHint);
41 canFocus(false);
42 static_cast<ScopeWidget *>(m_widget)->setScene(m_scene);
43 static_cast<ScopeWidget *>(m_widget)->setResizeAnchor(QGraphicsView::AnchorViewCenter);
44 // static_cast<ScopeWidget *>(m_widget)->setRenderHints(QPainter::Antialiasing);
45 m_label = new QLabel(this);
46 QPalette palette = m_widget->palette();
47 palette.setColor(QPalette::WindowText, QColor(196, 196, 196));
48 m_label->setPalette(palette);
49 auto font = m_label->font();
50 font.setPixelSize(11);
51 m_label->setFont(font);
52 m_label->setText("Scope");
53 m_label->move(10, 0);
54 m_label->resize(500, 25);
55
56 m_params = new ScopeParams(nullptr,
57 m_scene,
58 static_cast<ScopeWidget *>(m_widget),
59 &scopeLock,
60 this->width(),
61 this->height());
62 m_params->triggerMode = TriggerMode::NoTrigger;
63
64 m_scopeData = new ScopeData(m_params);
65 m_lissajouData = new LissajouData(m_params);
66 m_poincareData = new PoincareData(m_params);
67
68 m_dataDisplay = (DataDisplay *)m_scopeData;
69 m_dataDisplay->show();
70
71 // Default properties
72 setProperty("QCS_type", "scope");
73 setProperty("QCS_zoomx", 1.0);
74 setProperty("QCS_zoomy", 1.0);
75 setProperty("QCS_dispx", 1.0);
76 setProperty("QCS_dispy", 1.0);
77 setProperty("QCS_mode", "lin");
78 setProperty("QCS_triggermode", "NoTrigger");
79 }
80
81 QuteScope::~QuteScope()
82 {
83 delete m_poincareData;
84 delete m_lissajouData;
85 delete m_scopeData;
86 delete m_params;
87 }
88
89 TriggerMode triggerNameToMode(QString s) {
90 if(s == "NoTrigger")
91 return TriggerMode::NoTrigger;
92 else if(s =="TriggerUp")
93 return TriggerMode::TriggerUp;
94 else
95 return TriggerMode::NoTrigger;
96 }
97
98 QString triggerModeToName(TriggerMode t) {
99 if(t == TriggerMode::NoTrigger)
100 return "NoTrigger";
101 else if(t == TriggerMode::TriggerUp)
102 return "TriggerUp";
103 }
104
105
106 QString QuteScope::getWidgetLine()
107 {
108 #ifdef USE_WIDGET_MUTEX
109 widgetLock.lockForRead();
110 #endif
111 QString line = "ioGraph {" + QString::number(x()) + ", " + QString::number(y()) + "} ";
112 line += "{"+ QString::number(width()) +", "+ QString::number(height()) +"} ";
113 line += property("QCS_type").toString() + " " + QString::number(property("QCS_zoomx").toDouble(), 'f', 6) + " ";
114 line += QString::number((int) m_value) + " ";
115 line += m_channel;
116 // qDebug("QuteScope::getWidgetLine() %s", line.toStdString().c_str());
117 #ifdef USE_WIDGET_MUTEX
118 widgetLock.unlock();
119 #endif
120 return line;
121 }
122
123 QString QuteScope::getWidgetXmlText()
124 {
125 xmlText = "";
126 QXmlStreamWriter s(&xmlText);
127 createXmlWriter(s);
128 #ifdef USE_WIDGET_MUTEX
129 widgetLock.lockForRead();
130 #endif
131
132 s.writeTextElement("value", QString::number(m_value, 'f', 8));
133 s.writeTextElement("type", property("QCS_type").toString());
134 s.writeTextElement("zoomx", QString::number(property("QCS_zoomx").toDouble(), 'f', 8));
135 s.writeTextElement("zoomy", QString::number(property("QCS_zoomy").toDouble(), 'f', 8));
136 s.writeTextElement("dispx", QString::number(property("QCS_dispx").toDouble(), 'f', 8));
137 s.writeTextElement("dispy", QString::number(property("QCS_dispy").toDouble(), 'f', 8));
138 s.writeTextElement("mode", QString::number(property("QCS_mode").toDouble(), 'f', 8));
139 s.writeTextElement("triggermode", property("QCS_triggermode").toString());
140 s.writeEndElement();
141 #ifdef USE_WIDGET_MUTEX
142 widgetLock.unlock();
143 #endif
144 return xmlText;
145 }
146
147 QString QuteScope::getWidgetType()
148 {
149 return QString("BSBScope");
150 }
151
152 void QuteScope::setType(QString type)
153 {
154 updateLabel();
155 m_dataDisplay->hide();
156 if (type == "scope") {
157 m_dataDisplay = (DataDisplay *)m_scopeData;
158 }
159 else if (type == "lissajou") {
160 m_dataDisplay = (DataDisplay *)m_lissajouData;
161 }
162 else if (type == "poincare") {
163 m_dataDisplay = (DataDisplay *)m_poincareData;
164 }
165 m_dataDisplay->show();
166 }
167
168 void QuteScope::setValue(double value)
169 {
170 QuteWidget::setValue(value);
171 updateLabel();
172 }
173
174 void QuteScope::setUd(CsoundUserData *ud)
175 {
176 m_params->ud = ud;
177 }
178
179 void QuteScope::updateLabel()
180 {
181 QString chan;
182 if ((int) m_value < 0) {
183 chan = tr("all", "meaning 'all' channels in scope, 4 letter max");
184 }
185 else if ((int) m_value <= 0) {
186 chan = tr("None", "meaning 'no' channels in scope, 4 letter max");
187 }
188 else {
189 chan = QString::number((int) m_value );
190 }
191 m_label->setText(tr("Scope ch:") + chan);
192 }
193
194
195 void QuteScope::applyInternalProperties()
196 {
197 QuteWidget::applyInternalProperties();
198 setType(property("QCS_type").toString());
199 setValue(property("QCS_value").toDouble());
200 m_params->triggerMode = triggerNameToMode(property("QCS_triggermode").toString());
201 }
202
203 void QuteScope::createPropertiesDialog()
204 {
205 QuteWidget::createPropertiesDialog();
206 dialog->setWindowTitle("Scope");
207 // channelLabel->hide();
208 // nameLineEdit->hide();
209 QLabel *label = new QLabel(dialog);
210 label->setText("Type");
211 layout->addWidget(label, 6, 0, Qt::AlignRight|Qt::AlignVCenter);
212 typeComboBox = new QComboBox(dialog);
213 typeComboBox->addItem("Oscilloscope", QVariant(QString("scope")));
214 typeComboBox->addItem("Lissajou curve", QVariant(QString("lissajou")));
215 typeComboBox->addItem("Poincare map", QVariant(QString("poincare")));
216 // typeComboBox->addItem("Spectrogram", QVariant(QString("fft")));
217 layout->addWidget(typeComboBox, 6, 1, Qt::AlignLeft|Qt::AlignVCenter);
218 label = new QLabel(dialog);
219 label->setText("Channel");
220 layout->addWidget(label, 6, 2, Qt::AlignRight|Qt::AlignVCenter);
221 channelBox = new QComboBox(dialog);
222 channelBox->addItem("all", QVariant((int) -255));
223 int maxChannels = 16;
224 if(m_params->ud != nullptr && m_params->ud->csEngine->isRunning()) {
225 maxChannels = m_params->ud->numChnls;
226 }
227 for(int i=1; i <= maxChannels; i++) {
228 channelBox->addItem(QString::number(i), QVariant((int) i));
229 }
230 channelBox->addItem("none", QVariant((int) 0));
231 layout->addWidget(channelBox, 6, 3, Qt::AlignLeft|Qt::AlignVCenter);
232 label = new QLabel(dialog);
233 label->setText("Zoom X");
234 layout->addWidget(label, 8, 0, Qt::AlignRight|Qt::AlignVCenter);
235 zoomxBox = new QDoubleSpinBox(dialog);
236 zoomxBox->setRange(1, 20);
237 layout->addWidget(zoomxBox, 8, 1, Qt::AlignLeft|Qt::AlignVCenter);
238 label = new QLabel(dialog);
239 label->setText("Zoom Y");
240 layout->addWidget(label, 8, 2, Qt::AlignRight|Qt::AlignVCenter);
241 zoomyBox = new QDoubleSpinBox(dialog);
242 zoomyBox->setRange(1, 20);
243 layout->addWidget(zoomyBox, 8, 3, Qt::AlignLeft|Qt::AlignVCenter);
244 #ifdef USE_WIDGET_MUTEX
245 widgetLock.lockForRead();
246 #endif
247 typeComboBox->setCurrentIndex(typeComboBox->findData(QVariant(property("QCS_type").toString())));
248 channelBox->setCurrentIndex(channelBox->findData(QVariant((int) m_value)));
249 zoomxBox->setValue(property("QCS_zoomx").toDouble());
250 zoomyBox->setValue(property("QCS_zoomy").toDouble());
251
252 label = new QLabel("Trigger");
253 layout->addWidget(label, 9, 0, Qt::AlignRight|Qt::AlignVCenter);
254 triggerBox = new QComboBox(dialog);
255 triggerBox->addItem("No Trigger", "NoTrigger");
256 triggerBox->addItem("Trigger Up", "TriggerUp");
257 triggerBox->setCurrentIndex(triggerBox->findData(property("QCS_triggermode").toString()));
258 layout->addWidget(triggerBox, 9, 1, Qt::AlignLeft|Qt::AlignVCenter);
259 #ifdef USE_WIDGET_MUTEX
260 widgetLock.unlock();
261 #endif
262 }
263
264 void QuteScope::applyProperties()
265 {
266 #ifdef USE_WIDGET_MUTEX
267 widgetLock.lockForRead();
268 #endif
269 setProperty("QCS_type", typeComboBox->itemData(typeComboBox->currentIndex()).toString());
270 setProperty("QCS_zoomx", zoomxBox->value());
271 setProperty("QCS_zoomy", zoomyBox->value());
272 setProperty("QCS_value", channelBox->itemData(channelBox->currentIndex()).toInt());
273 auto triggerModeStr = triggerBox->currentData().toString();
274 setProperty("QCS_triggermode", triggerModeStr);
275 m_params->triggerMode = triggerNameToMode(triggerModeStr);
276 #ifdef USE_WIDGET_MUTEX
277 widgetLock.unlock();
278 #endif
279 //Must be last to make sure the widgetChanged signal is last
280 QuteWidget::applyProperties();
281 }
282
283 void QuteScope::resizeEvent(QResizeEvent * event)
284 {
285 QuteWidget::resizeEvent(event);
286 m_params->setWidth(this->width());
287 m_params->setHeight(this->height());
288 m_scopeData->resize();
289 m_lissajouData->resize();
290 m_poincareData->resize();
291 // QGraphicsScene *m_scene = static_cast<ScopeWidget *>(m_widget)->scene();
292 // m_scene->setSceneRect(-m_ud->zerodBFS, m_ud->zerodBFS, width() - 5, m_ud->zerodBFS *2);
293 // static_cast<ScopeWidget *>(m_widget)->setSceneRect(-m_ud->zerodBFS , m_ud->zerodBFS, width() - 5, m_ud->zerodBFS *2);
294 }
295
296 void QuteScope::updateData()
297 {
298 m_dataDisplay->updateData((int) m_value,
299 property("QCS_zoomx").toDouble(),
300 property("QCS_zoomy").toDouble(),
301 static_cast<ScopeWidget *>(m_widget)->freeze);
302 }
303
304 ScopeItem::ScopeItem(int width, int height)
305 {
306 m_width = width;
307 m_height = height;
308 }
309
310 void ScopeItem::paint(QPainter *p,
311 const QStyleOptionGraphicsItem */*option*/,
312 QWidget */*widget*/)
313 {
314 p->setPen(m_pen);
315 p->drawPoints(m_polygon);
316 }
317
318 void ScopeItem::setPen(const QPen & pen)
319 {
320 m_pen = pen;
321 }
322
323 void ScopeItem::setPolygon(const QPolygonF & polygon)
324 {
325 m_polygon = polygon;
326 update(boundingRect());
327 }
328
329 void ScopeItem::setSize(int width, int height)
330 {
331 m_width = width;
332 m_height = height;
333 prepareGeometryChange();
334 }
335
336 ScopeData::ScopeData(ScopeParams *params) : DataDisplay(params)
337 {
338 curveData.resize(m_params->width + 2);
339 curve = new QGraphicsPolygonItem(/*&curveData*/);
340 curve->setPen(QPen(Qt::green, 0));
341 curve->setPen(QPen(QColor("#40FF40"), 0));
342
343 curve->hide();
344 m_params->scene->addItem(curve);
345 }
346
347 void ScopeData::resize()
348 {
349 curveData.resize(m_params->width + 2);
350 }
351
352 void ScopeData::updateData(int channel, double zoomx, double zoomy, bool freeze)
353 {
354 CsoundUserData *ud = m_params->ud;
355 int width = m_params->width;
356 int height = m_params->height;
357 if (ud == 0 || !ud->csEngine->isRunning() )
358 return;
359 if (freeze)
360 return;
361 double value;
362 int numChnls = ud->numChnls;
363 MYFLT newValue;
364 if (channel == 0 || channel > numChnls ) {
365 return;
366 }
367 channel = (channel < 0 ? -1: channel - 1);
368 #ifdef USE_WIDGET_MUTEX
369 //FIXME is this locking needed, or should a separate locking mechanism be implemented?
370 QReadWriteLock *mutex = m_params->mutex;
371 mutex->lockForWrite();
372 #endif
373 // FIXME how to make sure the buffer is read before it is flushed when recorded?
374 // Have another buffer?
375 RingBuffer *buffer = &ud->audioOutputBuffer;
376 buffer->lock();
377 QList<MYFLT> list = buffer->buffer;
378 buffer->unlock();
379 long listSize = list.size();
380 long offset = buffer->currentPos;
381 long dataToRead = width;
382 // search for trig
383 long trigOffset = 0;
384 double lastValue = 1.0;
385 long maxFrames = width < dataToRead ? width: dataToRead;
386
387 if(m_params->triggerMode == TriggerMode::TriggerUp) {
388 if(channel >= 0) {
389 for(int i=0; i < maxFrames; i++) {
390 int idx = (int)((offset + (int)(i*numChnls*zoomx) + channel) % listSize);
391 double value = list[idx];
392 if(value >= 0 && lastValue < 0) {
393 trigOffset = idx - offset - channel;
394 break;
395 }
396 lastValue = value;
397 }
398 }
399 else {
400 for(int i=0; i < maxFrames; i++) {
401 int baseidx = (int)((offset + (int)(i*numChnls*zoomx)) % listSize);
402 double value = 0;
403 for(int chan = 0; chan < numChnls; chan++) {
404 double newValue = list[baseidx+chan];
405 if(fabs(newValue) > fabs(value))
406 value = newValue;
407 }
408 if(value >= 0 && lastValue < 0) {
409 trigOffset = baseidx - offset;
410 break;
411 }
412 lastValue = value;
413 }
414
415 }
416 offset += trigOffset;
417 }
418 double factor = numChnls * zoomx;
419 int halfheight = height/2;
420 if(channel >= 0) {
421 for(int i = 0; i < maxFrames; i++) {
422 int idx = (int)((offset+channel+ (int)(i*factor)) % listSize);
423 value = (double) list[idx];
424 curveData[i+1] = QPoint(i, -zoomy*value*halfheight);
425 }
426 } else {
427 for(int i = 0; i < maxFrames; i++) {
428 value = 0;
429 for(int chan = 0; chan < numChnls; chan++) {
430 int idx = (int)((offset+chan+ (int)(i*factor)) % listSize);
431 newValue = (double) list[idx];
432 if(fabs(newValue) > fabs(value))
433 value = newValue;
434 }
435 curveData[i+1] = QPoint(i, -zoomy*value*halfheight);
436 }
437
438 }
439
440
441 /*
442 for (int i = 0; i < width; i++) {
443 value = 0;
444 for (int j = 0; j < (int) zoomx; j++) {
445 if (channel == -1) {
446 // all channels
447 newValue = 0;
448 for (int k = 0; k < numChnls; k++) {
449 int bufIdx = (int)((((i*zoomx)+j)*numChnls) + offset + k) % listSize;
450 newValue += list[bufIdx];
451 }
452 newValue /= numChnls;
453 if (fabs(newValue) > fabs(value))
454 value = -(double) newValue;
455 }
456 else {
457 int bufIdx = (int)((((i*zoomx)+j)*numChnls) + offset + channel) % listSize;
458 if (fabs(list[bufIdx]) > fabs(value))
459 value = (double) -list[bufIdx];
460 }
461 }
462 curveData[i+1] = QPoint(i, zoomy*value*height/2);
463 }
464 */
465 // buffer->currentReadPos += width;
466 buffer->currentReadPos = (offset + dataToRead) % buffer->size;
467 m_params->widget->setSceneRect(0, -height/2, width, height );
468 curveData.last() = QPoint(width-4, 0);
469 curveData.first() = QPoint(0, 0);
470 curve->setPolygon(curveData);
471 #ifdef USE_WIDGET_MUTEX
472 mutex->unlock();
473 #endif
474 }
475
476 void ScopeData::show()
477 {
478 curve->show();
479 }
480
481 void ScopeData::hide()
482 {
483 curve->hide();
484 }
485
486 LissajouData::LissajouData(ScopeParams *params) : DataDisplay(params)
487 {
488 curveData.resize(m_params->width);
489 curve = new ScopeItem(m_params->width, m_params->height);
490 auto pen = QPen(Qt::green);
491 pen.setCosmetic(true);
492 curve->setPen(pen);
493 curve->hide();
494 m_params->scene->addItem(curve);
495 }
496
497 void LissajouData::resize()
498 {
499 // We take 8 times the display width points for each pass
500 // to have a smooth animation
501 curveData.resize(m_params->width * 8);
502 curve->setSize(m_params->width, m_params->height);
503 }
504
505 void LissajouData::updateData(int channel, double zoomx, double zoomy, bool freeze)
506 {
507 // The decimation factor (zoom) is not used here
508 CsoundUserData *ud = m_params->ud;
509 int width = m_params->width;
510 int height = m_params->height;
511 if (ud == nullptr || !ud->csEngine->isRunning())
512 return;
513 if (freeze)
514 return;
515 double x, y;
516 int numChnls = ud->numChnls;
517 // We take two consecutives channels, the first one for abscissas and
518 // the second one for ordinates
519 if (channel == 0 || channel >= numChnls || numChnls < 2) {
520 return;
521 }
522 channel = (channel < 0 ? 0 : channel - 1);
523 #ifdef USE_WIDGET_MUTEX
524 QReadWriteLock *mutex = m_params->mutex;
525 mutex->lockForWrite();
526 #endif
527 RingBuffer *buffer = &ud->audioOutputBuffer;
528 buffer->lock();
529 QList<MYFLT> list = buffer->buffer;
530 buffer->unlock();
531 long listSize = list.size();
532 long offset = buffer->currentPos;
533 for (int i = 0; i < curveData.size(); i++) {
534 int bufferIndex = (int)((i*numChnls) + offset + channel) % listSize;
535 x = (double)list[bufferIndex];
536 bufferIndex = (bufferIndex + 1) % listSize;
537 y = (double) -list[bufferIndex];
538 curveData[i] = QPoint(x*width*zoomx/4, y*height*zoomy/4);
539 }
540 m_params->widget->setSceneRect(-width/2, -height/2, width, height );
541 curve->setPolygon(curveData);
542 #ifdef USE_WIDGET_MUTEX
543 mutex->unlock();
544 #endif
545 }
546
547 void LissajouData::show()
548 {
549 curve->show();
550 }
551
552 void LissajouData::hide()
553 {
554 curve->hide();
555 }
556
557 PoincareData::PoincareData(ScopeParams *params) : DataDisplay(params)
558 {
559 curveData.resize(m_params->width);
560 curve = new ScopeItem(m_params->width, m_params->height);
561 curve->setPen(QPen(Qt::green, 0));
562 curve->hide();
563 lastValue = 0.0;
564 m_params->scene->addItem(curve);
565 }
566
567 void PoincareData::resize()
568 {
569 // We take 8 times the display width points for each pass
570 // to have a smooth animation
571 curveData.resize(m_params->width * 8);
572 curve->setSize(m_params->width, m_params->height);
573 }
574
575 void PoincareData::updateData(int channel, double zoomx, double zoomy, bool freeze)
576 {
577 CsoundUserData *ud = m_params->ud;
578 int width = m_params->width;
579 int height = m_params->height;
580 if (ud == 0 || !ud->csEngine->isRunning() )
581 return;
582 if (freeze)
583 return;
584 double value;
585 int numChnls = ud->numChnls;
586 if (channel == 0 || channel > numChnls) {
587 return;
588 }
589 channel = (channel < 0 ? 0 : channel - 1);
590 #ifdef USE_WIDGET_MUTEX
591 QReadWriteLock *mutex = m_params->mutex;
592 mutex->lockForWrite();
593 #endif
594 RingBuffer *buffer = &ud->audioOutputBuffer;
595 buffer->lock();
596 QList<MYFLT> list = buffer->buffer;
597 buffer->unlock();
598 long listSize = list.size();
599 long offset = buffer->currentPos;
600 for (int i = 0; i < curveData.size(); i++) {
601 int bufferIndex = (int)((i*zoomx*numChnls) + offset + channel) % listSize;
602 value = (double)list[bufferIndex];
603 curveData[i] = QPoint(lastValue*width*zoomx/2, -value*height*zoomy/2);
604 lastValue = value;
605 }
606 m_params->widget->setSceneRect(-width/2, -height/2, width, height );
607 curve->setPolygon(curveData);
608 #ifdef USE_WIDGET_MUTEX
609 mutex->unlock();
610 #endif
611 }
612
613 void PoincareData::show()
614 {
615 curve->show();
616 }
617
618 void PoincareData::hide()
619 {
620 curve->hide();
621 }
622