svcore  1.9
RDFExporter.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     This file copyright 2008 QMUL.
00008 
00009     This program is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU General Public License as
00011     published by the Free Software Foundation; either version 2 of the
00012     License, or (at your option) any later version.  See the file
00013     COPYING included with this distribution for more information.
00014 */
00015 
00016 #include "RDFExporter.h"
00017 #include "RDFFeatureWriter.h"
00018 
00019 #include <vamp-hostsdk/Plugin.h>
00020 
00021 #include "data/model/Model.h"
00022 #include "data/model/RegionModel.h"
00023 #include "data/model/NoteModel.h"
00024 #include "data/model/SparseOneDimensionalModel.h"
00025 #include "data/model/SparseTimeValueModel.h"
00026 #include "data/model/TextModel.h"
00027 #include "data/model/EditableDenseThreeDimensionalModel.h"
00028 
00029 bool
00030 RDFExporter::canExportModel(Model *m)
00031 {
00032     if (dynamic_cast<RegionModel *>(m)) return true;
00033     if (dynamic_cast<NoteModel *>(m)) return true;
00034     if (dynamic_cast<SparseTimeValueModel *>(m)) return true;
00035     if (dynamic_cast<SparseOneDimensionalModel *>(m)) return true;
00036     if (dynamic_cast<TextModel *>(m)) return true;
00037     // no, looks like we never implemented this one
00038 //    if (dynamic_cast<EditableDenseThreeDimensionalModel *>(m)) return true;
00039     return false;
00040 }
00041 
00042 RDFExporter::RDFExporter(QString path, Model *m) :
00043     m_path(path),
00044     m_model(m),
00045     m_fw(new RDFFeatureWriter())
00046 {
00047     map<string, string> params;
00048     params["one-file"] = path.toStdString();
00049     params["force"] = "true";
00050     m_fw->setParameters(params);
00051 }
00052 
00053 RDFExporter::~RDFExporter()
00054 {
00055     delete m_fw;
00056 }
00057 
00058 bool
00059 RDFExporter::isOK() const
00060 {
00061     return true;
00062 }
00063 
00064 QString
00065 RDFExporter::getError() const
00066 {
00067     return "";
00068 }
00069 
00070 void
00071 RDFExporter::write()
00072 {
00073     QString trackId; // nil
00074     Transform transform; // nil
00075     Vamp::Plugin::OutputDescriptor output; // nil
00076     std::string summaryType; // nil
00077 
00078     Vamp::Plugin::FeatureList features;
00079     features.push_back(Vamp::Plugin::Feature());
00080     Vamp::Plugin::Feature &f = features[0];
00081     int sr = m_model->getSampleRate();
00082 
00083     {
00084         RegionModel *m = dynamic_cast<RegionModel *>(m_model);
00085         if (m) {
00086             f.hasTimestamp = true;
00087             f.hasDuration = true;
00088             const RegionModel::PointList &pl(m->getPoints());
00089             for (RegionModel::PointList::const_iterator i = pl.begin(); 
00090                  i != pl.end(); ++i) {
00091                 f.timestamp = Vamp::RealTime::frame2RealTime(i->frame, sr);
00092                 f.duration = Vamp::RealTime::frame2RealTime(i->duration, sr);
00093                 f.values.clear();
00094                 f.values.push_back(i->value);
00095                 f.label = i->label.toStdString();
00096                 m_fw->write(trackId, transform, output, features, summaryType);
00097             }
00098             return;
00099         }
00100     }
00101     {
00102         NoteModel *m = dynamic_cast<NoteModel *>(m_model);
00103         if (m) {
00104             f.hasTimestamp = true;
00105             f.hasDuration = true;
00106             const NoteModel::PointList &pl(m->getPoints());
00107             for (NoteModel::PointList::const_iterator i = pl.begin(); 
00108                  i != pl.end(); ++i) {
00109                 f.timestamp = Vamp::RealTime::frame2RealTime(i->frame, sr);
00110                 f.duration = Vamp::RealTime::frame2RealTime(i->duration, sr);
00111                 f.values.clear();
00112                 f.values.push_back(i->value);
00113                 f.values.push_back(i->level);
00114                 f.label = i->label.toStdString();
00115                 m_fw->write(trackId, transform, output, features, summaryType);
00116             }
00117             return;
00118         }
00119     }
00120     {     
00121         SparseOneDimensionalModel *m = dynamic_cast<SparseOneDimensionalModel *>(m_model);
00122         if (m) {
00123             f.hasTimestamp = true;
00124             f.hasDuration = false;
00125             const SparseOneDimensionalModel::PointList &pl(m->getPoints());
00126             for (SparseOneDimensionalModel::PointList::const_iterator i = pl.begin(); 
00127                  i != pl.end(); ++i) {
00128                 f.timestamp = Vamp::RealTime::frame2RealTime(i->frame, sr);
00129                 f.values.clear();
00130                 f.label = i->label.toStdString();
00131                 m_fw->write(trackId, transform, output, features, summaryType);
00132             }
00133             return;
00134         }
00135     }
00136     {            
00137         SparseTimeValueModel *m = dynamic_cast<SparseTimeValueModel *>(m_model);
00138         if (m) {
00139             f.hasTimestamp = true;
00140             f.hasDuration = false;
00141             const SparseTimeValueModel::PointList &pl(m->getPoints());
00142             for (SparseTimeValueModel::PointList::const_iterator i = pl.begin(); 
00143                  i != pl.end(); ++i) {
00144                 f.timestamp = Vamp::RealTime::frame2RealTime(i->frame, sr);
00145                 f.values.clear();
00146                 f.values.push_back(i->value);
00147                 f.label = i->label.toStdString();
00148                 m_fw->write(trackId, transform, output, features, summaryType);
00149             }
00150             return;
00151         }
00152     }
00153     {     
00154         TextModel *m = dynamic_cast<TextModel *>(m_model);
00155         if (m) {
00156             f.hasTimestamp = true;
00157             f.hasDuration = false;
00158             const TextModel::PointList &pl(m->getPoints());
00159             m_fw->setFixedEventTypeURI("af:Text");
00160             for (TextModel::PointList::const_iterator i = pl.begin(); 
00161                  i != pl.end(); ++i) {
00162                 f.timestamp = Vamp::RealTime::frame2RealTime(i->frame, sr);
00163                 f.values.clear();
00164                 f.values.push_back(i->height);
00165                 f.label = i->label.toStdString();
00166                 m_fw->write(trackId, transform, output, features, summaryType);
00167             }
00168             return;
00169         }
00170     }
00171 }
00172 
00173 QString
00174 RDFExporter::getSupportedExtensions()
00175 {
00176     return "*.ttl *.n3";
00177 }
00178