# HG changeset patch # User Chris Cannam # Date 1536673011 -3600 # Node ID fbe8afdfa8a643b2e6a89d3c8bef1f5776c27054 # Parent 9c09a3f0513921425e8b392c6f1d26e9062e840c Add recorded/converted locations logic to this library diff -r 9c09a3f05139 -r fbe8afdfa8a6 base/RecordDirectory.cpp --- /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 +#include + +#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); + } +} + + diff -r 9c09a3f05139 -r fbe8afdfa8a6 base/RecordDirectory.h --- /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 + +/** + * 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 diff -r 9c09a3f05139 -r fbe8afdfa8a6 base/TempDirectory.h --- 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 #include diff -r 9c09a3f05139 -r fbe8afdfa8a6 data/fileio/CSVFileReader.cpp --- 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 +#include #include #include #include #include #include +#include #include #include @@ -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)); +} + diff -r 9c09a3f05139 -r fbe8afdfa8a6 data/fileio/CSVFileReader.h --- 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; }; diff -r 9c09a3f05139 -r fbe8afdfa8a6 files.pri --- 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 \