svcore  1.9
CSVFeatureWriter.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006 
00007     Sonic Annotator
00008     A utility for batch feature extraction from audio files.
00009 
00010     Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London.
00011     Copyright 2007-2008 QMUL.
00012 
00013     This program is free software; you can redistribute it and/or
00014     modify it under the terms of the GNU General Public License as
00015     published by the Free Software Foundation; either version 2 of the
00016     License, or (at your option) any later version.  See the file
00017     COPYING included with this distribution for more information.
00018 */
00019 
00020 #include "CSVFeatureWriter.h"
00021 
00022 #include <iostream>
00023 
00024 #include <QRegExp>
00025 #include <QTextStream>
00026 
00027 using namespace std;
00028 using namespace Vamp;
00029 
00030 CSVFeatureWriter::CSVFeatureWriter() :
00031     FileFeatureWriter(SupportOneFilePerTrackTransform |
00032                       SupportOneFileTotal,
00033                       "csv"),
00034     m_separator(","),
00035     m_sampleTiming(false)
00036 {
00037 }
00038 
00039 CSVFeatureWriter::~CSVFeatureWriter()
00040 {
00041 }
00042 
00043 CSVFeatureWriter::ParameterList
00044 CSVFeatureWriter::getSupportedParameters() const
00045 {
00046     ParameterList pl = FileFeatureWriter::getSupportedParameters();
00047     Parameter p;
00048     
00049     p.name = "separator";
00050     p.description = "Column separator for output.  Default is \",\" (comma).";
00051     p.hasArg = true;
00052     pl.push_back(p);
00053     
00054     p.name = "sample-timing";
00055     p.description = "Show timings as sample frame counts instead of in seconds.";
00056     p.hasArg = false;
00057     pl.push_back(p);
00058 
00059     return pl;
00060 }
00061 
00062 void
00063 CSVFeatureWriter::setParameters(map<string, string> &params)
00064 {
00065     FileFeatureWriter::setParameters(params);
00066 
00067     SVDEBUG << "CSVFeatureWriter::setParameters" << endl;
00068     for (map<string, string>::iterator i = params.begin();
00069          i != params.end(); ++i) {
00070         cerr << i->first << " -> " << i->second << endl;
00071         if (i->first == "separator") {
00072             m_separator = i->second.c_str();
00073         } else if (i->first == "sample-timing") {
00074             m_sampleTiming = true;
00075         }
00076     }
00077 }
00078 
00079 void
00080 CSVFeatureWriter::write(QString trackId,
00081                         const Transform &transform,
00082                         const Plugin::OutputDescriptor& ,
00083                         const Plugin::FeatureList& features,
00084                         std::string summaryType)
00085 {
00086     // Select appropriate output file for our track/transform
00087     // combination
00088 
00089     QTextStream *sptr = getOutputStream(trackId, transform.getIdentifier());
00090     if (!sptr) {
00091         throw FailedToOpenOutputStream(trackId, transform.getIdentifier());
00092     }
00093 
00094     QTextStream &stream = *sptr;
00095 
00096     for (unsigned int i = 0; i < features.size(); ++i) {
00097 
00098         if (m_stdout || m_singleFileName != "") {
00099             if (trackId != m_prevPrintedTrackId) {
00100                 stream << "\"" << trackId << "\"" << m_separator;
00101                 m_prevPrintedTrackId = trackId;
00102             } else {
00103                 stream << m_separator;
00104             }
00105         }
00106 
00107         if (m_sampleTiming) {
00108 
00109             stream << Vamp::RealTime::realTime2Frame
00110                 (features[i].timestamp, transform.getSampleRate());
00111 
00112             if (features[i].hasDuration) {
00113                 stream << m_separator;
00114                 stream << Vamp::RealTime::realTime2Frame
00115                     (features[i].duration, transform.getSampleRate());
00116             }
00117 
00118         } else {
00119 
00120             QString timestamp = features[i].timestamp.toString().c_str();
00121             timestamp.replace(QRegExp("^ +"), "");
00122             stream << timestamp;
00123 
00124             if (features[i].hasDuration) {
00125                 QString duration = features[i].duration.toString().c_str();
00126                 duration.replace(QRegExp("^ +"), "");
00127                 stream << m_separator << duration;
00128             }            
00129         }
00130 
00131         if (summaryType != "") {
00132             stream << m_separator << summaryType.c_str();
00133         }
00134 
00135         for (unsigned int j = 0; j < features[i].values.size(); ++j) {
00136             stream << m_separator << features[i].values[j];
00137         }
00138 
00139         if (features[i].label != "") {
00140             stream << m_separator << "\"" << features[i].label.c_str() << "\"";
00141         }
00142 
00143         stream << "\n";
00144     }
00145 }
00146 
00147