# HG changeset patch # User Chris Cannam # Date 1391628235 0 # Node ID e33f9d052503daba798a71e6b2dc8d33ab3770e3 # Parent 2067bce063a94941688a8e98ab9fc380e43cd573 Import pitch-track layer (currently csv-only) diff -r 2067bce063a9 -r e33f9d052503 src/Analyser.cpp --- a/src/Analyser.cpp Tue Feb 04 17:47:01 2014 +0000 +++ b/src/Analyser.cpp Wed Feb 05 19:23:55 2014 +0000 @@ -414,6 +414,25 @@ } void +Analyser::takePitchTrackFrom(Layer *otherLayer) +{ + Layer *myLayer = m_layers[PitchTrack]; + if (!myLayer) return; + + Clipboard clip; + + Selection sel = Selection(myLayer->getModel()->getStartFrame(), + myLayer->getModel()->getEndFrame()); + myLayer->deleteSelection(sel); + + sel = Selection(otherLayer->getModel()->getStartFrame(), + otherLayer->getModel()->getEndFrame()); + otherLayer->copy(m_pane, sel, clip); + + myLayer->paste(m_pane, clip, 0, false); +} + +void Analyser::getEnclosingSelectionScope(size_t f, size_t &f0, size_t &f1) { FlexiNoteLayer *flexiNoteLayer = diff -r 2067bce063a9 -r e33f9d052503 src/Analyser.h --- a/src/Analyser.h Tue Feb 04 17:47:01 2014 +0000 +++ b/src/Analyser.h Wed Feb 05 19:23:55 2014 +0000 @@ -114,6 +114,20 @@ */ void clearReAnalysis(); + /** + * Import the pitch track from the given layer into our + * pitch-track layer. + */ + void takePitchTrackFrom(Layer *layer); + + Pane *getPane() { + return m_pane; + } + + Layer *getLayer(Component type) { + return m_layers[type]; + } + signals: void layersChanged(); diff -r 2067bce063a9 -r e33f9d052503 src/MainWindow.cpp --- a/src/MainWindow.cpp Tue Feb 04 17:47:01 2014 +0000 +++ b/src/MainWindow.cpp Wed Feb 05 19:23:55 2014 +0000 @@ -47,6 +47,9 @@ #include "layer/ColourDatabase.h" #include "base/Selection.h" +#include "rdf/RDFImporter.h" +#include "data/fileio/DataFileReaderFactory.h" +#include "data/fileio/CSVFormat.h" #include "data/fileio/CSVFileWriter.h" #include "data/fileio/MIDIFileWriter.h" #include "rdf/RDFExporter.h" @@ -423,6 +426,12 @@ this, SLOT(setupRecentFilesMenu())); menu->addSeparator(); + action = new QAction(tr("Import Pitch Track Data..."), this); + action->setStatusTip(tr("Import pitch-track data from a file")); + connect(action, SIGNAL(triggered()), this, SLOT(importPitchLayer())); + connect(this, SIGNAL(canImportLayer(bool)), action, SLOT(setEnabled(bool))); + menu->addAction(action); + action = new QAction(tr("Export Pitch Track Data..."), this); action->setStatusTip(tr("Export pitch-track data to a file")); connect(action, SIGNAL(triggered()), this, SLOT(exportPitchLayer())); @@ -1482,18 +1491,105 @@ } void +MainWindow::importPitchLayer() +{ + QString path = getOpenFileName(FileFinder::LayerFileNoMidiNonSV); + if (path == "") return; + + FileOpenStatus status = importPitchLayer(path); + + if (status == FileOpenFailed) { + emit hideSplash(); + QMessageBox::critical(this, tr("Failed to open file"), + tr("File open failed
Layer file %1 could not be opened.").arg(path)); + return; + } else if (status == FileOpenWrongMode) { + emit hideSplash(); + QMessageBox::critical(this, tr("Failed to open file"), + tr("Audio required
Unable to load layer data from \"%1\" without an audio file.
Please load at least one audio file before importing annotations.").arg(path));
+ }
+}
+
+MainWindow::FileOpenStatus
+MainWindow::importPitchLayer(FileSource source)
+{
+ if (!source.isAvailable()) return FileOpenFailed;
+ source.waitForData();
+
+ QString path = source.getLocalFilename();
+
+ RDFImporter::RDFDocumentType rdfType =
+ RDFImporter::identifyDocumentType(QUrl::fromLocalFile(path).toString());
+
+ if (rdfType != RDFImporter::NotRDF) {
+
+ //!!!
+ return FileOpenFailed;
+
+ } else if (source.getExtension().toLower() == "svl" ||
+ (source.getExtension().toLower() == "xml" &&
+ (SVFileReader::identifyXmlFile(source.getLocalFilename())
+ == SVFileReader::SVLayerFile))) {
+
+ //!!!
+ return FileOpenFailed;
+
+ } else {
+
+ try {
+
+ CSVFormat format(path);
+ format.setSampleRate(getMainModel()->getSampleRate());
+
+ if (format.getModelType() != CSVFormat::TwoDimensionalModel) {
+ //!!! error report
+ return FileOpenFailed;
+ }
+
+ Model *model = DataFileReaderFactory::loadCSV
+ (path, format, getMainModel()->getSampleRate());
+
+ if (model) {
+
+ SVDEBUG << "MainWindow::importPitchLayer: Have model" << endl;
+
+ CommandHistory::getInstance()->startCompoundOperation
+ (tr("Import Pitch Track"), true);
+
+ Layer *newLayer = m_document->createImportedLayer(model);
+
+ m_analyser->takePitchTrackFrom(newLayer);
+
+ m_document->deleteLayer(newLayer);
+
+ CommandHistory::getInstance()->endCompoundOperation();
+
+ //!!! swap all data in to existing layer instead of this
+
+ m_recentFiles.addFile(source.getLocation());
+
+ if (!source.isRemote()) {
+ registerLastOpenedFilePath
+ (FileFinder::LayerFile,
+ path); // for file dialog
+ }
+
+ return FileOpenSucceeded;
+ }
+ } catch (DataFileReaderFactory::Exception e) {
+ if (e == DataFileReaderFactory::ImportCancelled) {
+ return FileOpenCancelled;
+ }
+ }
+ }
+
+ return FileOpenFailed;
+}
+
+void
MainWindow::exportPitchLayer()
{
- Layer *layer = 0;
- for (int i = 0; i < m_paneStack->getPaneCount(); ++i) {
- Pane *pane = m_paneStack->getPane(i);
- for (int j = 0; j < pane->getLayerCount(); ++j) {
- layer = qobject_cast