File: NoiseDialog.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (389 lines) | stat: -rw-r--r-- 12,541 bytes parent folder | download
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
/***************************************************************************
        NoiseDialog.cpp  -  dialog for the "noise" plugin
                             -------------------
    begin                : Sat Sep 28 2013
    copyright            : (C) 2013 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 <QColor>
#include <QPainter>
#include <QPushButton>
#include <QRadioButton>
#include <QSlider>
#include <QSpinBox>

#include <KHelpClient>
#include <KLocalizedString>

#include "libkwave/String.h"
#include "libkwave/Utils.h"

#include "libgui/Colors.h"
#include "libgui/CurveWidget.h"
#include "libgui/ImageView.h"
#include "libgui/InvertableSpinBox.h"
#include "libgui/OverViewCache.h"
#include "libgui/ScaleWidget.h"

#include "NoiseDialog.h"

//***************************************************************************
Kwave::NoiseDialog::NoiseDialog(QWidget *parent,
                                  Kwave::OverViewCache *overview_cache)
    :QDialog(parent), Kwave::PluginSetupDialog(), Ui::NoiseDlg(),
     m_noise(0.1), m_mode(MODE_DECIBEL),
     m_enable_updates(true), m_overview_cache(overview_cache)
{
    setupUi(this);
    setModal(true);

    // process changed in mode selection
    connect(rbPercentage, SIGNAL(toggled(bool)),
            this, SLOT(modeChanged(bool)));
    connect(rbLogarithmic, SIGNAL(toggled(bool)),
            this, SLOT(modeChanged(bool)));

    // changes in the slider or spinbox
    connect(slider, SIGNAL(valueChanged(int)),
            this, SLOT(sliderChanged(int)));
    connect(spinbox, SIGNAL(valueChanged(int)),
            this, SLOT(spinboxChanged(int)));
    // click to the "Listen" button
    connect(btListen, SIGNAL(toggled(bool)),
            this, SLOT(listenToggled(bool)));

    // force activation of the layout
    layout()->activate();

    // give the preview image a odd height, for better symmetry
    int h = preview->height();
    if (~h & 1) h++;
    preview->setFixedHeight(h);

    // expand the "Listen" button to it's maximum width
    listenToggled(true);
    if (btListen->width() > btListen->minimumWidth())
        btListen->setMinimumWidth(btListen->width());
    listenToggled(false);
    if (btListen->width() > btListen->minimumWidth())
        btListen->setMinimumWidth(btListen->width());

    // set the initial size of the dialog
    h     = (sizeHint().height() * 12) / 10;
    int w = (3 * h) / 4;
    if (sizeHint().width() > w) w = sizeHint().width();
    setFixedSize(w, h);

    // set default: 10%
    setMode(m_mode);

    connect(buttonBox_Help->button(QDialogButtonBox::Help), SIGNAL(clicked()),
            this,   SLOT(invokeHelp()));

    // set the focus onto the "OK" button
    buttonBox->button(QDialogButtonBox::Ok)->setFocus();
}

//***************************************************************************
Kwave::NoiseDialog::~NoiseDialog()
{
    // better stop pre-listen now
    listenToggled(false);

    delete m_overview_cache;
    m_overview_cache = nullptr;
}

//***************************************************************************
void Kwave::NoiseDialog::setMode(Mode mode)
{
    double value = m_noise;
    m_mode = mode;
    bool old_enable_updates = m_enable_updates;
    m_enable_updates = false;

    switch (m_mode) {
        case MODE_PERCENT: {
            rbPercentage->setChecked(true);

            slider->setMinimum(1);
            slider->setMaximum(100);
            slider->setPageStep(100);
            slider->setTickInterval(10);
            spinbox->setMinimum(1);
            spinbox->setMaximum(100);
            break;
        }
        case MODE_DECIBEL: {
            rbLogarithmic->setChecked(true);

            slider->setMinimum(-21);
            slider->setMaximum(0);
            slider->setPageStep(6);
            slider->setTickInterval(3);
            spinbox->setMinimum(-21);
            spinbox->setMaximum(0);
            break;
        }
        DEFAULT_IMPOSSIBLE;
    }

    // update the value in the display
    updateDisplay(value);
    m_enable_updates = old_enable_updates;
}

//***************************************************************************
void Kwave::NoiseDialog::modeChanged(bool)
{
    bool old_enable_updates = m_enable_updates;
    m_enable_updates = false;

    if (rbPercentage->isChecked())  setMode(MODE_PERCENT);
    if (rbLogarithmic->isChecked()) setMode(MODE_DECIBEL);

    m_enable_updates = old_enable_updates;
}

//***************************************************************************
void Kwave::NoiseDialog::updateDisplay(double value)
{
    int new_spinbox_value = 0;
    int new_slider_value  = 0;
    bool old_enable_updates = m_enable_updates;
    m_enable_updates = false;

    Kwave::OverViewCache::MinMaxArray m_minmax;

    if (!qFuzzyCompare(m_noise, value)) {

        // take over the new factor
        m_noise = value;

        // update the preview widget
        if (m_overview_cache && preview) {
            int width  = preview->width();
            int height = preview->height();
            QColor color_bg    = Kwave::Colors::Normal.background;
            QColor color_sig   = Kwave::Colors::Normal.interpolated;
            QColor color_noise = Kwave::Colors::Normal.sample;

            // get the min/max information
            int count = m_overview_cache->getMinMax(width, m_minmax);

            QImage image(width, height, QImage::Format_ARGB32_Premultiplied);
            QPainter p;
            p.begin(&image);
            p.fillRect(image.rect(), color_bg);

            // calculate scaling factor and (noise level / 2) in pixel
            const int middle = height >> 1;
            const int noise_2 = Kwave::toInt(middle * m_noise);

            /*
             * mixing of noise goes like this:
             *
             * y[t] = x[t] * (1 - n) + noise(t) * n;
             *
             * and has this effect on min/max:
             *
             * [---noise---]          [---noise---]
             *     [min -------------------max]
             * ^     ^    ^           ^     ^     ^
             * |     |    |           |     |     |
             * y1    y2   y3          y4    y5    y6
             */

            for (int x = 0; x < count; ++x) {
                int y2 = Kwave::toInt((sample2double(m_minmax[x].min) *
                    (1.0 - m_noise)) * middle);
                int y5 = Kwave::toInt((sample2double(m_minmax[x].max) *
                    (1.0 - m_noise)) * middle);
                int y1 = y2 - noise_2;
                int y3 = y2 + noise_2;
                int y4 = y5 - noise_2;
                int y6 = y5 + noise_2;

                if (y4 > y3) {
                    // noise around "min" [y1 ... y3]
                    p.setPen(color_noise);
                    p.drawLine(x, middle - y3, x, middle - y1);

                    // noise around "max" [y4 ... y6]
                    p.drawLine(x, middle - y6, x, middle - y4);

                    // original signal [y3 ... y4 ]
                    p.setPen(color_sig);
                    p.drawLine(x, middle - y4, x, middle - y3);
                } else {
                    // only noise [y1 ... y6]
                    p.setPen(color_noise);
                    p.drawLine(x, middle - y6, x, middle - y1);
                }
            }

            // zero line
            p.setCompositionMode(QPainter::CompositionMode_SourceOver);
            p.setPen(Kwave::Colors::Normal.zero);
            p.drawLine(0, middle, width - 1, middle);

            p.end();

            // update the image view
            preview->setImage(image);
        }

        // emit the noise level change to the plugin
        emit levelChanged(m_noise);
    }

    switch (m_mode) {
        case MODE_PERCENT: {
            // factor 1.0 means 100%
            new_spinbox_value = Kwave::toInt(rint(value * 100.0));
            new_slider_value = new_spinbox_value;
            spinbox->setPrefix(_(""));
            spinbox->setSuffix(_("%"));
            spinbox->setInverse(false);
            break;
        }
        case MODE_DECIBEL: {
            // factor 1.0 means 0dB
            if (!qFuzzyIsNull(value))
                new_slider_value = Kwave::toInt(rint(20.0 * log10(value)));
            else
                new_slider_value = 0;
            new_spinbox_value = new_slider_value;
            if (new_spinbox_value >= 0) {
                spinbox->setPrefix(new_spinbox_value ? _("+") : _("+/- "));
            } else {
                // negative value
                spinbox->setPrefix(_(""));
            }
            spinbox->setSuffix(_(" ") + i18n("dB"));
            spinbox->setInverse(false);
            break;
        }
        DEFAULT_IMPOSSIBLE;
    }

    // update the spinbox
    if (spinbox->value() != new_spinbox_value)
        spinbox->setValue(new_spinbox_value);

    // update the slider, it's inverse => top=maximum, bottom=minimum !
    int sv = slider->maximum() + slider->minimum() - new_slider_value;
    if (slider->value() != sv) slider->setValue(sv);

    m_enable_updates = old_enable_updates;
}

