annotate transform/CSVFeatureWriter.cpp @ 628:001db550bd48

* Add option to import time+duration (or time+endtime) from CSV files (importing to Region layers) * Fix ffwd/rwd in Region layers so as to behave like time-value layers
author Chris Cannam
date Thu, 08 Jul 2010 14:22:28 +0000
parents 4541581067f3
children a41b4e30e3d9
rev   line source
Chris@498 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@498 2
Chris@498 3 /*
Chris@498 4 Sonic Visualiser
Chris@498 5 An audio file viewer and annotation editor.
Chris@498 6
Chris@498 7 Sonic Annotator
Chris@498 8 A utility for batch feature extraction from audio files.
Chris@498 9
Chris@498 10 Mark Levy, Chris Sutton and Chris Cannam, Queen Mary, University of London.
Chris@498 11 Copyright 2007-2008 QMUL.
Chris@498 12
Chris@498 13 This program is free software; you can redistribute it and/or
Chris@498 14 modify it under the terms of the GNU General Public License as
Chris@498 15 published by the Free Software Foundation; either version 2 of the
Chris@498 16 License, or (at your option) any later version. See the file
Chris@498 17 COPYING included with this distribution for more information.
Chris@498 18 */
Chris@498 19
Chris@498 20 #include "CSVFeatureWriter.h"
Chris@498 21
Chris@498 22 #include <iostream>
Chris@498 23
Chris@498 24 #include <QRegExp>
Chris@498 25 #include <QTextStream>
Chris@498 26
Chris@498 27 using namespace std;
Chris@498 28 using namespace Vamp;
Chris@498 29
Chris@498 30 CSVFeatureWriter::CSVFeatureWriter() :
Chris@498 31 FileFeatureWriter(SupportOneFilePerTrackTransform |
Chris@498 32 SupportOneFileTotal,
Chris@498 33 "csv"),
Chris@498 34 m_separator(",")
Chris@498 35 {
Chris@498 36 }
Chris@498 37
Chris@498 38 CSVFeatureWriter::~CSVFeatureWriter()
Chris@498 39 {
Chris@498 40 }
Chris@498 41
Chris@498 42 CSVFeatureWriter::ParameterList
Chris@498 43 CSVFeatureWriter::getSupportedParameters() const
Chris@498 44 {
Chris@498 45 ParameterList pl = FileFeatureWriter::getSupportedParameters();
Chris@498 46 Parameter p;
Chris@498 47
Chris@498 48 p.name = "separator";
Chris@498 49 p.description = "Column separator for output. Default is \",\" (comma).";
Chris@498 50 p.hasArg = true;
Chris@498 51 pl.push_back(p);
Chris@498 52
Chris@498 53 return pl;
Chris@498 54 }
Chris@498 55
Chris@498 56 void
Chris@498 57 CSVFeatureWriter::setParameters(map<string, string> &params)
Chris@498 58 {
Chris@498 59 FileFeatureWriter::setParameters(params);
Chris@498 60
Chris@498 61 cerr << "CSVFeatureWriter::setParameters" << endl;
Chris@498 62 for (map<string, string>::iterator i = params.begin();
Chris@498 63 i != params.end(); ++i) {
Chris@498 64 cerr << i->first << " -> " << i->second << endl;
Chris@498 65 if (i->first == "separator") {
Chris@498 66 m_separator = i->second.c_str();
Chris@498 67 }
Chris@498 68 }
Chris@498 69 }
Chris@498 70
Chris@498 71 void
Chris@498 72 CSVFeatureWriter::write(QString trackId,
Chris@498 73 const Transform &transform,
Chris@498 74 const Plugin::OutputDescriptor& output,
Chris@498 75 const Plugin::FeatureList& features,
Chris@498 76 std::string summaryType)
Chris@498 77 {
Chris@498 78 // Select appropriate output file for our track/transform
Chris@498 79 // combination
Chris@498 80
Chris@498 81 QTextStream *sptr = getOutputStream(trackId, transform.getIdentifier());
Chris@604 82 if (!sptr) {
Chris@604 83 throw FailedToOpenOutputStream(trackId, transform.getIdentifier());
Chris@604 84 }
Chris@498 85
Chris@498 86 QTextStream &stream = *sptr;
Chris@498 87
Chris@498 88 for (unsigned int i = 0; i < features.size(); ++i) {
Chris@498 89
Chris@514 90 if (m_stdout || m_singleFileName != "") {
Chris@514 91 if (trackId != m_prevPrintedTrackId) {
Chris@514 92 stream << "\"" << trackId << "\"" << m_separator;
Chris@514 93 m_prevPrintedTrackId = trackId;
Chris@514 94 } else {
Chris@514 95 stream << m_separator;
Chris@514 96 }
Chris@514 97 }
Chris@514 98
Chris@498 99 QString timestamp = features[i].timestamp.toString().c_str();
Chris@498 100 timestamp.replace(QRegExp("^ +"), "");
Chris@514 101 stream << timestamp;
Chris@498 102
Chris@514 103 if (features[i].hasDuration) {
Chris@514 104 QString duration = features[i].duration.toString().c_str();
Chris@514 105 duration.replace(QRegExp("^ +"), "");
Chris@514 106 stream << m_separator << duration;
Chris@514 107 }
Chris@498 108
Chris@498 109 if (summaryType != "") {
Chris@498 110 stream << m_separator << summaryType.c_str();
Chris@498 111 }
Chris@498 112
Chris@498 113 for (unsigned int j = 0; j < features[i].values.size(); ++j) {
Chris@498 114 stream << m_separator << features[i].values[j];
Chris@498 115 }
Chris@498 116
Chris@514 117 if (features[i].label != "") {
Chris@514 118 stream << m_separator << "\"" << features[i].label.c_str() << "\"";
Chris@514 119 }
Chris@514 120
Chris@498 121 stream << "\n";
Chris@498 122 }
Chris@498 123 }
Chris@498 124
Chris@498 125