File: NoisePlugin.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 (149 lines) | stat: -rw-r--r-- 4,938 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
/*************************************************************************
    NoisePlugin.cpp  -  overwrites the selected range of samples with noise
                             -------------------
    begin                : Wed Dec 12 2001
    copyright            : (C) 2001 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 <new>

#include <errno.h>

#include <KLocalizedString> // for the i18n macro

#include "libkwave/Connect.h"
#include "libkwave/MultiTrackSource.h"
#include "libkwave/MultiTrackWriter.h"
#include "libkwave/PluginManager.h"
#include "libkwave/SampleSink.h"
#include "libkwave/SampleSource.h"
#include "libkwave/undo/UndoTransactionGuard.h"

#include "libgui/OverViewCache.h"

#include "NoiseDialog.h"
#include "NoiseGenerator.h"
#include "NoisePlugin.h"

KWAVE_PLUGIN(noise, NoisePlugin)

//***************************************************************************
Kwave::NoisePlugin::NoisePlugin(QObject *parent, const QVariantList &args)
    :Kwave::FilterPlugin(parent, args), m_level(1.0), m_last_level(0.0)
{
}

//***************************************************************************
Kwave::NoisePlugin::~NoisePlugin()
{
}

//***************************************************************************
int Kwave::NoisePlugin::interpreteParameters(QStringList &params)
{
    bool ok;
    QString param;

    // evaluate the parameter list
    if (params.count() != 2) return -EINVAL;

    param = params[0];
    m_level = param.toDouble(&ok);
    Q_ASSERT(ok);
    if (!ok) return -EINVAL;

    param = params[1];
    unsigned int mode = param.toUInt(&ok);
    Q_ASSERT(ok);
    if (!ok || (mode > 2)) return -EINVAL;

    // all parameters accepted
    return 0;
}

//***************************************************************************
Kwave::PluginSetupDialog *Kwave::NoisePlugin::createDialog(QWidget *parent)
{
    Q_UNUSED(parent)

    // initialize the overview cache
    Kwave::SignalManager &mgr = manager().signalManager();
    QVector<unsigned int> tracks;
    sample_index_t first, last;
    sample_index_t length = selection(&tracks, &first, &last, true);
    Kwave::OverViewCache *overview_cache =
        new(std::nothrow) Kwave::OverViewCache(mgr, first, length,
            tracks.isEmpty() ? nullptr : &tracks);
    Q_ASSERT(overview_cache);

    // create the setup dialog
    Kwave::NoiseDialog *dialog =
        new(std::nothrow) Kwave::NoiseDialog(parentWidget(), overview_cache);
    if (!dialog) {
        delete overview_cache;
        return nullptr;
    }

    // connect the signals for detecting value changes in pre-listen mode
    connect(dialog, SIGNAL(levelChanged(double)),
            this,   SLOT(setNoiseLevel(double)));

    return dialog;
}

//***************************************************************************
Kwave::SampleSource *Kwave::NoisePlugin::createFilter(unsigned int tracks)
{
    return new(std::nothrow)
        Kwave::MultiTrackSource<Kwave::NoiseGenerator, true>(tracks);
}

//***************************************************************************
bool Kwave::NoisePlugin::paramsChanged()
{
    return (!qFuzzyCompare(m_level, m_last_level));
}

//***************************************************************************
void Kwave::NoisePlugin::updateFilter(Kwave::SampleSource *filter,
                                      bool force)
{
    if (!filter) return;

    if (!qFuzzyCompare(m_level, m_last_level) || force)
        filter->setAttribute(SLOT(setNoiseLevel(QVariant)),
                             QVariant(m_level));

    m_last_level = m_level;
}

//***************************************************************************
QString Kwave::NoisePlugin::actionName()
{
    return i18n("Add Noise");
}

//***************************************************************************
void Kwave::NoisePlugin::setNoiseLevel(double level)
{
    m_level = level;
}

//***************************************************************************
#include "NoisePlugin.moc"
//***************************************************************************
//***************************************************************************

#include "moc_NoisePlugin.cpp"