File: ZeroPlugin.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 (161 lines) | stat: -rw-r--r-- 5,437 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
150
151
152
153
154
155
156
157
158
159
160
161
/***************************************************************************
         ZeroPlugin.cpp  -  wipes out the selected range of samples to zero
                             -------------------
    begin                : Fri Jun 01 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 <math.h>
#include <new>

#include <KLocalizedString> // for the i18n macro

#include <QList>
#include <QStringList>

#include "libkwave/MultiTrackWriter.h"
#include "libkwave/PluginManager.h"
#include "libkwave/Utils.h"
#include "libkwave/Writer.h"
#include "libkwave/undo/UndoTransactionGuard.h"

#include "libgui/SelectTimeWidget.h" // for selection mode

#include "ZeroPlugin.h"

KWAVE_PLUGIN(zero, ZeroPlugin)

#define ZERO_COUNT (64 * 1024)

//***************************************************************************
Kwave::ZeroPlugin::ZeroPlugin(QObject *parent, const QVariantList &args)
    :Kwave::Plugin(parent, args), m_zeroes()
{
}

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

//***************************************************************************
void Kwave::ZeroPlugin::run(QStringList params)
{
    QVector<unsigned int> tracks;
    sample_index_t first = 0;
    sample_index_t last  = 0;
    bool succeeded = true;

    Kwave::UndoTransactionGuard undo_guard(*this, i18n("Silence"));

    Kwave::MultiTrackWriter *writers = nullptr;

    /*
     * new mode: insert a range filled with silence:
     * -> usage: zero(<mode>, <range>)
     */
    if (params.count() == 2) {
        // get the current selection
        selection(&tracks, &first, &last, false);

        // mode for the time (like in selectrange plugin)
        bool ok = true;
        int mode = params[0].toInt(&ok);
        Q_ASSERT(ok);
        if (!ok) return;
        Q_ASSERT(
            (mode == static_cast<int>(Kwave::SelectTimeWidget::byTime)) ||
            (mode == static_cast<int>(Kwave::SelectTimeWidget::bySamples)) ||
            (mode == static_cast<int>(Kwave::SelectTimeWidget::byPercents))
        );
        if ((mode != static_cast<int>(Kwave::SelectTimeWidget::byTime)) &&
            (mode != static_cast<int>(Kwave::SelectTimeWidget::bySamples)) &&
            (mode != static_cast<int>(Kwave::SelectTimeWidget::byPercents)))
        {
            return;
        }

        // length of the range of zeroes to insert
        unsigned int time = params[1].toUInt(&ok);
        Q_ASSERT(ok);
        if (!ok) return;

        // convert from time to samples
        sample_index_t length = Kwave::SelectTimeWidget::timeToSamples(
            static_cast<Kwave::SelectTimeWidget::Mode>(mode),
            time, signalRate(), signalLength());

        // some sanity check
        Q_ASSERT(length);
        Q_ASSERT(!tracks.isEmpty());
        if (!length || tracks.isEmpty()) return; // nothing to do

        last  = first + length - 1;
        writers = new(std::nothrow) Kwave::MultiTrackWriter(signalManager(),
            tracks, Kwave::Insert, first, last);
    } else {
        writers = new(std::nothrow) Kwave::MultiTrackWriter(signalManager(),
            Kwave::Overwrite);
    }

    if (!writers) return; // out-of-memory

    // break if aborted
    if (!writers->tracks()) return;

    // connect the progress dialog
    connect(writers, SIGNAL(progress(qreal)),
            this,    SLOT(updateProgress(qreal)),
            Qt::BlockingQueuedConnection);

    first = (*writers)[0]->first();
    last  = (*writers)[0]->last();
    unsigned int count = writers->tracks();

    // get the buffer with zeroes for faster filling
    if (m_zeroes.size() != ZERO_COUNT) {
        succeeded &= m_zeroes.resize(ZERO_COUNT);
        Q_ASSERT(succeeded);
        m_zeroes.fill(0);
    }
    Q_ASSERT(m_zeroes.size() == ZERO_COUNT);

    // loop over the sample range
    while ((first <= last) && !shouldStop() && succeeded) {
        sample_index_t rest = last - first + 1;
        if (rest < m_zeroes.size()) {
            succeeded &= m_zeroes.resize(Kwave::toUint(rest));
            Q_ASSERT(succeeded);
            if (!succeeded) break;
        }

        // loop over all writers
        unsigned int w;
        for (w = 0; w < count; w++) {
            *((*writers)[w]) << m_zeroes;
        }

        first += m_zeroes.size();
    }

    delete writers;
}

//***************************************************************************
#include "ZeroPlugin.moc"
//***************************************************************************
//***************************************************************************

#include "moc_ZeroPlugin.cpp"