]> git.sesse.net Git - kdenlive/blob - src/audioscopes/audiospectrum.cpp
ef0e3c68bfd677d15bc3f917d0f6ebbb6001a700
[kdenlive] / src / audioscopes / audiospectrum.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Simon Andreas Eugster (simon.eu@gmail.com)      *
3  *   This file is part of kdenlive. See www.kdenlive.org.                  *
4  *                                                                         *
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  ***************************************************************************/
10
11
12
13 #include "audiospectrum.h"
14 #include "ffttools.h"
15 #include "tools/kiss_fftr.h"
16
17 #include <QMenu>
18 #include <QPainter>
19 #include <QMouseEvent>
20
21 #include <iostream>
22
23 // Enables debugging, like writing a GNU Octave .m file to /tmp
24 //#define DEBUG_AUDIOSPEC
25
26 #ifdef DEBUG_AUDIOSPEC
27 #include <QDebug>
28 #endif
29
30 #define MIN_DB_VALUE -120
31 #define MAX_FREQ_VALUE 96000
32 #define MIN_FREQ_VALUE 1000
33
34 AudioSpectrum::AudioSpectrum(QWidget *parent) :
35         AbstractAudioScopeWidget(true, parent),
36         m_fftTools(),
37         m_lastFFT(),
38         m_lastFFTLock(1)
39 {
40     ui = new Ui::AudioSpectrum_UI;
41     ui->setupUi(this);
42
43
44     m_aResetHz = new QAction(i18n("Reset maximum frequency to sampling rate"), this);
45
46
47     m_menu->addSeparator();
48     m_menu->addAction(m_aResetHz);
49     m_menu->removeAction(m_aRealtime);
50
51
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));
56
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);
60
61
62     bool b = true;
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()));
66     Q_ASSERT(b);
67
68
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."));
73
74     AbstractScopeWidget::init();
75 }
76 AudioSpectrum::~AudioSpectrum()
77 {
78     writeConfig();
79
80     delete m_aResetHz;
81 }
82
83 void AudioSpectrum::readConfig()
84 {
85     AbstractScopeWidget::readConfig();
86
87     KSharedConfigPtr config = KGlobal::config();
88     KConfigGroup scopeConfig(config, AbstractScopeWidget::configName());
89
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);
95
96     if (m_freqMax == 0) {
97         m_customFreq = false;
98         m_freqMax = 10000;
99     } else {
100         m_customFreq = true;
101     }
102 }
103 void AudioSpectrum::writeConfig()
104 {
105     KSharedConfigPtr config = KGlobal::config();
106     KConfigGroup scopeConfig(config, AbstractScopeWidget::configName());
107
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);
112     if (m_customFreq) {
113         scopeConfig.writeEntry("freqMax", m_freqMax);
114     } else {
115         scopeConfig.writeEntry("freqMax", 0);
116     }
117
118     scopeConfig.sync();
119 }
120
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; }
125
126 QImage AudioSpectrum::renderBackground(uint) { return QImage(); }
127
128 QImage AudioSpectrum::renderAudioScope(uint, const QVector<int16_t> audioFrame, const int freq, const int num_channels,
129                                        const int num_samples, const int)
130 {
131     if (audioFrame.size() > 63) {
132         if (!m_customFreq) {
133             m_freqMax = freq / 2;
134         }
135
136         QTime start = QTime::currentTime();
137
138
139         // Determine the window size to use. It should be
140         // * not bigger than the number of samples actually available
141         // * divisible by 2
142         int fftWindow = ui->windowSize->itemData(ui->windowSize->currentIndex()).toInt();
143         if (fftWindow > num_samples) {
144             fftWindow = num_samples;
145         }
146         if ((fftWindow & 1) == 1) {
147             fftWindow--;
148         }
149
150         // Show the window size used, for information
151         ui->labelFFTSizeNumber->setText(QVariant(fftWindow).toString());
152
153
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);
159
160
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));
167
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();
171
172
173         // Draw the spectrum
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();
180         int yMax;
181
182         for (uint i = 0; i < w; i++) {
183             yMax = (dbMap[i] - m_dBmin) / (m_dBmax-m_dBmin) * (h-1);
184             if (yMax < 0) {
185                 yMax = 0;
186             } else if (yMax >= (int)h) {
187                 yMax = h-1;
188             }
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));
191             }
192         }
193
194         emit signalScopeRenderingFinished(start.elapsed(), 1);
195
196
197         return spectrum;
198     } else {
199         emit signalScopeRenderingFinished(0, 1);
200         return QImage();
201     }
202 }
203 QImage AudioSpectrum::renderHUD(uint)
204 {
205     QTime start = QTime::currentTime();
206
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();
217
218     QImage hud(m_scopeRect.size(), QImage::Format_ARGB32);
219     hud.fill(qRgba(0,0,0,0));
220
221     QPainter davinci(&hud);
222     davinci.setPen(AbstractScopeWidget::penLight);
223
224     int y;
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.
229             break;
230         }
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));
233     }
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));
238
239     const uint hzDiff = ceil( ((float)minDistX)/m_innerScopeRect.width() * m_freqMax / 1000 ) * 1000;
240     int x = 0;
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;
246
247         if (x <= rightBorder) {
248             davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
249         }
250         if (hz < m_freqMax && x+textDistY < leftDist + m_innerScopeRect.width()) {
251             davinci.drawText(x-4, y, QVariant(hz/1000).toString());
252         } else {
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));
256         }
257
258         if (hz > 0) {
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) {
264                     break;
265                 }
266                 davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()-1);
267             }
268         }
269     }
270
271     if (m_mouseWithinWidget && mouseX < m_innerScopeRect.width()-1) {
272         davinci.setPen(AbstractScopeWidget::penThin);
273
274         x = leftDist + mouseX;
275
276         float db = 0;
277         float freq = ((float) mouseX)/(m_innerScopeRect.width()-1) * m_freqMax;
278         bool drawDb = false;
279
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);
284
285             db = dbMap[mouseX];
286             y = topDist + m_innerScopeRect.height()-1 - (dbMap[mouseX] - m_dBmin) / (m_dBmax-m_dBmin) * (m_innerScopeRect.height()-1);
287
288             if (y < (int)topDist + m_innerScopeRect.height()-1) {
289                 drawDb = true;
290                 davinci.drawLine(x, y, leftDist + m_innerScopeRect.width()-1, y);
291             }
292         } else {
293             y = topDist + mouseY;
294         }
295         m_lastFFTLock.release();
296
297         if (y > (int)topDist + mouseY) {
298             y = topDist+ mouseY;
299         }
300         davinci.drawLine(x, y, x, topDist + m_innerScopeRect.height()-1);
301
302         if (drawDb) {
303             QPoint dist(20, -20);
304             QRect rect(
305                         leftDist + mouseX + dist.x(),
306                         topDist + mouseY + dist.y(),
307                         100,
308                         40
309                         );
310             if (rect.right() > (int)leftDist + m_innerScopeRect.width()-1) {
311                 // Mirror the rectangle at the y axis to keep it inside the widget
312                 rect = QRect(
313                             rect.topLeft() - QPoint(rect.width() + 2*dist.x(), 0),
314                             rect.size());
315             }
316
317             QRect textRect(
318                         rect.topLeft() + QPoint(12, 4),
319                         rect.size()
320                         );
321
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))
327                                  + "\n"
328                                  + i18n("%1 kHz", QString("%1").arg(freq/1000, 0, 'f', 2))));
329         }
330
331     }
332
333
334     emit signalHUDRenderingFinished(start.elapsed(), 1);
335     return hud;
336 }
337
338 QRect AudioSpectrum::scopeRect()
339 {
340     m_scopeRect = QRect(
341             QPoint(
342                     10,                                     // Left
343                     ui->verticalSpacer->geometry().top()+6  // Top
344             ),
345             AbstractAudioScopeWidget::rect().bottomRight()
346     );
347     m_innerScopeRect = QRect(
348             QPoint(
349                     m_scopeRect.left()+6,                   // Left
350                     m_scopeRect.top()+6                     // Top
351             ), QPoint(
352                     ui->verticalSpacer->geometry().right()-70,
353                     ui->verticalSpacer->geometry().bottom()-40
354             )
355     );
356     return m_scopeRect;
357 }
358
359 void AudioSpectrum::slotResetMaxFreq()
360 {
361     m_customFreq = false;
362     forceUpdateHUD();
363     forceUpdateScope();
364 }
365
366
367 ///// EVENTS /////
368
369 void AudioSpectrum::handleMouseDrag(const QPoint movement, const RescaleDirection rescaleDirection, const Qt::KeyboardModifiers rescaleModifiers)
370 {
371     if (rescaleDirection == North) {
372         // Nort-South direction: Adjust the dB scale
373
374         if ((rescaleModifiers & Qt::ShiftModifier) == 0) {
375
376             // By default adjust the min dB value
377             m_dBmin += movement.y();
378
379         } else {
380
381             // Adjust max dB value if Shift is pressed.
382             m_dBmax += movement.y();
383
384         }
385
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
388         if (m_dBmax > 0) {
389             m_dBmax = 0;
390         }
391         if (m_dBmin < MIN_DB_VALUE) {
392             m_dBmin = MIN_DB_VALUE;
393         }
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;
401                 if (m_dBmax > 0) {
402                     m_dBmax = 0;
403                     m_dBmin = -6;
404                 }
405             } else {
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;
411                 }
412             }
413         }
414
415         forceUpdateHUD();
416         forceUpdateScope();
417
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;
423         }
424         if (m_freqMax > MAX_FREQ_VALUE) {
425             m_freqMax = MAX_FREQ_VALUE;
426         }
427         m_customFreq = true;
428
429         forceUpdateHUD();
430         forceUpdateScope();
431     }
432 }
433
434
435 const QVector<float> AudioSpectrum::interpolatePeakPreserving(const QVector<float> in, const uint targetSize, uint left, uint right, float fill)
436 {
437     if (right == 0) {
438         right = in.size()-1;
439     }
440     Q_ASSERT(targetSize > 0);
441     Q_ASSERT(left < right);
442
443     QVector<float> out(targetSize);
444
445
446     float x;
447     float x_prev = 0;
448     int xi;
449     uint i;
450     for (i = 0; i < targetSize; i++) {
451
452         // i:  Target index
453         // x:  Interpolated source index (float!)
454         // xi: floor(x)
455
456         // Transform [0,targetSize-1] to [left,right]
457         x = ((float) i) / (targetSize-1) * (right-left) + left;
458         xi = (int) floor(x);
459
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.
463             break;
464         }
465
466
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
470             out[i] = in[xi];
471         } else {
472             if (in[xi] > in[xi+1]
473                 && x_prev < xi) {
474                 // This is a hack to preserve peaks.
475                 // Consider f = {0, 100, 0}
476                 //          x = {0.5,  1.5}
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).)
480                 out[i] = in[xi];
481             } else {
482                 out[i] =   (xi+1 - x) * in[xi]
483                       + (x - xi)   * in[xi+1];
484             }
485         }
486         x_prev = x;
487     }
488     // Fill the rest of the vector if the right border exceeds the input vector.
489     for (; i < targetSize; i++) {
490         out[i] = fill;
491     }
492
493     return out;
494 }
495
496
497 #ifdef DEBUG_AUDIOSPEC
498 #undef DEBUG_AUDIOSPEC
499 #endif
500
501 #undef MIN_DB_VALUE
502 #undef MAX_FREQ_VALUE
503 #undef MIN_FREQ_VALUE