diff transform/RealTimeEffectModelTransformer.cpp @ 1740:fe3f7f8df3a3 by-id

More work on transformers
author Chris Cannam
date Wed, 26 Jun 2019 17:25:20 +0100
parents 7a56bb85030f
children 6d09d68165a4
line wrap: on
line diff
--- a/transform/RealTimeEffectModelTransformer.cpp	Wed Jun 26 14:59:09 2019 +0100
+++ b/transform/RealTimeEffectModelTransformer.cpp	Wed Jun 26 17:25:20 2019 +0100
@@ -58,8 +58,11 @@
         return;
     }
 
-    DenseTimeValueModel *input = getConformingInput();
-    if (!input) return;
+    auto input = ModelById::getAs<DenseTimeValueModel>(getInputModel());
+    if (!input) {
+        SVCERR << "RealTimeEffectModelTransformer: Input is absent or of wrong type" << endl;
+        return;
+    }
 
     m_plugin = factory->instantiatePlugin(pluginId, 0, 0,
                                           input->getSampleRate(),
@@ -87,19 +90,21 @@
             outputChannels = input->getChannelCount();
         }
 
-        WritableWaveFileModel *model = new WritableWaveFileModel
+        auto model = std::make_shared<WritableWaveFileModel>
             (input->getSampleRate(), outputChannels);
 
-        m_outputs.push_back(model);
+        ModelById::add(model);
+        m_outputs.push_back(model->getId());
 
     } else {
         
-        SparseTimeValueModel *model = new SparseTimeValueModel
-            (input->getSampleRate(), transform.getBlockSize(), 0.0, 0.0, false);
-
+        auto model = std::make_shared<SparseTimeValueModel>
+            (input->getSampleRate(), transform.getBlockSize(),
+             0.0, 0.0, false);
         if (m_units != "") model->setScaleUnits(m_units);
 
-        m_outputs.push_back(model);
+        ModelById::add(model);
+        m_outputs.push_back(model->getId());
     }
 }
 
@@ -108,38 +113,39 @@
     delete m_plugin;
 }
 
-DenseTimeValueModel *
-RealTimeEffectModelTransformer::getConformingInput()
-{
-    DenseTimeValueModel *dtvm =
-        dynamic_cast<DenseTimeValueModel *>(getInputModel());
-    if (!dtvm) {
-        SVDEBUG << "RealTimeEffectModelTransformer::getConformingInput: WARNING: Input model is not conformable to DenseTimeValueModel" << endl;
-    }
-    return dtvm;
-}
-
 void
 RealTimeEffectModelTransformer::run()
 {
-    DenseTimeValueModel *input = getConformingInput();
-    if (!input) return;
-
-    while (!input->isReady() && !m_abandoned) {
-        SVDEBUG << "RealTimeEffectModelTransformer::run: Waiting for input model to be ready..." << endl;
-        usleep(500000);
-    }
-    if (m_abandoned) {
+    if (m_outputs.empty()) {
+        abandon();
         return;
     }
-    if (m_outputs.empty()) {
+
+    bool ready = false;
+    while (!ready && !m_abandoned) {
+        { // scope so as to release input shared_ptr before sleeping
+            auto input = ModelById::getAs<DenseTimeValueModel>(getInputModel());
+            if (!input) {
+                abandon();
+                return;
+            }
+            ready = input->isReady();
+        }
+        if (!ready) {
+            SVDEBUG << "RealTimeEffectModelTransformer::run: Waiting for input model to be ready..." << endl;
+            usleep(500000);
+        }
+    }
+    if (m_abandoned) return;
+
+    auto input = ModelById::getAs<DenseTimeValueModel>(getInputModel());
+    if (!input) {
+        abandon();
         return;
     }
-    
-    SparseTimeValueModel *stvm =
-        dynamic_cast<SparseTimeValueModel *>(m_outputs[0]);
-    WritableWaveFileModel *wwfm =
-        dynamic_cast<WritableWaveFileModel *>(m_outputs[0]);
+
+    auto stvm = ModelById::getAs<SparseTimeValueModel>(m_outputs[0]);
+    auto wwfm = ModelById::getAs<WritableWaveFileModel>(m_outputs[0]);
 
     if (!stvm && !wwfm) {
         return;
@@ -157,8 +163,8 @@
 
     float **inbufs = m_plugin->getAudioInputBuffers();
 
-    sv_frame_t startFrame = m_input.getModel()->getStartFrame();
-    sv_frame_t endFrame = m_input.getModel()->getEndFrame();
+    sv_frame_t startFrame = input->getStartFrame();
+    sv_frame_t endFrame = input->getEndFrame();
 
     Transform transform = m_transforms[0];
     
@@ -242,20 +248,6 @@
             }
         }
 
-/*
-        cerr << "Input for plugin: " << m_plugin->getAudioInputCount() << " channels "<< endl;
-
-        for (int ch = 0; ch < m_plugin->getAudioInputCount(); ++ch) {
-            cerr << "Input channel " << ch << endl;
-            for (int i = 0; i < 100; ++i) {
-                cerr << inbufs[ch][i] << " ";
-                if (isnan(inbufs[ch][i])) {
-                    cerr << "\n\nWARNING: NaN in audio input" << endl;
-                }
-            }
-        }
-*/
-
         m_plugin->run(RealTime::frame2RealTime(blockFrame, sampleRate));
 
         if (stvm) {