1 /***************************************************************************
2 * Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com) *
3 * This file is part of kdenlive. See www.kdenlive.org. *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
11 #include "qtconcurrentrun.h"
13 #include "abstractaudioscopewidget.h"
21 #include <QMouseEvent>
24 const int REALTIME_FPS = 30;
26 const QColor light(250, 238, 226, 255);
27 const QColor dark(40, 40, 39, 255);
28 const QColor dark2(25, 25, 23, 255);
30 const QPen AbstractAudioScopeWidget::penThick(QBrush(QColor(250, 250, 250)), 2, Qt::SolidLine);
31 const QPen AbstractAudioScopeWidget::penThin(QBrush(QColor(250, 250, 250)), 1, Qt::SolidLine);
32 const QPen AbstractAudioScopeWidget::penLight(QBrush(QColor(200, 200, 250, 150)), 1, Qt::SolidLine);
33 const QPen AbstractAudioScopeWidget::penDark(QBrush(QColor(0, 0, 20, 250)), 1, Qt::SolidLine);
35 AbstractAudioScopeWidget::AbstractAudioScopeWidget(Monitor *projMonitor, Monitor *clipMonitor, bool trackMouse, QWidget *parent) :
37 m_projMonitor(projMonitor),
38 m_clipMonitor(clipMonitor),
40 m_mouseWithinWidget(false),
43 m_accelFactorScope(1),
44 m_accelFactorBackground(1),
47 m_semaphoreBackground(1),
48 initialDimensionUpdateDone(false),
49 m_requestForcedUpdate(false)
52 m_scopePalette = QPalette();
53 m_scopePalette.setBrush(QPalette::Window, QBrush(dark2));
54 m_scopePalette.setBrush(QPalette::Base, QBrush(dark));
55 m_scopePalette.setBrush(QPalette::Button, QBrush(dark));
56 m_scopePalette.setBrush(QPalette::Text, QBrush(light));
57 m_scopePalette.setBrush(QPalette::WindowText, QBrush(light));
58 m_scopePalette.setBrush(QPalette::ButtonText, QBrush(light));
59 this->setPalette(m_scopePalette);
60 this->setAutoFillBackground(true);
62 m_aAutoRefresh = new QAction(i18n("Auto Refresh"), this);
63 m_aAutoRefresh->setCheckable(true);
64 m_aRealtime = new QAction(i18n("Realtime (with precision loss)"), this);
65 m_aRealtime->setCheckable(true);
67 m_menu = new QMenu(this);
68 m_menu->setPalette(m_scopePalette);
69 m_menu->addAction(m_aAutoRefresh);
70 m_menu->addAction(m_aRealtime);
72 this->setContextMenuPolicy(Qt::CustomContextMenu);
74 m_activeRender = (m_clipMonitor->isActive()) ? m_clipMonitor->render : m_projMonitor->render;
77 b &= connect(this, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(customContextMenuRequested(QPoint)));
79 //b &= connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
80 b &= connect(m_activeRender, SIGNAL(frameUpdated(QImage)), this, SLOT(slotRenderZoneUpdated(QImage)));
82 b &= connect(this, SIGNAL(signalHUDRenderingFinished(uint, uint)), this, SLOT(slotHUDRenderingFinished(uint, uint)));
83 b &= connect(this, SIGNAL(signalScopeRenderingFinished(uint, uint)), this, SLOT(slotScopeRenderingFinished(uint, uint)));
84 b &= connect(this, SIGNAL(signalBackgroundRenderingFinished(uint, uint)), this, SLOT(slotBackgroundRenderingFinished(uint, uint)));
85 b &= connect(m_aRealtime, SIGNAL(toggled(bool)), this, SLOT(slotResetRealtimeFactor(bool)));
86 b &= connect(m_aAutoRefresh, SIGNAL(toggled(bool)), this, SLOT(slotAutoRefreshToggled(bool)));
89 // Enable mouse tracking if desired.
90 // Causes the mouseMoved signal to be emitted when the mouse moves inside the
91 // widget, even when no mouse button is pressed.
92 this->setMouseTracking(trackMouse);
95 AbstractAudioScopeWidget::~AbstractAudioScopeWidget()
100 delete m_aAutoRefresh;
104 void AbstractAudioScopeWidget::init()
106 m_widgetName = widgetName();
110 void AbstractAudioScopeWidget::readConfig()
112 KSharedConfigPtr config = KGlobal::config();
113 KConfigGroup scopeConfig(config, configName());
114 m_aAutoRefresh->setChecked(scopeConfig.readEntry("autoRefresh", true));
115 m_aRealtime->setChecked(scopeConfig.readEntry("realtime", false));
119 void AbstractAudioScopeWidget::writeConfig()
121 KSharedConfigPtr config = KGlobal::config();
122 KConfigGroup scopeConfig(config, configName());
123 scopeConfig.writeEntry("autoRefresh", m_aAutoRefresh->isChecked());
124 scopeConfig.writeEntry("realtime", m_aRealtime->isChecked());
128 QString AbstractAudioScopeWidget::configName()
130 return "Scope_" + m_widgetName;
133 void AbstractAudioScopeWidget::prodHUDThread()
135 if (this->visibleRegion().isEmpty()) {
136 // qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating HUD.";
138 if (m_semaphoreHUD.tryAcquire(1)) {
139 Q_ASSERT(!m_threadHUD.isRunning());
141 m_newHUDFrames.fetchAndStoreRelaxed(0);
142 m_newHUDUpdates.fetchAndStoreRelaxed(0);
143 m_threadHUD = QtConcurrent::run(this, &AbstractAudioScopeWidget::renderHUD, m_accelFactorHUD);
144 // qDebug() << "HUD thread started in " << m_widgetName;
147 // qDebug() << "HUD semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadHUD.isRunning();
152 void AbstractAudioScopeWidget::prodScopeThread()
154 // Only start a new thread if the scope is actually visible
155 // and not hidden by another widget on the stack and if user want the scope to update.
156 if (this->visibleRegion().isEmpty() || (!m_aAutoRefresh->isChecked() && !m_requestForcedUpdate)) {
157 // qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating scope.";
159 // Try to acquire the semaphore. This must only succeed if m_threadScope is not running
160 // anymore. Therefore the semaphore must NOT be released before m_threadScope ends.
161 // If acquiring the semaphore fails, the thread is still running.
162 if (m_semaphoreScope.tryAcquire(1)) {
163 Q_ASSERT(!m_threadScope.isRunning());
165 m_newScopeFrames.fetchAndStoreRelaxed(0);
166 m_newScopeUpdates.fetchAndStoreRelaxed(0);
168 Q_ASSERT(m_accelFactorScope > 0);
170 // See http://doc.qt.nokia.com/latest/qtconcurrentrun.html#run about
171 // running member functions in a thread
172 m_threadScope = QtConcurrent::run(this, &AbstractAudioScopeWidget::renderScope,
173 m_accelFactorScope, m_audioFrame, m_freq, m_nChannels, m_nSamples);
174 m_requestForcedUpdate = false;
176 // qDebug() << "Scope thread started in " << m_widgetName;
179 // qDebug() << "Scope semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadScope.isRunning();
183 void AbstractAudioScopeWidget::prodBackgroundThread()
185 if (this->visibleRegion().isEmpty()) {
186 // qDebug() << "Scope " << m_widgetName << " is not visible. Not calculating background.";
188 if (m_semaphoreBackground.tryAcquire(1)) {
189 Q_ASSERT(!m_threadBackground.isRunning());
191 m_newBackgroundFrames.fetchAndStoreRelaxed(0);
192 m_newBackgroundUpdates.fetchAndStoreRelaxed(0);
193 m_threadBackground = QtConcurrent::run(this, &AbstractAudioScopeWidget::renderBackground, m_accelFactorBackground);
194 // qDebug() << "Background thread started in " << m_widgetName;
197 // qDebug() << "Background semaphore locked, not prodding in " << m_widgetName << ". Thread running: " << m_threadBackground.isRunning();
202 void AbstractAudioScopeWidget::forceUpdate(bool doUpdate)
204 // qDebug() << "Force update called in " << widgetName() << ". Arg: " << doUpdate;
208 m_requestForcedUpdate = true;
209 m_newHUDUpdates.fetchAndAddRelaxed(1);
210 m_newScopeUpdates.fetchAndAddRelaxed(1);
211 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
214 prodBackgroundThread();
216 void AbstractAudioScopeWidget::forceUpdateHUD()
218 m_newHUDUpdates.fetchAndAddRelaxed(1);
222 void AbstractAudioScopeWidget::forceUpdateScope()
224 m_newScopeUpdates.fetchAndAddRelaxed(1);
225 m_requestForcedUpdate = true;
229 void AbstractAudioScopeWidget::forceUpdateBackground()
231 m_newBackgroundUpdates.fetchAndAddRelaxed(1);
232 prodBackgroundThread();
239 void AbstractAudioScopeWidget::mouseReleaseEvent(QMouseEvent *event)
241 if (!m_aAutoRefresh->isChecked()) {
242 m_requestForcedUpdate = true;
243 m_activeRender->sendFrameUpdate();
247 prodBackgroundThread();
248 QWidget::mouseReleaseEvent(event);
251 void AbstractAudioScopeWidget::resizeEvent(QResizeEvent *event)
253 // Update the dimension of the available rect for painting
254 m_scopeRect = scopeRect();
257 QWidget::resizeEvent(event);
260 void AbstractAudioScopeWidget::showEvent(QShowEvent *event)
262 QWidget::showEvent(event);
263 m_scopeRect = scopeRect();
266 void AbstractAudioScopeWidget::paintEvent(QPaintEvent *)
268 QPainter davinci(this);
269 davinci.drawImage(m_scopeRect.topLeft(), m_imgBackground);
270 davinci.drawImage(m_scopeRect.topLeft(), m_imgScope);
271 davinci.drawImage(m_scopeRect.topLeft(), m_imgHUD);
274 void AbstractAudioScopeWidget::mouseMoveEvent(QMouseEvent *event)
276 m_mousePos = event->pos();
277 m_mouseWithinWidget = true;
278 emit signalMousePositionChanged();
280 void AbstractAudioScopeWidget::leaveEvent(QEvent *)
282 m_mouseWithinWidget = false;
283 emit signalMousePositionChanged();
286 void AbstractAudioScopeWidget::customContextMenuRequested(const QPoint &pos)
288 m_menu->exec(this->mapToGlobal(pos));
291 uint AbstractAudioScopeWidget::calculateAccelFactorHUD(uint oldMseconds, uint)
293 return ceil((float)oldMseconds*REALTIME_FPS / 1000);
295 uint AbstractAudioScopeWidget::calculateAccelFactorScope(uint oldMseconds, uint)
297 return ceil((float)oldMseconds*REALTIME_FPS / 1000);
299 uint AbstractAudioScopeWidget::calculateAccelFactorBackground(uint oldMseconds, uint)
301 return ceil((float)oldMseconds*REALTIME_FPS / 1000);
307 void AbstractAudioScopeWidget::slotHUDRenderingFinished(uint mseconds, uint oldFactor)
309 // qDebug() << "HUD rendering has finished, waiting for termination in " << m_widgetName;
310 m_threadHUD.waitForFinished();
311 m_imgHUD = m_threadHUD.result();
313 m_semaphoreHUD.release(1);
317 if (m_aRealtime->isChecked()) {
318 accel = calculateAccelFactorHUD(mseconds, oldFactor);
319 if (m_accelFactorHUD < 1) {
322 m_accelFactorHUD = accel;
325 if ((m_newHUDFrames > 0 && m_aAutoRefresh->isChecked()) || m_newHUDUpdates > 0) {
326 // qDebug() << "Trying to start a new HUD thread for " << m_widgetName
327 // << ". New frames/updates: " << m_newHUDFrames << "/" << m_newHUDUpdates;
332 void AbstractAudioScopeWidget::slotScopeRenderingFinished(uint mseconds, uint oldFactor)
334 // The signal can be received before the thread has really finished. So we
335 // need to wait until it has really finished before starting a new thread.
336 // qDebug() << "Scope rendering has finished, waiting for termination in " << m_widgetName;
337 m_threadScope.waitForFinished();
338 m_imgScope = m_threadScope.result();
340 // The scope thread has finished. Now we can release the semaphore, allowing a new thread.
341 // See prodScopeThread where the semaphore is acquired again.
342 m_semaphoreScope.release(1);
345 // Calculate the acceleration factor hint to get «realtime» updates.
347 if (m_aRealtime->isChecked()) {
348 accel = calculateAccelFactorScope(mseconds, oldFactor);
350 // If mseconds happens to be 0.
353 // Don't directly calculate with m_accelFactorScope as we are dealing with concurrency.
354 // If m_accelFactorScope is set to 0 at the wrong moment, who knows what might happen
355 // then :) Therefore use a local variable.
356 m_accelFactorScope = accel;
359 if ((m_newScopeFrames > 0 && m_aAutoRefresh->isChecked()) || m_newScopeUpdates > 0) {
360 // qDebug() << "Trying to start a new scope thread for " << m_widgetName
361 // << ". New frames/updates: " << m_newScopeFrames << "/" << m_newScopeUpdates;
366 void AbstractAudioScopeWidget::slotBackgroundRenderingFinished(uint mseconds, uint oldFactor)
368 // qDebug() << "Background rendering has finished, waiting for termination in " << m_widgetName;
369 m_threadBackground.waitForFinished();
370 m_imgBackground = m_threadBackground.result();
372 m_semaphoreBackground.release(1);
376 if (m_aRealtime->isChecked()) {
377 accel = calculateAccelFactorBackground(mseconds, oldFactor);
378 if (m_accelFactorBackground < 1) {
381 m_accelFactorBackground = accel;
384 if ((m_newBackgroundFrames > 0 && m_aAutoRefresh->isChecked()) || m_newBackgroundUpdates > 0) {
385 // qDebug() << "Trying to start a new background thread for " << m_widgetName
386 // << ". New frames/updates: " << m_newBackgroundFrames << "/" << m_newBackgroundUpdates;
387 prodBackgroundThread();;
391 void AbstractAudioScopeWidget::slotActiveMonitorChanged(bool isClipMonitor)
393 // qDebug() << "Active monitor has changed in " << m_widgetName << ". Is the clip monitor active now? " << isClipMonitor;
395 bool b = m_activeRender->disconnect(this);
398 m_activeRender = (isClipMonitor) ? m_clipMonitor->render : m_projMonitor->render;
400 //b &= connect(m_activeRender, SIGNAL(rendererPosition(int)), this, SLOT(slotRenderZoneUpdated()));
401 b &= connect(m_activeRender, SIGNAL(frameUpdated(QImage)), this, SLOT(slotRenderZoneUpdated(QImage)));
404 // Update the scope for the new monitor.
407 prodBackgroundThread();
410 void AbstractAudioScopeWidget::slotRenderZoneUpdated()
412 m_newHUDFrames.fetchAndAddRelaxed(1);
413 m_newScopeFrames.fetchAndAddRelaxed(1);
414 m_newBackgroundFrames.fetchAndAddRelaxed(1);
416 // qDebug() << "Monitor incoming. New frames total HUD/Scope/Background: " << m_newHUDFrames
417 // << "/" << m_newScopeFrames << "/" << m_newBackgroundFrames;
419 if (this->visibleRegion().isEmpty()) {
420 // qDebug() << "Scope of widget " << m_widgetName << " is not at the top, not rendering.";
422 if (m_aAutoRefresh->isChecked()) {
425 prodBackgroundThread();
430 void AbstractAudioScopeWidget::slotRenderZoneUpdated(QImage frame)
432 m_scopeImage = frame;
433 slotRenderZoneUpdated();
436 void AbstractAudioScopeWidget::slotReceiveAudio(const QVector<int16_t>& sampleData, int freq, int num_channels, int num_samples)
438 qDebug() << "Received audio. Size is " << (int) sampleData.size() << ".";
439 if (sampleData.size() > 0) {
440 qDebug() << sampleData.data()[0];
445 void AbstractAudioScopeWidget::slotReceiveAudioTemp(const QByteArray arr)
447 qDebug() << "Audio signal received";
450 void AbstractAudioScopeWidget::slotResetRealtimeFactor(bool realtimeChecked)
452 if (!realtimeChecked) {
453 m_accelFactorHUD = 1;
454 m_accelFactorScope = 1;
455 m_accelFactorBackground = 1;
459 bool AbstractAudioScopeWidget::autoRefreshEnabled()
461 return m_aAutoRefresh->isChecked();
464 void AbstractAudioScopeWidget::slotAutoRefreshToggled(bool autoRefresh)
466 if (isVisible()) emit requestAutoRefresh(autoRefresh);
467 // TODO only if depends on input
470 m_requestForcedUpdate = true;
471 m_activeRender->sendFrameUpdate();