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 #ifndef SV_RDF_FEATURE_WRITER_H
21 #define SV_RDF_FEATURE_WRITER_H
22 
23 #include <string>
24 #include <map>
25 #include <set>
26 
27 #include <QString>
28 
29 #include "transform/FileFeatureWriter.h"
30 
31 #include "PluginRDFDescription.h"
32 
33 using std::string;
34 using std::map;
35 using std::set;
36 using std::pair;
37 
38 class QTextStream;
39 class QFile;
40 
41 class RDFFeatureWriter : public FileFeatureWriter
42 {
43 public:
44     RDFFeatureWriter();
45     virtual ~RDFFeatureWriter();
46 
47     string getDescription() const override;
48 
49     ParameterList getSupportedParameters() const override;
50     void setParameters(map<string, string> &params) override;
51 
52     void setTrackMetadata(QString trackid, TrackMetadata metadata) override;
53 
54     void write(QString trackid,
55                        const Transform &transform,
56                        const Vamp::Plugin::OutputDescriptor &output,
57                        const Vamp::Plugin::FeatureList &features,
58                        std::string summaryType = "") override;
59 
60     virtual void setFixedEventTypeURI(QString uri); // something of a hack
61 
62     void finish() override;
63 
getWriterTag()64     QString getWriterTag() const override { return "rdf"; }
65 
66 private:
67     typedef map<QString, PluginRDFDescription> RDFDescriptionMap; // by plugin id
68     RDFDescriptionMap m_rdfDescriptions;
69 
70     typedef map<QString, TrackMetadata> TrackMetadataMap;
71     TrackMetadataMap m_metadata;
72 
haveTitleArtistMetadata(QString trackId)73     bool haveTitleArtistMetadata(QString trackId) const {
74         // Formerly in various places we used to test whether a track
75         // appeared in the metadata map at all, in order to determine
76         // whether it had any associated metadata. That won't work any
77         // more because metadata now includes duration, which can
78         // appear even if no title/artist are given and which is not
79         // something whose presence indicates the involvement of a
80         // "publication Track". So check for artist/title explicitly.
81         auto mitr = m_metadata.find(trackId);
82         if (mitr == m_metadata.end()) return false;
83         return (mitr->second.title != "" || mitr->second.maker != "");
84     }
85 
86     QString m_fixedEventTypeURI;
87 
88     void reviewFileForAppending(QString filename) override;
89 
90     void writePrefixes(QTextStream *);
91     void writeSignalDescription(QTextStream *, QString);
92     void writeLocalFeatureTypes(QTextStream *,
93                                 const Transform &,
94                                 const Vamp::Plugin::OutputDescriptor &,
95                                 PluginRDFDescription &,
96                                 std::string summaryType);
97 
98     void writeSparseRDF(QTextStream *stream,
99                         const Transform &transform,
100                         const Vamp::Plugin::OutputDescriptor &output,
101                         const Vamp::Plugin::FeatureList &features,
102                         PluginRDFDescription &desc,
103                         QString timelineURI);
104 
105     void writeTrackLevelRDF(QTextStream *stream,
106                             const Transform &transform,
107                             const Vamp::Plugin::OutputDescriptor &output,
108                             const Vamp::Plugin::FeatureList &features,
109                             PluginRDFDescription &desc,
110                             QString signalURI);
111 
112     void writeDenseRDF(QTextStream *stream,
113                        const Transform &transform,
114                        const Vamp::Plugin::OutputDescriptor &output,
115                        const Vamp::Plugin::FeatureList &features,
116                        PluginRDFDescription &desc,
117                        QString signalURI,
118                        QString timelineURI);
119 
120     set<QString> m_startedTrackIds;
121 
122     map<QTextStream *, set<Transform> > m_startedStreamTransforms;
123 
124     map<QString, QString> m_trackTrackURIs;
125     map<QString, QString> m_trackTimelineURIs;
126     map<QString, QString> m_trackSignalURIs;
127 
128     map<Transform, QString> m_transformURIs;
129     map<Transform, QString> m_syntheticEventTypeURIs;
130     map<Transform, QString> m_syntheticSignalTypeURIs;
131 
132     typedef pair<QString, Transform> StringTransformPair;
133     typedef pair<QTextStream *, QString> StreamBuffer;
134     map<StringTransformPair, StreamBuffer> m_openDenseFeatures; // signal URI + transform -> stream + text
135     QString m_userAudioFileUri;
136     QString m_userTrackUri;
137     QString m_userMakerUri;
138 
139     bool m_plain;
140 
141     bool m_network;
142     bool m_networkRetrieved;
143 
144     long m_count;
145 };
146 
147 #endif
148