kwave  18.07.70
ZeroPlugin.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  ZeroPlugin.cpp - wipes out the selected range of samples to zero
3  -------------------
4  begin : Fri Jun 01 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 <math.h>
21 #include <new>
22 
23 #include <KLocalizedString> // for the i18n macro
24 
25 #include <QList>
26 #include <QStringList>
27 
29 #include "libkwave/PluginManager.h"
30 #include "libkwave/Utils.h"
31 #include "libkwave/Writer.h"
33 
34 #include "libgui/SelectTimeWidget.h" // for selection mode
35 
36 #include "ZeroPlugin.h"
37 
38 KWAVE_PLUGIN(zero, ZeroPlugin)
39 
40 #define ZERO_COUNT (64 * 1024)
41 
42 //***************************************************************************
43 Kwave::ZeroPlugin::ZeroPlugin(QObject *parent, const QVariantList &args)
44  :Kwave::Plugin(parent, args), m_zeroes()
45 {
46 }
47 
48 //***************************************************************************
50 {
51 }
52 
53 //***************************************************************************
54 void Kwave::ZeroPlugin::run(QStringList params)
55 {
56  QList<unsigned int> tracks;
57  sample_index_t first = 0;
58  sample_index_t last = 0;
59  bool succeeded = true;
60 
61  Kwave::UndoTransactionGuard undo_guard(*this, i18n("Silence"));
62 
63  Kwave::MultiTrackWriter *writers = Q_NULLPTR;
64 
65  /*
66  * new mode: insert a range filled with silence:
67  * -> usage: zero(<mode>, <range>)
68  */
69  if (params.count() == 2) {
70  // get the current selection
71  selection(&tracks, &first, &last, false);
72 
73  // mode for the time (like in selectrange plugin)
74  bool ok = true;
75  int mode = params[0].toInt(&ok);
76  Q_ASSERT(ok);
77  if (!ok) return;
78  Q_ASSERT(
79  (mode == static_cast<int>(Kwave::SelectTimeWidget::byTime)) ||
80  (mode == static_cast<int>(Kwave::SelectTimeWidget::bySamples)) ||
81  (mode == static_cast<int>(Kwave::SelectTimeWidget::byPercents))
82  );
83  if ((mode != static_cast<int>(Kwave::SelectTimeWidget::byTime)) &&
84  (mode != static_cast<int>(Kwave::SelectTimeWidget::bySamples)) &&
85  (mode != static_cast<int>(Kwave::SelectTimeWidget::byPercents)))
86  {
87  return;
88  }
89 
90  // length of the range of zeroes to insert
91  unsigned int time = params[1].toUInt(&ok);
92  Q_ASSERT(ok);
93  if (!ok) return;
94 
95  // convert from time to samples
97  static_cast<Kwave::SelectTimeWidget::Mode>(mode),
98  time, signalRate(), signalLength());
99 
100  // some sanity check
101  Q_ASSERT(length);
102  Q_ASSERT(!tracks.isEmpty());
103  if (!length || tracks.isEmpty()) return; // nothing to do
104 
105  last = first + length - 1;
106  writers = new(std::nothrow) Kwave::MultiTrackWriter(signalManager(),
107  tracks, Kwave::Insert, first, last);
108  } else {
109  writers = new(std::nothrow) Kwave::MultiTrackWriter(signalManager(),
111  }
112 
113  if (!writers) return; // out-of-memory
114 
115  // break if aborted
116  if (!writers->tracks()) return;
117 
118  // connect the progress dialog
119  connect(writers, SIGNAL(progress(qreal)),
120  this, SLOT(updateProgress(qreal)),
121  Qt::BlockingQueuedConnection);
122 
123  first = (*writers)[0]->first();
124  last = (*writers)[0]->last();
125  unsigned int count = writers->tracks();
126 
127  // get the buffer with zeroes for faster filling
128  if (m_zeroes.size() != ZERO_COUNT) {
129  succeeded &= m_zeroes.resize(ZERO_COUNT);
130  Q_ASSERT(succeeded);
131  m_zeroes.fill(0);
132  }
133  Q_ASSERT(m_zeroes.size() == ZERO_COUNT);
134 
135  // loop over the sample range
136  while ((first <= last) && !shouldStop() && succeeded) {
137  sample_index_t rest = last - first + 1;
138  if (rest < m_zeroes.size()) {
139  succeeded &= m_zeroes.resize(Kwave::toUint(rest));
140  Q_ASSERT(succeeded);
141  if (!succeeded) break;
142  }
143 
144  // loop over all writers
145  unsigned int w;
146  for (w = 0; w < count; w++) {
147  *((*writers)[w]) << m_zeroes;
148  }
149 
150  first += m_zeroes.size();
151  }
152 
153  delete writers;
154 }
155 
156 //***************************************************************************
157 #include "ZeroPlugin.moc"
158 //***************************************************************************
159 //***************************************************************************
virtual unsigned int tracks() const Q_DECL_OVERRIDE
Definition: App.h:33
virtual ~ZeroPlugin() Q_DECL_OVERRIDE
Definition: ZeroPlugin.cpp:49
virtual void run(QStringList params) Q_DECL_OVERRIDE
Definition: ZeroPlugin.cpp:54
Kwave::SignalManager & signalManager()
Definition: Plugin.cpp:444
quint64 sample_index_t
Definition: Sample.h:28
bool connect(Kwave::StreamObject &source, const char *output, Kwave::StreamObject &sink, const char *input)
Definition: Connect.cpp:48
ZeroPlugin(QObject *parent, const QVariantList &args)
Definition: ZeroPlugin.cpp:43
static sample_index_t timeToSamples(Mode mode, quint64 time, double rate, sample_index_t length)
#define ZERO_COUNT
Definition: ZeroPlugin.cpp:40
virtual sample_index_t signalLength()
Definition: Plugin.cpp:462
virtual double signalRate()
Definition: Plugin.cpp:468
#define KWAVE_PLUGIN(name, class)
Definition: Plugin.h:54
void fill(sample_t value)
Definition: SampleArray.cpp:68
unsigned int size() const
virtual void updateProgress(qreal progress)
Definition: Plugin.cpp:260
unsigned int toUint(T x)
Definition: Utils.h:109
static double zero(double)
Definition: Functions.cpp:83
bool resize(unsigned int size) Q_REQUIRED_RESULT
bool shouldStop() const
Definition: Plugin.h:120
Kwave::SampleArray m_zeroes
Definition: ZeroPlugin.h:62
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