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@189: #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@206: m_forceEnd(false), Chris@206: m_digits(6) 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@159: 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 more general 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@206: p.name = "digits"; Chris@206: p.description = "Specify the number of significant digits to use when printing transform outputs. Outputs are represented internally using single-precision floating-point, so digits beyond the 8th or 9th place are usually meaningless. The default is 6."; Chris@206: p.hasArg = true; Chris@206: pl.push_back(p); Chris@206: 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@206: } else if (i->first == "digits") { Chris@206: int digits = atoi(i->second.c_str()); Chris@206: if (digits <= 0 || digits > 100) { Chris@324: SVCERR << "LabFeatureWriter: ERROR: Invalid or out-of-range value for number of significant digits: " << i->second << endl; Chris@324: SVCERR << "LabFeatureWriter: NOTE: Continuing with default settings" << endl; Chris@206: } else { Chris@206: m_digits = digits; Chris@206: } 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@157: std::string) Chris@154: { Chris@154: // Select appropriate output file for our track/transform Chris@154: // combination Chris@154: Chris@157: TransformId transformId = transform.getIdentifier(); Chris@157: Chris@189: QTextStream *sptr = getOutputStream Chris@189: (trackId, transformId, QTextCodec::codecForName("UTF-8")); Chris@154: if (!sptr) { Chris@157: throw FailedToOpenOutputStream(trackId, transformId); Chris@154: } Chris@154: Chris@154: QTextStream &stream = *sptr; Chris@154: Chris@157: int n = features.size(); Chris@154: Chris@157: if (n == 0) return; Chris@154: Chris@176: DataId tt(trackId, transform); Chris@154: Chris@157: if (m_pending.find(tt) != m_pending.end()) { Chris@157: writeFeature(stream, m_pending[tt], &features[0]); Chris@157: m_pending.erase(tt); Chris@157: } Chris@154: Chris@157: if (m_forceEnd) { Chris@157: // can't write final feature until we know its end time Chris@157: --n; Chris@157: m_pending[tt] = features[n]; Chris@157: } Chris@154: Chris@157: for (int i = 0; i < n; ++i) { Chris@157: writeFeature(stream, features[i], m_forceEnd ? &features[i+1] : 0); Chris@154: } Chris@154: } Chris@154: Chris@157: void Chris@157: LabFeatureWriter::finish() Chris@157: { Chris@157: for (PendingFeatures::const_iterator i = m_pending.begin(); Chris@157: i != m_pending.end(); ++i) { Chris@176: DataId tt = i->first; Chris@157: Plugin::Feature f = i->second; Chris@189: QTextStream *sptr = getOutputStream Chris@189: (tt.first, tt.second.getIdentifier(), Chris@189: QTextCodec::codecForName("UTF-8")); Chris@157: if (!sptr) { Chris@176: throw FailedToOpenOutputStream(tt.first, tt.second.getIdentifier()); Chris@157: } Chris@157: QTextStream &stream = *sptr; Chris@157: // final feature has its own time as end time (we can't Chris@157: // reliably determine the end of audio file, and because of Chris@157: // the nature of block processing, the feature could even Chris@157: // start beyond that anyway) Chris@157: writeFeature(stream, f, &f); Chris@157: } Chris@158: Chris@158: m_pending.clear(); Chris@157: } Chris@154: Chris@157: void Chris@157: LabFeatureWriter::writeFeature(QTextStream &stream, Chris@157: const Plugin::Feature &f, Chris@157: const Plugin::Feature *optionalNextFeature) Chris@157: { Chris@157: QString sep = "\t"; Chris@157: Chris@157: QString timestamp = f.timestamp.toString().c_str(); Chris@157: timestamp.replace(QRegExp("^ +"), ""); Chris@157: stream << timestamp; Chris@157: Chris@157: Vamp::RealTime endTime; Chris@157: bool haveEndTime = true; Chris@157: Chris@157: if (f.hasDuration) { Chris@157: endTime = f.timestamp + f.duration; Chris@157: } else if (optionalNextFeature) { Chris@157: endTime = optionalNextFeature->timestamp; Chris@157: } else { Chris@157: haveEndTime = false; Chris@157: } Chris@157: Chris@157: if (haveEndTime) { Chris@157: QString e = endTime.toString().c_str(); Chris@157: e.replace(QRegExp("^ +"), ""); Chris@157: stream << sep << e; Chris@157: } Chris@157: Chris@157: for (unsigned int j = 0; j < f.values.size(); ++j) { Chris@206: stream << sep << QString("%1").arg(f.values[j], 0, 'g', m_digits); Chris@157: } Chris@157: Chris@157: if (f.label != "") { Chris@157: stream << sep << "\"" << f.label.c_str() << "\""; Chris@157: } Chris@157: Chris@157: stream << "\n"; Chris@157: } Chris@157: Chris@157: