Chris@498: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@498: Chris@498: /* Chris@498: Sonic Visualiser Chris@498: An audio file viewer and annotation editor. Chris@498: Chris@498: Sonic Annotator Chris@498: A utility for batch feature extraction from audio files. Chris@498: Chris@498: Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London. Chris@498: Copyright 2007-2008 QMUL. Chris@498: Chris@498: This program is free software; you can redistribute it and/or Chris@498: modify it under the terms of the GNU General Public License as Chris@498: published by the Free Software Foundation; either version 2 of the Chris@498: License, or (at your option) any later version. See the file Chris@498: COPYING included with this distribution for more information. Chris@498: */ Chris@498: Chris@498: #include "FileFeatureWriter.h" Chris@498: Chris@498: #include "base/Exceptions.h" Chris@498: Chris@498: #include Chris@498: #include Chris@498: #include Chris@498: #include Chris@498: #include Chris@498: Chris@498: using namespace std; Chris@498: using namespace Vamp; Chris@498: Chris@498: FileFeatureWriter::FileFeatureWriter(int support, Chris@498: QString extension) : Chris@498: m_support(support), Chris@498: m_extension(extension), Chris@498: m_manyFiles(false), Chris@498: m_stdout(false), Chris@498: m_append(false), Chris@498: m_force(false) Chris@498: { Chris@498: if (!(m_support & SupportOneFilePerTrack)) { Chris@498: if (m_support & SupportOneFilePerTrackTransform) { Chris@498: m_manyFiles = true; Chris@498: } else if (m_support & SupportOneFileTotal) { Chris@498: m_singleFileName = QString("output.%1").arg(m_extension); Chris@498: } else { Chris@498: cerr << "FileFeatureWriter::FileFeatureWriter: ERROR: Invalid support specification " << support << endl; Chris@498: } Chris@498: } Chris@498: } Chris@498: Chris@498: FileFeatureWriter::~FileFeatureWriter() Chris@498: { Chris@498: while (!m_streams.empty()) { Chris@498: m_streams.begin()->second->flush(); Chris@498: delete m_streams.begin()->second; Chris@498: m_streams.erase(m_streams.begin()); Chris@498: } Chris@498: while (!m_files.empty()) { Chris@498: delete m_files.begin()->second; Chris@498: m_files.erase(m_files.begin()); Chris@498: } Chris@498: } Chris@498: Chris@498: FileFeatureWriter::ParameterList Chris@498: FileFeatureWriter::getSupportedParameters() const Chris@498: { Chris@498: ParameterList pl; Chris@498: Parameter p; Chris@498: Chris@498: p.name = "basedir"; Chris@498: p.description = "Base output directory path. (The default is the same directory as the input file.)"; Chris@498: p.hasArg = true; Chris@498: pl.push_back(p); Chris@498: Chris@498: if (m_support & SupportOneFilePerTrackTransform && Chris@498: m_support & SupportOneFilePerTrack) { Chris@498: p.name = "many-files"; Chris@498: 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.)"; Chris@498: p.hasArg = false; Chris@498: pl.push_back(p); Chris@498: } Chris@498: Chris@498: if (m_support & SupportOneFileTotal) { Chris@498: if (m_support & ~SupportOneFileTotal) { // not only option Chris@498: p.name = "one-file"; Chris@498: p.description = "Write all transform results for all input files into the single named output file."; Chris@498: p.hasArg = true; Chris@498: pl.push_back(p); Chris@498: } Chris@498: p.name = "stdout"; Chris@498: p.description = "Write all transform results directly to standard output."; Chris@498: p.hasArg = false; Chris@498: pl.push_back(p); Chris@498: } Chris@498: Chris@498: p.name = "force"; Chris@498: p.description = "If an output file already exists, overwrite it."; Chris@498: p.hasArg = false; Chris@498: pl.push_back(p); Chris@498: Chris@498: p.name = "append"; Chris@498: p.description = "If an output file already exists, append data to it."; Chris@498: p.hasArg = false; Chris@498: pl.push_back(p); Chris@498: Chris@498: return pl; Chris@498: } Chris@498: Chris@498: void Chris@498: FileFeatureWriter::setParameters(map ¶ms) Chris@498: { Chris@498: for (map::iterator i = params.begin(); Chris@498: i != params.end(); ++i) { Chris@498: if (i->first == "basedir") { Chris@498: m_baseDir = i->second.c_str(); Chris@498: } else if (i->first == "many-files") { Chris@498: if (m_support & SupportOneFilePerTrackTransform && Chris@498: m_support & SupportOneFilePerTrack) { Chris@498: if (m_singleFileName != "") { Chris@498: cerr << "FileFeatureWriter::setParameters: WARNING: Both one-file and many-files parameters provided, ignoring many-files" << endl; Chris@498: } else { Chris@498: m_manyFiles = true; Chris@498: } Chris@498: } Chris@498: } else if (i->first == "one-file") { Chris@498: if (m_support & SupportOneFileTotal) { Chris@498: if (m_support & ~SupportOneFileTotal) { // not only option Chris@498: if (m_manyFiles) { Chris@498: cerr << "FileFeatureWriter::setParameters: WARNING: Both many-files and one-file parameters provided, ignoring one-file" << endl; Chris@498: } else { Chris@498: m_singleFileName = i->second.c_str(); Chris@498: } Chris@498: } Chris@498: } Chris@498: } else if (i->first == "stdout") { Chris@498: if (m_support & SupportOneFileTotal) { Chris@498: if (m_singleFileName != "") { Chris@498: cerr << "FileFeatureWriter::setParameters: WARNING: Both stdout and one-file provided, ignoring stdout" << endl; Chris@498: } else { Chris@498: m_stdout = true; Chris@498: } Chris@498: } Chris@498: } else if (i->first == "append") { Chris@498: m_append = true; Chris@498: } else if (i->first == "force") { Chris@498: m_force = true; Chris@498: } Chris@498: } Chris@498: } Chris@498: Chris@498: QString FileFeatureWriter::getOutputFilename(QString trackId, Chris@498: TransformId transformId) Chris@498: { Chris@498: if (m_singleFileName != "") { Chris@498: if (QFileInfo(m_singleFileName).exists() && !(m_force || m_append)) { Chris@498: cerr << "FileFeatureWriter: ERROR: Specified output file \"" << m_singleFileName.toStdString() << "\" exists and neither force nor append flag is specified -- not overwriting" << endl; Chris@498: return ""; Chris@498: } Chris@498: return m_singleFileName; Chris@498: } Chris@498: Chris@498: if (m_stdout) return ""; Chris@498: Chris@498: QUrl url(trackId); Chris@498: QString scheme = url.scheme().toLower(); Chris@498: bool local = (scheme == "" || scheme == "file" || scheme.length() == 1); Chris@498: Chris@498: QString dirname, basename; Chris@498: QString infilename = url.toLocalFile(); Chris@498: if (infilename == "") infilename = url.path(); Chris@498: basename = QFileInfo(infilename).baseName(); Chris@507: Chris@507: cerr << "trackId = " << trackId.toStdString() << ", url = " << url.toString().toStdString() << ", infilename = " Chris@507: << infilename.toStdString() << ", basename = " << basename.toStdString() << endl; Chris@498: Chris@498: Chris@498: if (m_baseDir != "") dirname = QFileInfo(m_baseDir).absoluteFilePath(); Chris@498: else if (local) dirname = QFileInfo(infilename).absolutePath(); Chris@498: else dirname = QDir::currentPath(); Chris@498: Chris@498: QString filename; Chris@498: Chris@498: if (m_manyFiles && transformId != "") { Chris@506: filename = QString("%1-%2.%3").arg(basename).arg(transformId).arg(m_extension); Chris@498: } else { Chris@498: filename = QString("%1.%2").arg(basename).arg(m_extension); Chris@498: } Chris@498: Chris@498: filename = QDir(dirname).filePath(filename); Chris@498: Chris@498: if (QFileInfo(filename).exists() && !(m_force || m_append)) { Chris@498: cerr << "FileFeatureWriter: ERROR: Output file \"" << filename.toStdString() << "\" exists (for input file or URL \"" << trackId.toStdString() << "\" and transform \"" << transformId.toStdString() << "\") and neither force nor append is specified -- not overwriting" << endl; Chris@498: return ""; Chris@498: } Chris@498: Chris@498: return filename; Chris@498: } Chris@498: Chris@498: Chris@498: QFile *FileFeatureWriter::getOutputFile(QString trackId, Chris@498: TransformId transformId) Chris@498: { Chris@498: pair key; Chris@498: Chris@498: if (m_singleFileName != "") { Chris@498: key = pair("", ""); Chris@498: } else if (m_manyFiles) { Chris@498: key = pair(trackId, transformId); Chris@498: } else { Chris@498: key = pair(trackId, ""); Chris@498: } Chris@498: Chris@498: if (m_files.find(key) == m_files.end()) { Chris@498: Chris@498: QString filename = getOutputFilename(trackId, transformId); Chris@498: Chris@498: if (filename == "") { // stdout Chris@498: return 0; Chris@498: } Chris@498: Chris@498: cerr << "FileFeatureWriter: NOTE: Using output filename \"" Chris@498: << filename.toStdString() << "\"" << endl; Chris@498: Chris@498: QFile *file = new QFile(filename); Chris@498: QIODevice::OpenMode mode = (QIODevice::WriteOnly); Chris@498: if (m_append) mode |= QIODevice::Append; Chris@498: Chris@498: if (!file->open(mode)) { Chris@498: cerr << "FileFeatureWriter: ERROR: Failed to open output file \"" << filename.toStdString() Chris@498: << "\" for writing" << endl; Chris@498: delete file; Chris@498: m_files[key] = 0; Chris@498: throw FailedToOpenFile(filename); Chris@498: } Chris@498: Chris@498: m_files[key] = file; Chris@498: } Chris@498: Chris@498: return m_files[key]; Chris@498: } Chris@498: Chris@498: Chris@498: QTextStream *FileFeatureWriter::getOutputStream(QString trackId, Chris@498: TransformId transformId) Chris@498: { Chris@498: QFile *file = getOutputFile(trackId, transformId); Chris@498: if (!file && !m_stdout) { Chris@498: return 0; Chris@498: } Chris@498: Chris@498: if (m_streams.find(file) == m_streams.end()) { Chris@498: if (m_stdout) { Chris@498: m_streams[file] = new QTextStream(stdout); Chris@498: } else { Chris@498: m_streams[file] = new QTextStream(file); Chris@498: } Chris@498: } Chris@498: Chris@498: return m_streams[file]; Chris@498: } Chris@498: