1 /***************************************************************************
2       NotchFilterPlugin.cpp  -  Plugin for simple notch filtering
3                              -------------------
4     begin                : Thu Jun 19 2003
5     copyright            : (C) 2003 by Dave Flogeras
6     email                : d.flogeras@unb.ca
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #include "config.h"
19 
20 #include <errno.h>
21 #include <math.h>
22 #include <new>
23 
24 #include <QStringList>
25 
26 #include <KLocalizedString>
27 
28 #include "NotchFilter.h"
29 #include "NotchFilterDialog.h"
30 #include "NotchFilterPlugin.h"
31 #include "libkwave/MultiTrackSource.h"
32 
KWAVE_PLUGIN(notch_filter,NotchFilterPlugin)33 KWAVE_PLUGIN(notch_filter, NotchFilterPlugin)
34 
35 //***************************************************************************
36 Kwave::NotchFilterPlugin::NotchFilterPlugin(QObject *parent,
37                                             const QVariantList &args)
38     :Kwave::FilterPlugin(parent, args),
39      m_frequency(3500.0), m_last_freq(100), m_bw(100), m_last_bw(200)
40 {
41 }
42 
43 //***************************************************************************
~NotchFilterPlugin()44 Kwave::NotchFilterPlugin::~NotchFilterPlugin()
45 {
46 }
47 
48 //***************************************************************************
interpreteParameters(QStringList & params)49 int Kwave::NotchFilterPlugin::interpreteParameters(QStringList &params)
50 {
51     bool ok;
52     QString param;
53 
54     // evaluate the parameter list
55     if (params.count() != 2) return -EINVAL;
56 
57     param = params[0];
58     m_frequency = param.toDouble(&ok);
59     Q_ASSERT(ok);
60     if (!ok) return -EINVAL;
61 
62     param = params[1];
63     m_bw = param.toDouble(&ok);
64     Q_ASSERT(ok);
65     if (!ok) return -EINVAL;
66 
67     return 0;
68 }
69 
70 //***************************************************************************
createDialog(QWidget * parent)71 Kwave::PluginSetupDialog *Kwave::NotchFilterPlugin::createDialog(QWidget *parent)
72 {
73     Kwave::NotchFilterDialog *dialog =
74 	new(std::nothrow) Kwave::NotchFilterDialog(parent, signalRate());
75     Q_ASSERT(dialog);
76     if (!dialog) return Q_NULLPTR;
77 
78     // connect the signals for detecting value changes in pre-listen mode
79     connect(dialog, SIGNAL(freqChanged(double)),
80             this, SLOT(setFreqValue(double)));
81     connect(dialog, SIGNAL(bwChanged(double)),
82     	    this, SLOT(setBwValue(double)));
83     return dialog;
84 }
85 
86 //***************************************************************************
createFilter(unsigned int tracks)87 Kwave::SampleSource *Kwave::NotchFilterPlugin::createFilter(unsigned int tracks)
88 {
89     return new(std::nothrow)
90 	Kwave::MultiTrackSource<Kwave::NotchFilter, true>(tracks);
91 }
92 
93 //***************************************************************************
paramsChanged()94 bool Kwave::NotchFilterPlugin::paramsChanged()
95 {
96     return (!qFuzzyCompare(m_frequency, m_last_freq) ||
97             !qFuzzyCompare(m_bw, m_last_bw));
98 }
99 
100 //***************************************************************************
updateFilter(Kwave::SampleSource * filter,bool force)101 void Kwave::NotchFilterPlugin::updateFilter(Kwave::SampleSource *filter,
102                                             bool force)
103 {
104     double sr = signalRate();
105 
106     if (!filter) return;
107 
108     if (!qFuzzyCompare(m_frequency, m_last_freq) || force)
109 	filter->setAttribute(SLOT(setFrequency(QVariant)),
110 	    QVariant((m_frequency * 2.0 * M_PI) / sr));
111 
112     if (!qFuzzyCompare(m_bw, m_last_bw) || force)
113 	filter->setAttribute(SLOT(setBandwidth(QVariant)),
114 	    QVariant((m_bw * 2.0 * M_PI) / sr));
115 
116     m_last_freq  = m_frequency;
117     m_last_bw    = m_bw;
118 }
119 
120 //***************************************************************************
actionName()121 QString Kwave::NotchFilterPlugin::actionName()
122 {
123     return i18n("Notch Filter");
124 }
125 
126 //***************************************************************************
setFreqValue(double frequency)127 void Kwave::NotchFilterPlugin::setFreqValue(double frequency)
128 {
129     m_frequency = frequency;
130 }
131 
132 //***************************************************************************
setBwValue(double bw)133 void Kwave::NotchFilterPlugin::setBwValue(double bw)
134 {
135     m_bw = bw;
136 }
137 
138 //***************************************************************************
139 #include "NotchFilterPlugin.moc"
140 //***************************************************************************
141 //***************************************************************************
142