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 "CSVFeatureWriter.h" Chris@498: Chris@498: #include Chris@498: Chris@498: #include Chris@498: #include Chris@1035: #include Chris@498: Chris@498: using namespace std; Chris@498: using namespace Vamp; Chris@498: Chris@498: CSVFeatureWriter::CSVFeatureWriter() : Chris@498: FileFeatureWriter(SupportOneFilePerTrackTransform | Chris@997: SupportOneFileTotal | Chris@997: SupportStdOut, Chris@498: "csv"), Chris@669: m_separator(","), Chris@1000: m_sampleTiming(false), Chris@1001: m_endTimes(false), Chris@1002: m_forceEnd(false), Chris@1002: m_omitFilename(false) Chris@498: { Chris@498: } Chris@498: Chris@498: CSVFeatureWriter::~CSVFeatureWriter() Chris@498: { Chris@498: } Chris@498: Chris@998: string Chris@998: CSVFeatureWriter::getDescription() const Chris@998: { Chris@998: return "Write features in comma-separated (CSV) format. If transforms are being written to a single file or to stdout, the first column in the output will contain the input audio filename, or an empty string if the feature hails from the same audio file as its predecessor. If transforms are being written to multiple files, the audio filename column will be omitted. Subsequent columns will contain the feature timestamp, then any or all of duration, values, and label."; Chris@998: } Chris@998: Chris@498: CSVFeatureWriter::ParameterList Chris@498: CSVFeatureWriter::getSupportedParameters() const Chris@498: { Chris@498: ParameterList pl = FileFeatureWriter::getSupportedParameters(); Chris@498: Parameter p; Chris@498: Chris@498: p.name = "separator"; Chris@498: p.description = "Column separator for output. Default is \",\" (comma)."; Chris@498: p.hasArg = true; Chris@498: pl.push_back(p); Chris@669: Chris@1002: p.name = "omit-filename"; Chris@1002: p.description = "Omit the filename column. May result in confusion if sending more than one audio file's features to the same CSV output."; Chris@1002: p.hasArg = false; Chris@1002: pl.push_back(p); Chris@1002: Chris@669: p.name = "sample-timing"; Chris@669: p.description = "Show timings as sample frame counts instead of in seconds."; Chris@669: p.hasArg = false; Chris@669: pl.push_back(p); Chris@1000: Chris@1000: p.name = "end-times"; Chris@1000: p.description = "Show start and end time instead of start and duration, for features with duration."; Chris@1000: p.hasArg = false; Chris@1000: pl.push_back(p); Chris@498: Chris@1001: p.name = "fill-ends"; Chris@1001: p.description = "Include durations (or end times) even for features without duration, by using the gap to the next feature instead."; Chris@1001: p.hasArg = false; Chris@1001: pl.push_back(p); Chris@1001: Chris@498: return pl; Chris@498: } Chris@498: Chris@498: void Chris@498: CSVFeatureWriter::setParameters(map ¶ms) Chris@498: { Chris@498: FileFeatureWriter::setParameters(params); Chris@498: Chris@690: SVDEBUG << "CSVFeatureWriter::setParameters" << endl; Chris@498: for (map::iterator i = params.begin(); Chris@498: i != params.end(); ++i) { Chris@498: cerr << i->first << " -> " << i->second << endl; Chris@498: if (i->first == "separator") { Chris@498: m_separator = i->second.c_str(); Chris@1002: cerr << "m_separator = " << m_separator << endl; Chris@1002: if (m_separator == "\\t") { Chris@1002: m_separator = QChar::Tabulation; Chris@1002: } Chris@669: } else if (i->first == "sample-timing") { Chris@669: m_sampleTiming = true; Chris@1000: } else if (i->first == "end-times") { Chris@1000: m_endTimes = true; Chris@1001: } else if (i->first == "fill-ends") { Chris@1001: m_forceEnd = true; Chris@1002: } else if (i->first == "omit-filename") { Chris@1002: m_omitFilename = true; Chris@498: } Chris@498: } Chris@498: } Chris@498: Chris@498: void Chris@498: CSVFeatureWriter::write(QString trackId, Chris@498: const Transform &transform, Chris@930: const Plugin::OutputDescriptor& , Chris@498: const Plugin::FeatureList& features, Chris@498: std::string summaryType) Chris@498: { Chris@1001: TransformId transformId = transform.getIdentifier(); Chris@1001: Chris@498: // Select appropriate output file for our track/transform Chris@498: // combination Chris@498: Chris@1035: QTextStream *sptr = getOutputStream(trackId, Chris@1035: transformId, Chris@1035: QTextCodec::codecForName("UTF-8")); Chris@604: if (!sptr) { Chris@1001: throw FailedToOpenOutputStream(trackId, transformId); Chris@604: } Chris@498: Chris@498: QTextStream &stream = *sptr; Chris@498: Chris@1001: int n = features.size(); Chris@498: Chris@1001: if (n == 0) return; Chris@1001: Chris@1006: DataId tt(trackId, transform); Chris@1001: Chris@1001: if (m_pending.find(tt) != m_pending.end()) { Chris@1001: writeFeature(tt, Chris@1001: stream, Chris@1001: m_pending[tt], Chris@1001: &features[0], Chris@1001: m_pendingSummaryTypes[tt]); Chris@1001: m_pending.erase(tt); Chris@1001: m_pendingSummaryTypes.erase(tt); Chris@1001: } Chris@1001: Chris@1001: if (m_forceEnd) { Chris@1001: // can't write final feature until we know its end time Chris@1001: --n; Chris@1001: m_pending[tt] = features[n]; Chris@1001: m_pendingSummaryTypes[tt] = summaryType; Chris@1001: } Chris@1001: Chris@1001: for (int i = 0; i < n; ++i) { Chris@1001: writeFeature(tt, Chris@1001: stream, Chris@1001: features[i], Chris@1001: m_forceEnd ? &features[i+1] : 0, Chris@1001: summaryType); Chris@1001: } Chris@1001: } Chris@1001: Chris@1001: void Chris@1001: CSVFeatureWriter::finish() Chris@1001: { Chris@1001: for (PendingFeatures::const_iterator i = m_pending.begin(); Chris@1001: i != m_pending.end(); ++i) { Chris@1006: DataId tt = i->first; Chris@1001: Plugin::Feature f = i->second; Chris@1035: QTextStream *sptr = getOutputStream(tt.first, Chris@1035: tt.second.getIdentifier(), Chris@1035: QTextCodec::codecForName("UTF-8")); Chris@1001: if (!sptr) { Chris@1006: throw FailedToOpenOutputStream(tt.first, tt.second.getIdentifier()); Chris@1001: } Chris@1001: QTextStream &stream = *sptr; Chris@1001: // final feature has its own time as end time (we can't Chris@1001: // reliably determine the end of audio file, and because of Chris@1001: // the nature of block processing, the feature could even Chris@1001: // start beyond that anyway) Chris@1001: writeFeature(tt, stream, f, &f, m_pendingSummaryTypes[tt]); Chris@1001: } Chris@1001: Chris@1001: m_pending.clear(); Chris@1001: } Chris@1001: Chris@1001: void Chris@1006: CSVFeatureWriter::writeFeature(DataId tt, Chris@1001: QTextStream &stream, Chris@1001: const Plugin::Feature &f, Chris@1001: const Plugin::Feature *optionalNextFeature, Chris@1001: std::string summaryType) Chris@1001: { Chris@1001: QString trackId = tt.first; Chris@1006: Transform transform = tt.second; Chris@1001: Chris@1002: if (!m_omitFilename) { Chris@1002: if (m_stdout || m_singleFileName != "") { Chris@1002: if (trackId != m_prevPrintedTrackId) { Chris@1002: stream << "\"" << trackId << "\"" << m_separator; Chris@1002: m_prevPrintedTrackId = trackId; Chris@1002: } else { Chris@1002: stream << m_separator; Chris@1002: } Chris@1001: } Chris@1001: } Chris@1001: Chris@1001: Vamp::RealTime duration; Chris@1001: bool haveDuration = true; Chris@1001: Chris@1001: if (f.hasDuration) { Chris@1001: duration = f.duration; Chris@1001: } else if (optionalNextFeature) { Chris@1001: duration = optionalNextFeature->timestamp - f.timestamp; Chris@1001: } else { Chris@1001: haveDuration = false; Chris@1001: } Chris@1001: Chris@1001: if (m_sampleTiming) { Chris@1001: Chris@1006: float rate = transform.getSampleRate(); Chris@1001: Chris@1001: stream << Vamp::RealTime::realTime2Frame(f.timestamp, rate); Chris@1001: Chris@1001: if (haveDuration) { Chris@1001: stream << m_separator; Chris@1001: if (m_endTimes) { Chris@1001: stream << Vamp::RealTime::realTime2Frame Chris@1001: (f.timestamp + duration, rate); Chris@514: } else { Chris@1001: stream << Vamp::RealTime::realTime2Frame(duration, rate); Chris@514: } Chris@514: } Chris@514: Chris@1001: } else { Chris@498: Chris@1001: QString timestamp = f.timestamp.toString().c_str(); Chris@1001: timestamp.replace(QRegExp("^ +"), ""); Chris@1001: stream << timestamp; Chris@669: Chris@1001: if (haveDuration) { Chris@1001: if (m_endTimes) { Chris@1001: QString endtime = Chris@1001: (f.timestamp + duration).toString().c_str(); Chris@1001: endtime.replace(QRegExp("^ +"), ""); Chris@1001: stream << m_separator << endtime; Chris@1001: } else { Chris@1001: QString d = duration.toString().c_str(); Chris@1001: d.replace(QRegExp("^ +"), ""); Chris@1001: stream << m_separator << d; Chris@669: } Chris@1001: } Chris@1001: } Chris@669: Chris@1001: if (summaryType != "") { Chris@1001: stream << m_separator << summaryType.c_str(); Chris@498: } Chris@1001: Chris@1001: for (unsigned int j = 0; j < f.values.size(); ++j) { Chris@1001: stream << m_separator << f.values[j]; Chris@1001: } Chris@1001: Chris@1001: if (f.label != "") { Chris@1001: stream << m_separator << "\"" << f.label.c_str() << "\""; Chris@1001: } Chris@1001: Chris@1001: stream << "\n"; Chris@498: } Chris@498: Chris@498: