1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
/*************************************************************************
LevelMeter.cpp - multi-track audio level meter
-------------------
original copyright : Copyright 2002 Rik Hemsley (rikkus) <rik@kde.org>
begin : Mon Nov 17 2003
copyright : (C) 2003 by Thomas Eschenbacher
email : Thomas.Eschenbacher@gmx.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "config.h"
#include <math.h>
#include <new>
#include <QApplication>
#include <QBrush>
#include <QColor>
#include <QFont>
#include <QImage>
#include <QPainter>
#include <QPixmap>
#include <QTimer>
#include <KLocalizedString>
#include "libkwave/Utils.h"
#include "LevelMeter.h"
/** number of display updates per second */
static const float UPDATES_PER_SECOND = 8.0f;
/** lowpass frequency used for rising the current (fast) value [Hz] */
static const float F_FAST_RISE = (20.0f);
/** lowpass frequency used for decay of the current (fast) value [Hz] */
static const float F_FAST_DECAY = (0.5f);
/** lowpass frequency used for rising the peak values */
static const float F_PEAK_RISE = (F_FAST_RISE);
/** lowpass frequency used for decay of peak values [Hz] */
static const float F_PEAK_DECAY = (0.005f);
//***************************************************************************
Kwave::LevelMeter::LevelMeter(QWidget *parent)
:QWidget(parent),
m_tracks(0), m_sample_rate(0), m_yf(), m_yp(),
m_fast_queue(), m_peak_queue(),
m_current_fast(), m_current_peak(), m_timer(),
m_color_low(Qt::green),
m_color_normal(Qt::yellow),
m_color_high(Qt::red)
{
setAttribute(Qt::WA_OpaquePaintEvent);
m_timer = new(std::nothrow) QTimer(this);
Q_ASSERT(m_timer);
connect(m_timer, SIGNAL(timeout()),
this, SLOT(timedUpdate()));
}
//***************************************************************************
Kwave::LevelMeter::~LevelMeter()
{
setTracks(0);
}
//***************************************************************************
void Kwave::LevelMeter::paintEvent(QPaintEvent *)
{
drawContents();
}
//***************************************************************************
void Kwave::LevelMeter::resizeEvent(QResizeEvent *)
{
repaint();
}
//***************************************************************************
void Kwave::LevelMeter::setTracks(unsigned int tracks)
{
if (Kwave::toInt(tracks) == m_tracks) return;
m_tracks = tracks;
reset(); // re-create all arrays etc.
}
//***************************************************************************
void Kwave::LevelMeter::setSampleRate(double rate)
{
if (qFuzzyCompare(static_cast<float>(rate), m_sample_rate)) return;
m_sample_rate = static_cast<float>(rate);
}
//***************************************************************************
void Kwave::LevelMeter::updateTrack(unsigned int track,
const Kwave::SampleArray &buffer)
{
Q_ASSERT(Kwave::toInt(track) < m_tracks);
if (Kwave::toInt(track) >= m_tracks) return;
// calculate the number of samples per update (approx)
const unsigned int samples = buffer.size();
const unsigned int samples_per_update = Kwave::toUint(
rintf(ceilf(m_sample_rate / UPDATES_PER_SECOND)));
unsigned int next_fraction = samples_per_update;
const unsigned int queue_depth = ((samples / samples_per_update) + 2);
/* fast update: rise */
float Fg = F_FAST_RISE / m_sample_rate;
float n = 1.0f / tanf(float(M_PI) * Fg);
const float a0_fr = 1.0f / (1.0f + n);
const float b1_fr = (1.0f - n) / (1.0f + n);
/* fast update: decay */
Fg = F_FAST_DECAY / m_sample_rate;
n = 1.0f / tanf(float(M_PI) * Fg);
const float a0_fd = 1.0f / (1.0f + n);
const float b1_fd = (1.0f - n) / (1.0f + n);
/* peak value: rise */
Fg = F_PEAK_RISE / m_sample_rate;
n = 1.0f / tanf(float(M_PI) * Fg);
const float a0_pr = 1.0f / (1.0f + n);
const float b1_pr = (1.0f - n) / (1.0f + n);
/* peak value: decay */
Fg = F_PEAK_DECAY / m_sample_rate;
n = 1.0f / tanf(float(M_PI) * Fg);
const float a0_pd = 1.0f / (1.0f + n);
const float b1_pd = (1.0f - n) / (1.0f + n);
float yf = m_yf[track];
float yp = m_yp[track];
float last_x = yf;
for (unsigned int t = 0; t < samples; ++t) {
float x = fabsf(sample2float(buffer[t])); /* rectifier */
/* fast value */
if (x > yf) yf = (a0_fr * x) + (a0_fr * last_x) - (b1_fr * yf); // rise
yf = (a0_fd * x) + (a0_fd * last_x) - (b1_fd * yf); // decay
/* peak value */
if (x > yp) yp = (a0_pr * x) + (a0_pr * last_x) - (b1_pr * yp); // rise
yp = (a0_pd * x) + (a0_pd * last_x) - (b1_pd * yp); // decay
// remember x[t-1]
last_x = x;
// enqueue new values if limit reached
if ((t > next_fraction) || (t == samples-1)) {
next_fraction += samples_per_update;
// merge the last fractional part to the last normal part
if ((next_fraction + samples_per_update) > samples)
next_fraction = samples-1;
enqueue(track, yf, yp, queue_depth);
}
}
m_yf[track] = yf;
m_yp[track] = yp;
}
//***************************************************************************
void Kwave::LevelMeter::reset()
{
if (m_timer && m_timer->isActive()) m_timer->stop();
m_yf.resize(m_tracks);
m_yf.fill(0.0);
m_fast_queue.resize(m_tracks);
m_current_fast.resize(m_tracks);
m_current_fast.fill(0.0);
m_yp.resize(m_tracks);
m_yp.fill(0.0);
m_peak_queue.resize(m_tracks);
m_current_peak.resize(m_tracks);
m_current_peak.fill(0.0);
}
//***************************************************************************
void Kwave::LevelMeter::enqueue(unsigned int track, float fast, float peak,
unsigned int queue_depth)
{
Q_ASSERT(Kwave::toInt(track) < m_tracks);
Q_ASSERT(m_peak_queue.size() == m_fast_queue.size());
Q_ASSERT(m_fast_queue.size() >= m_tracks);
Q_ASSERT(m_peak_queue.size() >= m_tracks);
if ((Kwave::toInt(track) >= m_tracks) ||
(m_fast_queue.size() < Kwave::toInt(m_tracks)) ||
(m_peak_queue.size() < m_tracks)) return;
Q_ASSERT(m_peak_queue[track].size() == m_fast_queue[track].size());
if (m_peak_queue[track].size() != m_fast_queue[track].size()) return;
// remove old entries
while (m_fast_queue[track].size() > Kwave::toInt(queue_depth)) {
// qDebug("LevelMeter::enqueue(): purging old entry (%u/%u)",
// m_fast_queue.size(), queue_depth);
m_fast_queue[track].dequeue();
m_peak_queue[track].dequeue();
}
// put into the queue
m_fast_queue[track].enqueue(fast);
m_peak_queue[track].enqueue(peak);
// restart the timer if necessary
if (m_timer && !m_timer->isActive()) {
m_timer->setInterval(Kwave::toInt(1000 / UPDATES_PER_SECOND));
m_timer->setSingleShot(false);
m_timer->start();
}
}
//***************************************************************************
bool Kwave::LevelMeter::dequeue(unsigned int track, float &fast, float &peak)
{
Q_ASSERT(m_peak_queue.size() == m_fast_queue.size());
Q_ASSERT(m_fast_queue.size() >= m_tracks);
Q_ASSERT(m_peak_queue.size() >= m_tracks);
if ((Kwave::toInt(track) >= m_tracks) ||
(m_fast_queue.size() < m_tracks) ||
(m_peak_queue.size() < m_tracks)) return false;
Q_ASSERT(m_peak_queue[track].size() == m_fast_queue[track].size());
if (m_peak_queue[track].size() != m_fast_queue[track].size())
return false;
// check if the queues are empty
if (m_fast_queue[track].isEmpty()) return false;
if (m_peak_queue[track].isEmpty()) return false;
// get the values from the front of the queue
fast = m_fast_queue[track].dequeue();
peak = m_peak_queue[track].dequeue();
return true;
}
//***************************************************************************
void Kwave::LevelMeter::timedUpdate()
{
float fast;
float peak;
bool need_update = false;
for (int track=0; track < m_tracks; track++) {
if (dequeue(track, fast, peak)) {
// set the new "current" values
m_current_fast[track] = fast;
m_current_peak[track] = peak;
// remember that we have to update the display
need_update = true;
}
}
// refresh the display if needed
if (need_update) repaint();
}
//***************************************************************************
void Kwave::LevelMeter::drawScale(QPainter &p)
{
// draw the levels in 3dB steps, like -12dB -9dB -6dB -3dB and 0dB
QFontMetrics fm = p.fontMetrics();
QRect rect = fm.boundingRect(i18n("%1 dB", -999));
const int border = 4;
const int w = width() - 2 * border;
const int h = height();
const int tw = rect.width();
const int th = rect.height();
const int y = ((height() - th) / 2);
const int r = 5;
int db = 0;
int right = width();
const QColor textcolor = palette().buttonText().color();
const QBrush brush(palette().window().color());
Q_ASSERT(th);
if (!th) return;
p.setBrush(brush);
while (right > tw + border) {
// find the first position in dB which is not overlapping
// the last output position
QString txt;
int x;
do {
txt = i18n("%1 dB", db);
x = Kwave::toInt(static_cast<double>(w) *
pow(10.0, static_cast<double>(db) / 20.0));
db -= 3; // one step left == -3dB
} while ((x > right) && (x >= tw));
if (x < tw) break;
// calculate the text position
int text_width = fm.boundingRect(txt).width();
x += border;
x -= text_width + 3;
// dim the text background area
p.setOpacity(0.66);
p.setPen(Qt::NoPen);
p.drawRoundedRect(
x - r , y - r,
text_width + 2 * r , th + 2 * r,
(200 * r) / th , (200 * r) / th,
Qt::RelativeSize
);
// draw the text, right/center aligned
p.setOpacity(1.0);
p.setPen(textcolor);
p.drawText(x, 1, text_width, h, Qt::AlignCenter, txt);
// new right border == one character left from last one
right = x - th;
}
}
//***************************************************************************
/*
Original idea:
Copyright 2002 Rik Hemsley (rikkus) <rik@kde.org>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
*/
void Kwave::LevelMeter::drawContents()
{
QPainter p;
int track;
Q_ASSERT(width() > 0);
Q_ASSERT(height() > 0);
p.begin(this);
// fill the background
p.fillRect(rect(), palette().window().color());
const unsigned int border = 4;
const unsigned int cell = 3;
const unsigned int w = width() - (border * 2) - (cell * 2);
const unsigned int h = (height() - border) / (m_tracks ? m_tracks : 1);
const unsigned int w_low = Kwave::toUint(w * 0.7); // -3 dB
const unsigned int w_high = Kwave::toUint(w * 0.85); // -1.5dB
for (track = 0; track < m_tracks; ++track) {
// show a bar up to the "fast" value
const unsigned int fast = Kwave::toUint(m_current_fast[track] *
static_cast<float>(w));
for (unsigned int i = 0; i < w; i += cell * 2) {
QColor color;
if (i >= w_high)
color = m_color_high;
else if (i >= w_low)
color = m_color_normal;
else
color = m_color_low;
p.fillRect(
border + cell + i,
border + (track * h),
cell, h-border,
(i > fast) ? color.darker() : color
);
}
// draw the peak value
unsigned int peak = Kwave::toUint(
m_current_peak[track] * static_cast<float>(w));
QColor peak_color;
if (peak >= w_high)
peak_color = m_color_high;
else if (peak >= w_low)
peak_color = m_color_normal;
else
peak_color = m_color_low;
p.fillRect(
border + cell + peak,
border + (track * h),
cell, h - border,
peak_color.lighter()
);
}
// draw the scale / dB numbers
drawScale(p);
p.end();
}
//***************************************************************************
//***************************************************************************
#include "moc_LevelMeter.cpp"
|