File: CSVFeatureWriter.cpp

package info (click to toggle)
sonic-visualiser 2.4.1~repack1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,432 kB
  • ctags: 9,637
  • sloc: cpp: 91,711; ansic: 1,138; sh: 1,012; xml: 64; makefile: 36
file content (147 lines) | stat: -rw-r--r-- 4,243 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.

    Sonic Annotator
    A utility for batch feature extraction from audio files.

    Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London.
    Copyright 2007-2008 QMUL.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

#include "CSVFeatureWriter.h"

#include <iostream>

#include <QRegExp>
#include <QTextStream>

using namespace std;
using namespace Vamp;

CSVFeatureWriter::CSVFeatureWriter() :
    FileFeatureWriter(SupportOneFilePerTrackTransform |
                      SupportOneFileTotal,
                      "csv"),
    m_separator(","),
    m_sampleTiming(false)
{
}

CSVFeatureWriter::~CSVFeatureWriter()
{
}

CSVFeatureWriter::ParameterList
CSVFeatureWriter::getSupportedParameters() const
{
    ParameterList pl = FileFeatureWriter::getSupportedParameters();
    Parameter p;
    
    p.name = "separator";
    p.description = "Column separator for output.  Default is \",\" (comma).";
    p.hasArg = true;
    pl.push_back(p);
    
    p.name = "sample-timing";
    p.description = "Show timings as sample frame counts instead of in seconds.";
    p.hasArg = false;
    pl.push_back(p);

    return pl;
}

void
CSVFeatureWriter::setParameters(map<string, string> &params)
{
    FileFeatureWriter::setParameters(params);

    SVDEBUG << "CSVFeatureWriter::setParameters" << endl;
    for (map<string, string>::iterator i = params.begin();
         i != params.end(); ++i) {
        cerr << i->first << " -> " << i->second << endl;
        if (i->first == "separator") {
            m_separator = i->second.c_str();
        } else if (i->first == "sample-timing") {
            m_sampleTiming = true;
        }
    }
}

void
CSVFeatureWriter::write(QString trackId,
                        const Transform &transform,
                        const Plugin::OutputDescriptor& ,
                        const Plugin::FeatureList& features,
                        std::string summaryType)
{
    // Select appropriate output file for our track/transform
    // combination

    QTextStream *sptr = getOutputStream(trackId, transform.getIdentifier());
    if (!sptr) {
        throw FailedToOpenOutputStream(trackId, transform.getIdentifier());
    }

    QTextStream &stream = *sptr;

    for (unsigned int i = 0; i < features.size(); ++i) {

        if (m_stdout || m_singleFileName != "") {
            if (trackId != m_prevPrintedTrackId) {
                stream << "\"" << trackId << "\"" << m_separator;
                m_prevPrintedTrackId = trackId;
            } else {
                stream << m_separator;
            }
        }

        if (m_sampleTiming) {

            stream << Vamp::RealTime::realTime2Frame
                (features[i].timestamp, transform.getSampleRate());

            if (features[i].hasDuration) {
                stream << m_separator;
                stream << Vamp::RealTime::realTime2Frame
                    (features[i].duration, transform.getSampleRate());
            }

        } else {

            QString timestamp = features[i].timestamp.toString().c_str();
            timestamp.replace(QRegExp("^ +"), "");
            stream << timestamp;

            if (features[i].hasDuration) {
                QString duration = features[i].duration.toString().c_str();
                duration.replace(QRegExp("^ +"), "");
                stream << m_separator << duration;
            }            
        }

        if (summaryType != "") {
            stream << m_separator << summaryType.c_str();
        }

        for (unsigned int j = 0; j < features[i].values.size(); ++j) {
            stream << m_separator << features[i].values[j];
        }

        if (features[i].label != "") {
            stream << m_separator << "\"" << features[i].label.c_str() << "\"";
        }

        stream << "\n";
    }
}