comparison runner/LabFeatureWriter.cpp @ 157:946115b8badd labfile

Proper implementation of fill-ends flag in LabFeatureWriter
author Chris Cannam
date Wed, 15 Oct 2014 08:00:01 +0100
parents 6ff4da31db8b
children a2310369b2cc
comparison
equal deleted inserted replaced
156:0fd5c3c28814 157:946115b8badd
76 void 76 void
77 LabFeatureWriter::write(QString trackId, 77 LabFeatureWriter::write(QString trackId,
78 const Transform &transform, 78 const Transform &transform,
79 const Plugin::OutputDescriptor& , 79 const Plugin::OutputDescriptor& ,
80 const Plugin::FeatureList& features, 80 const Plugin::FeatureList& features,
81 std::string summaryType) 81 std::string)
82 { 82 {
83 // Select appropriate output file for our track/transform 83 // Select appropriate output file for our track/transform
84 // combination 84 // combination
85 85
86 QTextStream *sptr = getOutputStream(trackId, transform.getIdentifier()); 86 TransformId transformId = transform.getIdentifier();
87
88 QTextStream *sptr = getOutputStream(trackId, transformId);
87 if (!sptr) { 89 if (!sptr) {
88 throw FailedToOpenOutputStream(trackId, transform.getIdentifier()); 90 throw FailedToOpenOutputStream(trackId, transformId);
89 } 91 }
90 92
91 QTextStream &stream = *sptr; 93 QTextStream &stream = *sptr;
92 94
93 QString sep = "\t"; 95 int n = features.size();
94 96
95 for (unsigned int i = 0; i < features.size(); ++i) { 97 if (n == 0) return;
96 98
97 QString timestamp = features[i].timestamp.toString().c_str(); 99 TrackTransformPair tt(trackId, transformId);
98 timestamp.replace(QRegExp("^ +"), "");
99 stream << timestamp;
100 100
101 Vamp::RealTime endTime; 101 if (m_pending.find(tt) != m_pending.end()) {
102 bool haveEndTime = true; 102 writeFeature(stream, m_pending[tt], &features[0]);
103 m_pending.erase(tt);
104 }
103 105
104 if (features[i].hasDuration) { 106 if (m_forceEnd) {
105 endTime = features[i].timestamp + features[i].duration; 107 // can't write final feature until we know its end time
106 } else if (m_forceEnd) { 108 --n;
107 if (i+1 < features.size()) { 109 m_pending[tt] = features[n];
108 endTime = features[i+1].timestamp; 110 }
109 } else {
110 //!!! what to do??? can we get the end time of the input file?
111 endTime = features[i].timestamp;
112 }
113 } else {
114 haveEndTime = false;
115 }
116 111
117 if (haveEndTime) { 112 for (int i = 0; i < n; ++i) {
118 QString e = endTime.toString().c_str(); 113 writeFeature(stream, features[i], m_forceEnd ? &features[i+1] : 0);
119 e.replace(QRegExp("^ +"), "");
120 stream << sep << e;
121 }
122
123 for (unsigned int j = 0; j < features[i].values.size(); ++j) {
124 stream << sep << features[i].values[j];
125 }
126
127 if (features[i].label != "") {
128 stream << sep << "\"" << features[i].label.c_str() << "\"";
129 }
130
131 stream << "\n";
132 } 114 }
133 } 115 }
134 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 }
135 136
137 void
138 LabFeatureWriter::writeFeature(QTextStream &stream,
139 const Plugin::Feature &f,
140 const Plugin::Feature *optionalNextFeature)
141 {
142 QString sep = "\t";
143
144 QString timestamp = f.timestamp.toString().c_str();
145 timestamp.replace(QRegExp("^ +"), "");
146 stream << timestamp;
147
148 Vamp::RealTime endTime;
149 bool haveEndTime = true;
150
151 if (f.hasDuration) {
152 endTime = f.timestamp + f.duration;
153 } else if (optionalNextFeature) {
154 endTime = optionalNextFeature->timestamp;
155 } else {
156 haveEndTime = false;
157 }
158
159 if (haveEndTime) {
160 QString e = endTime.toString().c_str();
161 e.replace(QRegExp("^ +"), "");
162 stream << sep << e;
163 }
164
165 for (unsigned int j = 0; j < f.values.size(); ++j) {
166 stream << sep << f.values[j];
167 }
168
169 if (f.label != "") {
170 stream << sep << "\"" << f.label.c_str() << "\"";
171 }
172
173 stream << "\n";
174 }
175
176