comparison transform/CSVFeatureWriter.cpp @ 498:fdf5930b7ccc

* Bring FeatureWriter and RDFFeatureWriter into the fold (from Runner) so that we can use them to export features from SV as well
author Chris Cannam
date Fri, 28 Nov 2008 13:47:11 +0000
parents
children 6acdddf6f99e
comparison
equal deleted inserted replaced
497:b6dc6c7f402c 498:fdf5930b7ccc
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
30 CSVFeatureWriter::CSVFeatureWriter() :
31 FileFeatureWriter(SupportOneFilePerTrackTransform |
32 SupportOneFileTotal,
33 "csv"),
34 m_separator(",")
35 {
36 }
37
38 CSVFeatureWriter::~CSVFeatureWriter()
39 {
40 }
41
42 CSVFeatureWriter::ParameterList
43 CSVFeatureWriter::getSupportedParameters() const
44 {
45 ParameterList pl = FileFeatureWriter::getSupportedParameters();
46 Parameter p;
47
48 p.name = "separator";
49 p.description = "Column separator for output. Default is \",\" (comma).";
50 p.hasArg = true;
51 pl.push_back(p);
52
53 return pl;
54 }
55
56 void
57 CSVFeatureWriter::setParameters(map<string, string> &params)
58 {
59 FileFeatureWriter::setParameters(params);
60
61 cerr << "CSVFeatureWriter::setParameters" << endl;
62 for (map<string, string>::iterator i = params.begin();
63 i != params.end(); ++i) {
64 cerr << i->first << " -> " << i->second << endl;
65 if (i->first == "separator") {
66 m_separator = i->second.c_str();
67 }
68 }
69 }
70
71 void
72 CSVFeatureWriter::write(QString trackId,
73 const Transform &transform,
74 const Plugin::OutputDescriptor& output,
75 const Plugin::FeatureList& features,
76 std::string summaryType)
77 {
78 // Select appropriate output file for our track/transform
79 // combination
80
81 QTextStream *sptr = getOutputStream(trackId, transform.getIdentifier());
82 if (!sptr) return; //!!! this is probably better handled with an exception
83
84 QTextStream &stream = *sptr;
85
86 for (unsigned int i = 0; i < features.size(); ++i) {
87
88 QString timestamp = features[i].timestamp.toString().c_str();
89 timestamp.replace(QRegExp("^ +"), "");
90
91 stream << timestamp;
92
93 if (summaryType != "") {
94 stream << m_separator << summaryType.c_str();
95 }
96
97 for (unsigned int j = 0; j < features[i].values.size(); ++j) {
98 stream << m_separator << features[i].values[j];
99 }
100
101 stream << "\n";
102 }
103 }
104
105