kwave  18.07.70
AmplifyFreePlugin.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  AmplifyFreePlugin.cpp - Plugin for free amplification curves
3  -------------------
4  begin : Sun Sep 02 2001
5  copyright : (C) 2001 by Thomas Eschenbacher
6  email : Thomas.Eschenbacher@gmx.de
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 <KLocalizedString>
21 #include <QStringList>
22 
23 #include "libkwave/Connect.h"
27 #include "libkwave/Parser.h"
28 #include "libkwave/PluginManager.h"
29 #include "libkwave/String.h"
31 #include "libkwave/modules/Mul.h"
33 
34 #include "AmplifyFreeDialog.h"
35 #include "AmplifyFreePlugin.h"
36 
37 KWAVE_PLUGIN(amplifyfree, AmplifyFreePlugin)
38 
39 //***************************************************************************
41  const QVariantList &args)
42  :Kwave::Plugin(parent, args),
43  m_action_name(), m_params(), m_curve(), m_cmd_map()
44 {
45  m_cmd_map[_("fade in")] = i18n("Fade In");
46  m_cmd_map[_("fade out")] = i18n("Fade Out");
47  m_cmd_map[_("fade intro")] = i18n("Fade Intro");
48  m_cmd_map[_("fade leadout")] = i18n("Fade Leadout");
49 }
50 
51 //***************************************************************************
53 {
54 }
55 
56 //***************************************************************************
58 {
59  // store last parameters
60  m_params = params;
61 
62  m_action_name = _("");
63  if (params.count() < 2) return -1;
64  if (params.count() & 1) return -1; // no. of params must be even
65 
66  // first list entry == name of operation
67  m_action_name = (params[0].length() && m_cmd_map.contains(params[0])) ?
68  m_cmd_map[params[0]] : i18n("Amplify Free");
69 
70  // convert string list into command again...
71  QString cmd = _("curve(");
72  for (int i = 1; i < params.count(); ++i) {
73  cmd += params[i];
74  if ((i + 1) < params.count()) cmd += _(",");
75  }
76  cmd += _(")");
77 
78  // and initialize our curve with it
79  m_curve.fromCommand(cmd);
80 
81  return 0;
82 }
83 
84 //***************************************************************************
85 QStringList *Kwave::AmplifyFreePlugin::setup(QStringList &previous_params)
86 {
87  // try to interprete the previous parameters
88  interpreteParameters(previous_params);
89 
90  // create the setup dialog
91  Kwave::AmplifyFreeDialog *dialog =
93  Q_ASSERT(dialog);
94  if (!dialog) return Q_NULLPTR;
95 
96  // remove the first list entry (action name), the rest is for the dialog
97  if ((m_params.count() > 2) && !(m_params.count() & 1)) {
98  QStringList curve_params = m_params;
99  curve_params.takeFirst(); // ignore action name
100  dialog->setParams(curve_params);
101  }
102 
103  QStringList *list = new QStringList();
104  Q_ASSERT(list);
105  if (list && dialog->exec()) {
106  // user has pressed "OK"
107  *list << _("amplify free");
108  QString cmd = dialog->getCommand();
109  Kwave::Parser p(cmd);
110  while (!p.isDone()) *list << p.nextParam();
111 
112  qDebug("setup -> emitCommand('%s')", DBG(cmd));
113  emitCommand(cmd);
114  } else {
115  // user pressed "Cancel"
116  if (list) delete list;
117  list = Q_NULLPTR;
118  }
119 
120  if (dialog) delete dialog;
121  return list;
122 }
123 
124 //***************************************************************************
126 {
127  return m_action_name;
128 }
129 
130 //***************************************************************************
131 int Kwave::AmplifyFreePlugin::start(QStringList &params)
132 {
133  interpreteParameters(params);
134  return Kwave::Plugin::start(params);
135 }
136 
137 //***************************************************************************
138 void Kwave::AmplifyFreePlugin::run(QStringList params)
139 {
140  sample_index_t first, last;
141  QList<unsigned int> track_list;
142 
143  interpreteParameters(params);
144 
145  Kwave::UndoTransactionGuard undo_guard(*this, m_action_name);
146 
147  sample_index_t input_length = selection(&track_list, &first, &last, true);
148  unsigned int tracks = track_list.count();
149 
150  // create all objects
152  signalManager(), selectedTracks(), first, last);
153  Kwave::CurveStreamAdapter curve(m_curve, input_length);
155  first, last);
157 
158  // break if aborted
159  if (!sink.tracks()) return;
160 
161  // connect them
162  bool ok = true;
163  if (ok) ok = Kwave::connect(
164  source, SIGNAL(output(Kwave::SampleArray)),
165  mul, SLOT(input_a(Kwave::SampleArray)));
166  if (ok) ok = Kwave::connect(
167  curve, SIGNAL(output(Kwave::SampleArray)),
168  mul, SLOT(input_b(Kwave::SampleArray)));
169  if (ok) ok = Kwave::connect(
170  mul, SIGNAL(output(Kwave::SampleArray)),
171  sink, SLOT(input(Kwave::SampleArray)));
172  if (!ok) {
173  return;
174  }
175 
176  // connect the progress dialog
177  connect(&sink, SIGNAL(progress(qreal)),
178  this, SLOT(updateProgress(qreal)),
179  Qt::BlockingQueuedConnection);
180 
181  // transport the samples
182  qDebug("AmplifyFreePlugin: filter started...");
183  while (!shouldStop() && !source.done()) {
184  source.goOn();
185  curve.goOn();
186  /* mul.goOn(); */
187  }
188  qDebug("AmplifyFreePlugin: filter done.");
189 }
190 
191 //***************************************************************************
192 #include "AmplifyFreePlugin.moc"
193 //***************************************************************************
194 //***************************************************************************
void emitCommand(const QString &command)
Definition: Plugin.cpp:510
virtual unsigned int tracks() const Q_DECL_OVERRIDE
Definition: App.h:33
virtual bool done() const Q_DECL_OVERRIDE
QWidget * parentWidget() const
Definition: Plugin.cpp:450
Kwave::SignalManager & signalManager()
Definition: Plugin.cpp:444
virtual int start(QStringList &params) Q_DECL_OVERRIDE
quint64 sample_index_t
Definition: Sample.h:28
void fromCommand(const QString &command)
Definition: Curve.cpp:55
bool connect(Kwave::StreamObject &source, const char *output, Kwave::StreamObject &sink, const char *input)
Definition: Connect.cpp:48
virtual void goOn() Q_DECL_OVERRIDE
virtual void goOn() Q_DECL_OVERRIDE
virtual void run(QStringList params) Q_DECL_OVERRIDE
int interpreteParameters(QStringList &params)
virtual ~AmplifyFreePlugin() Q_DECL_OVERRIDE
#define KWAVE_PLUGIN(name, class)
Definition: Plugin.h:54
#define _(m)
Definition: memcpy.c:66
bool isDone() const
Definition: Parser.h:70
#define DBG(qs)
Definition: String.h:55
virtual QString progressText() Q_DECL_OVERRIDE
virtual void updateProgress(qreal progress)
Definition: Plugin.cpp:260
virtual const QList< unsigned int > selectedTracks()
Definition: Plugin.cpp:474
virtual QStringList * setup(QStringList &previous_params) Q_DECL_OVERRIDE
virtual int start(QStringList &params)
Definition: Plugin.cpp:142
void setParams(QStringList &params)
QMap< QString, QString > m_cmd_map
const QString & nextParam()
Definition: Parser.cpp:175
bool shouldStop() const
Definition: Plugin.h:120
virtual sample_index_t selection(QList< unsigned int > *tracks=Q_NULLPTR, sample_index_t *left=Q_NULLPTR, sample_index_t *right=Q_NULLPTR, bool expand_if_empty=false)
Definition: Plugin.cpp:480