* (at your option) any later version. *
***************************************************************************/
+
+
#include "audiospectrum.h"
+#include "ffttools.h"
#include "tools/kiss_fftr.h"
#include <QMenu>
#include <QMouseEvent>
#include <iostream>
-//#include <fstream>
-//bool fileWritten = false;
+// Enables debugging, like writing a GNU Octave .m file to /tmp
+//#define DEBUG_AUDIOSPEC
+#ifdef DEBUG_AUDIOSPEC
+#include <fstream>
+#include <QDebug>
+bool fileWritten = false;
+#endif
+
+#define MIN_DB_VALUE -120
+#define MAX_FREQ_VALUE 96000
+#define MIN_FREQ_VALUE 1000
const QString AudioSpectrum::directions[] = {"North", "Northeast", "East", "Southeast"};
AudioSpectrum::AudioSpectrum(QWidget *parent) :
AbstractAudioScopeWidget(false, parent),
+ m_fftCfgs(),
+ m_windowFunctions(),
+ m_freqMax(10000),
+ m_customFreq(false),
m_rescaleMinDist(8),
m_rescaleVerticalThreshold(2.0f),
m_rescaleActive(false),
ui = new Ui::AudioSpectrum_UI;
ui->setupUi(this);
- m_distance = QSize(65, 30);
- m_freqMax = 10000;
-
-
- m_aLin = new QAction(i18n("Linear scale"), this);
- m_aLin->setCheckable(true);
- m_aLog = new QAction(i18n("Logarithmic scale"), this);
- m_aLog->setCheckable(true);
- m_agScale = new QActionGroup(this);
- m_agScale->addAction(m_aLin);
- m_agScale->addAction(m_aLog);
+ m_aResetHz = new QAction(i18n("Reset maximum frequency to sampling rate"), this);
- m_aLockHz = new QAction(i18n("Lock maximum frequency"), this);
- m_aLockHz->setCheckable(true);
- m_aLockHz->setEnabled(false);
-
-// m_menu->addSeparator()->setText(i18n("Scale"));
-// m_menu->addAction(m_aLin);
-// m_menu->addAction(m_aLog);
m_menu->addSeparator();
- m_menu->addAction(m_aLockHz);
+ m_menu->addAction(m_aResetHz);
+ m_menu->removeAction(m_aRealtime);
ui->windowSize->addItem("256", QVariant(256));
ui->windowSize->addItem("1024", QVariant(1024));
ui->windowSize->addItem("2048", QVariant(2048));
- m_cfg = kiss_fftr_alloc(ui->windowSize->itemData(ui->windowSize->currentIndex()).toInt(), 0,0,0);
+ ui->windowFunction->addItem(i18n("Rectangular window"), FFTTools::Window_Rect);
+ ui->windowFunction->addItem(i18n("Triangular window"), FFTTools::Window_Triangle);
+ ui->windowFunction->addItem(i18n("Hamming window"), FFTTools::Window_Hamming);
bool b = true;
- b &= connect(ui->windowSize, SIGNAL(currentIndexChanged(int)), this, SLOT(slotUpdateCfg()));
+ b &= connect(m_aResetHz, SIGNAL(triggered()), this, SLOT(slotResetMaxFreq()));
Q_ASSERT(b);
+
+ ui->labelFFTSize->setToolTip(i18n("The maximum window size is limited by the number of samples per frame."));
+ ui->windowSize->setToolTip(i18n("A bigger window improves the accuracy at the cost of computational power."));
+ 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."));
+
AbstractScopeWidget::init();
}
AudioSpectrum::~AudioSpectrum()
{
writeConfig();
- free(m_cfg);
- delete m_agScale;
- delete m_aLin;
- delete m_aLog;
- delete m_aLockHz;
+ QHash<QString, kiss_fftr_cfg>::iterator i;
+ for (i = m_fftCfgs.begin(); i != m_fftCfgs.end(); i++) {
+ free(*i);
+ }
+ delete m_aResetHz;
}
void AudioSpectrum::readConfig()
KSharedConfigPtr config = KGlobal::config();
KConfigGroup scopeConfig(config, AbstractScopeWidget::configName());
- QString scale = scopeConfig.readEntry("scale");
- if (scale == "lin") {
- m_aLin->setChecked(true);
- } else {
- m_aLog->setChecked(true);
- }
- m_aLockHz->setChecked(scopeConfig.readEntry("lockHz", false));
+
ui->windowSize->setCurrentIndex(scopeConfig.readEntry("windowSize", 0));
+ ui->windowFunction->setCurrentIndex(scopeConfig.readEntry("windowFunction", 0));
m_dBmax = scopeConfig.readEntry("dBmax", 0);
m_dBmin = scopeConfig.readEntry("dBmin", -70);
+ m_freqMax = scopeConfig.readEntry("freqMax", 0);
+ if (m_freqMax == 0) {
+ m_customFreq = false;
+ m_freqMax = 10000;
+ } else {
+ m_customFreq = true;
+ }
}
void AudioSpectrum::writeConfig()
{
KSharedConfigPtr config = KGlobal::config();
KConfigGroup scopeConfig(config, AbstractScopeWidget::configName());
- QString scale;
- if (m_aLin->isChecked()) {
- scale = "lin";
- } else {
- scale = "log";
- }
- scopeConfig.writeEntry("scale", scale);
+
scopeConfig.writeEntry("windowSize", ui->windowSize->currentIndex());
- scopeConfig.writeEntry("lockHz", m_aLockHz->isChecked());
+ scopeConfig.writeEntry("windowFunction", ui->windowFunction->currentIndex());
scopeConfig.writeEntry("dBmax", m_dBmax);
scopeConfig.writeEntry("dBmin", m_dBmin);
+ if (m_customFreq) {
+ scopeConfig.writeEntry("freqMax", m_freqMax);
+ } else {
+ scopeConfig.writeEntry("freqMax", 0);
+ }
+
scopeConfig.sync();
}
QImage AudioSpectrum::renderAudioScope(uint, const QVector<int16_t> audioFrame, const int freq, const int num_channels, const int num_samples)
{
if (audioFrame.size() > 63) {
- m_freqMax = freq / 2;
+ if (!m_customFreq) {
+ m_freqMax = freq / 2;
+ }
QTime start = QTime::currentTime();
- bool customCfg = false;
- kiss_fftr_cfg myCfg = m_cfg;
+
+ // Determine the window size to use. It should be
+ // * not bigger than the number of samples actually available
+ // * divisible by 2
int fftWindow = ui->windowSize->itemData(ui->windowSize->currentIndex()).toInt();
if (fftWindow > num_samples) {
fftWindow = num_samples;
- customCfg = true;
}
if ((fftWindow & 1) == 1) {
fftWindow--;
- customCfg = true;
}
- if (customCfg) {
+
+ // Show the window size used, for information
+ ui->labelFFTSizeNumber->setText(QVariant(fftWindow).toString());
+
+ // Get the kiss_fft configuration from the config cache
+ // or build a new configuration if the requested one is not available.
+ kiss_fftr_cfg myCfg;
+ const QString signature = cfgSignature(fftWindow);
+ if (m_fftCfgs.contains(signature)) {
+#ifdef DEBUG_AUDIOSPEC
+ qDebug() << "Re-using FFT configuration with size " << fftWindow;
+#endif
+ myCfg = m_fftCfgs.value(signature);
+ } else {
+#ifdef DEBUG_AUDIOSPEC
+ qDebug() << "Creating FFT configuration with size " << fftWindow;
+#endif
myCfg = kiss_fftr_alloc(fftWindow, 0,0,0);
+ m_fftCfgs.insert(signature, myCfg);
}
float data[fftWindow];
float freqSpectrum[fftWindow/2];
- int16_t maxSig = 0;
- for (int i = 0; i < fftWindow; i++) {
- if (audioFrame.data()[i*num_channels] > maxSig) {
- maxSig = audioFrame.data()[i*num_channels];
- }
- }
-
- // The resulting FFT vector is only half as long
+ // Prepare frequency space vector. The resulting FFT vector is only half as long.
kiss_fft_cpx freqData[fftWindow/2];
+
// Copy the first channel's audio into a vector for the FFT display
// (only one channel handled at the moment)
if (num_samples < fftWindow) {
std::fill(&data[num_samples], &data[fftWindow-1], 0);
}
+
+ FFTTools::WindowType windowType = (FFTTools::WindowType) ui->windowFunction->itemData(ui->windowFunction->currentIndex()).toInt();
+ QVector<float> window;
+ float windowScaleFactor = 1;
+ if (windowType != FFTTools::Window_Rect) {
+ const QString signature = FFTTools::windowSignature(windowType, fftWindow, 0);
+ if (m_windowFunctions.contains(signature)) {
+#ifdef DEBUG_AUDIOSPEC
+ qDebug() << "Re-using window function with signature " << signature;
+#endif
+ window = m_windowFunctions.value(signature);
+ } else {
+#ifdef DEBUG_AUDIOSPEC
+ qDebug() << "Building new window function with signature " << signature;
+#endif
+ window = FFTTools::window(windowType, fftWindow, 0);
+ m_windowFunctions.insert(signature, window);
+ }
+ windowScaleFactor = 1.0/window[fftWindow];
+ }
+
+ // Normalize signals to [0,1] to get correct dB values later on
for (int i = 0; i < num_samples && i < fftWindow; i++) {
- // Normalize signals to [0,1] to get correct dB values later on
- data[i] = (float) audioFrame.data()[i*num_channels] / 32767.0f;
+ if (windowType != FFTTools::Window_Rect) {
+ data[i] = (float) audioFrame.data()[i*num_channels] / 32767.0f * window[i];
+ } else {
+ data[i] = (float) audioFrame.data()[i*num_channels] / 32767.0f;
+ }
}
// Calculate the Fast Fourier Transform for the input data
kiss_fftr(myCfg, data, freqData);
- float val;
- // Get the minimum and the maximum value of the Fourier transformed (for scaling)
+ // Logarithmic scale: 20 * log ( 2 * magnitude / N ) with magnitude = sqrt(r² + i²)
+ // with N = FFT size (after FFT, 1/2 window size)
for (int i = 0; i < fftWindow/2; i++) {
- if (m_aLog->isChecked()) {
- // Logarithmic scale: 20 * log ( 2 * magnitude / N )
- // with N = FFT size (after FFT, 1/2 window size)
- val = 20*log(pow(pow(fabs(freqData[i].r),2) + pow(fabs(freqData[i].i),2), .5)/((float)fftWindow/2.0f))/log(10);
- } else {
- // sqrt(r² + i²)
- val = pow(pow(fabs(freqData[i].r),2) + pow(fabs(freqData[i].i),2), .5);
- }
- freqSpectrum[i] = val;
+ // Logarithmic scale: 20 * log ( 2 * magnitude / N ) with magnitude = sqrt(r² + i²)
+ // with N = FFT size (after FFT, 1/2 window size)
+ 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);;
}
// Draw the spectrum
- QImage spectrum(scopeRect().size(), QImage::Format_ARGB32);
+ QImage spectrum(m_scopeRect.size(), QImage::Format_ARGB32);
spectrum.fill(qRgba(0,0,0,0));
- uint w = scopeRect().size().width();
- uint h = scopeRect().size().height();
+ const uint w = m_innerScopeRect.width();
+ const uint h = m_innerScopeRect.height();
+ const uint leftDist = m_innerScopeRect.left() - m_scopeRect.left();
+ const uint topDist = m_innerScopeRect.top() - m_scopeRect.top();
+ float f;
float x;
+ float x_prev = 0;
+ float val;
+ int xi;
for (uint i = 0; i < w; i++) {
- x = i/((float) w) * fftWindow/2;
+ // i: Pixel coordinate
+ // f: Target frequency
+ // x: Frequency array index (float!) corresponding to the pixel
+ // xi: floor(x)
+
+ f = i/((float) w-1.0) * m_freqMax;
+ x = 2*f/freq * (fftWindow/2 - 1);
+ xi = (int) floor(x);
+
+ if (x >= fftWindow/2) {
+ break;
+ }
// Use linear interpolation in order to get smoother display
- if (i == 0 || i == w-1) {
- val = freqSpectrum[i];
+ if (i == 0 || xi == fftWindow/2-1) {
+ // ... except if we are at the left or right border of the display or the spectrum
+ val = freqSpectrum[xi];
} else {
- // Use floor(x)+1 instead of ceil(x) as floor(x) == ceil(x) is possible.
- val = (floor(x)+1 - x)*freqSpectrum[(int) floor(x)] + (x-floor(x))*freqSpectrum[(int) floor(x)+1];
+
+ if (freqSpectrum[xi] > freqSpectrum[xi+1]
+ && x_prev < xi) {
+ // This is a hack to preserve peaks.
+ // Consider f = {0, 100, 0}
+ // x = {0.5, 1.5}
+ // Then x is 50 both times, and the 100 peak is lost.
+ // Get it back here for the first x after the peak.
+ val = freqSpectrum[xi];
+ } else {
+ val = (xi+1 - x) * freqSpectrum[xi]
+ + (x - xi) * freqSpectrum[xi+1];
+ }
}
// freqSpectrum values range from 0 to -inf as they are relative dB values.
for (uint y = 0; y < h*(1 - (val - m_dBmax)/(m_dBmin-m_dBmax)) && y < h; y++) {
- spectrum.setPixel(i, h-y-1, qRgba(225, 182, 255, 255));
+ spectrum.setPixel(leftDist + i, topDist + h-y-1, qRgba(225, 182, 255, 255));
}
+
+ x_prev = x;
}
emit signalScopeRenderingFinished(start.elapsed(), 1);
- /*
+#ifdef DEBUG_AUDIOSPEC
if (!fileWritten || true) {
std::ofstream mFile;
mFile.open("/tmp/freq.m");
} else {
qDebug() << "File already written.";
}
- //*/
-
- if (customCfg) {
- free(myCfg);
- }
+#endif
return spectrum;
} else {
{
QTime start = QTime::currentTime();
- const QRect rect = scopeRect();
// Minimum distance between two lines
const uint minDistY = 30;
const uint minDistX = 40;
- const uint textDist = 5;
- const uint dbDiff = ceil((float)minDistY/rect.height() * (m_dBmax-m_dBmin));
+ const uint textDistX = 10;
+ const uint textDistY = 25;
+ const uint topDist = m_innerScopeRect.top() - m_scopeRect.top();
+ const uint leftDist = m_innerScopeRect.left() - m_scopeRect.left();
+ const uint dbDiff = ceil((float)minDistY/m_innerScopeRect.height() * (m_dBmax-m_dBmin));
- QImage hud(AbstractAudioScopeWidget::rect().size(), QImage::Format_ARGB32);
+ QImage hud(m_scopeRect.size(), QImage::Format_ARGB32);
hud.fill(qRgba(0,0,0,0));
QPainter davinci(&hud);
- davinci.setPen(AbstractAudioScopeWidget::penLight);
+ davinci.setPen(AbstractScopeWidget::penLight);
int y;
for (int db = -dbDiff; db > m_dBmin; db -= dbDiff) {
- y = rect.height() * ((float)db)/(m_dBmin - m_dBmax);
- davinci.drawLine(0, y, rect.width()-1, y);
- davinci.drawText(rect.width() + textDist, y + 8, i18n("%1 dB", m_dBmax + db));
+ y = topDist + m_innerScopeRect.height() * ((float)db)/(m_dBmin - m_dBmax);
+ if (y-topDist > m_innerScopeRect.height()-minDistY+10) {
+ // Abort here, there is still a line left for min dB to paint which needs some room.
+ break;
+ }
+ davinci.drawLine(leftDist, y, leftDist + m_innerScopeRect.width()-1, y);
+ davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, y + 6, i18n("%1 dB", m_dBmax + db));
}
+ davinci.drawLine(leftDist, topDist, leftDist + m_innerScopeRect.width()-1, topDist);
+ davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, topDist+6, i18n("%1 dB", m_dBmax));
+ davinci.drawLine(leftDist, topDist+m_innerScopeRect.height()-1, leftDist + m_innerScopeRect.width()-1, topDist+m_innerScopeRect.height()-1);
+ davinci.drawText(leftDist + m_innerScopeRect.width() + textDistX, topDist+m_innerScopeRect.height()+6, i18n("%1 dB", m_dBmin));
+
+ const uint hzDiff = ceil( ((float)minDistX)/m_innerScopeRect.width() * m_freqMax / 1000 ) * 1000;
+ int x = 0;
+ const int rightBorder = leftDist + m_innerScopeRect.width()-1;
+ y = topDist + m_innerScopeRect.height() + textDistY;
+ for (uint hz = 0; x <= rightBorder; hz += hzDiff) {
+ davinci.setPen(AbstractScopeWidget::penLight);
+ x = leftDist + m_innerScopeRect.width() * ((float)hz)/m_freqMax;
+
+ if (x <= rightBorder) {
+ davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
+ }
+ if (hz < m_freqMax && x+textDistY < leftDist + m_innerScopeRect.width()) {
+ davinci.drawText(x-4, y, QVariant(hz/1000).toString());
+ } else {
+ x = leftDist + m_innerScopeRect.width();
+ davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()+6);
+ davinci.drawText(x-10, y, i18n("%1 kHz").arg((double)m_freqMax/1000, 0, 'f', 1));
+ }
-
- const uint hzDiff = ceil( ((float)minDistX)/rect.width() * m_freqMax / 1000 ) * 1000;
- int x;
- for (uint hz = hzDiff; hz < m_freqMax; hz += hzDiff) {
- x = rect.width() * ((float)hz)/m_freqMax;
- davinci.drawLine(x, 0, x, rect.height()+4);
- davinci.drawText(x-4, rect.height() + 20, QVariant(hz/1000).toString());
+ if (hz > 0) {
+ // Draw finer lines between the main lines
+ davinci.setPen(AbstractScopeWidget::penLightDots);
+ for (uint dHz = 3; dHz > 0; dHz--) {
+ x = leftDist + m_innerScopeRect.width() * ((float)hz - dHz * hzDiff/4.0f)/m_freqMax;
+ if (x > rightBorder) {
+ break;
+ }
+ davinci.drawLine(x, topDist, x, topDist + m_innerScopeRect.height()-1);
+ }
+ }
}
- davinci.drawText(rect.width(), rect.height() + 20, "[kHz]");
emit signalHUDRenderingFinished(start.elapsed(), 1);
}
QRect AudioSpectrum::scopeRect() {
- return QRect(QPoint(0, 0), AbstractAudioScopeWidget::rect().size() - m_distance);
+ m_scopeRect = QRect(
+ QPoint(
+ 10, // Left
+ ui->verticalSpacer->geometry().top()+6 // Top
+ ),
+ AbstractAudioScopeWidget::rect().bottomRight()
+ );
+ m_innerScopeRect = QRect(
+ QPoint(
+ m_scopeRect.left()+6, // Left
+ m_scopeRect.top()+6 // Top
+ ), QPoint(
+ ui->verticalSpacer->geometry().right()-70,
+ ui->verticalSpacer->geometry().bottom()-40
+ )
+ );
+ return m_scopeRect;
}
-
-void AudioSpectrum::slotUpdateCfg()
+void AudioSpectrum::slotResetMaxFreq()
{
- free(m_cfg);
- m_cfg = kiss_fftr_alloc(ui->windowSize->itemData(ui->windowSize->currentIndex()).toInt(), 0,0,0);
+ m_customFreq = false;
+ forceUpdateHUD();
+ forceUpdateScope();
}
}
- // Ensure the dB values lie in [-100, 0]
+ // Ensure the dB values lie in [-100, 0] (or rather [MIN_DB_VALUE, 0])
// 0 is the upper bound, everything below -70 dB is most likely noise
if (m_dBmax > 0) {
m_dBmax = 0;
}
- if (m_dBmin < -100) {
- m_dBmin = -100;
+ if (m_dBmin < MIN_DB_VALUE) {
+ m_dBmin = MIN_DB_VALUE;
}
// Ensure there is at least 6 dB between the minimum and the maximum value;
// lower values hardly make sense
} else {
// max was adjusted, adjust min
m_dBmin = m_dBmax - 6;
- if (m_dBmin < -100) {
- m_dBmin = -100;
- m_dBmax = -100+6;
+ if (m_dBmin < MIN_DB_VALUE) {
+ m_dBmin = MIN_DB_VALUE;
+ m_dBmax = MIN_DB_VALUE+6;
}
}
}
forceUpdateHUD();
forceUpdateScope();
+ } else if (m_rescaleClockDirection == AudioSpectrum::East) {
+ // East-West direction: Adjust the maximum frequency
+ m_freqMax -= 100*movement.x();
+ if (m_freqMax < MIN_FREQ_VALUE) {
+ m_freqMax = MIN_FREQ_VALUE;
+ }
+ if (m_freqMax > MAX_FREQ_VALUE) {
+ m_freqMax = MAX_FREQ_VALUE;
+ }
+ m_customFreq = true;
+
+ forceUpdateHUD();
+ forceUpdateScope();
}
} else {
m_rescaleClockDirection = AudioSpectrum::Southeast;
}
-// qDebug() << "Diff is " << diff << "; chose " << directions[m_rescaleClockDirection] << " as direction";
+#ifdef DEBUG_AUDIOSPEC
+ qDebug() << "Diff is " << diff << "; chose " << directions[m_rescaleClockDirection] << " as direction";
+#endif
m_rescalePropertiesLocked = true;
}
}
AbstractAudioScopeWidget::mouseReleaseEvent(event);
}
+
+const QString AudioSpectrum::cfgSignature(const int size)
+{
+ return QString("s%1").arg(size);
+}
+
+
+#ifdef DEBUG_AUDIOSPEC
+#undef DEBUG_AUDIOSPEC
+#endif
+
+#undef MIN_DB_VALUE
+#undef MAX_FREQ_VALUE
+#undef MIN_FREQ_VALUE