changeset 1519:fbe8afdfa8a6 import-audio-data

Add recorded/converted locations logic to this library
author Chris Cannam
date Tue, 11 Sep 2018 14:36:51 +0100
parents 9c09a3f05139
children 954d0cf29ca7
files base/RecordDirectory.cpp base/RecordDirectory.h base/TempDirectory.h data/fileio/CSVFileReader.cpp data/fileio/CSVFileReader.h files.pri
diffstat 6 files changed, 162 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/base/RecordDirectory.cpp	Tue Sep 11 14:36:51 2018 +0100
@@ -0,0 +1,67 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    Sonic Visualiser
+    An audio file viewer and annotation editor.
+    Centre for Digital Music, Queen Mary, University of London.
+    
+    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.  See the file
+    COPYING included with this distribution for more information.
+*/
+
+#include "RecordDirectory.h"
+#include "TempDirectory.h"
+
+#include <QDir>
+#include <QDateTime>
+
+#include "Debug.h"
+
+QString
+RecordDirectory::getRecordContainerDirectory()
+{
+    QDir parent(TempDirectory::getInstance()->getContainingPath());
+    QString subdirname("recorded");
+
+    if (!parent.mkpath(subdirname)) {
+        SVCERR << "ERROR: RecordDirectory::getRecordContainerDirectory: Failed to create recorded dir in \"" << parent.canonicalPath() << "\"" << endl;
+        return "";
+    } else {
+        return parent.filePath(subdirname);
+    }
+}
+
+QString
+RecordDirectory::getRecordDirectory()
+{
+    QDir parent(getRecordContainerDirectory());
+    QDateTime now = QDateTime::currentDateTime();
+    QString subdirname = QString("%1").arg(now.toString("yyyyMMdd"));
+
+    if (!parent.mkpath(subdirname)) {
+        SVCERR << "ERROR: RecordDirectory::getRecordDirectory: Failed to create recorded dir in \"" << parent.canonicalPath() << "\"" << endl;
+        return "";
+    } else {
+        return parent.filePath(subdirname);
+    }
+}
+
+QString
+RecordDirectory::getConvertedAudioDirectory()
+{
+    QDir parent(getRecordContainerDirectory());
+    QDateTime now = QDateTime::currentDateTime();
+    QString subdirname = "converted";
+
+    if (!parent.mkpath(subdirname)) {
+        SVCERR << "ERROR: RecordDirectory::getConvertedAudioDirectory: Failed to create recorded dir in \"" << parent.canonicalPath() << "\"" << endl;
+        return "";
+    } else {
+        return parent.filePath(subdirname);
+    }
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/base/RecordDirectory.h	Tue Sep 11 14:36:51 2018 +0100
@@ -0,0 +1,59 @@
+/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
+
+/*
+    Sonic Visualiser
+    An audio file viewer and annotation editor.
+    Centre for Digital Music, Queen Mary, University of London.
+    
+    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.  See the file
+    COPYING included with this distribution for more information.
+*/
+
+#ifndef SV_RECORD_DIRECTORY_H
+#define SV_RECORD_DIRECTORY_H
+
+#include <QString>
+
+/**
+ * Report the intended target location for recorded audio files.
+ */
+class RecordDirectory
+{
+public:
+    /**
+     * Return the directory in which a recorded file should be saved.
+     * This may vary depending on the current date and time, and so
+     * should be queried afresh for each recording. The directory will
+     * also be created if it does not yet exist.
+     *
+     * Returns an empty string if the record directory did not exist
+     * and could not be created.
+     */
+    static QString getRecordDirectory();
+
+    /**
+     * Return the root "recorded files" directory. If
+     * getRecordDirectory() is returning a datestamped directory, then
+     * this will be its parent. The directory will also be created if
+     * it does not yet exist.
+     *
+     * Returns an empty string if the record directory did not exist
+     * and could not be created.
+     */
+    static QString getRecordContainerDirectory();
+
+    /**
+     * Return the directory in which an audio file converted from a
+     * data file should be saved. The directory will also be created if
+     * it does not yet exist.
+     *
+     * Returns an empty string if the directory did not exist and
+     * could not be created.
+     */
+    static QString getConvertedAudioDirectory();
+};
+
+#endif
--- a/base/TempDirectory.h	Sat Sep 08 20:53:48 2018 +0100
+++ b/base/TempDirectory.h	Tue Sep 11 14:36:51 2018 +0100
@@ -13,8 +13,8 @@
     COPYING included with this distribution for more information.
 */
 
-#ifndef _TEMP_DIRECTORY_H_
-#define _TEMP_DIRECTORY_H_
+#ifndef SV_TEMP_DIRECTORY_H
+#define SV_TEMP_DIRECTORY_H
 
 #include <QString>
 #include <QMutex>
--- a/data/fileio/CSVFileReader.cpp	Sat Sep 08 20:53:48 2018 +0100
+++ b/data/fileio/CSVFileReader.cpp	Tue Sep 11 14:36:51 2018 +0100
@@ -19,6 +19,7 @@
 #include "base/RealTime.h"
 #include "base/StringBits.h"
 #include "base/ProgressReporter.h"
+#include "base/RecordDirectory.h"
 #include "model/SparseOneDimensionalModel.h"
 #include "model/SparseTimeValueModel.h"
 #include "model/EditableDenseThreeDimensionalModel.h"
@@ -28,11 +29,13 @@
 #include "DataFileReaderFactory.h"
 
 #include <QFile>
+#include <QDir>
 #include <QFileInfo>
 #include <QString>
 #include <QRegExp>
 #include <QStringList>
 #include <QTextStream>
+#include <QDateTime>
 
 #include <iostream>
 #include <map>
@@ -306,6 +309,8 @@
             QStringList list = StringBits::split(line, separator, allowQuoting);
             if (!model) {
 
+                QString modelName = m_filename;
+                
                 switch (modelType) {
 
                 case CSVFormat::OneDimensionalModel:
@@ -341,16 +346,18 @@
                 {
                     bool normalise = (m_format.getAudioSampleRange()
                                       == CSVFormat::SampleRangeOther);
+                    QString path = getConvertedAudioFilePath();
                     modelW = new WritableWaveFileModel
-                        (sampleRate, valueColumns, QString(), normalise);
+                        (sampleRate, valueColumns, path, normalise);
+                    modelName = QFileInfo(path).fileName();
                     model = modelW;
                     break;
                 }
                 }
 
                 if (model && model->isOK()) {
-                    if (m_filename != "") {
-                        model->setObjectName(m_filename);
+                    if (modelName != "") {
+                        model->setObjectName(modelName);
                     }
                 }
             }
@@ -623,3 +630,23 @@
     return model;
 }
 
+QString
+CSVFileReader::getConvertedAudioFilePath() const
+{
+    QString base = m_filename;
+    base.replace(QRegExp("[/\\,.:;~<>\"'|?%*]+"), "_");
+
+    QString convertedFileDir = RecordDirectory::getConvertedAudioDirectory();
+    if (convertedFileDir == "") {
+        SVCERR << "WARNING: CSVFileReader::getConvertedAudioFilePath: Failed to retrieve converted audio directory" << endl;
+        return "";
+    }
+
+    auto ms = QDateTime::currentDateTime().toMSecsSinceEpoch();
+    auto s = ms / 1000; // there is a toSecsSinceEpoch in Qt 5.8 but
+                        // we currently want to support older versions
+    
+    return QDir(convertedFileDir).filePath
+        (QString("%1-%2.wav").arg(base).arg(s));
+}
+
--- a/data/fileio/CSVFileReader.h	Sat Sep 08 20:53:48 2018 +0100
+++ b/data/fileio/CSVFileReader.h	Tue Sep 11 14:36:51 2018 +0100
@@ -72,6 +72,8 @@
 
     sv_frame_t convertTimeValue(QString, int lineno, sv_samplerate_t sampleRate,
                                 int windowSize) const;
+
+    QString getConvertedAudioFilePath() const;
 };
 
 
--- a/files.pri	Sat Sep 08 20:53:48 2018 +0100
+++ b/files.pri	Tue Sep 11 14:36:51 2018 +0100
@@ -24,6 +24,7 @@
            base/RangeMapper.h \
            base/RealTime.h \
            base/RecentFiles.h \
+           base/RecordDirectory.h \
            base/ResourceFinder.h \
            base/RingBuffer.h \
            base/ScaleTickIntervals.h \
@@ -160,6 +161,7 @@
            base/RangeMapper.cpp \
            base/RealTimeSV.cpp \
            base/RecentFiles.cpp \
+           base/RecordDirectory.cpp \
            base/ResourceFinder.cpp \
            base/Selection.cpp \
            base/Serialiser.cpp \