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
25 #ifdef DEBUG_AUDIOSPEC
27 bool fileWritten = false;
30 #define MIN_DB_VALUE -120
32 const QString AudioSpectrum::directions[] = {"North", "Northeast", "East", "Southeast"};
34 AudioSpectrum::AudioSpectrum(QWidget *parent) :
35 AbstractAudioScopeWidget(false, parent),
38 m_rescaleVerticalThreshold(2.0f),
39 m_rescaleActive(false),
40 m_rescalePropertiesLocked(false),
43 ui = new Ui::AudioSpectrum_UI;
46 m_distance = QSize(65, 30);
50 m_aLockHz = new QAction(i18n("Lock maximum frequency"), this);
51 m_aLockHz->setCheckable(true);
52 m_aLockHz->setEnabled(false);
55 m_menu->addSeparator();
56 m_menu->addAction(m_aLockHz);
59 ui->windowSize->addItem("256", QVariant(256));
60 ui->windowSize->addItem("512", QVariant(512));
61 ui->windowSize->addItem("1024", QVariant(1024));
62 ui->windowSize->addItem("2048", QVariant(2048));
64 ui->windowFunction->addItem(i18n("Rectangular window"), FFTTools::Window_Rect);
65 ui->windowFunction->addItem(i18n("Triangular window"), FFTTools::Window_Triangle);
66 ui->windowFunction->addItem(i18n("Hamming window"), FFTTools::Window_Hamming);
69 m_cfg = kiss_fftr_alloc(ui->windowSize->itemData(ui->windowSize->currentIndex()).toInt(), 0,0,0);
70 //m_windowFunctions.insert("tri512", FFTTools::window(FFTTools::Window_Hamming, 8, 0));
71 // TODO Window function cache
75 b &= connect(ui->windowSize, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateCfg()));
78 AbstractScopeWidget::init();
80 AudioSpectrum::~AudioSpectrum()
88 void AudioSpectrum::readConfig()
90 AbstractScopeWidget::readConfig();
92 KSharedConfigPtr config = KGlobal::config();
93 KConfigGroup scopeConfig(config, AbstractScopeWidget::configName());
94 m_aLockHz->setChecked(scopeConfig.readEntry("lockHz", false));
95 ui->windowSize->setCurrentIndex(scopeConfig.readEntry("windowSize", 0));
96 m_dBmax = scopeConfig.readEntry("dBmax", 0);
97 m_dBmin = scopeConfig.readEntry("dBmin", -70);
98 ui->windowFunction->setCurrentIndex(scopeConfig.readEntry("windowFunction", 0));
100 void AudioSpectrum::writeConfig()
102 KSharedConfigPtr config = KGlobal::config();
103 KConfigGroup scopeConfig(config, AbstractScopeWidget::configName());
104 scopeConfig.writeEntry("windowSize", ui->windowSize->currentIndex());
105 scopeConfig.writeEntry("windowFunction", ui->windowFunction->currentIndex());
106 scopeConfig.writeEntry("lockHz", m_aLockHz->isChecked());
107 scopeConfig.writeEntry("dBmax", m_dBmax);
108 scopeConfig.writeEntry("dBmin", m_dBmin);
112 QString AudioSpectrum::widgetName() const { return QString("AudioSpectrum"); }
113 bool AudioSpectrum::isBackgroundDependingOnInput() const { return false; }
114 bool AudioSpectrum::isScopeDependingOnInput() const { return true; }
115 bool AudioSpectrum::isHUDDependingOnInput() const { return false; }
117 QImage AudioSpectrum::renderBackground(uint) { return QImage(); }
119 QImage AudioSpectrum::renderAudioScope(uint, const QVector<int16_t> audioFrame, const int freq, const int num_channels, const int num_samples)
121 if (audioFrame.size() > 63) {
122 m_freqMax = freq / 2;
124 QTime start = QTime::currentTime();
126 bool customCfg = false;
127 kiss_fftr_cfg myCfg = m_cfg;
128 int fftWindow = ui->windowSize->itemData(ui->windowSize->currentIndex()).toInt();
129 if (fftWindow > num_samples) {
130 fftWindow = num_samples;
133 if ((fftWindow & 1) == 1) {
138 myCfg = kiss_fftr_alloc(fftWindow, 0,0,0);
141 float data[fftWindow];
142 float freqSpectrum[fftWindow/2];
145 for (int i = 0; i < fftWindow; i++) {
146 if (audioFrame.data()[i*num_channels] > maxSig) {
147 maxSig = audioFrame.data()[i*num_channels];
151 // Prepare frequency space vector. The resulting FFT vector is only half as long.
152 kiss_fft_cpx freqData[fftWindow/2];
156 // Copy the first channel's audio into a vector for the FFT display
157 // (only one channel handled at the moment)
158 if (num_samples < fftWindow) {
159 std::fill(&data[num_samples], &data[fftWindow-1], 0);
162 FFTTools::WindowType windowType = (FFTTools::WindowType) ui->windowFunction->itemData(ui->windowFunction->currentIndex()).toInt();
163 QVector<float> window;
164 float windowScaleFactor = 1;
165 if (windowType != FFTTools::Window_Rect) {
166 window = FFTTools::window(windowType, fftWindow, 0);
167 windowScaleFactor = 1.0/window[fftWindow];
168 qDebug() << "Using a window scaling factor of " << windowScaleFactor;
171 // Normalize signals to [0,1] to get correct dB values later on
172 for (int i = 0; i < num_samples && i < fftWindow; i++) {
173 if (windowType != FFTTools::Window_Rect) {
174 data[i] = (float) audioFrame.data()[i*num_channels] / 32767.0f * window[i];
176 data[i] = (float) audioFrame.data()[i*num_channels] / 32767.0f;
180 // Calculate the Fast Fourier Transform for the input data
181 kiss_fftr(myCfg, data, freqData);
184 // Logarithmic scale: 20 * log ( 2 * magnitude / N ) with magnitude = sqrt(r² + i²)
185 // with N = FFT size (after FFT, 1/2 window size)
186 for (int i = 0; i < fftWindow/2; i++) {
187 // Logarithmic scale: 20 * log ( 2 * magnitude / N ) with magnitude = sqrt(r² + i²)
188 // with N = FFT size (after FFT, 1/2 window size)
189 freqSpectrum[i] = 20*log(pow(pow(fabs(freqData[i].r * windowScaleFactor),2) + pow(fabs(freqData[i].i * windowScaleFactor),2), .5)/((float)fftWindow/2.0f))/log(10);;
195 QImage spectrum(m_scopeRect.size(), QImage::Format_ARGB32);
196 spectrum.fill(qRgba(0,0,0,0));
197 uint w = m_innerScopeRect.width();
198 uint h = m_innerScopeRect.height();
203 for (uint i = 0; i < w; i++) {
205 // i: Pixel coordinate
206 // x: Frequency array index (float!) corresponding to the pixel
209 x = i/((float) w) * fftWindow/2;
212 // Use linear interpolation in order to get smoother display
213 if (i == 0 || i == w-1) {
214 val = freqSpectrum[i];
217 if (freqSpectrum[xi] > freqSpectrum[xi+1]
219 // This is a hack to preserve peaks.
220 // Consider f = {0, 100, 0}
222 // Then x is 50 both times, and the 100 peak is lost.
223 // Get it back here for the first x after the peak.
224 val = freqSpectrum[xi];
226 val = (xi+1 - x) * freqSpectrum[xi]
227 + (x - xi) * freqSpectrum[xi+1];
231 // freqSpectrum values range from 0 to -inf as they are relative dB values.
232 for (uint y = 0; y < h*(1 - (val - m_dBmax)/(m_dBmin-m_dBmax)) && y < h; y++) {
233 spectrum.setPixel(i, h-y-1, qRgba(225, 182, 255, 255));
239 emit signalScopeRenderingFinished(start.elapsed(), 1);
241 #ifdef DEBUG_AUDIOSPEC
242 if (!fileWritten || true) {
244 mFile.open("/tmp/freq.m");
246 qDebug() << "Opening file failed.";
250 for (int sample = 0; sample < 256; sample++) {
251 mFile << data[sample] << " ";
255 mFile << "freq = [ ";
256 for (int sample = 0; sample < 256; sample++) {
257 mFile << freqData[sample].r << "+" << freqData[sample].i << "*i ";
263 qDebug() << "File written.";
266 qDebug() << "File already written.";
276 emit signalScopeRenderingFinished(0, 1);
280 QImage AudioSpectrum::renderHUD(uint)
282 QTime start = QTime::currentTime();
284 // Minimum distance between two lines
285 const uint minDistY = 30;
286 const uint minDistX = 40;
287 const uint textDist = 5;
288 const uint dbDiff = ceil((float)minDistY/m_innerScopeRect.height() * (m_dBmax-m_dBmin));
290 QImage hud(AbstractAudioScopeWidget::rect().size(), QImage::Format_ARGB32);
291 hud.fill(qRgba(0,0,0,0));
293 QPainter davinci(&hud);
294 davinci.setPen(AbstractAudioScopeWidget::penLight);
297 for (int db = -dbDiff; db > m_dBmin; db -= dbDiff) {
298 y = m_innerScopeRect.height() * ((float)db)/(m_dBmin - m_dBmax);
299 davinci.drawLine(0, y, m_innerScopeRect.width()-1, y);
300 davinci.drawText(m_innerScopeRect.width() + textDist, y + 6, i18n("%1 dB", m_dBmax + db));
304 // TODO more vertical lines in-between
305 const uint hzDiff = ceil( ((float)minDistX)/m_innerScopeRect.width() * m_freqMax / 1000 ) * 1000;
307 for (uint hz = hzDiff; hz < m_freqMax; hz += hzDiff) {
308 x = m_innerScopeRect.width() * ((float)hz)/m_freqMax;
309 davinci.drawLine(x, 0, x, m_innerScopeRect.height()+4);
310 davinci.drawText(x-4, m_innerScopeRect.height() + 20, QVariant(hz/1000).toString());
312 davinci.drawText(m_innerScopeRect.width(), m_innerScopeRect.height() + 20, "[kHz]");
315 emit signalHUDRenderingFinished(start.elapsed(), 1);
319 QRect AudioSpectrum::scopeRect() {
320 m_innerScopeRect = QRect(
323 ui->verticalSpacer->geometry().top() // Top
325 ui->verticalSpacer->geometry().right()-70,
326 ui->verticalSpacer->geometry().bottom()-40
330 m_innerScopeRect.topLeft(),
331 AbstractAudioScopeWidget::rect().bottomRight()
337 void AudioSpectrum::slotUpdateCfg()
340 m_cfg = kiss_fftr_alloc(ui->windowSize->itemData(ui->windowSize->currentIndex()).toInt(), 0,0,0);
346 void AudioSpectrum::mouseMoveEvent(QMouseEvent *event)
348 QPoint movement = event->pos()-m_rescaleStartPoint;
350 if (m_rescaleActive) {
351 if (m_rescalePropertiesLocked) {
352 // Direction is known, now adjust parameters
354 // Reset the starting point to make the next moveEvent relative to the current one
355 m_rescaleStartPoint = event->pos();
358 if (!m_rescaleFirstRescaleDone) {
359 // We have just learned the desired direction; Normalize the movement to one pixel
360 // to avoid a jump by m_rescaleMinDist
362 if (movement.x() != 0) {
363 movement.setX(movement.x() / abs(movement.x()));
365 if (movement.y() != 0) {
366 movement.setY(movement.y() / abs(movement.y()));
369 m_rescaleFirstRescaleDone = true;
372 if (m_rescaleClockDirection == AudioSpectrum::North) {
373 // Nort-South direction: Adjust the dB scale
375 if ((m_rescaleModifiers & Qt::ShiftModifier) == 0) {
377 // By default adjust the min dB value
378 m_dBmin += movement.y();
382 // Adjust max dB value if Shift is pressed.
383 m_dBmax += movement.y();
387 // Ensure the dB values lie in [-100, 0] (or rather [MIN_DB_VALUE, 0])
388 // 0 is the upper bound, everything below -70 dB is most likely noise
392 if (m_dBmin < MIN_DB_VALUE) {
393 m_dBmin = MIN_DB_VALUE;
395 // Ensure there is at least 6 dB between the minimum and the maximum value;
396 // lower values hardly make sense
397 if (m_dBmax - m_dBmin < 6) {
398 if ((m_rescaleModifiers & Qt::ShiftModifier) == 0) {
399 // min was adjusted; Try to adjust the max value to maintain the
400 // minimum dB difference of 6 dB
401 m_dBmax = m_dBmin + 6;
407 // max was adjusted, adjust min
408 m_dBmin = m_dBmax - 6;
409 if (m_dBmin < MIN_DB_VALUE) {
410 m_dBmin = MIN_DB_VALUE;
411 m_dBmax = MIN_DB_VALUE+6;
423 // Detect the movement direction here.
424 // This algorithm relies on the aspect ratio of dy/dx (size and signum).
425 if (movement.manhattanLength() > m_rescaleMinDist) {
426 float diff = ((float) movement.y())/movement.x();
428 if (abs(diff) > m_rescaleVerticalThreshold || movement.x() == 0) {
429 m_rescaleClockDirection = AudioSpectrum::North;
430 } else if (abs(diff) < 1/m_rescaleVerticalThreshold) {
431 m_rescaleClockDirection = AudioSpectrum::East;
432 } else if (diff < 0) {
433 m_rescaleClockDirection = AudioSpectrum::Northeast;
435 m_rescaleClockDirection = AudioSpectrum::Southeast;
437 #ifdef DEBUG_AUDIOSPEC
438 qDebug() << "Diff is " << diff << "; chose " << directions[m_rescaleClockDirection] << " as direction";
440 m_rescalePropertiesLocked = true;
444 AbstractAudioScopeWidget::mouseMoveEvent(event);
448 void AudioSpectrum::mousePressEvent(QMouseEvent *event)
450 if (event->button() == Qt::LeftButton) {
451 // Rescaling mode starts
452 m_rescaleActive = true;
453 m_rescalePropertiesLocked = false;
454 m_rescaleFirstRescaleDone = false;
455 m_rescaleStartPoint = event->pos();
456 m_rescaleModifiers = event->modifiers();
459 AbstractAudioScopeWidget::mousePressEvent(event);
463 void AudioSpectrum::mouseReleaseEvent(QMouseEvent *event)
465 m_rescaleActive = false;
466 m_rescalePropertiesLocked = false;
468 AbstractAudioScopeWidget::mouseReleaseEvent(event);
472 #ifdef DEBUG_AUDIOSPEC
473 #undef DEBUG_AUDIOSPEC