comparison runner/LabFeatureWriter.cpp @ 161:4b19d824a213 jams

Merge from default branch
author Chris Cannam
date Wed, 15 Oct 2014 11:33:14 +0100
parents 237ccacbb85e
children 59abb58b1855
comparison
equal deleted inserted replaced
153:ad96fd5f9cd7 161:4b19d824a213
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 "LabFeatureWriter.h"
21
22 #include <iostream>
23
24 #include <QRegExp>
25 #include <QTextStream>
26
27 using namespace std;
28 using namespace Vamp;
29
30 LabFeatureWriter::LabFeatureWriter() :
31 FileFeatureWriter(SupportOneFilePerTrackTransform |
32 SupportStdOut,
33 "lab"),
34 m_forceEnd(false)
35 {
36 }
37
38 LabFeatureWriter::~LabFeatureWriter()
39 {
40 }
41
42 string
43 LabFeatureWriter::getDescription() const
44 {
45 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.)";
46 }
47
48 LabFeatureWriter::ParameterList
49 LabFeatureWriter::getSupportedParameters() const
50 {
51 ParameterList pl = FileFeatureWriter::getSupportedParameters();
52
53 Parameter p;
54
55 p.name = "fill-ends";
56 p.description = "Include end times even for features without duration, by using the gap to the next feature instead.";
57 p.hasArg = false;
58 pl.push_back(p);
59
60 return pl;
61 }
62
63 void
64 LabFeatureWriter::setParameters(map<string, string> &params)
65 {
66 FileFeatureWriter::setParameters(params);
67
68 for (map<string, string>::iterator i = params.begin();
69 i != params.end(); ++i) {
70 if (i->first == "fill-ends") {
71 m_forceEnd = true;
72 }
73 }
74 }
75
76 void
77 LabFeatureWriter::write(QString trackId,
78 const Transform &transform,
79 const Plugin::OutputDescriptor& ,
80 const Plugin::FeatureList& features,
81 std::string)
82 {
83 // Select appropriate output file for our track/transform
84 // combination
85
86 TransformId transformId = transform.getIdentifier();
87
88 QTextStream *sptr = getOutputStream(trackId, transformId);
89 if (!sptr) {
90 throw FailedToOpenOutputStream(trackId, transformId);
91 }
92
93 QTextStream &stream = *sptr;
94
95 int n = features.size();
96
97 if (n == 0) return;
98
99 TrackTransformPair tt(trackId, transformId);
100
101 if (m_pending.find(tt) != m_pending.end()) {
102 writeFeature(stream, m_pending[tt], &features[0]);
103 m_pending.erase(tt);
104 }
105
106 if (m_forceEnd) {
107 // can't write final feature until we know its end time
108 --n;
109 m_pending[tt] = features[n];
110 }
111
112 for (int i = 0; i < n; ++i) {
113 writeFeature(stream, features[i], m_forceEnd ? &features[i+1] : 0);
114 }
115 }
116
117 void
118 LabFeatureWriter::finish()
119 {
120 for (PendingFeatures::const_iterator i = m_pending.begin();
121 i != m_pending.end(); ++i) {
122 TrackTransformPair tt = i->first;
123 Plugin::Feature f = i->second;
124 QTextStream *sptr = getOutputStream(tt.first, tt.second);
125 if (!sptr) {
126 throw FailedToOpenOutputStream(tt.first, tt.second);
127 }
128 QTextStream &stream = *sptr;
129 // final feature has its own time as end time (we can't
130 // reliably determine the end of audio file, and because of
131 // the nature of block processing, the feature could even
132 // start beyond that anyway)
133 writeFeature(stream, f, &f);
134 }
135
136 m_pending.clear();
137 }
138
139 void
140 LabFeatureWriter::writeFeature(QTextStream &stream,
141 const Plugin::Feature &f,
142 const Plugin::Feature *optionalNextFeature)
143 {
144 QString sep = "\t";
145
146 QString timestamp = f.timestamp.toString().c_str();
147 timestamp.replace(QRegExp("^ +"), "");
148 stream << timestamp;
149
150 Vamp::RealTime endTime;
151 bool haveEndTime = true;
152
153 if (f.hasDuration) {
154 endTime = f.timestamp + f.duration;
155 } else if (optionalNextFeature) {
156 endTime = optionalNextFeature->timestamp;
157 } else {
158 haveEndTime = false;
159 }
160
161 if (haveEndTime) {
162 QString e = endTime.toString().c_str();
163 e.replace(QRegExp("^ +"), "");
164 stream << sep << e;
165 }
166
167 for (unsigned int j = 0; j < f.values.size(); ++j) {
168 stream << sep << f.values[j];
169 }
170
171 if (f.label != "") {
172 stream << sep << "\"" << f.label.c_str() << "\"";
173 }
174
175 stream << "\n";
176 }
177
178