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 ***************************************************************************/
13 #include "audiospectrum.h"
15 #include "tools/kiss_fftr.h"
19 #include <QMouseEvent>
23 // Enables debugging, like writing a GNU Octave .m file to /tmp
24 //#define DEBUG_AUDIOSPEC
26 #ifdef DEBUG_AUDIOSPEC
30 #define MIN_DB_VALUE -120
31 #define MAX_FREQ_VALUE 96000
32 #define MIN_FREQ_VALUE 1000
34 AudioSpectrum::AudioSpectrum(QWidget *parent) :
35 AbstractAudioScopeWidget(true, parent),
40 ui = new Ui::AudioSpectrum_UI;
44 m_aResetHz = new QAction(i18n("Reset maximum frequency to sampling rate"), this);
47 m_menu->addSeparator();
48 m_menu->addAction(m_aResetHz);
49 m_menu->removeAction(m_aRealtime);
52 ui->windowSize->addItem("256", QVariant(256));
53 ui->windowSize->addItem("512", QVariant(512));
54 ui->windowSize->addItem("1024", QVariant(1024));
55 ui->windowSize->addItem("2048", QVariant(2048));
57 ui->windowFunction->addItem(i18n("Rectangular window"), FFTTools::Window_Rect);
58 ui->windowFunction->addItem(i18n("Triangular window"), FFTTools::Window_Triangle);
59 ui->windowFunction->addItem(i18n("Hamming window"), FFTTools::Window_Hamming);
63 b &= connect(m_aResetHz, SIGNAL(triggered()), this, SLOT(slotResetMaxFreq()));
64 b &= connect(ui->windowFunction, SIGNAL(currentIndexChanged(int)), this, SLOT(forceUpdate()));
65 b &= connect(this, SIGNAL(signalMousePositionChanged()), this, SLOT(forceUpdateHUD()));
69 // Note: These strings are used in both Spectogram and AudioSpectrum. Ideally change both (if necessary) to reduce workload on translators
70 ui->labelFFTSize->setToolTip(i18n("The maximum window size is limited by the number of samples per frame."));
71 ui->windowSize->setToolTip(i18n("A bigger window improves the accuracy at the cost of computational power."));
72 ui->windowFunction->setToolTip(i18n("The rectangular window function is good for signals with equal signal strength (narrow peak), but creates more smearing. See Window function on Wikipedia."));
74 AbstractScopeWidget::init();
76 AudioSpectrum::~AudioSpectrum()
83 void AudioSpectrum::readConfig()
85 AbstractScopeWidget::readConfig();
87 KSharedConfigPtr config = KGlobal::config();
88 KConfigGroup scopeConfig(config, AbstractScopeWidget::configName());
90 ui->windowSize->setCurrentIndex(scopeConfig.readEntry("windowSize", 0));
91 ui->windowFunction->setCurrentIndex(scopeConfig.readEntry("windowFunction", 0));
92 m_dBmax = scopeConfig.readEntry("dBmax", 0);
93 m_dBmin = scopeConfig.readEntry("dBmin", -70);
94 m_freqMax = scopeConfig.readEntry("freqMax", 0);
103 void AudioSpectrum::writeConfig()
105 KSharedConfigPtr config = KGlobal::config();
106 KConfigGroup scopeConfig(config, AbstractScopeWidget::configName());
108 scopeConfig.writeEntry("windowSize", ui->windowSize->currentIndex());
109 scopeConfig.writeEntry("windowFunction", ui->windowFunction->currentIndex());
110 scopeConfig.writeEntry("dBmax", m_dBmax);
111 scopeConfig.writeEntry("dBmin", m_dBmin);
113 scopeConfig.writeEntry("freqMax", m_freqMax);
115 scopeConfig.writeEntry("freqMax", 0);
121 QString AudioSpectrum::widgetName() const { return QString("AudioSpectrum"); }
122 bool AudioSpectrum::isBackgroundDependingOnInput() const { return false; }
123 bool AudioSpectrum::isScopeDependingOnInput() const { return true; }
124 bool AudioSpectrum::isHUDDependingOnInput() const { return false; }
126 QImage AudioSpectrum::renderBackground(uint) { return QImage(); }
128 QImage AudioSpectrum::renderAudioScope(uint, const QVector<int16_t> audioFrame, const int freq, const int num_channels,
129 const int num_samples, const int)
131 if (audioFrame.size() > 63) {
133 m_freqMax = freq / 2;
136 QTime start = QTime::currentTime();
139 // Determine the window size to use. It should be
140 // * not bigger than the number of samples actually available
142 int fftWindow = ui->windowSize->itemData(ui->windowSize->currentIndex()).toInt();
143 if (fftWindow > num_samples) {
144 fftWindow = num_samples;
146 if ((fftWindow & 1) == 1) {
150 // Show the window size used, for information
151 ui->labelFFTSizeNumber->setText(QVariant(fftWindow).toString());
154 // Get the spectral power distribution of the input samples,
155 // using the given window size and function
156 float freqSpectrum[fftWindow/2];
157 FFTTools::WindowType windowType = (FFTTools::WindowType) ui->windowFunction->itemData(ui->windowFunction->currentIndex()).toInt();
158 m_fftTools.fftNormalized(audioFrame, 0, num_channels, freqSpectrum, windowType, fftWindow, 0);
161 // Store the current FFT window (for the HUD) and run the interpolation
162 // for easy pixel-based dB value access
163 QVector<float> dbMap;
164 m_lastFFTLock.acquire();
165 m_lastFFT = QVector<float>(fftWindow/2);
166 memcpy(m_lastFFT.data(), &(freqSpectrum[0]), fftWindow/2 * sizeof(float));
168 uint right = ((float) m_freqMax)/(m_freq) * (m_lastFFT.size() - 1);
169 dbMap = interpolatePeakPreserving(m_lastFFT, m_innerScopeRect.width(), 0, right, -120);
170 m_lastFFTLock.release();
174 QImage spectrum(m_scopeRect.size(), QImage::Format_ARGB32);
175 spectrum.fill(qRgba(0,0,0,0));
176 const uint w = m_innerScopeRect.width();
177 const uint h = m_innerScopeRect.height();
178 const uint leftDist = m_innerScopeRect.left() - m_scopeRect.left();
179 const uint topDist = m_innerScopeRect.top() - m_scopeRect.top();
182 for (uint i = 0; i < w; i++) {
183 yMax = (dbMap[i] - m_dBmin) / (m_dBmax-m_dBmin) * (h-1);
186 } else if (yMax >= (int)h) {
189 for (int y = 0; y < yMax && y < (int)h; y++) {
190 spectrum.setPixel(leftDist + i, topDist + h-y-1, qRgba(225, 182, 255, 255));
194 emit signalScopeRenderingFinished(start.elapsed(), 1);
199 emit signalScopeRenderingFinished(0, 1);
203 QImage AudioSpectrum::renderHUD(uint)
205 QTime start = QTime::currentTime();
207 // Minimum distance between two lines
208 const uint minDistY = 30;
209 const uint minDistX = 40;
210 const uint textDistX = 10;
211 const uint textDistY = 25;
212 const uint topDist = m_innerScopeRect.top() - m_scopeRect.top();
213 const uint leftDist = m_innerScopeRect.left() - m_scopeRect.left();
214 const uint dbDiff = ceil((float)minDistY/m_innerScopeRect.height() * (m_dBmax-m_dBmin));
215 const int mouseX = m_mousePos.x() - m_innerScopeRect.left();
216 const int mouseY = m_mousePos.y() - m_innerScopeRect.top();
218 QImage hud(m_scopeRect.size(), QImage::Format_ARGB32);
219 hud.fill(qRgba(0,0,0,0));
221 QPainter davinci(&hud);
222 davinci.setPen(AbstractScopeWidget::penLight);
225 for (int db = -dbDiff; db > m_dBmin; db -= dbDiff) {
226 y = topDist + m_innerScopeRect.height() * ((float)db)/(m_dBmin - m_dBmax);
227 if (y-topDist > m_innerScopeRect.height()-minDistY+10) {
228 // Abort here, there is still a line left for min dB to paint which needs some room.
231 davinci.drawLine(leftDist, y, leftDist + m_innerScopeRect.width()-1, y);
232 davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, y + 6, i18n("%1 dB", m_dBmax + db));
234 davinci.drawLine(leftDist, topDist, leftDist + m_innerScopeRect.width()-1, topDist);
235 davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, topDist+6, i18n("%1 dB", m_dBmax));
236 davinci.drawLine(leftDist, topDist+m_innerScopeRect.height()-1, leftDist + m_innerScopeRect.width()-1, topDist+m_innerScopeRect.height()-1);
237 davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, topDist+m_innerScopeRect.height()+6, i18n("%1 dB", m_dBmin));
239 const uint hzDiff = ceil( ((float)minDistX)/m_innerScopeRect.width() * m_freqMax / 1000 ) * 1000;
241 const int rightBorder = leftDist + m_innerScopeRect.width()-1;
242 y = topDist + m_innerScopeRect.height() + textDistY;
243 for (uint hz = 0; x <= rightBorder; hz += hzDiff) {
244 davinci.setPen(AbstractScopeWidget::penLight);
245 x = leftDist + m_innerScopeRect.width() * ((float)hz)/m_freqMax;
247 if (x <= rightBorder) {
248 davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
250 if (hz < m_freqMax && x+textDistY < leftDist + m_innerScopeRect.width()) {
251 davinci.drawText(x-4, y, QVariant(hz/1000).toString());
253 x = leftDist + m_innerScopeRect.width();
254 davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
255 davinci.drawText(x-10, y, i18n("%1 kHz").arg((double)m_freqMax/1000, 0, 'f', 1));
259 // Draw finer lines between the main lines
260 davinci.setPen(AbstractScopeWidget::penLightDots);
261 for (uint dHz = 3; dHz > 0; dHz--) {
262 x = leftDist + m_innerScopeRect.width() * ((float)hz - dHz * hzDiff/4.0f)/m_freqMax;
263 if (x > rightBorder) {
266 davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()-1);
271 if (m_mouseWithinWidget && mouseX < m_innerScopeRect.width()-1) {
272 davinci.setPen(AbstractScopeWidget::penThin);
274 x = leftDist + mouseX;
277 float freq = ((float) mouseX)/(m_innerScopeRect.width()-1) * m_freqMax;
280 m_lastFFTLock.acquire();
281 if (m_lastFFT.size() > 0) {
282 uint right = ((float) m_freqMax)/(m_freq) * (m_lastFFT.size() - 1);
283 QVector<float> dbMap = AudioSpectrum::interpolatePeakPreserving(m_lastFFT, m_innerScopeRect.width(), 0, right, -120);
286 y = topDist + m_innerScopeRect.height()-1 - (dbMap[mouseX] - m_dBmin) / (m_dBmax-m_dBmin) * (m_innerScopeRect.height()-1);
288 if (y < (int)topDist + m_innerScopeRect.height()-1) {
290 davinci.drawLine(x, y, leftDist + m_innerScopeRect.width()-1, y);
293 y = topDist + mouseY;
295 m_lastFFTLock.release();
297 if (y > (int)topDist + mouseY) {
300 davinci.drawLine(x, y, x, topDist + m_innerScopeRect.height()-1);
303 QPoint dist(20, -20);
305 leftDist + mouseX + dist.x(),
306 topDist + mouseY + dist.y(),
310 if (rect.right() > (int)leftDist + m_innerScopeRect.width()-1) {
311 // Mirror the rectangle at the y axis to keep it inside the widget
313 rect.topLeft() - QPoint(rect.width() + 2*dist.x(), 0),
318 rect.topLeft() + QPoint(12, 4),
322 davinci.fillRect(rect, AbstractScopeWidget::penBackground.brush());
323 davinci.setPen(AbstractScopeWidget::penLighter);
324 davinci.drawRect(rect);
325 davinci.drawText(textRect, QString(
326 i18n("%1 dB", QString("%1").arg(db, 0, 'f', 2))
328 + i18n("%1 kHz", QString("%1").arg(freq/1000, 0, 'f', 2))));
334 emit signalHUDRenderingFinished(start.elapsed(), 1);
338 QRect AudioSpectrum::scopeRect()
343 ui->verticalSpacer->geometry().top()+6 // Top
345 AbstractAudioScopeWidget::rect().bottomRight()
347 m_innerScopeRect = QRect(
349 m_scopeRect.left()+6, // Left
350 m_scopeRect.top()+6 // Top
352 ui->verticalSpacer->geometry().right()-70,
353 ui->verticalSpacer->geometry().bottom()-40
359 void AudioSpectrum::slotResetMaxFreq()
361 m_customFreq = false;
369 void AudioSpectrum::handleMouseDrag(const QPoint movement, const RescaleDirection rescaleDirection, const Qt::KeyboardModifiers rescaleModifiers)
371 if (rescaleDirection == North) {
372 // Nort-South direction: Adjust the dB scale
374 if ((rescaleModifiers & Qt::ShiftModifier) == 0) {
376 // By default adjust the min dB value
377 m_dBmin += movement.y();
381 // Adjust max dB value if Shift is pressed.
382 m_dBmax += movement.y();
386 // Ensure the dB values lie in [-100, 0] (or rather [MIN_DB_VALUE, 0])
387 // 0 is the upper bound, everything below -70 dB is most likely noise
391 if (m_dBmin < MIN_DB_VALUE) {
392 m_dBmin = MIN_DB_VALUE;
394 // Ensure there is at least 6 dB between the minimum and the maximum value;
395 // lower values hardly make sense
396 if (m_dBmax - m_dBmin < 6) {
397 if ((rescaleModifiers & Qt::ShiftModifier) == 0) {
398 // min was adjusted; Try to adjust the max value to maintain the
399 // minimum dB difference of 6 dB
400 m_dBmax = m_dBmin + 6;
406 // max was adjusted, adjust min
407 m_dBmin = m_dBmax - 6;
408 if (m_dBmin < MIN_DB_VALUE) {
409 m_dBmin = MIN_DB_VALUE;
410 m_dBmax = MIN_DB_VALUE+6;
418 } else if (rescaleDirection == East) {
419 // East-West direction: Adjust the maximum frequency
420 m_freqMax -= 100*movement.x();
421 if (m_freqMax < MIN_FREQ_VALUE) {
422 m_freqMax = MIN_FREQ_VALUE;
424 if (m_freqMax > MAX_FREQ_VALUE) {
425 m_freqMax = MAX_FREQ_VALUE;
435 const QVector<float> AudioSpectrum::interpolatePeakPreserving(const QVector<float> in, const uint targetSize, uint left, uint right, float fill)
440 Q_ASSERT(targetSize > 0);
441 Q_ASSERT(left < right);
443 QVector<float> out(targetSize);
450 for (i = 0; i < targetSize; i++) {
453 // x: Interpolated source index (float!)
456 // Transform [0,targetSize-1] to [left,right]
457 x = ((float) i) / (targetSize-1) * (right-left) + left;
460 if (x > in.size()-1) {
461 // This may happen if right > in.size()-1; Fill the rest of the vector
462 // with the default value now.
467 // Use linear interpolation in order to get smoother display
468 if (i == 0 || i == targetSize-1) {
469 // ... except if we are at the left or right border of the display or the spectrum
472 if (in[xi] > in[xi+1]
474 // This is a hack to preserve peaks.
475 // Consider f = {0, 100, 0}
477 // Then x is 50 both times, and the 100 peak is lost.
478 // Get it back here for the first x after the peak (which is at xi).
479 // (x is the first after the peak if the previous x was smaller than floor(x).)
482 out[i] = (xi+1 - x) * in[xi]
483 + (x - xi) * in[xi+1];
488 // Fill the rest of the vector if the right border exceeds the input vector.
489 for (; i < targetSize; i++) {
497 #ifdef DEBUG_AUDIOSPEC
498 #undef DEBUG_AUDIOSPEC
502 #undef MAX_FREQ_VALUE
503 #undef MIN_FREQ_VALUE