comparison runner/LabFeatureWriter.cpp @ 154:6ff4da31db8b labfile

Implement .lab file writer
author Chris Cannam
date Tue, 14 Oct 2014 17:30:44 +0100
parents
children 946115b8badd
comparison
equal deleted inserted replaced
148:04945e74d314 154:6ff4da31db8b
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 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 summaryType)
82 {
83 // Select appropriate output file for our track/transform
84 // combination
85
86 QTextStream *sptr = getOutputStream(trackId, transform.getIdentifier());
87 if (!sptr) {
88 throw FailedToOpenOutputStream(trackId, transform.getIdentifier());
89 }
90
91 QTextStream &stream = *sptr;
92
93 QString sep = "\t";
94
95 for (unsigned int i = 0; i < features.size(); ++i) {
96
97 QString timestamp = features[i].timestamp.toString().c_str();
98 timestamp.replace(QRegExp("^ +"), "");
99 stream << timestamp;
100
101 Vamp::RealTime endTime;
102 bool haveEndTime = true;
103
104 if (features[i].hasDuration) {
105 endTime = features[i].timestamp + features[i].duration;
106 } else if (m_forceEnd) {
107 if (i+1 < features.size()) {
108 endTime = features[i+1].timestamp;
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
117 if (haveEndTime) {
118 QString e = endTime.toString().c_str();
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 }
133 }
134
135