annotate widgets/PluginParameterDialog.cpp @ 148:4e7980e3b48e

* Handle plugins with weird block size requirements properly
author Chris Cannam
date Wed, 20 Sep 2006 15:47:16 +0000
parents e2ad3543c8a1
children 3dade4b025b7
rev   line source
Chris@62 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@62 2
Chris@62 3 /*
Chris@62 4 Sonic Visualiser
Chris@62 5 An audio file viewer and annotation editor.
Chris@62 6 Centre for Digital Music, Queen Mary, University of London.
Chris@62 7 This file copyright 2006 Chris Cannam.
Chris@62 8
Chris@62 9 This program is free software; you can redistribute it and/or
Chris@62 10 modify it under the terms of the GNU General Public License as
Chris@62 11 published by the Free Software Foundation; either version 2 of the
Chris@62 12 License, or (at your option) any later version. See the file
Chris@62 13 COPYING included with this distribution for more information.
Chris@62 14 */
Chris@62 15
Chris@62 16 #include "PluginParameterDialog.h"
Chris@62 17
Chris@62 18 #include "PluginParameterBox.h"
Chris@140 19 #include "WindowTypeSelector.h"
Chris@62 20
Chris@106 21 #include "vamp-sdk/Plugin.h"
Chris@147 22 #include "vamp-sdk/PluginHostAdapter.h"
Chris@106 23
Chris@62 24 #include <QGridLayout>
Chris@62 25 #include <QLabel>
Chris@62 26 #include <QGroupBox>
Chris@62 27 #include <QHBoxLayout>
Chris@143 28 #include <QVBoxLayout>
Chris@62 29 #include <QPushButton>
Chris@69 30 #include <QMessageBox>
Chris@69 31 #include <QComboBox>
Chris@143 32 #include <QSettings>
Chris@62 33
Chris@71 34 PluginParameterDialog::PluginParameterDialog(Vamp::PluginBase *plugin,
Chris@69 35 int sourceChannels,
Chris@69 36 int targetChannels,
Chris@69 37 int defaultChannel,
Chris@106 38 QString output,
Chris@140 39 bool showWindowSize,
Chris@140 40 bool showFrequencyDomainOptions,
Chris@62 41 QWidget *parent) :
Chris@62 42 QDialog(parent),
Chris@62 43 m_plugin(plugin),
Chris@69 44 m_channel(defaultChannel),
Chris@145 45 m_stepSize(0),
Chris@145 46 m_blockSize(0),
Chris@145 47 m_windowType(HanningWindow),
Chris@62 48 m_parameterBox(0)
Chris@62 49 {
Chris@122 50 setWindowTitle(tr("Plugin Parameters"));
Chris@122 51
Chris@62 52 QGridLayout *grid = new QGridLayout;
Chris@62 53 setLayout(grid);
Chris@62 54
Chris@62 55 QGroupBox *pluginBox = new QGroupBox;
Chris@62 56 pluginBox->setTitle(tr("Plugin"));
Chris@62 57 grid->addWidget(pluginBox, 0, 0);
Chris@62 58
Chris@62 59 QGridLayout *subgrid = new QGridLayout;
Chris@62 60 pluginBox->setLayout(subgrid);
Chris@62 61
Chris@63 62 subgrid->setSpacing(0);
Chris@63 63 subgrid->setMargin(10);
Chris@63 64
Chris@62 65 QFont font(pluginBox->font());
Chris@62 66 font.setBold(true);
Chris@62 67
Chris@62 68 QLabel *nameLabel = new QLabel(plugin->getDescription().c_str());
Chris@146 69 nameLabel->setWordWrap(true);
Chris@62 70 nameLabel->setFont(font);
Chris@62 71
Chris@62 72 QLabel *makerLabel = new QLabel(plugin->getMaker().c_str());
Chris@146 73 makerLabel->setWordWrap(true);
Chris@62 74
Chris@106 75 QLabel *outputLabel = 0;
Chris@106 76
Chris@106 77 if (output != "") {
Chris@106 78
Chris@147 79 Vamp::PluginHostAdapter *fePlugin = dynamic_cast<Vamp::PluginHostAdapter *>(m_plugin);
Chris@106 80
Chris@106 81 if (fePlugin) {
Chris@106 82
Chris@106 83 std::vector<Vamp::Plugin::OutputDescriptor> od =
Chris@106 84 fePlugin->getOutputDescriptors();
Chris@106 85
Chris@106 86 if (od.size() > 1) {
Chris@106 87
Chris@106 88 for (size_t i = 0; i < od.size(); ++i) {
Chris@106 89 if (od[i].name == output.toStdString()) {
Chris@106 90 outputLabel = new QLabel(od[i].description.c_str());
Chris@146 91 outputLabel->setWordWrap(true);
Chris@106 92 break;
Chris@106 93 }
Chris@106 94 }
Chris@106 95 }
Chris@106 96 }
Chris@106 97 }
Chris@106 98
Chris@62 99 QLabel *versionLabel = new QLabel(QString("%1")
Chris@62 100 .arg(plugin->getPluginVersion()));
Chris@146 101 versionLabel->setWordWrap(true);
Chris@62 102
Chris@62 103 QLabel *copyrightLabel = new QLabel(plugin->getCopyright().c_str());
Chris@146 104 copyrightLabel->setWordWrap(true);
Chris@62 105
Chris@63 106 QLabel *typeLabel = new QLabel(plugin->getType().c_str());
Chris@146 107 typeLabel->setWordWrap(true);
Chris@63 108 typeLabel->setFont(font);
Chris@63 109
Chris@147 110 QLabel *label = new QLabel(tr("Name:"));
Chris@147 111 label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
Chris@147 112 subgrid->addWidget(label, 0, 0);
Chris@62 113 subgrid->addWidget(nameLabel, 0, 1);
Chris@62 114
Chris@147 115 label = new QLabel(tr("Type:"));
Chris@147 116 label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
Chris@147 117 subgrid->addWidget(label, 1, 0);
Chris@63 118 subgrid->addWidget(typeLabel, 1, 1);
Chris@62 119
Chris@106 120 int outputOffset = 0;
Chris@106 121 if (outputLabel) {
Chris@147 122 label = new QLabel(tr("Output:"));
Chris@147 123 label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
Chris@147 124 subgrid->addWidget(label, 2, 0);
Chris@106 125 subgrid->addWidget(outputLabel, 2, 1);
Chris@106 126 outputOffset = 1;
Chris@106 127 }
Chris@62 128
Chris@147 129 label = new QLabel(tr("Maker:"));
Chris@147 130 label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
Chris@147 131 subgrid->addWidget(label, 2 + outputOffset, 0);
Chris@106 132 subgrid->addWidget(makerLabel, 2 + outputOffset, 1);
Chris@63 133
Chris@147 134 label = new QLabel(tr("Copyright: "));
Chris@147 135 label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
Chris@147 136 subgrid->addWidget(label, 3 + outputOffset, 0);
Chris@106 137 subgrid->addWidget(copyrightLabel, 3 + outputOffset, 1);
Chris@106 138
Chris@147 139 label = new QLabel(tr("Version:"));
Chris@147 140 label->setAlignment(Qt::AlignTop | Qt::AlignLeft);
Chris@147 141 subgrid->addWidget(label, 4 + outputOffset, 0);
Chris@106 142 subgrid->addWidget(versionLabel, 4 + outputOffset, 1);
Chris@62 143
Chris@62 144 subgrid->setColumnStretch(1, 2);
Chris@62 145
Chris@62 146 QGroupBox *paramBox = new QGroupBox;
Chris@62 147 paramBox->setTitle(tr("Plugin Parameters"));
Chris@62 148 grid->addWidget(paramBox, 1, 0);
Chris@62 149 grid->setRowStretch(1, 10);
Chris@62 150
Chris@62 151 QHBoxLayout *paramLayout = new QHBoxLayout;
Chris@63 152 paramLayout->setMargin(0);
Chris@62 153 paramBox->setLayout(paramLayout);
Chris@62 154
Chris@62 155 m_parameterBox = new PluginParameterBox(m_plugin);
Chris@64 156 connect(m_parameterBox, SIGNAL(pluginConfigurationChanged(QString)),
Chris@64 157 this, SIGNAL(pluginConfigurationChanged(QString)));
Chris@62 158 paramLayout->addWidget(m_parameterBox);
Chris@62 159
Chris@143 160 m_advanced = new QFrame;
Chris@143 161 QVBoxLayout *advancedLayout = new QVBoxLayout;
Chris@143 162 advancedLayout->setMargin(0);
Chris@143 163 m_advanced->setLayout(advancedLayout);
Chris@143 164 grid->addWidget(m_advanced, 2, 0);
Chris@143 165
Chris@143 166 bool haveAdvanced = false;
Chris@143 167
Chris@69 168 if (sourceChannels != targetChannels) {
Chris@69 169
Chris@69 170 // At the moment we can only cope with the case where
Chris@69 171 // sourceChannels > targetChannels and targetChannels == 1
Chris@69 172
Chris@69 173 if (sourceChannels < targetChannels) {
Chris@69 174
Chris@69 175 QMessageBox::warning
Chris@69 176 (parent,
Chris@69 177 tr("Channel mismatch"),
Chris@69 178 tr("This plugin requires at least %1 input channels, but only %2 %3 available. The plugin probably will not work correctly.").arg(targetChannels).arg(sourceChannels).arg(sourceChannels != 1 ? tr("are") : tr("is")),
Chris@69 179 QMessageBox::Ok,
Chris@69 180 QMessageBox::NoButton);
Chris@69 181
Chris@69 182 } else {
Chris@69 183
Chris@69 184 QGroupBox *channelBox = new QGroupBox;
Chris@69 185 channelBox->setTitle(tr("Channels"));
Chris@143 186 advancedLayout->addWidget(channelBox);
Chris@143 187 haveAdvanced = true;
Chris@69 188
Chris@69 189 QVBoxLayout *channelLayout = new QVBoxLayout;
Chris@69 190 channelBox->setLayout(channelLayout);
Chris@69 191
Chris@69 192 if (targetChannels != 1) {
Chris@69 193
Chris@69 194 channelLayout->addWidget
Chris@69 195 (new QLabel(tr("This plugin accepts no more than %1 input channels,\nbut %2 are available. Only the first %3 will be used.\n")
Chris@69 196 .arg(targetChannels)
Chris@69 197 .arg(sourceChannels)
Chris@69 198 .arg(targetChannels)));
Chris@69 199
Chris@69 200 } else {
Chris@69 201
Chris@69 202 channelLayout->addWidget(new QLabel(tr("This plugin only has a single channel input,\nbut the source has %1 channels.").arg(sourceChannels)));
Chris@69 203
Chris@69 204 QComboBox *channelCombo = new QComboBox;
Chris@76 205 channelCombo->addItem(tr("Use mean of source channels"));
Chris@69 206 for (int i = 0; i < sourceChannels; ++i) {
Chris@69 207 channelCombo->addItem(tr("Use channel %1 only").arg(i + 1));
Chris@69 208 }
Chris@69 209
Chris@69 210 connect(channelCombo, SIGNAL(activated(int)),
Chris@69 211 this, SLOT(channelComboChanged(int)));
Chris@69 212
Chris@69 213 channelLayout->addWidget(channelCombo);
Chris@69 214 }
Chris@69 215 }
Chris@69 216 }
Chris@69 217
Chris@140 218 if (showWindowSize) {
Chris@140 219
Chris@147 220 Vamp::PluginHostAdapter *fePlugin = dynamic_cast<Vamp::PluginHostAdapter *>(m_plugin);
Chris@140 221 int size = 1024;
Chris@140 222 int increment = 1024;
Chris@140 223 if (fePlugin) {
Chris@140 224 size = fePlugin->getPreferredBlockSize();
Chris@146 225 std::cerr << "Feature extraction plugin \"" << fePlugin->getDescription() << "\" reports preferred block size as " << size << std::endl;
Chris@140 226 if (size == 0) size = 1024;
Chris@140 227 increment = fePlugin->getPreferredStepSize();
Chris@146 228 if (increment == 0) {
Chris@146 229 if (fePlugin->getInputDomain() == Vamp::Plugin::TimeDomain) {
Chris@146 230 increment = size;
Chris@146 231 } else {
Chris@146 232 increment = size/2;
Chris@146 233 }
Chris@146 234 }
Chris@146 235 } else {
Chris@147 236 std::cerr << "Plugin " << plugin << " is not a feature extraction plugin (it's a " << typeid(*plugin).name() << ")" << std::endl;//!!!
Chris@140 237 }
Chris@140 238
Chris@140 239 QGroupBox *windowBox = new QGroupBox;
Chris@140 240 windowBox->setTitle(tr("Processing"));
Chris@143 241 advancedLayout->addWidget(windowBox);
Chris@143 242 haveAdvanced = true;
Chris@140 243
Chris@140 244 QGridLayout *windowLayout = new QGridLayout;
Chris@140 245 windowBox->setLayout(windowLayout);
Chris@140 246
Chris@140 247 if (showFrequencyDomainOptions) {
Chris@140 248 windowLayout->addWidget(new QLabel(tr("Window size:")), 0, 0);
Chris@140 249 } else {
Chris@140 250 windowLayout->addWidget(new QLabel(tr("Audio frames per block:")), 0, 0);
Chris@140 251 }
Chris@140 252
Chris@143 253 std::cerr << "size: " << size << ", increment: " << increment << std::endl;
Chris@143 254
Chris@145 255 //!!! deal with block and step sizes (coming from the plugin's
Chris@145 256 // preferences) that don't fit into the default list
Chris@145 257
Chris@140 258 QComboBox *blockSizeCombo = new QComboBox;
Chris@140 259 blockSizeCombo->setEditable(true);
Chris@147 260 bool found = false;
Chris@143 261 for (int i = 0; i < 14; ++i) {
Chris@140 262 int val = pow(2, i + 3);
Chris@140 263 blockSizeCombo->addItem(QString("%1").arg(val));
Chris@147 264 if (val == size) {
Chris@147 265 blockSizeCombo->setCurrentIndex(i);
Chris@147 266 found = true;
Chris@147 267 }
Chris@147 268 }
Chris@147 269 if (!found) {
Chris@147 270 blockSizeCombo->addItem(QString("%1").arg(size));
Chris@147 271 blockSizeCombo->setCurrentIndex(blockSizeCombo->count() - 1);
Chris@140 272 }
Chris@143 273 blockSizeCombo->setValidator(new QIntValidator(1, pow(2, 18), this));
Chris@146 274 connect(blockSizeCombo, SIGNAL(textChanged(QString)),
Chris@145 275 this, SLOT(blockSizeComboChanged(QString)));
Chris@140 276 windowLayout->addWidget(blockSizeCombo, 0, 1);
Chris@140 277
Chris@148 278 windowLayout->addWidget(new QLabel(tr("Window increment:")), 1, 0);
Chris@148 279
Chris@148 280 QComboBox *incrementCombo = new QComboBox;
Chris@148 281 incrementCombo->setEditable(true);
Chris@148 282 found = false;
Chris@148 283 for (int i = 0; i < 14; ++i) {
Chris@148 284 int val = pow(2, i + 3);
Chris@148 285 incrementCombo->addItem(QString("%1").arg(val));
Chris@148 286 if (val == increment) {
Chris@148 287 incrementCombo->setCurrentIndex(i);
Chris@148 288 found = true;
Chris@148 289 }
Chris@148 290 }
Chris@148 291 if (!found) {
Chris@148 292 incrementCombo->addItem(QString("%1").arg(increment));
Chris@148 293 incrementCombo->setCurrentIndex(incrementCombo->count() - 1);
Chris@148 294 }
Chris@148 295 incrementCombo->setValidator(new QIntValidator(1, pow(2, 18), this));
Chris@148 296 connect(incrementCombo, SIGNAL(textChanged(QString)),
Chris@148 297 this, SLOT(incrementComboChanged(QString)));
Chris@148 298 windowLayout->addWidget(incrementCombo, 1, 1);
Chris@148 299
Chris@140 300 if (showFrequencyDomainOptions) {
Chris@140 301
Chris@140 302 windowLayout->addWidget(new QLabel(tr("Window shape:")), 2, 0);
Chris@140 303 WindowTypeSelector *windowTypeSelector = new WindowTypeSelector;
Chris@145 304 connect(windowTypeSelector, SIGNAL(windowTypeChanged(WindowType type)),
Chris@145 305 this, SLOT(windowTypeChanged(type)));
Chris@140 306 windowLayout->addWidget(windowTypeSelector, 2, 1);
Chris@140 307 }
Chris@140 308 }
Chris@140 309
Chris@62 310 QHBoxLayout *hbox = new QHBoxLayout;
Chris@140 311 grid->addLayout(hbox, 4, 0);
Chris@143 312
Chris@143 313 bool advancedVisible = false;
Chris@143 314
Chris@143 315 if (haveAdvanced) {
Chris@143 316
Chris@143 317 m_advancedButton = new QPushButton(tr("Advanced >>"));
Chris@143 318 m_advancedButton->setCheckable(true);
Chris@143 319 connect(m_advancedButton, SIGNAL(clicked()), this, SLOT(advancedToggled()));
Chris@143 320
Chris@143 321 QSettings settings;
Chris@143 322 settings.beginGroup("PluginParameterDialog");
Chris@143 323 advancedVisible = settings.value("advancedvisible", false).toBool();
Chris@143 324 settings.endGroup();
Chris@143 325
Chris@143 326 m_advanced->setVisible(false);
Chris@143 327
Chris@143 328 hbox->addWidget(m_advancedButton);
Chris@143 329 }
Chris@143 330
Chris@62 331 QPushButton *ok = new QPushButton(tr("OK"));
Chris@62 332 QPushButton *cancel = new QPushButton(tr("Cancel"));
Chris@62 333 hbox->addStretch(10);
Chris@62 334 hbox->addWidget(ok);
Chris@62 335 hbox->addWidget(cancel);
Chris@62 336 connect(ok, SIGNAL(clicked()), this, SLOT(accept()));
Chris@62 337 connect(cancel, SIGNAL(clicked()), this, SLOT(reject()));
Chris@143 338
Chris@143 339 if (advancedVisible) {
Chris@143 340 m_advancedButton->setChecked(true);
Chris@143 341 advancedToggled();
Chris@143 342 }
Chris@62 343 }
Chris@62 344
Chris@62 345 PluginParameterDialog::~PluginParameterDialog()
Chris@62 346 {
Chris@62 347 }
Chris@62 348
Chris@69 349 void
Chris@145 350 PluginParameterDialog::getProcessingParameters(size_t &blockSize) const
Chris@145 351 {
Chris@145 352 blockSize = m_blockSize;
Chris@145 353 return;
Chris@145 354 }
Chris@145 355
Chris@145 356 void
Chris@145 357 PluginParameterDialog::getProcessingParameters(size_t &stepSize,
Chris@145 358 size_t &blockSize,
Chris@145 359 WindowType &windowType) const
Chris@145 360 {
Chris@145 361 stepSize = m_stepSize;
Chris@145 362 blockSize = m_blockSize;
Chris@145 363 windowType = m_windowType;
Chris@146 364 return;
Chris@145 365 }
Chris@145 366
Chris@145 367 void
Chris@145 368 PluginParameterDialog::blockSizeComboChanged(QString text)
Chris@145 369 {
Chris@145 370 m_blockSize = text.toInt();
Chris@146 371 std::cerr << "Block size changed to " << m_blockSize << std::endl;
Chris@145 372 }
Chris@145 373
Chris@145 374 void
Chris@145 375 PluginParameterDialog::incrementComboChanged(QString text)
Chris@145 376 {
Chris@145 377 m_stepSize = text.toInt();
Chris@146 378 //!!! rename increment to step size throughout
Chris@147 379 std::cerr << "Increment changed to " << m_stepSize << std::endl;
Chris@145 380 }
Chris@145 381
Chris@145 382 void
Chris@145 383 PluginParameterDialog::windowTypeChanged(WindowType type)
Chris@145 384 {
Chris@145 385 m_windowType = type;
Chris@145 386 }
Chris@145 387
Chris@145 388 void
Chris@143 389 PluginParameterDialog::advancedToggled()
Chris@143 390 {
Chris@143 391 bool visible = !m_advanced->isVisible();
Chris@143 392
Chris@143 393 m_advanced->setVisible(visible);
Chris@143 394
Chris@143 395 if (visible) m_advancedButton->setText(tr("Advanced <<"));
Chris@143 396 else m_advancedButton->setText(tr("Advanced >>"));
Chris@143 397
Chris@143 398 QSettings settings;
Chris@143 399 settings.beginGroup("PluginParameterDialog");
Chris@143 400 settings.setValue("advancedvisible", visible);
Chris@143 401 settings.endGroup();
Chris@143 402
Chris@143 403 std::cerr << "resize to " << sizeHint().width() << " x " << sizeHint().height() << std::endl;
Chris@143 404
Chris@143 405 setMaximumSize(sizeHint());
Chris@143 406 }
Chris@143 407
Chris@143 408 void
Chris@69 409 PluginParameterDialog::channelComboChanged(int index)
Chris@69 410 {
Chris@69 411 m_channel = index - 1;
Chris@69 412 }
Chris@69 413