annotate src/Analyser.cpp @ 273:29005aa146e2

fixed candidates exceeding the frequency-constrained selection horizontally (vertically still to come)
author matthiasm
date Thu, 10 Apr 2014 17:55:43 +0100
parents 8eb167e817e7
children 2a2fd6eb8fa8
rev   line source
Chris@6 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@6 2
Chris@6 3 /*
Chris@6 4 Tony
Chris@6 5 An intonation analysis and annotation tool
Chris@6 6 Centre for Digital Music, Queen Mary, University of London.
Chris@6 7 This file copyright 2006-2012 Chris Cannam and QMUL.
Chris@6 8
Chris@6 9 This program is free software; you can redistribute it and/or
Chris@6 10 modify it under the terms of the GNU General Public License as
Chris@6 11 published by the Free Software Foundation; either version 2 of the
Chris@6 12 License, or (at your option) any later version. See the file
Chris@6 13 COPYING included with this distribution for more information.
Chris@6 14 */
Chris@6 15
Chris@6 16 #include "Analyser.h"
Chris@6 17
Chris@6 18 #include "transform/TransformFactory.h"
Chris@6 19 #include "transform/ModelTransformer.h"
gyorgyf@14 20 #include "transform/FeatureExtractionModelTransformer.h"
Chris@6 21 #include "framework/Document.h"
Chris@6 22 #include "data/model/WaveFileModel.h"
Chris@6 23 #include "view/Pane.h"
Chris@6 24 #include "view/PaneStack.h"
Chris@6 25 #include "layer/Layer.h"
Chris@6 26 #include "layer/TimeValueLayer.h"
matthiasm@13 27 #include "layer/NoteLayer.h"
matthiasm@11 28 #include "layer/FlexiNoteLayer.h"
Chris@120 29 #include "layer/WaveformLayer.h"
Chris@6 30 #include "layer/ColourDatabase.h"
Chris@145 31 #include "layer/ColourMapper.h"
gyorgyf@16 32 #include "layer/LayerFactory.h"
Chris@145 33 #include "layer/SpectrogramLayer.h"
Chris@161 34 #include "layer/Colour3DPlotLayer.h"
Chris@199 35 #include "layer/ShowLayerCommand.h"
Chris@6 36
Chris@83 37 #include <QSettings>
Chris@83 38
Chris@163 39 using std::vector;
Chris@163 40
Chris@6 41 Analyser::Analyser() :
Chris@6 42 m_document(0),
Chris@6 43 m_fileModel(0),
Chris@133 44 m_paneStack(0),
Chris@188 45 m_pane(0),
Chris@188 46 m_currentCandidate(-1),
Chris@188 47 m_candidatesVisible(false)
Chris@6 48 {
Chris@83 49 QSettings settings;
Chris@83 50 settings.beginGroup("LayerDefaults");
Chris@83 51 settings.setValue
Chris@83 52 ("timevalues",
Chris@83 53 QString("<layer verticalScale=\"%1\" plotStyle=\"%2\" "
Chris@83 54 "scaleMinimum=\"%3\" scaleMaximum=\"%4\"/>")
Chris@145 55 .arg(int(TimeValueLayer::AutoAlignScale))
matthiasm@182 56 .arg(int(TimeValueLayer::PlotPoints))
Chris@83 57 .arg(27.5f).arg(880.f)); // temporary values: better get the real extents of the data from the model
Chris@83 58 settings.setValue
Chris@83 59 ("flexinotes",
Chris@83 60 QString("<layer verticalScale=\"%1\"/>")
Chris@83 61 .arg(int(FlexiNoteLayer::AutoAlignScale)));
Chris@83 62 settings.endGroup();
Chris@6 63 }
Chris@6 64
Chris@6 65 Analyser::~Analyser()
Chris@6 66 {
Chris@6 67 }
Chris@6 68
Chris@140 69 QString
Chris@6 70 Analyser::newFileLoaded(Document *doc, WaveFileModel *model,
Chris@6 71 PaneStack *paneStack, Pane *pane)
Chris@6 72 {
Chris@6 73 m_document = doc;
Chris@6 74 m_fileModel = model;
Chris@133 75 m_paneStack = paneStack;
Chris@6 76 m_pane = pane;
Chris@6 77
Chris@242 78 connect(doc, SIGNAL(layerAboutToBeDeleted(Layer *)),
Chris@242 79 this, SLOT(layerAboutToBeDeleted(Layer *)));
Chris@242 80
Chris@165 81 m_reAnalysingSelection = Selection();
Chris@165 82 m_reAnalysisCandidates.clear();
Chris@167 83 m_currentCandidate = -1;
Chris@184 84 m_candidatesVisible = false;
Chris@165 85
Chris@161 86 // Note that we need at least one main-model layer (time ruler,
Chris@161 87 // waveform or what have you). It could be hidden if we don't want
Chris@161 88 // to see it but it must exist.
Chris@6 89
Chris@161 90 QString warning, error;
Chris@161 91
Chris@260 92 cerr << "Analyser::newFileLoaded: about to check visualisations etc" << endl;
Chris@260 93
Chris@161 94 // This isn't fatal -- we can proceed without
Chris@161 95 // visualisations. Other failures are fatal though.
Chris@161 96 warning = addVisualisations();
Chris@161 97
Chris@161 98 error = addWaveform();
Chris@161 99 if (error != "") return error;
Chris@161 100
Chris@161 101 error = addAnalyses();
Chris@161 102 if (error != "") return error;
Chris@161 103
Chris@161 104 loadState(Audio);
Chris@161 105 loadState(PitchTrack);
Chris@161 106 loadState(Notes);
Chris@161 107 loadState(Spectrogram);
Chris@161 108
Chris@260 109 stackLayers();
Chris@260 110
Chris@161 111 emit layersChanged();
Chris@161 112
Chris@161 113 return warning;
Chris@161 114 }
Chris@161 115
Chris@226 116 void
Chris@226 117 Analyser::fileClosed()
Chris@226 118 {
Chris@226 119 cerr << "Analyser::fileClosed" << endl;
Chris@226 120 m_layers.clear();
Chris@226 121 m_reAnalysisCandidates.clear();
Chris@226 122 m_currentCandidate = -1;
Chris@226 123 m_reAnalysingSelection = Selection();
Chris@226 124 }
Chris@226 125
Chris@227 126 bool
Chris@227 127 Analyser::getDisplayFrequencyExtents(float &min, float &max)
Chris@227 128 {
Chris@227 129 if (!m_layers[Spectrogram]) return false;
Chris@227 130 return m_layers[Spectrogram]->getDisplayExtents(min, max);
Chris@227 131 }
Chris@227 132
Chris@227 133 bool
Chris@227 134 Analyser::setDisplayFrequencyExtents(float min, float max)
Chris@227 135 {
Chris@227 136 if (!m_layers[Spectrogram]) return false;
Chris@227 137 m_layers[Spectrogram]->setDisplayExtents(min, max);
Chris@242 138 return true;
Chris@227 139 }
Chris@227 140
Chris@161 141 QString
Chris@161 142 Analyser::addVisualisations()
Chris@161 143 {
Chris@227 144 // A spectrogram, off by default. Must go at the back because it's
Chris@227 145 // opaque
Chris@227 146
Chris@227 147 /* This is roughly what we'd do for a constant-Q spectrogram, but it
Chris@227 148 currently has issues with y-axis alignment
Chris@227 149
Chris@161 150 TransformFactory *tf = TransformFactory::getInstance();
Chris@161 151
Chris@161 152 QString name = "Constant-Q";
Chris@161 153 QString base = "vamp:cqvamp:cqvamp:";
Chris@161 154 QString out = "constantq";
Chris@6 155
Chris@161 156 QString notFound = tr("Transform \"%1\" not found, spectrogram will not be enabled.<br><br>Is the %2 Vamp plugin correctly installed?");
Chris@161 157 if (!tf->haveTransform(base + out)) {
Chris@161 158 return notFound.arg(base + out).arg(name);
Chris@161 159 }
Chris@161 160
Chris@161 161 Transform transform = tf->getDefaultTransformFor
Chris@161 162 (base + out, m_fileModel->getSampleRate());
Chris@162 163 transform.setParameter("bpo", 36);
Chris@161 164
Chris@161 165 Colour3DPlotLayer *spectrogram = qobject_cast<Colour3DPlotLayer *>
Chris@161 166 (m_document->createDerivedLayer(transform, m_fileModel));
Chris@161 167
Chris@161 168 if (!spectrogram) return tr("Transform \"%1\" did not run correctly (no layer or wrong layer type returned)").arg(base + out);
Chris@165 169 */
Chris@165 170
Chris@260 171 // As with all the visualisation layers, if we already have one in
Chris@260 172 // the pane we do not create another, just record its
Chris@260 173 // existence. (We create a new one when loading a new audio file,
Chris@260 174 // but just note the existing one when loading a complete session.)
Chris@260 175
Chris@260 176 for (int i = 0; i < m_pane->getLayerCount(); ++i) {
Chris@260 177 SpectrogramLayer *existing = qobject_cast<SpectrogramLayer *>
Chris@260 178 (m_pane->getLayer(i));
Chris@260 179 if (existing) {
Chris@260 180 cerr << "recording existing spectrogram layer" << endl;
Chris@260 181 m_layers[Spectrogram] = existing;
Chris@260 182 return "";
Chris@260 183 }
Chris@260 184 }
Chris@260 185
Chris@145 186 SpectrogramLayer *spectrogram = qobject_cast<SpectrogramLayer *>
Chris@145 187 (m_document->createMainModelLayer(LayerFactory::MelodicRangeSpectrogram));
Chris@165 188
Chris@145 189 spectrogram->setColourMap((int)ColourMapper::BlackOnWhite);
Chris@162 190 spectrogram->setNormalizeHybrid(true);
Chris@165 191 spectrogram->setGain(100);
Chris@145 192 m_document->addLayerToView(m_pane, spectrogram);
Chris@145 193 spectrogram->setLayerDormant(m_pane, true);
Chris@145 194
Chris@145 195 m_layers[Spectrogram] = spectrogram;
Chris@145 196
Chris@161 197 return "";
Chris@161 198 }
Chris@161 199
Chris@161 200 QString
Chris@161 201 Analyser::addWaveform()
Chris@161 202 {
Chris@131 203 // Our waveform layer is just a shadow, light grey and taking up
Chris@260 204 // little space at the bottom.
Chris@260 205
Chris@260 206 // As with the spectrogram above, if one exists already we just
Chris@260 207 // use it
Chris@260 208 for (int i = 0; i < m_pane->getLayerCount(); ++i) {
Chris@260 209 WaveformLayer *existing = qobject_cast<WaveformLayer *>
Chris@260 210 (m_pane->getLayer(i));
Chris@260 211 if (existing) {
Chris@260 212 cerr << "recording existing waveform layer" << endl;
Chris@260 213 m_layers[Audio] = existing;
Chris@260 214 return "";
Chris@260 215 }
Chris@260 216 }
Chris@120 217
Chris@120 218 WaveformLayer *waveform = qobject_cast<WaveformLayer *>
Chris@120 219 (m_document->createMainModelLayer(LayerFactory::Waveform));
Chris@120 220
Chris@120 221 waveform->setMiddleLineHeight(0.9);
Chris@120 222 waveform->setShowMeans(false); // too small & pale for this
Chris@120 223 waveform->setBaseColour
Chris@120 224 (ColourDatabase::getInstance()->getColourIndex(tr("Grey")));
Chris@130 225 PlayParameters *params = waveform->getPlayParameters();
justin@160 226 if (params) params->setPlayPan(-1);
Chris@128 227
Chris@128 228 m_document->addLayerToView(m_pane, waveform);
Chris@120 229
Chris@128 230 m_layers[Audio] = waveform;
Chris@161 231 return "";
Chris@161 232 }
Chris@161 233
Chris@161 234 QString
Chris@161 235 Analyser::addAnalyses()
Chris@161 236 {
Chris@260 237 // As with the spectrogram above, if these layers exist we use
Chris@260 238 // them
Chris@260 239 TimeValueLayer *existingPitch = 0;
Chris@260 240 FlexiNoteLayer *existingNotes = 0;
Chris@260 241 for (int i = 0; i < m_pane->getLayerCount(); ++i) {
Chris@260 242 if (!existingPitch) {
Chris@260 243 existingPitch = qobject_cast<TimeValueLayer *>(m_pane->getLayer(i));
Chris@260 244 }
Chris@260 245 if (!existingNotes) {
Chris@260 246 existingNotes = qobject_cast<FlexiNoteLayer *>(m_pane->getLayer(i));
Chris@260 247 }
Chris@260 248 }
Chris@260 249 if (existingPitch && existingNotes) {
Chris@260 250 cerr << "recording existing pitch and notes layers" << endl;
Chris@260 251 m_layers[PitchTrack] = existingPitch;
Chris@260 252 m_layers[Notes] = existingNotes;
Chris@260 253 return "";
Chris@260 254 } else if (existingPitch || existingNotes) {
Chris@260 255 return "One (but not both) of pitch and note track found in session";
Chris@260 256 }
Chris@260 257
Chris@161 258 TransformFactory *tf = TransformFactory::getInstance();
Chris@161 259
Chris@161 260 QString plugname = "pYIN";
Chris@161 261 QString base = "vamp:pyin:pyin:";
Chris@161 262 QString f0out = "smoothedpitchtrack";
Chris@161 263 QString noteout = "notes";
Chris@120 264
Chris@83 265 Transforms transforms;
Chris@138 266
Chris@138 267 /*!!! we could have more than one pitch track...
Chris@138 268 QString cx = "vamp:cepstral-pitchtracker:cepstral-pitchtracker:f0";
Chris@138 269 if (tf->haveTransform(cx)) {
Chris@138 270 Transform tx = tf->getDefaultTransformFor(cx);
Chris@138 271 TimeValueLayer *lx = qobject_cast<TimeValueLayer *>
Chris@138 272 (m_document->createDerivedLayer(tx, m_fileModel));
Chris@138 273 lx->setVerticalScale(TimeValueLayer::AutoAlignScale);
Chris@138 274 lx->setBaseColour(ColourDatabase::getInstance()->getColourIndex(tr("Bright Red")));
Chris@138 275 m_document->addLayerToView(m_pane, lx);
Chris@138 276 }
Chris@138 277 */
Chris@138 278
Chris@140 279 QString notFound = tr("Transform \"%1\" not found. Unable to analyse audio file.<br><br>Is the %2 Vamp plugin correctly installed?");
Chris@140 280 if (!tf->haveTransform(base + f0out)) {
Chris@140 281 return notFound.arg(base + f0out).arg(plugname);
Chris@140 282 }
Chris@140 283 if (!tf->haveTransform(base + noteout)) {
Chris@140 284 return notFound.arg(base + noteout).arg(plugname);
Chris@6 285 }
Chris@6 286
Chris@83 287 Transform t = tf->getDefaultTransformFor
Chris@83 288 (base + f0out, m_fileModel->getSampleRate());
Chris@83 289 t.setStepSize(256);
Chris@83 290 t.setBlockSize(2048);
Chris@6 291
Chris@83 292 transforms.push_back(t);
Chris@83 293
Chris@83 294 t.setOutput(noteout);
Chris@83 295
Chris@83 296 transforms.push_back(t);
Chris@83 297
Chris@83 298 std::vector<Layer *> layers =
Chris@83 299 m_document->createDerivedLayers(transforms, m_fileModel);
Chris@83 300
Chris@162 301 for (int i = 0; i < (int)layers.size(); ++i) {
Chris@83 302
Chris@162 303 FlexiNoteLayer *f = qobject_cast<FlexiNoteLayer *>(layers[i]);
Chris@162 304 TimeValueLayer *t = qobject_cast<TimeValueLayer *>(layers[i]);
Chris@162 305
Chris@162 306 if (f) m_layers[Notes] = f;
Chris@162 307 if (t) m_layers[PitchTrack] = t;
Chris@162 308
Chris@162 309 m_document->addLayerToView(m_pane, layers[i]);
Chris@6 310 }
Chris@161 311
Chris@162 312 ColourDatabase *cdb = ColourDatabase::getInstance();
Chris@162 313
Chris@162 314 TimeValueLayer *pitchLayer =
Chris@162 315 qobject_cast<TimeValueLayer *>(m_layers[PitchTrack]);
Chris@162 316 if (pitchLayer) {
Chris@162 317 pitchLayer->setBaseColour(cdb->getColourIndex(tr("Black")));
Chris@162 318 PlayParameters *params = pitchLayer->getPlayParameters();
Chris@162 319 if (params) params->setPlayPan(1);
Chris@162 320 }
Chris@162 321
Chris@162 322 FlexiNoteLayer *flexiNoteLayer =
Chris@162 323 qobject_cast<FlexiNoteLayer *>(m_layers[Notes]);
Chris@162 324 if (flexiNoteLayer) {
Chris@162 325 flexiNoteLayer->setBaseColour(cdb->getColourIndex(tr("Bright Blue")));
Chris@162 326 PlayParameters *params = flexiNoteLayer->getPlayParameters();
Chris@162 327 if (params) params->setPlayPan(1);
Chris@162 328 }
Chris@162 329
Chris@162 330 return "";
Chris@162 331 }
Chris@162 332
Chris@162 333 QString
Chris@192 334 Analyser::reAnalyseSelection(Selection sel, FrequencyRange range)
Chris@162 335 {
Chris@200 336 if (sel == m_reAnalysingSelection || sel.isEmpty()) return "";
Chris@165 337
Chris@252 338 if (!m_reAnalysisCandidates.empty()) {
Chris@252 339 CommandHistory::getInstance()->startCompoundOperation
Chris@252 340 (tr("Discard Previous Candidates"), true);
Chris@252 341 discardPitchCandidates();
Chris@252 342 CommandHistory::getInstance()->endCompoundOperation();
Chris@252 343 }
Chris@199 344
Chris@194 345 m_reAnalysingSelection = sel;
Chris@167 346
Chris@194 347 m_preAnalysis = Clipboard();
Chris@194 348 Layer *myLayer = m_layers[PitchTrack];
Chris@194 349 if (myLayer) {
Chris@194 350 myLayer->copy(m_pane, sel, m_preAnalysis);
Chris@194 351 }
Chris@165 352
Chris@162 353 TransformFactory *tf = TransformFactory::getInstance();
Chris@162 354
Chris@223 355 QString plugname1 = "pYIN";
Chris@223 356 QString plugname2 = "CHP";
Chris@223 357
Chris@162 358 QString base = "vamp:pyin:localcandidatepyin:";
Chris@162 359 QString out = "pitchtrackcandidates";
Chris@162 360
Chris@192 361 if (range.isConstrained()) {
Chris@223 362 base = "vamp:chp:constrainedharmonicpeak:";
Chris@223 363 out = "peak";
Chris@192 364 }
Chris@192 365
Chris@162 366 Transforms transforms;
Chris@162 367
Chris@223 368 QString notFound = tr("Transform \"%1\" not found. Unable to perform interactive analysis.<br><br>Are the %2 and %3 Vamp plugins correctly installed?");
Chris@162 369 if (!tf->haveTransform(base + out)) {
Chris@223 370 return notFound.arg(base + out).arg(plugname1).arg(plugname2);
Chris@162 371 }
Chris@162 372
Chris@162 373 Transform t = tf->getDefaultTransformFor
Chris@162 374 (base + out, m_fileModel->getSampleRate());
Chris@162 375 t.setStepSize(256);
Chris@162 376 t.setBlockSize(2048);
Chris@162 377
Chris@192 378 if (range.isConstrained()) {
Chris@192 379 t.setParameter("minfreq", range.min);
Chris@192 380 t.setParameter("maxfreq", range.max);
Chris@223 381 t.setBlockSize(4096);
Chris@192 382 }
Chris@192 383
matthiasm@273 384 // get time stamps that align with the 256-sample grid of the original extraction
matthiasm@273 385 int startSample = ceil(sel.getStartFrame()*1.0/256) * 256;
matthiasm@273 386 int endSample = ceil(sel.getEndFrame()*1.0/256) * 256;
matthiasm@273 387 if (!range.isConstrained()) {
matthiasm@273 388 startSample -= 4*256; // 4*256 is for 4 frames offset due to timestamp shift
matthiasm@273 389 endSample -= 4*256;
matthiasm@273 390 } else {
matthiasm@273 391 endSample -= 9*256; // MM says: not sure what the CHP plugin does there
matthiasm@273 392 }
matthiasm@273 393 RealTime start = RealTime::frame2RealTime(startSample, m_fileModel->getSampleRate());
matthiasm@273 394 RealTime end = RealTime::frame2RealTime(endSample, m_fileModel->getSampleRate());
Chris@164 395
Chris@164 396 RealTime duration;
Chris@164 397
Chris@164 398 if (sel.getEndFrame() > sel.getStartFrame()) {
Chris@164 399 duration = end - start;
Chris@164 400 }
Chris@164 401
Chris@200 402 cerr << "Analyser::reAnalyseSelection: start " << start << " end " << end << " original selection start " << sel.getStartFrame() << " end " << sel.getEndFrame() << " duration " << duration << endl;
Chris@200 403
Chris@200 404 if (duration <= RealTime::zeroTime) {
Chris@200 405 cerr << "Analyser::reAnalyseSelection: duration <= 0, not analysing" << endl;
Chris@200 406 return "";
Chris@200 407 }
Chris@200 408
Chris@164 409 t.setStartTime(start);
Chris@164 410 t.setDuration(duration);
Chris@162 411
Chris@162 412 transforms.push_back(t);
Chris@162 413
Chris@163 414 m_document->createDerivedLayersAsync(transforms, m_fileModel, this);
Chris@162 415
Chris@163 416 return "";
Chris@163 417 }
Chris@162 418
Chris@184 419 bool
Chris@184 420 Analyser::arePitchCandidatesShown() const
Chris@184 421 {
Chris@184 422 return m_candidatesVisible;
Chris@184 423 }
Chris@184 424
Chris@184 425 void
Chris@184 426 Analyser::showPitchCandidates(bool shown)
Chris@184 427 {
Chris@184 428 if (m_candidatesVisible == shown) return;
Chris@184 429
Chris@184 430 foreach (Layer *layer, m_reAnalysisCandidates) {
Chris@184 431 if (shown) {
Chris@199 432 CommandHistory::getInstance()->addCommand
Chris@199 433 (new ShowLayerCommand(m_pane, layer, true,
Chris@199 434 tr("Show Pitch Candidates")));
Chris@184 435 } else {
Chris@199 436 CommandHistory::getInstance()->addCommand
Chris@199 437 (new ShowLayerCommand(m_pane, layer, false,
Chris@199 438 tr("Hide Pitch Candidates")));
Chris@184 439 }
Chris@184 440 }
Chris@184 441
Chris@184 442 m_candidatesVisible = shown;
Chris@184 443 }
Chris@184 444
Chris@163 445 void
Chris@163 446 Analyser::layersCreated(vector<Layer *> primary,
Chris@163 447 vector<Layer *> additional)
Chris@163 448 {
Chris@165 449 //!!! how do we know these came from the right selection? user
Chris@165 450 //!!! might have made another one since this request was issued
Chris@165 451
Chris@226 452 if (m_reAnalysingSelection == Selection()) {
Chris@226 453 // We don't want these (actually, as above, this should check
Chris@226 454 // that the selection is the same as the one requested -- but
Chris@226 455 // all we're doing here is checking that the selection exists
Chris@226 456 // at all, so hasn't been cleared or the document deleted or
Chris@226 457 // whatever)
Chris@226 458 for (int i = 0; i < (int)primary.size(); ++i) {
Chris@226 459 m_document->deleteLayer(primary[i]);
Chris@226 460 }
Chris@226 461 for (int i = 0; i < (int)additional.size(); ++i) {
Chris@226 462 m_document->deleteLayer(additional[i]);
Chris@226 463 }
Chris@226 464 return;
Chris@226 465 }
Chris@226 466
Chris@199 467 CommandHistory::getInstance()->startCompoundOperation
Chris@199 468 (tr("Re-Analyse Selection"), true);
Chris@199 469
Chris@246 470 m_reAnalysisCandidates.clear();
Chris@246 471
Chris@167 472 vector<Layer *> all;
Chris@163 473 for (int i = 0; i < (int)primary.size(); ++i) {
Chris@167 474 all.push_back(primary[i]);
Chris@167 475 }
Chris@167 476 for (int i = 0; i < (int)additional.size(); ++i) {
Chris@167 477 all.push_back(additional[i]);
Chris@162 478 }
Chris@162 479
Chris@167 480 for (int i = 0; i < (int)all.size(); ++i) {
Chris@167 481 TimeValueLayer *t = qobject_cast<TimeValueLayer *>(all[i]);
Chris@165 482 if (t) {
Chris@167 483 PlayParameters *params = t->getPlayParameters();
Chris@167 484 if (params) {
Chris@167 485 params->setPlayAudible(false);
Chris@167 486 }
Chris@167 487 t->setBaseColour
Chris@167 488 (ColourDatabase::getInstance()->getColourIndex(tr("Bright Orange")));
Chris@239 489 t->setPresentationName("candidate");
Chris@199 490 m_document->addLayerToView(m_pane, t);
Chris@165 491 m_reAnalysisCandidates.push_back(t);
Chris@165 492 }
Chris@163 493 }
Chris@187 494
Chris@199 495 if (!all.empty()) {
Chris@199 496 bool show = m_candidatesVisible;
Chris@199 497 m_candidatesVisible = !show; // to ensure the following takes effect
Chris@199 498 showPitchCandidates(show);
Chris@199 499 }
Chris@199 500
Chris@199 501 CommandHistory::getInstance()->endCompoundOperation();
Chris@199 502
Chris@187 503 emit layersChanged();
Chris@6 504 }
Chris@6 505
Chris@198 506 bool
Chris@198 507 Analyser::haveHigherPitchCandidate() const
Chris@198 508 {
Chris@198 509 if (m_reAnalysisCandidates.empty()) return false;
Chris@198 510 return (m_currentCandidate < 0 ||
Chris@198 511 (m_currentCandidate + 1 < (int)m_reAnalysisCandidates.size()));
Chris@198 512 }
Chris@198 513
Chris@198 514 bool
Chris@198 515 Analyser::haveLowerPitchCandidate() const
Chris@198 516 {
Chris@198 517 if (m_reAnalysisCandidates.empty()) return false;
Chris@198 518 return (m_currentCandidate < 0 || m_currentCandidate >= 1);
Chris@198 519 }
Chris@198 520
gyorgyf@45 521 void
Chris@167 522 Analyser::switchPitchCandidate(Selection sel, bool up)
Chris@167 523 {
Chris@167 524 if (m_reAnalysisCandidates.empty()) return;
Chris@167 525
Chris@167 526 if (up) {
Chris@167 527 m_currentCandidate = m_currentCandidate + 1;
Chris@168 528 if (m_currentCandidate >= (int)m_reAnalysisCandidates.size()) {
Chris@167 529 m_currentCandidate = 0;
Chris@167 530 }
Chris@167 531 } else {
Chris@167 532 m_currentCandidate = m_currentCandidate - 1;
Chris@167 533 if (m_currentCandidate < 0) {
Chris@168 534 m_currentCandidate = (int)m_reAnalysisCandidates.size() - 1;
Chris@167 535 }
Chris@167 536 }
Chris@167 537
Chris@167 538 Layer *pitchTrack = m_layers[PitchTrack];
Chris@167 539 if (!pitchTrack) return;
Chris@167 540
Chris@167 541 Clipboard clip;
Chris@167 542 pitchTrack->deleteSelection(sel);
Chris@167 543 m_reAnalysisCandidates[m_currentCandidate]->copy(m_pane, sel, clip);
Chris@167 544 pitchTrack->paste(m_pane, clip, 0, false);
Chris@264 545
Chris@264 546 stackLayers();
Chris@260 547 }
Chris@167 548
Chris@260 549 void
Chris@260 550 Analyser::stackLayers()
Chris@260 551 {
Chris@167 552 // raise the pitch track, then notes on top (if present)
Chris@260 553 if (m_layers[PitchTrack]) {
Chris@260 554 m_paneStack->setCurrentLayer(m_pane, m_layers[PitchTrack]);
Chris@260 555 }
Chris@167 556 if (m_layers[Notes] && !m_layers[Notes]->isLayerDormant(m_pane)) {
Chris@167 557 m_paneStack->setCurrentLayer(m_pane, m_layers[Notes]);
Chris@167 558 }
Chris@167 559 }
Chris@167 560
Chris@167 561 void
Chris@168 562 Analyser::shiftOctave(Selection sel, bool up)
Chris@168 563 {
Chris@168 564 float factor = (up ? 2.f : 0.5f);
Chris@168 565
Chris@168 566 vector<Layer *> actOn;
Chris@168 567
Chris@168 568 Layer *pitchTrack = m_layers[PitchTrack];
Chris@168 569 if (pitchTrack) actOn.push_back(pitchTrack);
Chris@168 570
Chris@168 571 foreach (Layer *layer, actOn) {
Chris@168 572
Chris@168 573 Clipboard clip;
Chris@168 574 layer->copy(m_pane, sel, clip);
Chris@168 575 layer->deleteSelection(sel);
Chris@168 576
Chris@168 577 Clipboard shifted;
Chris@168 578 foreach (Clipboard::Point p, clip.getPoints()) {
Chris@168 579 if (p.haveValue()) {
Chris@168 580 Clipboard::Point sp = p.withValue(p.getValue() * factor);
Chris@168 581 shifted.addPoint(sp);
Chris@168 582 } else {
Chris@168 583 shifted.addPoint(p);
Chris@168 584 }
Chris@168 585 }
Chris@168 586
Chris@168 587 layer->paste(m_pane, shifted, 0, false);
Chris@168 588 }
Chris@168 589 }
Chris@168 590
Chris@168 591 void
Chris@184 592 Analyser::deletePitches(Selection sel)
Chris@168 593 {
Chris@168 594 Layer *pitchTrack = m_layers[PitchTrack];
Chris@168 595 if (!pitchTrack) return;
Chris@168 596
Chris@168 597 pitchTrack->deleteSelection(sel);
Chris@168 598 }
Chris@168 599
Chris@168 600 void
Chris@199 601 Analyser::abandonReAnalysis(Selection sel)
Chris@167 602 {
Chris@252 603 // A compound command is already in progress
Chris@252 604
Chris@199 605 discardPitchCandidates();
Chris@194 606
Chris@194 607 Layer *myLayer = m_layers[PitchTrack];
Chris@194 608 if (!myLayer) return;
Chris@194 609 myLayer->deleteSelection(sel);
Chris@194 610 myLayer->paste(m_pane, m_preAnalysis, 0, false);
Chris@167 611 }
Chris@167 612
Chris@167 613 void
Chris@269 614 Analyser::clearReAnalysis()
Chris@269 615 {
Chris@269 616 discardPitchCandidates();
Chris@269 617 }
Chris@269 618
Chris@269 619 void
Chris@199 620 Analyser::discardPitchCandidates()
Chris@199 621 {
Chris@242 622 if (!m_reAnalysisCandidates.empty()) {
Chris@252 623 // We don't use a compound command here, because we may be
Chris@252 624 // already in one. Caller bears responsibility for doing that
Chris@242 625 foreach (Layer *layer, m_reAnalysisCandidates) {
Chris@242 626 // This will cause the layer to be deleted later (ownership is
Chris@242 627 // transferred to the remove command)
Chris@242 628 m_document->removeLayerFromView(m_pane, layer);
Chris@242 629 }
Chris@243 630 m_reAnalysisCandidates.clear();
Chris@199 631 }
Chris@199 632
Chris@199 633 m_currentCandidate = -1;
Chris@199 634 m_reAnalysingSelection = Selection();
Chris@199 635 m_candidatesVisible = false;
Chris@199 636 }
Chris@199 637
Chris@199 638 void
Chris@242 639 Analyser::layerAboutToBeDeleted(Layer *doomed)
Chris@242 640 {
Chris@242 641 cerr << "Analyser::layerAboutToBeDeleted(" << doomed << ")" << endl;
Chris@242 642
Chris@242 643 vector<Layer *> notDoomed;
Chris@242 644
Chris@242 645 foreach (Layer *layer, m_reAnalysisCandidates) {
Chris@242 646 if (layer != doomed) {
Chris@242 647 notDoomed.push_back(layer);
Chris@242 648 }
Chris@242 649 }
Chris@242 650
Chris@242 651 m_reAnalysisCandidates = notDoomed;
Chris@242 652 }
Chris@242 653
Chris@242 654 void
Chris@174 655 Analyser::takePitchTrackFrom(Layer *otherLayer)
Chris@174 656 {
Chris@174 657 Layer *myLayer = m_layers[PitchTrack];
Chris@174 658 if (!myLayer) return;
Chris@174 659
Chris@174 660 Clipboard clip;
Chris@174 661
Chris@174 662 Selection sel = Selection(myLayer->getModel()->getStartFrame(),
Chris@174 663 myLayer->getModel()->getEndFrame());
Chris@174 664 myLayer->deleteSelection(sel);
Chris@174 665
Chris@174 666 sel = Selection(otherLayer->getModel()->getStartFrame(),
Chris@174 667 otherLayer->getModel()->getEndFrame());
Chris@174 668 otherLayer->copy(m_pane, sel, clip);
Chris@174 669
Chris@174 670 myLayer->paste(m_pane, clip, 0, false);
Chris@174 671 }
Chris@174 672
Chris@174 673 void
Chris@139 674 Analyser::getEnclosingSelectionScope(size_t f, size_t &f0, size_t &f1)
Chris@139 675 {
Chris@139 676 FlexiNoteLayer *flexiNoteLayer =
Chris@139 677 qobject_cast<FlexiNoteLayer *>(m_layers[Notes]);
Chris@139 678
Chris@139 679 int f0i = f, f1i = f;
Chris@139 680 size_t res = 1;
Chris@139 681
Chris@139 682 if (!flexiNoteLayer) {
Chris@139 683 f0 = f1 = f;
Chris@139 684 return;
Chris@139 685 }
Chris@139 686
Chris@139 687 flexiNoteLayer->snapToFeatureFrame(m_pane, f0i, res, Layer::SnapLeft);
Chris@139 688 flexiNoteLayer->snapToFeatureFrame(m_pane, f1i, res, Layer::SnapRight);
Chris@139 689
Chris@139 690 f0 = (f0i < 0 ? 0 : f0i);
Chris@139 691 f1 = (f1i < 0 ? 0 : f1i);
Chris@139 692 }
Chris@139 693
Chris@139 694 void
Chris@132 695 Analyser::saveState(Component c) const
Chris@132 696 {
Chris@132 697 bool v = isVisible(c);
Chris@132 698 bool a = isAudible(c);
Chris@132 699 QSettings settings;
Chris@132 700 settings.beginGroup("Analyser");
Chris@145 701 settings.setValue(QString("visible-%1").arg(int(c)), v);
Chris@145 702 settings.setValue(QString("audible-%1").arg(int(c)), a);
Chris@132 703 settings.endGroup();
Chris@132 704 }
Chris@132 705
Chris@132 706 void
Chris@132 707 Analyser::loadState(Component c)
Chris@132 708 {
Chris@132 709 QSettings settings;
Chris@132 710 settings.beginGroup("Analyser");
Chris@145 711 bool deflt = (c == Spectrogram ? false : true);
Chris@145 712 bool v = settings.value(QString("visible-%1").arg(int(c)), deflt).toBool();
Chris@145 713 bool a = settings.value(QString("audible-%1").arg(int(c)), true).toBool();
Chris@132 714 settings.endGroup();
Chris@132 715 setVisible(c, v);
Chris@132 716 setAudible(c, a);
Chris@132 717 }
Chris@132 718
Chris@132 719 void
gyorgyf@45 720 Analyser::setIntelligentActions(bool on)
gyorgyf@45 721 {
gyorgyf@45 722 std::cerr << "toggle setIntelligentActions " << on << std::endl;
Chris@128 723
Chris@128 724 FlexiNoteLayer *flexiNoteLayer =
Chris@128 725 qobject_cast<FlexiNoteLayer *>(m_layers[Notes]);
Chris@128 726 if (flexiNoteLayer) {
Chris@128 727 flexiNoteLayer->setIntelligentActions(on);
Chris@70 728 }
gyorgyf@45 729 }
Chris@128 730
Chris@128 731 bool
Chris@128 732 Analyser::isVisible(Component c) const
Chris@128 733 {
Chris@128 734 if (m_layers[c]) {
Chris@128 735 return !m_layers[c]->isLayerDormant(m_pane);
Chris@128 736 } else {
Chris@128 737 return false;
Chris@128 738 }
Chris@128 739 }
Chris@128 740
Chris@128 741 void
Chris@128 742 Analyser::setVisible(Component c, bool v)
Chris@128 743 {
Chris@128 744 if (m_layers[c]) {
Chris@128 745 m_layers[c]->setLayerDormant(m_pane, !v);
Chris@133 746
Chris@167 747 if (v) {
Chris@167 748 if (c == Notes) {
Chris@167 749 m_paneStack->setCurrentLayer(m_pane, m_layers[c]);
Chris@167 750 } else if (c == PitchTrack) {
Chris@167 751 // raise the pitch track, then notes on top (if present)
Chris@167 752 m_paneStack->setCurrentLayer(m_pane, m_layers[c]);
Chris@167 753 if (m_layers[Notes] &&
Chris@167 754 !m_layers[Notes]->isLayerDormant(m_pane)) {
Chris@167 755 m_paneStack->setCurrentLayer(m_pane, m_layers[Notes]);
Chris@167 756 }
Chris@167 757 }
Chris@133 758 }
Chris@133 759
Chris@128 760 m_pane->layerParametersChanged();
Chris@132 761 saveState(c);
Chris@128 762 }
Chris@128 763 }
Chris@128 764
Chris@128 765 bool
Chris@128 766 Analyser::isAudible(Component c) const
Chris@128 767 {
Chris@128 768 if (m_layers[c]) {
Chris@128 769 PlayParameters *params = m_layers[c]->getPlayParameters();
Chris@128 770 if (!params) return false;
Chris@128 771 return params->isPlayAudible();
Chris@128 772 } else {
Chris@128 773 return false;
Chris@128 774 }
Chris@128 775 }
Chris@128 776
Chris@128 777 void
Chris@128 778 Analyser::setAudible(Component c, bool a)
Chris@128 779 {
Chris@128 780 if (m_layers[c]) {
Chris@128 781 PlayParameters *params = m_layers[c]->getPlayParameters();
Chris@128 782 if (!params) return;
Chris@128 783 params->setPlayAudible(a);
Chris@132 784 saveState(c);
Chris@128 785 }
Chris@128 786 }
Chris@128 787
Chris@158 788 float
Chris@158 789 Analyser::getGain(Component c) const
Chris@158 790 {
Chris@158 791 if (m_layers[c]) {
Chris@158 792 PlayParameters *params = m_layers[c]->getPlayParameters();
Chris@158 793 if (!params) return 1.f;
Chris@158 794 return params->getPlayGain();
Chris@158 795 } else {
Chris@158 796 return 1.f;
Chris@158 797 }
Chris@158 798 }
Chris@158 799
Chris@158 800 void
Chris@158 801 Analyser::setGain(Component c, float gain)
Chris@158 802 {
Chris@158 803 if (m_layers[c]) {
Chris@158 804 PlayParameters *params = m_layers[c]->getPlayParameters();
Chris@158 805 if (!params) return;
Chris@158 806 params->setPlayGain(gain);
Chris@158 807 saveState(c);
Chris@158 808 }
Chris@158 809 }
Chris@158 810
Chris@158 811 float
Chris@158 812 Analyser::getPan(Component c) const
Chris@158 813 {
Chris@158 814 if (m_layers[c]) {
Chris@158 815 PlayParameters *params = m_layers[c]->getPlayParameters();
Chris@158 816 if (!params) return 1.f;
Chris@158 817 return params->getPlayPan();
Chris@158 818 } else {
Chris@158 819 return 1.f;
Chris@158 820 }
Chris@158 821 }
Chris@158 822
Chris@158 823 void
Chris@158 824 Analyser::setPan(Component c, float pan)
Chris@158 825 {
Chris@158 826 if (m_layers[c]) {
Chris@158 827 PlayParameters *params = m_layers[c]->getPlayParameters();
Chris@158 828 if (!params) return;
Chris@158 829 params->setPlayPan(pan);
Chris@158 830 saveState(c);
Chris@158 831 }
Chris@158 832 }
Chris@158 833
Chris@158 834
Chris@158 835