comparison transform/TransformFactory.cpp @ 0:da6937383da8

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