Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[84b6f5]: libkwave / KwavePlugin.cpp Maximize Restore History

Download this file

KwavePlugin.cpp    515 lines (431 with data), 15.5 kB

  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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/***************************************************************************
KwavePlugin.cpp - base class of all Kwave plugins
-------------------
begin : Thu Jul 27 2000
copyright : (C) 2000 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 <errno.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <dlfcn.h>
#include <QProgressDialog>
#include <QThread>
#include <QTime>
#include <QVector>
#include <QWidget>
#include <kapplication.h>
#include <klocale.h>
#include "libkwave/ConfirmCancelProxy.h"
#include "libkwave/KwavePlugin.h"
#include "libkwave/MultiTrackReader.h"
#include "libkwave/Sample.h"
#include "libkwave/SampleReader.h"
#include "libkwave/Signal.h"
#include "libkwave/PluginContext.h"
#include "libkwave/PluginManager.h"
#include "libkwave/PluginWorkerThread.h"
#include "libkwave/SignalManager.h"
#ifdef DEBUG
#include <execinfo.h> // for backtrace()
#endif
/** number of updates of the progress bat per second */
#define PROGRESS_UPDATES_PER_SECOND 4
/** maximum value of the progress bar */
#define PROGRESS_MAXIMUM 1000000
/*
A plugin can be unloaded through two different ways. The possible
scenarios are:
1. WITHOUT a thread
main thread: new --- release
2. WITH a plugin thread
main thread: new --- use --- start --- release
plugin thread: --- run --- release
The plugin can be unloaded either in the main thread or in the
context of the plugin's thread, depending on what occurs first.
*/
//***************************************************************************
Kwave::Plugin::Plugin(const PluginContext &c)
:m_context(c),
m_thread(0),
m_thread_lock(),
m_progress_enabled(true),
m_stop(false),
m_progress(0),
m_confirm_cancel(0),
m_usage_count(1),
m_usage_lock(),
m_progress_timer(),
m_current_progress(-1),
m_progress_lock(QMutex::Recursive)
{
connect(&m_progress_timer, SIGNAL(timeout()),
this, SLOT(updateProgressTick()),
Qt::DirectConnection);
}
//***************************************************************************
Kwave::Plugin::~Plugin()
{
// inform our owner that we close. This allows the plugin to
// delete itself
close();
// lock usage
QMutexLocker lock(&m_usage_lock);
{
QMutexLocker lock(&m_thread_lock);
if (m_thread) {
if (m_thread->isRunning()) m_thread->wait(5000);
if (m_thread->isRunning()) m_thread->stop();
if (m_thread->isRunning()) m_thread->wait(1000);
if (m_thread->isRunning()) {
// unable to stop the thread
qWarning("Kwave::Plugin::stop(): stale thread !");
}
delete m_thread;
m_thread = 0;
}
}
// finally get rid of the confirm/cancel proxy and the progress dialog
closeProgressDialog(this);
}
//***************************************************************************
const QString &Kwave::Plugin::name()
{
return m_context.m_name;
}
//***************************************************************************
const QString &Kwave::Plugin::version()
{
return m_context.m_version;
}
//***************************************************************************
const QString &Kwave::Plugin::author()
{
return m_context.m_author;
}
//***************************************************************************
void Kwave::Plugin::load(QStringList &)
{
}
//***************************************************************************
QStringList *Kwave::Plugin::setup(QStringList &)
{
QStringList *result = new QStringList();
Q_ASSERT(result);
return result;
}
//***************************************************************************
int Kwave::Plugin::start(QStringList &)
{
QMutexLocker lock(&m_thread_lock);
m_stop = false;
// check: start() must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
// create a progress dialog for processing mode (not used for pre-listen)
if (m_progress_enabled && !m_progress) {
m_progress = new QProgressDialog(parentWidget());
Q_ASSERT(m_progress);
}
// set up the progress dialog when in processing (not pre-listen) mode
if (m_progress_enabled && m_progress) {
sample_index_t first, last;
QList<unsigned int> tracks;
selection(&tracks, &first, &last, true);
m_progress->setModal(true);
m_progress->setVisible(false);
m_progress->setMinimumDuration(2000);
m_progress->setAutoClose(false);
m_progress->setMaximum(PROGRESS_MAXIMUM);
m_progress->setValue(0);
m_progress->setLabelText(progressText());
int h = m_progress->sizeHint().height();
int w = m_progress->sizeHint().height();
if (w < 4 * h) w = 4 * h;
m_progress->setFixedSize(w, h);
// use a "proxy" that asks for confirmation of cancel
if (!m_confirm_cancel) {
m_confirm_cancel = new ConfirmCancelProxy(m_progress,
0, 0, this, SLOT(cancel()));
Q_ASSERT(m_confirm_cancel);
}
connect(m_progress, SIGNAL(canceled()),
m_confirm_cancel, SLOT(cancel()));
connect(this, SIGNAL(setProgressText(const QString &)),
m_progress, SLOT(setLabelText(const QString &)),
Qt::QueuedConnection);
connect(this, SIGNAL(sigDone(Kwave::Plugin *)),
this, SLOT(closeProgressDialog(Kwave::Plugin *)),
Qt::QueuedConnection);
m_progress->setVisible(true);
}
return 0;
}
//***************************************************************************
QString Kwave::Plugin::progressText()
{
return i18n("Running plugin '%1'...", name());
}
//***************************************************************************
int Kwave::Plugin::stop()
{
cancel();
if (m_thread && m_thread->isRunning() &&
(QThread::currentThread() == m_thread)) {
qWarning("Kwave::Plugin::stop(): plugin '%s' called stop() from "\
"within it's own worker thread (from run() ?). "\
"This would produce a deadlock, dear %s, PLEASE FIX THIS !",
name().toLocal8Bit().data(),
author().toLocal8Bit().data());
#ifdef DEBUG
qDebug("pthread_self()=%p, tid=%p",
reinterpret_cast<void *>(QThread::currentThread()),
reinterpret_cast<void *>(m_thread));
void *buf[256];
size_t n = backtrace(buf, 256);
backtrace_symbols_fd(buf, n, 2);
#endif
return -EBUSY;
}
{
QMutexLocker lock(&m_thread_lock);
if (m_thread) {
if (m_thread->isRunning()) m_thread->wait(5000);
if (m_thread->isRunning()) m_thread->stop();
if (m_thread->isRunning()) m_thread->wait(1000);
if (m_thread->isRunning()) {
// unable to stop the thread
qWarning("Kwave::Plugin::stop(): stale thread !");
}
delete m_thread;
m_thread = 0;
}
}
return 0;
}
//***************************************************************************
void Kwave::Plugin::setProgressDialogEnabled(bool enable)
{
m_progress_enabled = enable;
}
//***************************************************************************
void Kwave::Plugin::updateProgress(qreal progress)
{
// check: this must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
// qDebug("Kwave::Plugin::updateProgress(%0.1f)", progress);
QMutexLocker lock(&m_progress_lock);
// take over the current progress
// note: no lock needed, this is called in the GUI thread context!
m_current_progress = qreal(PROGRESS_MAXIMUM / 100.0) * progress;
// start the timer for updating the progress bar if it is not active
if (!m_progress_timer.isActive() && m_progress) {
m_progress_timer.setSingleShot(true);
m_progress_timer.start(1000 / PROGRESS_UPDATES_PER_SECOND);
}
}
//***************************************************************************
void Kwave::Plugin::updateProgressTick()
{
// check: this must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
QMutexLocker lock(&m_progress_lock);
if (m_progress) m_progress->setValue(m_current_progress);
}
//***************************************************************************
void Kwave::Plugin::closeProgressDialog(Kwave::Plugin *)
{
// check: this must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
QMutexLocker lock(&m_progress_lock);
// stop the progress timer
m_progress_timer.stop();
m_progress_timer.disconnect();
if (m_confirm_cancel) m_confirm_cancel->disconnect();
if (m_progress) m_progress->disconnect();
// NOTE: as the progress dialog is *modal*, it is higly probable
// that this function is called from the context of the event
// loop that is provided by the progress dialog
// => deleting this object should be done somewhere later...
if (m_confirm_cancel) {
m_confirm_cancel->deleteLater();
m_confirm_cancel = 0;
}
if (m_progress) {
m_progress->done(0);
m_progress->deleteLater();
m_progress = 0;
}
}
//***************************************************************************
void Kwave::Plugin::cancel()
{
m_stop = true;
}
//***************************************************************************
int Kwave::Plugin::execute(QStringList &params)
{
QMutexLocker lock(&m_thread_lock);
m_stop = false;
m_thread = new Kwave::PluginWorkerThread(this, params);
Q_ASSERT(m_thread);
if (!m_thread) return -ENOMEM;
// increment the use count, it is decremented at the end of
// the run_wrapper when the thread is finished
use();
// start the thread, this executes run()
m_thread->start();
return 0;
}
//***************************************************************************
bool Kwave::Plugin::isRunning()
{
return (m_thread) && m_thread->isRunning();
}
//***************************************************************************
void Kwave::Plugin::run(QStringList)
{
}
//***************************************************************************
void Kwave::Plugin::run_wrapper(QStringList params)
{
// signal that we are running
emit sigRunning(this);
// start time measurement
QTime t;
t.start();
// call the plugin's run function in this worker thread context
run(params);
// evaluate the elapsed time
double seconds = static_cast<double>(t.elapsed()) * 1E-3;
qDebug("plugin %s done, running for %0.03g seconds",
name().toLocal8Bit().data(), seconds);
// emit the "done" signal
emit sigDone(this);
m_stop = false;
release();
}
//***************************************************************************
void Kwave::Plugin::close()
{
// only call stop() if we are NOT in the worker thread / run function !
if (m_thread && m_thread->isRunning() &&
(QThread::currentThread() != m_thread) )
{
// check: this must be called from the GUI thread only!
Q_ASSERT(this->thread() == QThread::currentThread());
Q_ASSERT(this->thread() == qApp->thread());
closeProgressDialog(this);
stop();
} else if ((QThread::currentThread() == m_thread)) {
qWarning("Kwave::Plugin::close -> called from worker thread?");
}
}
//***************************************************************************
void Kwave::Plugin::use()
{
QMutexLocker lock(&m_usage_lock);
Q_ASSERT(m_usage_count); // should be at least 1 (from constructor)
m_usage_count++;
}
//***************************************************************************
void Kwave::Plugin::release()
{
bool finished = false;
{
QMutexLocker lock(&m_usage_lock);
Q_ASSERT(m_usage_count);
if (m_usage_count) {
m_usage_count--;
if (!m_usage_count) finished = true;
}
}
if (finished) emit sigClosed(this);
}
//***************************************************************************
Kwave::PluginManager &Kwave::Plugin::manager()
{
return m_context.m_plugin_manager;
}
//***************************************************************************
SignalManager &Kwave::Plugin::signalManager()
{
return manager().signalManager();
}
//***************************************************************************
QWidget *Kwave::Plugin::parentWidget()
{
return manager().parentWidget();
}
//***************************************************************************
QString Kwave::Plugin::signalName()
{
return signalManager().signalName();
}
//***************************************************************************
sample_index_t Kwave::Plugin::signalLength()
{
return manager().signalLength();
}
//***************************************************************************
double Kwave::Plugin::signalRate()
{
return manager().signalRate();
}
//***************************************************************************
const QList<unsigned int> Kwave::Plugin::selectedTracks()
{
return manager().selectedTracks();
}
//***************************************************************************
sample_index_t Kwave::Plugin::selection(QList<unsigned int> *tracks,
sample_index_t *left,
sample_index_t *right,
bool expand_if_empty)
{
sample_index_t l = manager().selectionStart();
sample_index_t r = manager().selectionEnd();
// expand to the whole signal if left==right and expand_if_empty is set
if ((l == r) && (expand_if_empty)) {
l = 0;
r = manager().signalLength();
if (r) r--;
}
// get the list of selected tracks
if (tracks) *tracks = manager().selectedTracks();
if (left) *left = l;
if (right) *right = r;
return (r != l) ? (r - l + 1) : 0;
}
//***************************************************************************
void Kwave::Plugin::selectRange(sample_index_t offset, sample_index_t length)
{
manager().selectRange(offset, length);
}
//***************************************************************************
void *Kwave::Plugin::handle()
{
return m_context.m_handle;
}
//***************************************************************************
void Kwave::Plugin::emitCommand(const QString &command)
{
manager().enqueueCommand(command);
}
//***************************************************************************
#include "KwavePlugin.moc"
//***************************************************************************
//***************************************************************************