kwave  18.07.70
NotchFilterDialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  NotchFilterDialog.cpp - dialog for the "notch_filter" plugin
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 #include "math.h"
20 
21 #include <QObject>
22 #include <QPainter>
23 #include <QPushButton>
24 #include <QRadioButton>
25 #include <QSlider>
26 #include <QSpinBox>
27 #include <QWidget>
28 
29 #include <KHelpClient>
30 #include <KLocalizedString>
31 
32 #include "libkwave/String.h"
33 #include "libkwave/Utils.h"
34 
36 #include "libgui/ScaleWidget.h"
37 
38 #include "NotchFilter.h"
39 #include "NotchFilterDialog.h"
40 
41 //***************************************************************************
42 Kwave::NotchFilterDialog::NotchFilterDialog(QWidget *parent, double sample_rate)
43  :QDialog(parent), Kwave::PluginSetupDialog(),
44  Ui::NotchFilterDlg(),
45  m_frequency(3500),m_bw(100),
46  m_sample_rate(sample_rate), m_filter(Q_NULLPTR)
47 {
48  setupUi(this);
49  setModal(true);
50 
51  // set maximum frequency to sample rate / 2
52  double f_max = sample_rate / 2.0;
53 
54  slider->setMaximum(Kwave::toInt(f_max));
55  spinbox->setMaximum(Kwave::toInt(f_max));
56 
57  slider_2->setMaximum(Kwave::toInt(f_max));
58  spinbox_2->setMaximum(Kwave::toInt(f_max));
59 
60  // initialize the frequency scale widget
61  scale_freq->setMinMax(0, Kwave::toInt(f_max));
62  scale_freq->setLogMode(false);
63  scale_freq->setUnit(i18n("Hz"));
64 
65  // initialize the attenuation scale widget
66  scale_db->setMinMax(-24, +6);
67  scale_db->setLogMode(false);
68  scale_db->setUnit(i18n("dB"));
69 
70  // initialize the frequency response widget
71  freq_response->init(f_max, -24, +6);
72 
73  // set up the low pass filter dunction
74  m_filter = new NotchFilter();
75  freq_response->setFilter(m_filter);
76 
77  // initialize the controls and the curve display
78  slider->setValue(Kwave::toInt(m_frequency));
79  spinbox->setValue(Kwave::toInt(m_frequency));
80  slider_2->setValue(Kwave::toInt(m_bw));
81  spinbox_2->setValue(Kwave::toInt(m_bw));
82  updateDisplay();
83 
84  // changes in the slider or spinbox
85  connect(spinbox, SIGNAL(valueChanged(int)),
86  this, SLOT(freqValueChanged(int)));
87  connect(spinbox_2, SIGNAL(valueChanged(int)),
88  this, SLOT(bwValueChanged(int)));
89  // click to the "Listen" button
90  connect(btListen, SIGNAL(toggled(bool)),
91  this, SLOT(listenToggled(bool)));
92 
93  // expand the "Listen" button to it's maximum width
94  listenToggled(true);
95  if (btListen->width() > btListen->minimumWidth())
96  btListen->setMinimumWidth(btListen->width());
97  listenToggled(false);
98  if (btListen->width() > btListen->minimumWidth())
99  btListen->setMinimumWidth(btListen->width());
100 
101  // set the initial size of the dialog
102  int h = (width() * 3) / 5;
103  if (height() < h) resize(width(), h);
104  int w = (height() * 5) / 3;
105  if (width() < w) resize(w, height());
106 
107  connect(buttonHelp->button(QDialogButtonBox::Help), SIGNAL(clicked()),
108  this, SLOT(invokeHelp()));
109 
110  // set the focus onto the "OK" button
111  buttonBox->button(QDialogButtonBox::Ok)->setFocus();
112 }
113 
114 //***************************************************************************
116 {
117  // better stop pre-listen now
118  listenToggled(false);
119 
120  if (freq_response) freq_response->setFilter(Q_NULLPTR);
121  if (m_filter) delete m_filter;
122 }
123 
124 //***************************************************************************
126 {
127  if (Kwave::toInt(m_frequency) != pos) {
128  m_frequency = pos;
129  updateDisplay();
130 
131  emit freqChanged(m_frequency);
132  }
133 }
134 
135 //***************************************************************************
137 {
138  if (Kwave::toInt(m_bw) != pos) {
139  m_bw = pos;
140  updateDisplay();
141 
142  emit bwChanged(m_bw);
143  }
144 }
145 
146 //***************************************************************************
148 {
149  QStringList list;
150  list << QString::number(m_frequency);
151  list << QString::number(m_bw);
152  return list;
153 }
154 
155 //***************************************************************************
157 {
158  // evaluate the parameter list
159  bool ok;
160  double frequency = params[0].toDouble(&ok);
161  Q_ASSERT(ok);
162  if (ok) m_frequency = frequency;
163 
164  double bw = params[1].toDouble(&ok);
165  Q_ASSERT(ok);
166  if (ok) m_bw = bw;
167 
168  slider->setValue(Kwave::toInt(m_frequency));
169  spinbox->setValue(Kwave::toInt(m_frequency));
170 
171  slider_2->setValue(Kwave::toInt(m_bw));
172  spinbox_2->setValue(Kwave::toInt(m_bw));
173 
174  updateDisplay();
175 }
176 
177 //***************************************************************************
179 {
180  double fs = m_sample_rate;
181  if (m_filter && (fs > 0.0))
182  {
183  m_filter->setFrequency(QVariant(2.0 * M_PI * m_frequency / fs));
184  m_filter->setBandwidth(QVariant(2.0 * M_PI * m_bw / fs));
185  if (freq_response) freq_response->repaint();
186  }
187 }
188 
189 //***************************************************************************
191 {
192  Q_ASSERT(btListen);
193  if (!btListen) return;
194 
195  if (listen) {
196  // start pre-listen mode
197  emit startPreListen();
198  btListen->setText(i18n("&Stop"));
199  } else {
200  // stop pre-listen mode
201  emit stopPreListen();
202  btListen->setText(i18n("&Listen"));
203  }
204 }
205 
206 //***************************************************************************
208 {
209  if (btListen) btListen->setChecked(false);
210 }
211 
212 
213 //***************************************************************************
215 {
216  KHelpClient::invokeHelp(_("plugin_sect_notch_filter"));
217 }
218 
219 //***************************************************************************
220 //***************************************************************************
void setBandwidth(const QVariant bw)
Definition: App.h:33
NotchFilterDialog(QWidget *parent, double sample_rate)
bool connect(Kwave::StreamObject &source, const char *output, Kwave::StreamObject &sink, const char *input)
Definition: Connect.cpp:48
Kwave::NotchFilter * m_filter
void freqChanged(double freq)
void bwChanged(double bw)
int toInt(T x)
Definition: Utils.h:127
virtual void setParams(QStringList &params) Q_DECL_OVERRIDE
void setFrequency(const QVariant fc)
#define _(m)
Definition: memcpy.c:66
virtual QStringList params() Q_DECL_OVERRIDE
virtual ~NotchFilterDialog() Q_DECL_OVERRIDE
void listenToggled(bool listen)