Chris@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@0: Sonic Annotator Chris@0: A utility for batch feature extraction from audio files. Chris@0: Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London. Chris@0: Copyright 2007-2008 QMUL. Chris@0: Chris@0: This program is free software; you can redistribute it and/or Chris@0: modify it under the terms of the GNU General Public License as Chris@0: published by the Free Software Foundation; either version 2 of the Chris@0: License, or (at your option) any later version. See the file Chris@0: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@0: using namespace std; Chris@0: Chris@0: #include "DefaultFeatureWriter.h" Chris@0: Chris@325: #include Chris@325: #include Chris@325: Chris@325: DefaultFeatureWriter::DefaultFeatureWriter() : Chris@325: FileFeatureWriter(SupportStdOut, Chris@325: "xml") Chris@325: { Chris@325: } Chris@325: Chris@325: DefaultFeatureWriter::~DefaultFeatureWriter() Chris@325: { Chris@325: } Chris@325: Chris@144: string Chris@144: DefaultFeatureWriter::getDescription() const Chris@144: { Chris@144: return "Write features in a generic XML format, with or elements containing output name and some or all of timestamp, duration, values, and label."; Chris@144: } Chris@144: Chris@325: static QString Chris@325: toQStringAsStream(const RealTime &rt) Chris@325: { Chris@325: // just for historical compatibility, get the same formatting as Chris@325: // when streaming to an iostream Chris@325: std::stringstream out; Chris@325: out << rt; Chris@325: std::string s = out.str(); Chris@325: return QString::fromStdString(s); Chris@325: } Chris@325: Chris@325: void DefaultFeatureWriter::write(QString trackId, Chris@325: const Transform &transform, Chris@0: const Vamp::Plugin::OutputDescriptor& output, Chris@325: const Vamp::Plugin::FeatureList& features, Chris@0: std::string summaryType) Chris@0: { Chris@325: // Select appropriate output file for our track/transform Chris@325: // combination Chris@325: Chris@325: TransformId transformId = transform.getIdentifier(); Chris@325: Chris@325: QTextStream *sptr = getOutputStream Chris@325: (trackId, transformId, QTextCodec::codecForName("UTF-8")); Chris@325: if (!sptr) { Chris@325: throw FailedToOpenOutputStream(trackId, transformId); Chris@325: } Chris@325: Chris@325: QTextStream &stream = *sptr; Chris@325: Chris@325: int n = int(features.size()); Chris@325: Chris@325: if (n == 0) return; Chris@0: Chris@325: /* we write a generic XML output of the form Chris@0: Chris@325: Chris@325: output.name Chris@325: feature.timestamp Chris@325: output.binName[0]:feature.value[0]... Chris@325: Chris@325: Chris@0: */ Chris@0: Chris@325: for (int i = 0; i < n; ++i) { Chris@0: if (summaryType == "") { Chris@325: stream << "" << endl; Chris@0: } else { Chris@325: stream << "" << endl; Chris@0: } Chris@325: stream << "\t" << QString::fromStdString(output.name) Chris@325: << "" << endl; Chris@325: if (features[i].hasTimestamp) { Chris@325: stream << "\t" Chris@325: << toQStringAsStream(features[i].timestamp) Chris@325: << "" << endl; Chris@0: } Chris@325: if (features[i].hasDuration) { Chris@325: stream << "\t" Chris@325: << toQStringAsStream(features[i].duration) Chris@325: << "" << endl; Chris@0: } Chris@325: if (features[i].values.size() > 0) Chris@0: { Chris@325: stream << "\t"; Chris@325: for (int j = 0; j < (int)features[i].values.size(); ++j) { Chris@325: if (j > 0) { Chris@325: stream << " "; Chris@325: } Chris@325: if (output.binNames.size() > 0) { Chris@325: stream << QString::fromStdString(output.binNames[j]) << ":"; Chris@325: } Chris@325: stream << features[i].values[j]; Chris@0: } Chris@325: stream << "" << endl; Chris@0: } Chris@325: if (features[i].label.length() > 0) Chris@325: stream << "\t" << endl; Chris@0: if (summaryType == "") { Chris@325: stream << "" << endl; Chris@0: } else { Chris@325: stream << "" << endl; Chris@0: } Chris@0: } Chris@0: }