comparison transform/FeatureExtractionModelTransformer.cpp @ 1211:5a1198083d9a piper

Pull out model creation into the transformer thread run(), so that all communications with the plugin server happen on a single thread. Then make the model accessor wait for them to be created (which still happens right at the start of processing) before returning.
author Chris Cannam
date Mon, 17 Oct 2016 14:18:23 +0100
parents aa588c391d1a
children ba16388b937d
comparison
equal deleted inserted replaced
1210:584b2d7d7cd9 1211:5a1198083d9a
40 #include <QSettings> 40 #include <QSettings>
41 41
42 FeatureExtractionModelTransformer::FeatureExtractionModelTransformer(Input in, 42 FeatureExtractionModelTransformer::FeatureExtractionModelTransformer(Input in,
43 const Transform &transform) : 43 const Transform &transform) :
44 ModelTransformer(in, transform), 44 ModelTransformer(in, transform),
45 m_plugin(0) 45 m_plugin(0),
46 m_haveOutputs(false)
46 { 47 {
47 SVDEBUG << "FeatureExtractionModelTransformer::FeatureExtractionModelTransformer: plugin " << m_transforms.begin()->getPluginIdentifier() << ", outputName " << m_transforms.begin()->getOutput() << endl; 48 SVDEBUG << "FeatureExtractionModelTransformer::FeatureExtractionModelTransformer: plugin " << m_transforms.begin()->getPluginIdentifier() << ", outputName " << m_transforms.begin()->getOutput() << endl;
48 49
49 initialise(); 50 // initialise();
50 } 51 }
51 52
52 FeatureExtractionModelTransformer::FeatureExtractionModelTransformer(Input in, 53 FeatureExtractionModelTransformer::FeatureExtractionModelTransformer(Input in,
53 const Transforms &transforms) : 54 const Transforms &transforms) :
54 ModelTransformer(in, transforms), 55 ModelTransformer(in, transforms),
55 m_plugin(0) 56 m_plugin(0),
57 m_haveOutputs(false)
56 { 58 {
57 if (m_transforms.empty()) { 59 if (m_transforms.empty()) {
58 SVDEBUG << "FeatureExtractionModelTransformer::FeatureExtractionModelTransformer: " << transforms.size() << " transform(s)" << endl; 60 SVDEBUG << "FeatureExtractionModelTransformer::FeatureExtractionModelTransformer: " << transforms.size() << " transform(s)" << endl;
59 } else { 61 } else {
60 SVDEBUG << "FeatureExtractionModelTransformer::FeatureExtractionModelTransformer: " << transforms.size() << " transform(s), first has plugin " << m_transforms.begin()->getPluginIdentifier() << ", outputName " << m_transforms.begin()->getOutput() << endl; 62 SVDEBUG << "FeatureExtractionModelTransformer::FeatureExtractionModelTransformer: " << transforms.size() << " transform(s), first has plugin " << m_transforms.begin()->getPluginIdentifier() << ", outputName " << m_transforms.begin()->getOutput() << endl;
61 } 63 }
62 64
63 initialise(); 65 // initialise();
64 } 66 }
65 67
66 static bool 68 static bool
67 areTransformsSimilar(const Transform &t1, const Transform &t2) 69 areTransformsSimilar(const Transform &t1, const Transform &t2)
68 { 70 {
102 if (!input) { 104 if (!input) {
103 m_message = tr("Input model for feature extraction plugin \"%1\" is of wrong type (internal error?)").arg(pluginId); 105 m_message = tr("Input model for feature extraction plugin \"%1\" is of wrong type (internal error?)").arg(pluginId);
104 return false; 106 return false;
105 } 107 }
106 108
109 cerr << "instantiating plugin for transform in thread "
110 << QThread::currentThreadId() << endl;
111
107 m_plugin = factory->instantiatePlugin(pluginId, input->getSampleRate()); 112 m_plugin = factory->instantiatePlugin(pluginId, input->getSampleRate());
108 if (!m_plugin) { 113 if (!m_plugin) {
109 m_message = tr("Failed to instantiate plugin \"%1\"").arg(pluginId); 114 m_message = tr("Failed to instantiate plugin \"%1\"").arg(pluginId);
110 return false; 115 return false;
111 } 116 }
217 } 222 }
218 223
219 for (int j = 0; j < (int)m_transforms.size(); ++j) { 224 for (int j = 0; j < (int)m_transforms.size(); ++j) {
220 createOutputModels(j); 225 createOutputModels(j);
221 } 226 }
227
228 m_outputMutex.lock();
229 m_haveOutputs = true;
230 m_outputsCondition.wakeAll();
231 m_outputMutex.unlock();
222 232
223 return true; 233 return true;
224 } 234 }
225 235
226 void 236 void
477 out->setSourceModel(input); 487 out->setSourceModel(input);
478 m_outputs.push_back(out); 488 m_outputs.push_back(out);
479 } 489 }
480 } 490 }
481 491
492 void
493 FeatureExtractionModelTransformer::awaitOutputModels()
494 {
495 m_outputMutex.lock();
496 while (!m_haveOutputs) {
497 m_outputsCondition.wait(&m_outputMutex);
498 }
499 m_outputMutex.unlock();
500 }
501
482 FeatureExtractionModelTransformer::~FeatureExtractionModelTransformer() 502 FeatureExtractionModelTransformer::~FeatureExtractionModelTransformer()
483 { 503 {
484 // SVDEBUG << "FeatureExtractionModelTransformer::~FeatureExtractionModelTransformer()" << endl; 504 // SVDEBUG << "FeatureExtractionModelTransformer::~FeatureExtractionModelTransformer()" << endl;
485 delete m_plugin; 505 delete m_plugin;
486 for (int j = 0; j < (int)m_descriptors.size(); ++j) { 506 for (int j = 0; j < (int)m_descriptors.size(); ++j) {
564 } 584 }
565 585
566 void 586 void
567 FeatureExtractionModelTransformer::run() 587 FeatureExtractionModelTransformer::run()
568 { 588 {
589 initialise();
590
569 DenseTimeValueModel *input = getConformingInput(); 591 DenseTimeValueModel *input = getConformingInput();
570 if (!input) return; 592 if (!input) return;
571 593
572 if (m_outputs.empty()) return; 594 if (m_outputs.empty()) return;
573 595
707 getFrames(channelCount, blockFrame, blockSize, buffers); 729 getFrames(channelCount, blockFrame, blockSize, buffers);
708 } 730 }
709 731
710 if (m_abandoned) break; 732 if (m_abandoned) break;
711 733
734 cerr << "calling process() from thread "
735 << QThread::currentThreadId() << endl;
736
712 Vamp::Plugin::FeatureSet features = m_plugin->process 737 Vamp::Plugin::FeatureSet features = m_plugin->process
713 (buffers, RealTime::frame2RealTime(blockFrame, sampleRate).toVampRealTime()); 738 (buffers, RealTime::frame2RealTime(blockFrame, sampleRate).toVampRealTime());
714 739
715 if (m_abandoned) break; 740 if (m_abandoned) break;
716 741