1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/* -*- 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.
Sonic Annotator
A utility for batch feature extraction from audio files.
Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London.
Copyright 2007-2008 QMUL.
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 "FileFeatureWriter.h"
#include "base/Exceptions.h"
#include <QTextStream>
#include <QFile>
#include <QFileInfo>
#include <QUrl>
#include <QDir>
using namespace std;
using namespace Vamp;
namespace sv {
FileFeatureWriter::FileFeatureWriter(int support,
QString extension) :
m_prevstream(nullptr),
m_support(support),
m_extension(extension),
m_manyFiles(false),
m_stdout(false),
m_append(false),
m_force(false)
{
if (!(m_support & SupportOneFilePerTrack)) {
if (m_support & SupportOneFilePerTrackTransform) {
m_manyFiles = true;
} else if (m_support & SupportOneFileTotal) {
m_singleFileName = QString("output.%1").arg(m_extension);
} else {
SVCERR << "FileFeatureWriter::FileFeatureWriter: ERROR: Invalid support specification " << support << endl;
}
}
}
FileFeatureWriter::~FileFeatureWriter()
{
while (!m_streams.empty()) {
m_streams.begin()->second->flush();
delete m_streams.begin()->second;
m_streams.erase(m_streams.begin());
}
while (!m_files.empty()) {
if (m_files.begin()->second) {
SVDEBUG << "FileFeatureWriter::~FileFeatureWriter: NOTE: Closing feature file \""
<< m_files.begin()->second->fileName() << "\"" << endl;
delete m_files.begin()->second;
}
m_files.erase(m_files.begin());
}
}
FileFeatureWriter::ParameterList
FileFeatureWriter::getSupportedParameters() const
{
ParameterList pl;
Parameter p;
p.name = "basedir";
p.description = "Base output directory path. (The default is the same directory as the input file.) The directory must exist already.";
p.hasArg = true;
pl.push_back(p);
if (m_support & SupportOneFilePerTrackTransform &&
m_support & SupportOneFilePerTrack) {
p.name = "many-files";
p.description = "Create a separate output file for every combination of input file and transform. The output file names will be based on the input file names. (The default is to create one output file per input audio file, and write all transform results for that input into it.)";
p.hasArg = false;
pl.push_back(p);
}
if (m_support & SupportOneFileTotal) {
if (m_support & ~SupportOneFileTotal) { // not only option
p.name = "one-file";
if (m_support & SupportOneFilePerTrack) {
p.description = "Write all transform results for all input files into the single named output file. (The default is to create one output file per input audio file, and write all transform results for that input into it.)";
} else {
p.description = "Write all transform results for all input files into the single named output file. (The default is to create a separate output file for each combination of input audio file and transform.)";
}
p.hasArg = true;
pl.push_back(p);
}
}
if (m_support & SupportStdOut) {
p.name = "stdout";
p.description = "Write all transform results directly to standard output.";
p.hasArg = false;
pl.push_back(p);
}
p.name = "force";
p.description = "If an output file already exists, overwrite it.";
p.hasArg = false;
pl.push_back(p);
p.name = "append";
p.description = "If an output file already exists, append data to it.";
p.hasArg = false;
pl.push_back(p);
return pl;
}
void
FileFeatureWriter::setParameters(map<string, string> ¶ms)
{
for (map<string, string>::iterator i = params.begin();
i != params.end(); ++i) {
if (i->first == "basedir") {
m_baseDir = i->second.c_str();
} else if (i->first == "many-files") {
if (m_support & SupportOneFilePerTrackTransform &&
m_support & SupportOneFilePerTrack) {
if (m_singleFileName != "") {
SVCERR << "FileFeatureWriter::setParameters: WARNING: Both one-file and many-files parameters provided, ignoring many-files" << endl;
} else {
m_manyFiles = true;
}
}
} else if (i->first == "one-file") {
if (m_support & SupportOneFileTotal) {
if (m_support & ~SupportOneFileTotal) { // not only option
// No, we cannot do this test because m_manyFiles
// may be on by default (for any FileFeatureWriter
// that supports OneFilePerTrackTransform but not
// OneFilePerTrack), so we need to be able to
// override it
// if (m_manyFiles) {
// SVCERR << "FileFeatureWriter::setParameters: WARNING: Both many-files and one-file parameters provided, ignoring one-file" << endl;
// } else {
m_singleFileName = i->second.c_str();
// }
}
}
} else if (i->first == "stdout") {
if (m_support & SupportStdOut) {
if (m_singleFileName != "") {
SVCERR << "FileFeatureWriter::setParameters: WARNING: Both stdout and one-file provided, ignoring stdout" << endl;
} else {
m_stdout = true;
}
}
} else if (i->first == "append") {
m_append = true;
} else if (i->first == "force") {
m_force = true;
}
}
}
QString
FileFeatureWriter::createOutputFilename(QString trackId,
TransformId transformId)
{
if (m_singleFileName != "") {
if (QFileInfo(m_singleFileName).exists() && !(m_force || m_append)) {
SVCERR << endl << "FileFeatureWriter: ERROR: Specified output file \"" << m_singleFileName << "\" exists and neither --" << getWriterTag() << "-force nor --" << getWriterTag() << "-append flag is specified -- not overwriting" << endl;
SVCERR << "NOTE: To find out how to fix this problem, read the help for the --" << getWriterTag() << "-force" << endl << "and --" << getWriterTag() << "-append options" << endl;
return "";
}
return m_singleFileName;
}
if (m_stdout) {
return "";
}
QUrl url(trackId, QUrl::StrictMode);
QString scheme = url.scheme().toLower();
bool local = (scheme == "" || scheme == "file" || scheme.length() == 1);
QString dirname, basename;
QString infilename = url.toLocalFile();
if (infilename == "") {
infilename = url.path();
}
basename = QFileInfo(infilename).completeBaseName();
if (scheme.length() == 1) {
infilename = scheme + ":" + infilename; // DOS drive!
}
// cerr << "trackId = " << trackId << ", url = " << url.toString() << ", infilename = "
// << infilename << ", basename = " << basename << ", m_baseDir = " << m_baseDir << endl;
if (m_baseDir != "") dirname = QFileInfo(m_baseDir).absoluteFilePath();
else if (local) dirname = QFileInfo(infilename).absolutePath();
else dirname = QDir::currentPath();
// cerr << "dirname = " << dirname << endl;
QString filename;
if (m_manyFiles && transformId != "") {
filename = QString("%1_%2.%3").arg(basename).arg(transformId).arg(m_extension);
} else {
filename = QString("%1.%2").arg(basename).arg(m_extension);
}
filename.replace(':', '_'); // ':' not permitted in Windows
filename = QDir(dirname).filePath(filename);
if (QFileInfo(filename).exists() && !(m_force || m_append)) {
SVCERR << endl << "FileFeatureWriter: ERROR: Output file \"" << filename << "\" exists (for input file or URL \"" << trackId << "\" and transform \"" << transformId << "\") and neither --" << getWriterTag() << "-force nor --" << getWriterTag() << "-append is specified -- not overwriting" << endl;
SVCERR << "NOTE: To find out how to fix this problem, read the help for the --" << getWriterTag() << "-force" << endl << "and --" << getWriterTag() << "-append options" << endl;
return "";
}
return filename;
}
void
FileFeatureWriter::testOutputFile(QString trackId,
TransformId transformId)
{
// Obviously, if we're writing to stdout we can't test for an
// openable output file. But when writing a single file we don't
// want to either, because this test would fail on the second and
// subsequent input files (because the file would already exist).
// getOutputFile does the right thing in this case, so we just
// leave it to it
if (m_stdout || m_singleFileName != "") return;
QString filename = createOutputFilename(trackId, transformId);
if (filename == "") {
throw FailedToOpenOutputStream(trackId, transformId);
}
}
FileFeatureWriter::TrackTransformPair
FileFeatureWriter::getFilenameKey(QString trackId,
TransformId transformId)
{
TrackTransformPair key;
if (m_singleFileName != "") {
key = TrackTransformPair("", "");
} else if (m_manyFiles) {
key = TrackTransformPair(trackId, transformId);
} else {
key = TrackTransformPair(trackId, "");
}
return key;
}
QString
FileFeatureWriter::getOutputFilename(QString trackId,
TransformId transformId)
{
TrackTransformPair key = getFilenameKey(trackId, transformId);
if (m_filenames.find(key) == m_filenames.end()) {
m_filenames[key] = createOutputFilename(trackId, transformId);
}
return m_filenames[key];
}
QFile *
FileFeatureWriter::getOutputFile(QString trackId,
TransformId transformId)
{
TrackTransformPair key = getFilenameKey(trackId, transformId);
if (m_files.find(key) == m_files.end()) {
QString filename = createOutputFilename(trackId, transformId);
if (filename == "") { // stdout or failure
return nullptr;
}
SVDEBUG << "FileFeatureWriter: NOTE: Using output filename \""
<< filename << "\"" << endl;
if (m_append) {
SVDEBUG << "FileFeatureWriter: NOTE: Calling reviewFileForAppending" << endl;
reviewFileForAppending(filename);
}
QFile *file = new QFile(filename);
QIODevice::OpenMode mode = (QIODevice::WriteOnly);
if (m_append) mode |= QIODevice::Append;
if (!file->open(mode)) {
SVCERR << "FileFeatureWriter: ERROR: Failed to open output file \"" << filename
<< "\" for writing" << endl;
delete file;
m_files[key] = nullptr;
throw FailedToOpenFile(filename);
}
m_files[key] = file;
}
return m_files[key];
}
QTextStream *FileFeatureWriter::getOutputStream(QString trackId,
TransformId transformId,
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
QStringConverter::Encoding encoding
#else
QString encodingName
#endif
)
{
QFile *file = getOutputFile(trackId, transformId);
if (!file && !m_stdout) {
return nullptr;
}
if (m_streams.find(file) == m_streams.end()) {
if (m_stdout) {
m_streams[file] = new QTextStream(stdout);
} else {
m_streams[file] = new QTextStream(file);
}
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
m_streams[file]->setEncoding(encoding);
#else
m_streams[file]->setCodec(encodingName.toLocal8Bit().data());
#endif
}
QTextStream *stream = m_streams[file];
if (m_prevstream && stream != m_prevstream) {
m_prevstream->flush();
}
m_prevstream = stream;
return stream;
}
void
FileFeatureWriter::flush()
{
if (m_prevstream) {
m_prevstream->flush();
}
}
void
FileFeatureWriter::finish()
{
// SVDEBUG << "FileFeatureWriter::finish()" << endl;
if (m_singleFileName != "" || m_stdout) return;
while (!m_streams.empty()) {
m_streams.begin()->second->flush();
delete m_streams.begin()->second;
m_streams.erase(m_streams.begin());
}
while (!m_files.empty()) {
if (m_files.begin()->second) {
SVDEBUG << "FileFeatureWriter::finish: NOTE: Closing feature file \""
<< m_files.begin()->second->fileName() << "\"" << endl;
delete m_files.begin()->second;
}
m_files.erase(m_files.begin());
}
m_prevstream = nullptr;
}
} // end namespace sv
|