//***************************************************************************
void Kwave::NoiseDialog::sliderChanged(int pos)
{
    if (!m_enable_updates) return;

    int sv = slider->maximum() + slider->minimum() - pos;
    spinboxChanged(sv);
}

//***************************************************************************
void Kwave::NoiseDialog::spinboxChanged(int pos)
{
    if (!m_enable_updates) return;

    double factor = m_noise;

    switch (m_mode) {
        case MODE_PERCENT:
            // percentage
            factor = static_cast<double>(pos) / 100.0;
            break;
        case MODE_DECIBEL:
            // decibel
            factor = pow(10.0, pos / 20.0);
            break;
        DEFAULT_IMPOSSIBLE;
    }

    updateDisplay(factor);
}

//***************************************************************************
QStringList Kwave::NoiseDialog::params()
{
    QStringList list;
    list << QString::number(m_noise);
    list << QString::number(static_cast<int>(m_mode));
    return list;
}

//***************************************************************************
void Kwave::NoiseDialog::setParams(QStringList &params)
{
    // evaluate the parameter list
    double factor = params[0].toDouble();
    factor = qBound<double>(0.0, factor, 1.0);

    switch (params[1].toUInt()) {
        case 0:  m_mode = MODE_PERCENT; break;
        case 1:  m_mode = MODE_DECIBEL; break;
        default: m_mode = MODE_DECIBEL;
    }

    // update mode, using default factor 1.0
    m_noise = 1.0; // works with every mode
    setMode(m_mode);

    // update factor
    m_noise = -1.0 * factor; // just to get an initial refresh (fake change)
    updateDisplay(factor);
}

//***************************************************************************
void Kwave::NoiseDialog::listenToggled(bool listen)
{
    Q_ASSERT(btListen);
    if (!btListen) return;

    if (listen) {
        // start pre-listen mode
        emit startPreListen();
        btListen->setText(i18n("&Stop"));
    } else {
        // stop pre-listen mode
        emit stopPreListen();
        btListen->setText(i18n("&Listen"));
    }
}

//***************************************************************************
void Kwave::NoiseDialog::listenStopped()
{
    if (btListen) btListen->setChecked(false);
}

//***************************************************************************
void Kwave::NoiseDialog::invokeHelp()
{
    KHelpClient::invokeHelp(_("plugin_sect_noise"));
}

//***************************************************************************
//***************************************************************************

#include "moc_NoiseDialog.cpp"