Chris@154: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@154: Chris@154: /* Chris@154: Sonic Visualiser Chris@154: An audio file viewer and annotation editor. Chris@154: Chris@154: Sonic Annotator Chris@154: A utility for batch feature extraction from audio files. Chris@154: Chris@154: Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London. Chris@154: Copyright 2007-2008 QMUL. Chris@154: Chris@154: This program is free software; you can redistribute it and/or Chris@154: modify it under the terms of the GNU General Public License as Chris@154: published by the Free Software Foundation; either version 2 of the Chris@154: License, or (at your option) any later version. See the file Chris@154: COPYING included with this distribution for more information. Chris@154: */ Chris@154: Chris@154: #include "LabFeatureWriter.h" Chris@154: Chris@154: #include Chris@154: Chris@154: #include Chris@154: #include Chris@154: Chris@154: using namespace std; Chris@154: using namespace Vamp; Chris@154: Chris@154: LabFeatureWriter::LabFeatureWriter() : Chris@154: FileFeatureWriter(SupportOneFilePerTrackTransform | Chris@154: SupportStdOut, Chris@154: "lab"), Chris@154: m_forceEnd(false) Chris@154: { Chris@154: } Chris@154: Chris@154: LabFeatureWriter::~LabFeatureWriter() Chris@154: { Chris@154: } Chris@154: Chris@154: string Chris@154: LabFeatureWriter::getDescription() const Chris@154: { Chris@154: return "Write features in .lab, a tab-separated columnar format. The first column is always the feature start time in seconds. If the features have duration, the second column will be the feature end time in seconds. Remaining columns are the feature values (if any) and finally the feature label (if any). There is no identification of the audio file or the transform, so confusion will result if features from different audio or transforms are mixed. For more control over the output, consider using the CSV writer."; Chris@154: } Chris@154: Chris@154: LabFeatureWriter::ParameterList Chris@154: LabFeatureWriter::getSupportedParameters() const Chris@154: { Chris@154: ParameterList pl = FileFeatureWriter::getSupportedParameters(); Chris@154: Chris@154: Parameter p; Chris@154: Chris@154: p.name = "fill-ends"; Chris@154: p.description = "Include end times even for features without duration, by using the gap to the next feature instead."; Chris@154: p.hasArg = false; Chris@154: pl.push_back(p); Chris@154: Chris@154: return pl; Chris@154: } Chris@154: Chris@154: void Chris@154: LabFeatureWriter::setParameters(map ¶ms) Chris@154: { Chris@154: FileFeatureWriter::setParameters(params); Chris@154: Chris@154: for (map::iterator i = params.begin(); Chris@154: i != params.end(); ++i) { Chris@154: if (i->first == "fill-ends") { Chris@154: m_forceEnd = true; Chris@154: } Chris@154: } Chris@154: } Chris@154: Chris@154: void Chris@154: LabFeatureWriter::write(QString trackId, Chris@154: const Transform &transform, Chris@154: const Plugin::OutputDescriptor& , Chris@154: const Plugin::FeatureList& features, Chris@154: std::string summaryType) Chris@154: { Chris@154: // Select appropriate output file for our track/transform Chris@154: // combination Chris@154: Chris@154: QTextStream *sptr = getOutputStream(trackId, transform.getIdentifier()); Chris@154: if (!sptr) { Chris@154: throw FailedToOpenOutputStream(trackId, transform.getIdentifier()); Chris@154: } Chris@154: Chris@154: QTextStream &stream = *sptr; Chris@154: Chris@154: QString sep = "\t"; Chris@154: Chris@154: for (unsigned int i = 0; i < features.size(); ++i) { Chris@154: Chris@154: QString timestamp = features[i].timestamp.toString().c_str(); Chris@154: timestamp.replace(QRegExp("^ +"), ""); Chris@154: stream << timestamp; Chris@154: Chris@154: Vamp::RealTime endTime; Chris@154: bool haveEndTime = true; Chris@154: Chris@154: if (features[i].hasDuration) { Chris@154: endTime = features[i].timestamp + features[i].duration; Chris@154: } else if (m_forceEnd) { Chris@154: if (i+1 < features.size()) { Chris@154: endTime = features[i+1].timestamp; Chris@154: } else { Chris@154: //!!! what to do??? can we get the end time of the input file? Chris@154: endTime = features[i].timestamp; Chris@154: } Chris@154: } else { Chris@154: haveEndTime = false; Chris@154: } Chris@154: Chris@154: if (haveEndTime) { Chris@154: QString e = endTime.toString().c_str(); Chris@154: e.replace(QRegExp("^ +"), ""); Chris@154: stream << sep << e; Chris@154: } Chris@154: Chris@154: for (unsigned int j = 0; j < features[i].values.size(); ++j) { Chris@154: stream << sep << features[i].values[j]; Chris@154: } Chris@154: Chris@154: if (features[i].label != "") { Chris@154: stream << sep << "\"" << features[i].label.c_str() << "\""; Chris@154: } Chris@154: Chris@154: stream << "\n"; Chris@154: } Chris@154: } Chris@154: Chris@154: