File: PitchShiftDialog.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 (357 lines) | stat: -rw-r--r-- 10,735 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
/***************************************************************************
   PitchShiftDialog.cpp  -  dialog for the "pitch_shift" plugin
                             -------------------
    begin                : Sun Mar 23 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 <QPushButton>
#include <QRadioButton>
#include <QSlider>
#include <QSpinBox>

#include <KHelpClient>
#include <KLocalizedString>

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

#include "libgui/InvertableSpinBox.h"

#include "PitchShiftDialog.h"

//***************************************************************************
Kwave::PitchShiftDialog::PitchShiftDialog(QWidget *parent)
    :QDialog(parent), Ui::PitchShiftDlg(), Kwave::PluginSetupDialog(),
     m_speed(1.0), m_frequency(5.0), m_mode(MODE_FACTOR),
     m_enable_updates(true)
{
    setupUi(this);
    setModal(true);

    setMode(m_mode);

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

    // changes in the speed slider or spinbox
    connect(slSpeed, SIGNAL(valueChanged(int)),
            this, SLOT(sliderChanged(int)));
    connect(sbSpeed, SIGNAL(valueChanged(int)),
            this, SLOT(spinboxChanged(int)));

    // changes in the frequency spinbox
    connect(sbFrequency, SIGNAL(valueChanged(int)),
            this, SLOT(frequencyChanged(int)));

    // click to the "Listen" button
    connect(btListen, SIGNAL(toggled(bool)),
            this, SLOT(listenToggled(bool)));

    // 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
    setFixedHeight(sizeHint().height());
    int w = (height() * 3) / 5;
    if (width() < w) resize(w, height());

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

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

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

//***************************************************************************
void Kwave::PitchShiftDialog::setMode(Mode mode)
{
    double speed = m_speed;
    m_mode = mode;
    bool old_enable_updates = m_enable_updates;
    m_enable_updates = false;

    switch (m_mode) {
        case MODE_FACTOR: {
            rbFactor->setChecked(true);

            slSpeed->setMinimum(-9);
            slSpeed->setMaximum(+4);
            slSpeed->setPageStep(1);
            slSpeed->setTickInterval(1);

            sbSpeed->setMinimum(-10);
            sbSpeed->setMaximum(+10);
            sbSpeed->setSingleStep(1);
            break;
        }
        case MODE_PERCENT: {
            rbPercentage->setChecked(true);

            slSpeed->setMinimum(1);
            slSpeed->setMaximum(400);
            slSpeed->setPageStep(10);
            slSpeed->setTickInterval(50);

            sbSpeed->setMinimum(1);
            sbSpeed->setMaximum(400);
            sbSpeed->setSingleStep(1);
            break;
        }
        DEFAULT_IMPOSSIBLE;
    }

    // update the spped value in the display
    m_speed = speed;
    updateSpeed(m_speed);
    m_enable_updates = old_enable_updates;
}

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

    if (rbFactor->isChecked())      setMode(MODE_FACTOR);
    if (rbPercentage->isChecked())  setMode(MODE_PERCENT);

    m_enable_updates = old_enable_updates;
}

//***************************************************************************
void Kwave::PitchShiftDialog::updateSpeed(double speed)
{
    int new_spinbox_value = 0;
    int new_slider_value  = 0;
    bool old_enable_updates = m_enable_updates;
    m_enable_updates = false;

    switch (m_mode) {
        case MODE_FACTOR: {
            // -1 => /2
            //  0 => x1
            // +1 => x2
            if (Kwave::toInt(m_speed) >= 1) {
                // greater or equal to one -> multiply
                int new_value = Kwave::toInt(speed);
                sbSpeed->setPrefix(_("x "));
                sbSpeed->setSuffix(_(""));
                sbSpeed->setInverse(false);

                new_spinbox_value = new_value;
                new_slider_value = new_value-1;
            } else {
                // less than one -> divide
                int new_value = Kwave::toInt(-1.0 / speed);

                sbSpeed->setPrefix(_("1/"));
                sbSpeed->setSuffix(_(""));
                sbSpeed->setInverse(true);

                new_spinbox_value = -1*new_value;
                new_slider_value  = (new_value+1);
            }

            m_enable_updates = old_enable_updates;
            break;
        }
        case MODE_PERCENT: {
            // factor 1.0 means 100%
            new_spinbox_value = Kwave::toInt(rint(speed * 100.0));
            new_slider_value = new_spinbox_value;
            sbSpeed->setPrefix(_(""));
            sbSpeed->setSuffix(_("%"));
            sbSpeed->setInverse(false);
            break;
        }
        DEFAULT_IMPOSSIBLE;
    }

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

    // update the slider
    if (slSpeed->value() != new_slider_value)
        slSpeed->setValue(new_slider_value);

    m_enable_updates = old_enable_updates;
}

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

    double last_speed = m_speed;

    switch (m_mode) {
        case MODE_FACTOR: {
            // -1 <=> /2
            //  0 <=> x1
            // +1 <=> x2
            if (pos >= 0) {
                m_speed = (pos + 1);
            } else {
                m_speed = -1.0 / static_cast<double>(pos - 1);
            }
            updateSpeed(m_speed);
            break;
        }
        case MODE_PERCENT:
            spinboxChanged(pos);
            break;
        DEFAULT_IMPOSSIBLE;
    }

    // emit changes
    if (!qFuzzyCompare(m_speed, last_speed)) {
        emit changed(m_speed, m_frequency);
    }
}

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

    double last_speed = m_speed;
    int sv = sbSpeed->value();
    switch (m_mode) {
        case MODE_FACTOR: {
            // multiply or divide by factor
            // -1 <=> /2
            //  0 <=> x1
            // +1 <=> x2
            if (m_speed >= 1) {
                m_speed = sv ? sv : 0.5;
            } else {
                if (!sv) sv = 1;
                m_speed = 1.0 / static_cast<double>(sv);
            }
            break;
        }
        case MODE_PERCENT: {
            // percentage
            m_speed = static_cast<double>(pos) / 100.0;
            break;
        }
        DEFAULT_IMPOSSIBLE;
    }

    // emit changes
    if (!qFuzzyCompare(m_speed, last_speed)) {
        emit changed(m_speed, m_frequency);
    }

    updateSpeed(m_speed);
}

//***************************************************************************
void Kwave::PitchShiftDialog::frequencyChanged(int pos)
{
    // emit changes
    if (!qFuzzyCompare(m_frequency, pos)) {
        m_frequency = pos;
        emit changed(m_speed, m_frequency);
    }
}

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

//***************************************************************************
void Kwave::PitchShiftDialog::setParams(QStringList &params)
{
    // evaluate the parameter list
    double speed = params[0].toDouble();
    m_frequency  = params[1].toDouble();
    switch (params[2].toUInt()) {
        case 0: m_mode = MODE_FACTOR;  break;
        case 1: m_mode = MODE_PERCENT; break;
        default: m_mode = MODE_PERCENT;
    }

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

    // update speed factor
    m_speed = speed;
    updateSpeed(speed);
}

//***************************************************************************
void Kwave::PitchShiftDialog::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::PitchShiftDialog::listenStopped()
{
    Q_ASSERT(btListen);
    if (!btListen) return;

    btListen->setChecked(false);
}


//***************************************************************************
void Kwave::PitchShiftDialog::invokeHelp()
{
    KHelpClient::invokeHelp(_("plugin_sect_pitch_shift"));
}

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

#include "moc_PitchShiftDialog.cpp"