26 #include <QFutureSynchronizer> 28 #include <QStringList> 30 #include <QtConcurrentRun> 32 #include <KLocalizedString> 51 #define TARGET_LEVEL -12 57 const QVariantList &args)
74 QList<unsigned int> tracks;
78 if (!length || tracks.isEmpty())
return;
87 connect(&src, SIGNAL(progress(qreal)),
89 Qt::BlockingQueuedConnection);
103 tracks.count(),
this);
106 if (!sink.
tracks())
return;
109 connect(&source, SIGNAL(progress(qreal)),
111 Qt::BlockingQueuedConnection);
126 double gain = target / level;
127 qDebug(
"NormalizePlugin: gain=%g", gain);
131 db.sprintf(
"%+0.1f", 20 * log10(gain))));
133 normalizer.setAttribute(SLOT(setGain(QVariant)), QVariant(gain));
145 const unsigned int tracks = source.
tracks();
148 if (!window_size)
return 0;
151 QVector<Kwave::NormalizePlugin::Average> average(tracks);
152 for (
unsigned int t = 0; t < tracks; t++) {
153 average[t].fifo = QVector<double>(
SMOOTHLEN, double(0.0));
156 average[t].sum = 0.0;
157 average[t].max = 0.0;
161 QFutureSynchronizer<void> synchronizer;
163 for (
unsigned int t = 0; t < tracks; t++) {
165 if (!reader)
continue;
166 if (reader->
eof())
continue;
168 synchronizer.addFuture(QtConcurrent::run(
171 reader, &(average[t]), window_size
174 synchronizer.waitForFinished();
179 for (
unsigned int t = 0; t < tracks; t++) {
181 double pow = avg.
sum /
static_cast<double>(avg.
n);
182 if (pow > maxpow) maxpow = pow;
186 for (
unsigned int t = 0; t < tracks; t++) {
187 double p = average[t].max;
188 if (p > maxpow) maxpow = p;
192 double level = sqrt(maxpow);
200 unsigned int window_size)
204 unsigned int round = 0;
205 unsigned int loops = 5 * reader->
blockSize() / window_size;
208 while ((round++ < loops) && !reader->
eof()) {
209 unsigned int len = reader->
read(data, 0, window_size);
214 for (
unsigned int i = 0; i < len; i++) {
219 double pow = sum /
static_cast<double>(len);
222 unsigned int wp = average.
wp;
223 average.
sum -= average.
fifo[wp];
225 average.
fifo[wp] = pow;
230 double p = average.
sum /
static_cast<double>(
SMOOTHLEN);
231 if (p > average.
max) average.
max = p;
240 #include "NormalizePlugin.moc" virtual unsigned int tracks() const Q_DECL_OVERRIDE
virtual unsigned int tracks() const Q_DECL_OVERRIDE
Kwave::SignalManager & signalManager()
void getMaxPowerOfTrack(Kwave::SampleReader *reader, Kwave::NormalizePlugin::Average *average, unsigned int window_size)
bool connect(Kwave::StreamObject &source, const char *output, Kwave::StreamObject &sink, const char *input)
virtual void goOn() Q_DECL_OVERRIDE
virtual ~NormalizePlugin() Q_DECL_OVERRIDE
static double sample2double(const sample_t s)
virtual void run(QStringList params) Q_DECL_OVERRIDE
double getMaxPower(Kwave::MultiTrackReader &source)
virtual unsigned int blockSize() const
#define KWAVE_PLUGIN(name, class)
unsigned int read(Kwave::SampleArray &buffer, unsigned int dstoff, unsigned int length)
virtual void updateProgress(qreal progress)
void setProgressText(const QString &text)
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)