fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / midi_stretch.cc
index f33c58a0fd2f6c5e1dea6599c967af2aa4e7b954..70f44e19c1561c727ac465353565241f326302d1 100644 (file)
@@ -1,6 +1,6 @@
 /*
-    Copyright (C) 2008 Paul Davis 
-    Author:  Dave Robillard
+    Copyright (C) 2008 Paul Davis
+    Author: David Robillard
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 
 */
 
-#include <pbd/error.h>
+#include "pbd/error.h"
 
-#include <ardour/types.h>
-#include <ardour/midi_stretch.h>
-#include <ardour/session.h>
-#include <ardour/midi_region.h>
+#include "ardour/midi_model.h"
+#include "ardour/midi_region.h"
+#include "ardour/midi_source.h"
+#include "ardour/midi_stretch.h"
+#include "ardour/types.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
-MidiStretch::MidiStretch (Session& s, TimeFXRequest& req)
+MidiStretch::MidiStretch (Session& s, const TimeFXRequest& req)
        : Filter (s)
        , _request (req)
 {
@@ -42,21 +43,22 @@ MidiStretch::~MidiStretch ()
 }
 
 int
-MidiStretch::run (boost::shared_ptr<Region> r)
+MidiStretch::run (boost::shared_ptr<Region> r, Progress*)
 {
        SourceList nsrcs;
        char suffix[32];
 
        boost::shared_ptr<MidiRegion> region = boost::dynamic_pointer_cast<MidiRegion>(r);
-       if (!region)
+       if (!region) {
                return -1;
+       }
 
        /* the name doesn't need to be super-precise, but allow for 2 fractional
           digits just to disambiguate close but not identical stretches.
        */
-       
+
        snprintf (suffix, sizeof (suffix), "@%d", (int) floor (_request.time_fraction * 100.0f));
-       
+
        string new_name = region->name();
        string::size_type at = new_name.find ('@');
 
@@ -67,41 +69,53 @@ MidiStretch::run (boost::shared_ptr<Region> r)
        }
 
        new_name += suffix;
-       
+
        /* create new sources */
-       
+
        if (make_new_sources (region, nsrcs, suffix))
                return -1;
 
-       // FIXME: how to make a whole file region if it isn't?
-       //assert(region->whole_file());
-       
        boost::shared_ptr<MidiSource> src = region->midi_source(0);
-       src->load_model();
+       {
+               Source::Lock lock(src->mutex());
+               src->load_model(lock);
+       }
 
        boost::shared_ptr<MidiModel> old_model = src->model();
 
        boost::shared_ptr<MidiSource> new_src = boost::dynamic_pointer_cast<MidiSource>(nsrcs[0]);
-       assert(new_src);
+       if (!new_src) {
+               error << _("MIDI stretch created non-MIDI source") << endmsg;
+               return -1;
+       }
+
+       Glib::Threads::Mutex::Lock sl (new_src->mutex ());
 
+       new_src->load_model(sl, true);
        boost::shared_ptr<MidiModel> new_model = new_src->model();
        new_model->start_write();
-       
-       for (MidiModel::const_iterator i = old_model->begin(); i != old_model->end(); ++i) {
-               const double new_time = i->time() * _request.time_fraction;
-               
+
+       /* Note: pass true into force_discrete for the begin() iterator so that the model doesn't
+        * do interpolation of controller data when we stretch.
+        */
+       for (Evoral::Sequence<MidiModel::TimeType>::const_iterator i = old_model->begin (MidiModel::TimeType(), true);
+                       i != old_model->end(); ++i) {
+               const MidiModel::TimeType new_time = i->time() * (double)_request.time_fraction;
+
                // FIXME: double copy
-               MIDI::Event ev = MIDI::Event(*i, true);
-               ev.time() = new_time;
-               new_model->append(ev);
+               Evoral::Event<MidiModel::TimeType> ev(*i, true);
+               ev.set_time(new_time);
+               new_model->append(ev, Evoral::next_event_id());
        }
 
-       new_model->end_write();
-       new_model->set_edited(true);
-       
+       new_model->end_write (Evoral::Sequence<Evoral::Beats>::DeleteStuckNotes);
+       new_model->set_edited (true);
+
+       new_src->copy_interpolation_from (src);
+
        const int ret = finish (region, nsrcs, new_name);
-       
-       results[0]->set_length((nframes_t) floor (r->length() * _request.time_fraction), NULL);
+       /* non-musical */
+       results[0]->set_length((framecnt_t) floor (r->length() * _request.time_fraction), 0);
 
        return ret;
 }