svcore  1.9
CSVFeatureWriter.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6 
7  Sonic Annotator
8  A utility for batch feature extraction from audio files.
9 
10  Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London.
11  Copyright 2007-2008 QMUL.
12 
13  This program is free software; you can redistribute it and/or
14  modify it under the terms of the GNU General Public License as
15  published by the Free Software Foundation; either version 2 of the
16  License, or (at your option) any later version. See the file
17  COPYING included with this distribution for more information.
18 */
19 
20 #include "CSVFeatureWriter.h"
21 
22 #include <iostream>
23 
24 #include <QRegExp>
25 #include <QTextStream>
26 
27 using namespace std;
28 using namespace Vamp;
29 
31  FileFeatureWriter(SupportOneFilePerTrackTransform |
32  SupportOneFileTotal,
33  "csv"),
34  m_separator(","),
35  m_sampleTiming(false)
36 {
37 }
38 
40 {
41 }
42 
45 {
47  Parameter p;
48 
49  p.name = "separator";
50  p.description = "Column separator for output. Default is \",\" (comma).";
51  p.hasArg = true;
52  pl.push_back(p);
53 
54  p.name = "sample-timing";
55  p.description = "Show timings as sample frame counts instead of in seconds.";
56  p.hasArg = false;
57  pl.push_back(p);
58 
59  return pl;
60 }
61 
62 void
63 CSVFeatureWriter::setParameters(map<string, string> &params)
64 {
66 
67  SVDEBUG << "CSVFeatureWriter::setParameters" << endl;
68  for (map<string, string>::iterator i = params.begin();
69  i != params.end(); ++i) {
70  cerr << i->first << " -> " << i->second << endl;
71  if (i->first == "separator") {
72  m_separator = i->second.c_str();
73  } else if (i->first == "sample-timing") {
74  m_sampleTiming = true;
75  }
76  }
77 }
78 
79 void
80 CSVFeatureWriter::write(QString trackId,
81  const Transform &transform,
82  const Plugin::OutputDescriptor& ,
83  const Plugin::FeatureList& features,
84  std::string summaryType)
85 {
86  // Select appropriate output file for our track/transform
87  // combination
88 
89  QTextStream *sptr = getOutputStream(trackId, transform.getIdentifier());
90  if (!sptr) {
91  throw FailedToOpenOutputStream(trackId, transform.getIdentifier());
92  }
93 
94  QTextStream &stream = *sptr;
95 
96  for (unsigned int i = 0; i < features.size(); ++i) {
97 
98  if (m_stdout || m_singleFileName != "") {
99  if (trackId != m_prevPrintedTrackId) {
100  stream << "\"" << trackId << "\"" << m_separator;
101  m_prevPrintedTrackId = trackId;
102  } else {
103  stream << m_separator;
104  }
105  }
106 
107  if (m_sampleTiming) {
108 
109  stream << Vamp::RealTime::realTime2Frame
110  (features[i].timestamp, transform.getSampleRate());
111 
112  if (features[i].hasDuration) {
113  stream << m_separator;
114  stream << Vamp::RealTime::realTime2Frame
115  (features[i].duration, transform.getSampleRate());
116  }
117 
118  } else {
119 
120  QString timestamp = features[i].timestamp.toString().c_str();
121  timestamp.replace(QRegExp("^ +"), "");
122  stream << timestamp;
123 
124  if (features[i].hasDuration) {
125  QString duration = features[i].duration.toString().c_str();
126  duration.replace(QRegExp("^ +"), "");
127  stream << m_separator << duration;
128  }
129  }
130 
131  if (summaryType != "") {
132  stream << m_separator << summaryType.c_str();
133  }
134 
135  for (unsigned int j = 0; j < features[i].values.size(); ++j) {
136  stream << m_separator << features[i].values[j];
137  }
138 
139  if (features[i].label != "") {
140  stream << m_separator << "\"" << features[i].label.c_str() << "\"";
141  }
142 
143  stream << "\n";
144  }
145 }
146 
147 
virtual ParameterList getSupportedParameters() const
float getSampleRate() const
Definition: Transform.cpp:383
vector< Parameter > ParameterList
Definition: FeatureWriter.h:47
virtual ParameterList getSupportedParameters() const
virtual void setParameters(map< string, string > &params)
virtual void write(QString trackid, const Transform &transform, const Vamp::Plugin::OutputDescriptor &output, const Vamp::Plugin::FeatureList &features, std::string summaryType="")
virtual ~CSVFeatureWriter()
#define SVDEBUG
Definition: Debug.h:42
TransformId getIdentifier() const
Definition: Transform.cpp:182
virtual void setParameters(map< string, string > &params)
QString m_prevPrintedTrackId
QTextStream * getOutputStream(QString, TransformId)