Chris@0
|
1 /* -*- c-basic-offset: 4 -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 A waveform viewer and audio annotation editor.
|
Chris@0
|
5 Chris Cannam, Queen Mary University of London, 2005
|
Chris@0
|
6
|
Chris@0
|
7 This is experimental software. Not for distribution.
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 #include "TransformFactory.h"
|
Chris@0
|
11
|
Chris@0
|
12 #include "BeatDetectTransform.h"
|
Chris@0
|
13 #include "BeatDetectionFunctionTransform.h"
|
Chris@0
|
14 #include "FeatureExtractionPluginTransform.h"
|
Chris@0
|
15
|
Chris@0
|
16 #include "plugin/FeatureExtractionPluginFactory.h"
|
Chris@0
|
17
|
Chris@0
|
18 #include <iostream>
|
Chris@0
|
19
|
Chris@0
|
20 TransformFactory *
|
Chris@0
|
21 TransformFactory::m_instance = new TransformFactory;
|
Chris@0
|
22
|
Chris@0
|
23 TransformFactory *
|
Chris@0
|
24 TransformFactory::instance()
|
Chris@0
|
25 {
|
Chris@0
|
26 return m_instance;
|
Chris@0
|
27 }
|
Chris@0
|
28
|
Chris@0
|
29 TransformFactory::~TransformFactory()
|
Chris@0
|
30 {
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 TransformFactory::TransformList
|
Chris@0
|
34 TransformFactory::getAllTransforms()
|
Chris@0
|
35 {
|
Chris@0
|
36 TransformList list;
|
Chris@0
|
37 //!!! list.push_back(BeatDetectTransform::getName());
|
Chris@0
|
38 // list.push_back(BeatDetectionFunctionTransform::getName());
|
Chris@0
|
39
|
Chris@0
|
40 //!!!
|
Chris@0
|
41 std::vector<QString> fexplugs =
|
Chris@0
|
42 FeatureExtractionPluginFactory::getAllPluginIdentifiers();
|
Chris@0
|
43
|
Chris@0
|
44 for (size_t i = 0; i < fexplugs.size(); ++i) {
|
Chris@0
|
45
|
Chris@0
|
46 QString pluginId = fexplugs[i];
|
Chris@0
|
47
|
Chris@0
|
48 FeatureExtractionPluginFactory *factory =
|
Chris@0
|
49 FeatureExtractionPluginFactory::instanceFor(pluginId);
|
Chris@0
|
50
|
Chris@0
|
51 if (factory) {
|
Chris@0
|
52 //!!! well, really we want to be able to query this without having to instantiate
|
Chris@0
|
53
|
Chris@0
|
54 FeatureExtractionPlugin *plugin =
|
Chris@0
|
55 factory->instantiatePlugin(pluginId, 48000);
|
Chris@0
|
56
|
Chris@0
|
57 QString pluginDescription = plugin->getDescription().c_str();
|
Chris@0
|
58
|
Chris@0
|
59 if (plugin) {
|
Chris@0
|
60
|
Chris@0
|
61 FeatureExtractionPlugin::OutputList outputs =
|
Chris@0
|
62 plugin->getOutputDescriptors();
|
Chris@0
|
63
|
Chris@0
|
64 if (outputs.size() == 1) {
|
Chris@0
|
65 list.push_back
|
Chris@0
|
66 (TransformDesc
|
Chris@0
|
67 (QString("%1:%2").arg(pluginId).arg(outputs[0].name.c_str()),
|
Chris@0
|
68 pluginDescription));
|
Chris@0
|
69 } else {
|
Chris@0
|
70 for (size_t j = 0; j < outputs.size(); ++j) {
|
Chris@0
|
71 list.push_back
|
Chris@0
|
72 (TransformDesc
|
Chris@0
|
73 (QString("%1:%2").arg(pluginId).arg(outputs[j].name.c_str()),
|
Chris@0
|
74 QString("%1: %2").arg(pluginDescription)
|
Chris@0
|
75 .arg(outputs[j].description.c_str())));
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|
Chris@0
|
79 }
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 return list;
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 Transform *
|
Chris@0
|
86 TransformFactory::createTransform(TransformName name, Model *inputModel)
|
Chris@0
|
87 {
|
Chris@0
|
88 return createTransform(name, inputModel, true);
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 Transform *
|
Chris@0
|
92 TransformFactory::createTransform(TransformName name, Model *inputModel,
|
Chris@0
|
93 bool start)
|
Chris@0
|
94 {
|
Chris@0
|
95 Transform *transform = 0;
|
Chris@0
|
96
|
Chris@0
|
97 if (name == BeatDetectTransform::getName()) {
|
Chris@0
|
98 transform = new BeatDetectTransform(inputModel);
|
Chris@0
|
99 } else if (name == BeatDetectionFunctionTransform::getName()) {
|
Chris@0
|
100 transform = new BeatDetectionFunctionTransform(inputModel);
|
Chris@0
|
101 } else {
|
Chris@0
|
102 QString id = name.section(':', 0, 2);
|
Chris@0
|
103 QString output = name.section(':', 3);
|
Chris@0
|
104 if (FeatureExtractionPluginFactory::instanceFor(id)) {
|
Chris@0
|
105 transform = new FeatureExtractionPluginTransform(inputModel,
|
Chris@0
|
106 id, output);
|
Chris@0
|
107 } else {
|
Chris@0
|
108 std::cerr << "TransformFactory::createTransform: Unknown transform "
|
Chris@0
|
109 << name.toStdString() << std::endl;
|
Chris@0
|
110 }
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 if (start && transform) transform->start();
|
Chris@0
|
114 return transform;
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 Model *
|
Chris@0
|
118 TransformFactory::transform(TransformName name, Model *inputModel)
|
Chris@0
|
119 {
|
Chris@0
|
120 Transform *t = createTransform(name, inputModel, false);
|
Chris@0
|
121
|
Chris@0
|
122 if (!t) return 0;
|
Chris@0
|
123
|
Chris@0
|
124 connect(t, SIGNAL(finished()), this, SLOT(transformFinished()));
|
Chris@0
|
125
|
Chris@0
|
126 t->start();
|
Chris@0
|
127 return t->detachOutputModel();
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 void
|
Chris@0
|
131 TransformFactory::transformFinished()
|
Chris@0
|
132 {
|
Chris@0
|
133 QObject *s = sender();
|
Chris@0
|
134 Transform *transform = dynamic_cast<Transform *>(s);
|
Chris@0
|
135
|
Chris@0
|
136 if (!transform) {
|
Chris@0
|
137 std::cerr << "WARNING: TransformFactory::transformFinished: sender is not a transform" << std::endl;
|
Chris@0
|
138 return;
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 transform->wait(); // unnecessary but reassuring
|
Chris@0
|
142 delete transform;
|
Chris@0
|
143 }
|
Chris@0
|
144
|