[0a1223]: plugins / dialogs / sonagram / SonagramDialog.cpp Maximize Restore History

Download this file

SonagramDialog.cpp    434 lines (355 with data), 15.3 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
/***************************************************************************
SonagramDialog.cpp - dialog for setting up the sonagram window
-------------------
begin : Fri Jul 28 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 <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <qbuttongroup.h>
#include <qcheckbox.h>
#include <qcombobox.h>
#include <qdialog.h>
#include <qgroupbox.h>
#include <qkeycode.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qradiobutton.h>
#include <qstrlist.h>
#include <qtooltip.h>
#include <libkwave/WindowFunction.h>
#include <libgui/KwavePlugin.h>
#include <libgui/PluginContext.h>
#include <libgui/Slider.h>
#include <libgui/Dialog.h>
#include <kapp.h>
#include "SonagramDialog.h"
//***************************************************************************
static const char *FFT_Sizes[] =
{"64", "128", "256", "512", "1024", "2048", "4096", 0};
#ifndef max
#define max(x,y) (( x > y ) ? x : y )
#endif
//***************************************************************************
SonagramDialog::SonagramDialog(KwavePlugin &p)
:QDialog(p.parentWidget(), i18n("sonagram"), true)
{
m_bitmaplabel = 0;
m_cancel = 0;
m_ok = 0;
m_pointbox = 0;
m_pointlabel = 0;
m_pointslider = 0;
m_rbColor = 0;
m_windowlabel = 0;
m_windowtypebox = 0;
m_windowtypelabel = 0;
int h;
int w;
int i;
m_length = p.selection();
m_rate = p.signalRate();
// if nothing selected, select all
if (m_length <= 1) m_length = p.signalLength();
debug("SonagramDialog(): constructor");
WindowFunction wf(0);
setCaption (i18n("Set FFT/time resolution parameter"));
// ----------------------------------------------------------------------
// --- create all layout objects ------------------------------------
// ----------------------------------------------------------------------
// create the toplevel layout
QVBoxLayout *topLayout = new QVBoxLayout(this, 10);
ASSERT(topLayout);
if (!topLayout) return;
QHBoxLayout *pointsLayout = new QHBoxLayout();
ASSERT(pointsLayout);
if (!pointsLayout) return;
QHBoxLayout *windowsizeLayout = new QHBoxLayout();
ASSERT(windowsizeLayout);
if (!windowsizeLayout) return;
QHBoxLayout *bitmapLayout = new QHBoxLayout();
ASSERT(bitmapLayout);
if (!bitmapLayout) return;
QHBoxLayout *windowfuncLayout = new QHBoxLayout();
ASSERT(windowfuncLayout);
if (!windowfuncLayout) return;
// ----------------------------------------------------------------------
// --- FFT settings -------------------------------------------------
// ----------------------------------------------------------------------
QGroupBox *fft_frame = new QGroupBox(this);
ASSERT(fft_frame);
if (!fft_frame) return;
fft_frame->setTitle(i18n("FFT settings"));
topLayout->addWidget(fft_frame);
QVBoxLayout *fftLayout = new QVBoxLayout(fft_frame, 10);
ASSERT(fftLayout);
if (!fftLayout) return;
m_pointlabel = new QLabel(i18n("Number of FFT points:"), fft_frame);
ASSERT(m_pointlabel);
if (!m_pointlabel) return;
m_pointbox = new QComboBox (true, fft_frame);
ASSERT(m_pointbox);
if (!m_pointbox) return;
m_pointbox->insertStrList (FFT_Sizes, -1);
QToolTip::add(m_pointbox,
i18n("Try to choose numbers with small prime-factors, "\
"if choosing big window sizes.\n"\
"The computation will be much faster !"));
m_windowlabel = new QLabel("", fft_frame);
ASSERT(m_windowlabel);
if (!m_windowlabel) return;
m_windowtypebox = new QComboBox (true, fft_frame);
ASSERT(m_windowtypebox);
if (!m_windowtypebox) return;
for (i=0; i < wf.getCount(); i++) {
m_windowtypebox->insertItem(i18n(wf.getTypes()[i]));
}
QToolTip::add(m_windowtypebox,
i18n("Choose windowing function here. "\
"If fourier transformation should stay reversible, "\
"use the type <none>"));
m_bitmaplabel = new QLabel("", fft_frame);
ASSERT(m_bitmaplabel);
if (!m_bitmaplabel) return;
m_pointslider = new Slider (2, (m_length / 16), 1, 5,
Slider::Horizontal, fft_frame);
ASSERT(m_pointslider);
if (!m_pointslider) return;
m_windowtypelabel = new QLabel (i18n("Window Function :"), fft_frame);
ASSERT(m_windowtypelabel);
if (!m_windowtypelabel) return;
// -- create the fft frame's layout --
setPoints(1); // must set the minimum number of points to get
setBoxPoints(0); // the largest windowlabel
/* put together all the layouts */
fftLayout->addSpacing(fft_frame->fontMetrics().height() );
fftLayout->addLayout(pointsLayout);
fftLayout->addLayout(windowfuncLayout);
fftLayout->addLayout(bitmapLayout);
fftLayout->addLayout(windowsizeLayout);
h = max(m_pointbox->sizeHint().height(),
m_pointlabel->sizeHint().height());
// number of FFT points
w = m_pointlabel->sizeHint().width();
m_pointlabel->setFixedSize(w, h);
m_pointbox->setFixedHeight(h);
m_pointbox->adjustSize();
m_pointbox->setMinimumWidth(m_pointbox->width()+10);
m_pointbox->setMaximumWidth(m_pointbox->width()*2);
pointsLayout->addWidget(m_pointlabel, 0, AlignLeft | AlignCenter);
pointsLayout->addSpacing(10);
pointsLayout->addStretch(1);
pointsLayout->addWidget(m_pointbox, 1, AlignRight | AlignCenter);
// windowing function
m_windowtypelabel->setFixedHeight(h);
m_windowtypelabel->setFixedWidth(m_windowtypelabel->sizeHint().width());
m_windowtypebox->setFixedHeight(h);
m_windowtypebox->adjustSize();
m_windowtypebox->setMinimumWidth(m_windowtypebox->width()+10);
m_windowtypebox->setMaximumWidth(m_windowtypebox->width()*2);
windowfuncLayout->addWidget(m_windowtypelabel, 0, AlignLeft | AlignCenter);
windowfuncLayout->addSpacing(10);
windowfuncLayout->addStretch(1);
windowfuncLayout->addWidget(m_windowtypebox, 1, AlignRight | AlignCenter);
// size of the bitmap
m_bitmaplabel->setFixedSize(m_bitmaplabel->sizeHint().width()+20,h);
m_pointslider->setFixedHeight(h);
w = m_pointslider->sizeHint().width();
w = max(w, m_pointslider->sizeHint().height()*8);
m_pointslider->setMinimumWidth(w);
bitmapLayout->addWidget(m_bitmaplabel, 0, AlignLeft | AlignCenter);
bitmapLayout->addSpacing(10);
bitmapLayout->addWidget(m_pointslider, 1, AlignRight | AlignCenter);
// resulting window size
m_windowlabel->setFixedHeight(h);
m_windowlabel->setMinimumWidth(m_windowlabel->sizeHint().width()+10);
windowsizeLayout->addWidget(m_windowlabel, 0, AlignLeft | AlignCenter);
// ----------------------------------------------------------------------
// --- Layout for the lower part: display and update groups ---------
// ----------------------------------------------------------------------
QHBoxLayout *guiLayout = new QHBoxLayout();
ASSERT(guiLayout);
if (!guiLayout) return;
topLayout->addLayout(guiLayout);
// ----------------------------------------------------------------------
// --- display type selection: color/grayscale ----------------------
// ----------------------------------------------------------------------
QButtonGroup *display_group = new QButtonGroup(this);
ASSERT(display_group);
if (!display_group) return;
display_group->setTitle(i18n("display"));
guiLayout->addWidget(display_group);
QVBoxLayout *displayLayout = new QVBoxLayout(display_group, 10);
ASSERT(displayLayout);
if (!displayLayout) return;
displayLayout->addSpacing(display_group->fontMetrics().height());
m_rbColor = new QRadioButton(display_group);
ASSERT(m_rbColor);
if (!m_rbColor) return;
m_rbColor->setText(i18n("use colors"));
m_rbColor->setChecked(TRUE);
displayLayout->addWidget(m_rbColor);
m_rbColor->setMinimumSize(m_rbColor->sizeHint());
QToolTip::add(m_rbColor, i18n("use different colors for amplitude"));
QRadioButton *m_rbGreyScale = new QRadioButton(display_group);
ASSERT(m_rbGreyScale);
if (!m_rbGreyScale) return;
m_rbGreyScale->setText(i18n("greyscale"));
displayLayout->addWidget(m_rbGreyScale);
m_rbGreyScale->setMinimumSize(m_rbGreyScale->sizeHint());
QToolTip::add(m_rbGreyScale, i18n("use greyscale only for amplitude"));
// ----------------------------------------------------------------------
// --- update settings: track signal changes / follow selection -----
// ----------------------------------------------------------------------
QButtonGroup *update_group = new QButtonGroup(this);
ASSERT(update_group);
if (!update_group) return;
update_group->setTitle(i18n("update"));
guiLayout->addWidget(update_group);
QVBoxLayout *updateLayout = new QVBoxLayout(update_group, 10);
ASSERT(updateLayout);
if (!updateLayout) return;
updateLayout->addSpacing(update_group->fontMetrics().height());
QCheckBox *cbTrackChanges = new QCheckBox(update_group);
ASSERT(cbTrackChanges);
if (!cbTrackChanges) return;
cbTrackChanges->setText(i18n("on modifications"));
updateLayout->addWidget(cbTrackChanges);
cbTrackChanges->setMinimumSize(cbTrackChanges->sizeHint());
QToolTip::add(cbTrackChanges, i18n(
"automatically update the sonagram\n"\
"if the signal data has modified"));
QCheckBox *cbFollowSelection = new QCheckBox(update_group);
ASSERT(cbFollowSelection);
if (!cbFollowSelection) return;
cbFollowSelection->setText(i18n("follow selection"));
updateLayout->addWidget(cbFollowSelection);
cbFollowSelection->setMinimumSize(cbFollowSelection->sizeHint());
QToolTip::add(cbFollowSelection, i18n(
"automatically update the sonagram if the selection\n"\
"has been enlarged, reduced or moved"));
cbTrackChanges->setEnabled(false); // ###
cbFollowSelection->setEnabled(false); // ###
// ----------------------------------------------------------------------
// --- OK and Cancel buttons ----------------------------------------
// ----------------------------------------------------------------------
// -- separator --
QFrame *separator = new QFrame(this, "separator line");
ASSERT(separator);
if (!separator) return;
separator->setFrameStyle(QFrame::HLine | QFrame::Sunken);
separator->setFixedHeight(separator->sizeHint().height());
topLayout->addWidget(separator);
m_ok = new QPushButton (OK, this);
ASSERT(m_ok);
if (!m_ok) return;
m_cancel = new QPushButton (CANCEL, this);
ASSERT(m_cancel);
if (!m_cancel) return;
QHBoxLayout *buttonsLayout = new QHBoxLayout();
ASSERT(buttonsLayout);
if (!buttonsLayout) return;
topLayout->addLayout(buttonsLayout);
h = max(m_ok->sizeHint().height(), m_cancel->sizeHint().height());
w = max(m_ok->sizeHint().width(), m_cancel->sizeHint().width());
m_ok->setFixedSize(w, h);
m_cancel->setFixedSize(w, h);
buttonsLayout->addStretch(10);
buttonsLayout->addWidget(m_cancel, 0, AlignLeft | AlignCenter);
buttonsLayout->addSpacing(10);
buttonsLayout->addWidget(m_ok, 0, AlignRight | AlignCenter);
// activate the layout and set the window size
fftLayout->activate();
displayLayout->activate();
updateLayout->activate();
topLayout->activate();
w = sizeHint().width();
h = sizeHint().height();
topLayout->freeze(w, h);
// Set a size hint:
// try to make the image's aspect ratio (a) = sqrt(2)
//
// samples: s
// fft_points: p
// image_width: w = s / p
// image_height: h = p / 2
// a = w / h = 2*s / (p^2)
// => p = sqrt( 2 * s / a) )
const double aspect_ratio = sqrt(2);
double p = sqrt(2.0*(double)m_length/aspect_ratio);
// round down to an exponent of 2, this makes the image more
// wide than heigh and gives a fast calculation
int bits = (int)floor(log(p) / log(2));
if (bits < 2) bits = 2;
if (bits > 16) bits = 16;
setPoints(1 << (bits-1));
setBoxPoints(0);
m_ok->setAccel(Key_Return);
m_cancel->setAccel(Key_Escape);
m_ok->setFocus();
connect(m_ok , SIGNAL(clicked()), SLOT(accept()));
connect(m_cancel , SIGNAL(clicked()), SLOT(reject()));
connect(m_pointslider, SIGNAL(valueChanged(int)), SLOT(setPoints(int)));
connect(m_pointbox, SIGNAL(activated(int)), SLOT(setBoxPoints(int)));
}
//***************************************************************************
void SonagramDialog::parameters(QStrList &list)
{
ASSERT(m_pointbox);
ASSERT(m_windowtypebox);
ASSERT(m_rbColor);
QString param;
list.setAutoDelete(true);
list.clear();
// parameter #0: number of fft points
param = m_pointbox ? m_pointbox->currentText() : 0;
list.append(param);
// parameter #1: index of the window function
param.setNum(m_windowtypebox ? m_windowtypebox->currentItem() : 0);
list.append(param);
// parameter #2: flag: use color instead of greyscale
param.setNum(m_rbColor ? (m_rbColor->isChecked() ? 1 : 0) : 0);
list.append(param);
}
//***************************************************************************
void SonagramDialog::setPoints(int points)
{
ASSERT(points >= 0);
char ms_buf[32];
char buf[512];
points *= 2;
snprintf(buf, sizeof(buf), "%d", points);
m_pointbox->changeItem (buf, 0);
m_pointbox->setCurrentItem (0);
KwavePlugin::ms2string(ms_buf, sizeof(ms_buf), points * 1.0E3 / m_rate);
snprintf(buf, sizeof(buf), i18n("( resulting window size: %s )"), ms_buf);
m_windowlabel->setText(buf);
snprintf(buf, sizeof(buf), i18n("size of bitmap: %dx%d"),
(m_length / points) + 1, points/2);
m_bitmaplabel->setText(buf);
}
//***************************************************************************
void SonagramDialog::setBoxPoints(int num)
{
ASSERT(num >= 0);
int points = strtol(m_pointbox->text (num), 0, 0);
m_pointslider->setValue(points / 2);
}
//***************************************************************************
SonagramDialog::~SonagramDialog ()
{
debug("SonagramDialog::~SonagramDialog() done");
}
//***************************************************************************
//***************************************************************************