first vaguely working version using PresentationInfo
[ardour.git] / libs / ardour / auditioner.cc
index 15906eb4a1c412706ec4fbf0a029ad537fb950ab..0c7ba0bc1c15b81c4c335ad2430df2a9d5c3485f 100644 (file)
 #include "pbd/error.h"
 
 #include "ardour/amp.h"
-#include "ardour/audioregion.h"
+#include "ardour/audio_diskstream.h"
+#include "ardour/audio_port.h"
 #include "ardour/audioengine.h"
 #include "ardour/audioplaylist.h"
+#include "ardour/audioregion.h"
 #include "ardour/auditioner.h"
-#include "ardour/audio_port.h"
 #include "ardour/data_type.h"
 #include "ardour/delivery.h"
+#include "ardour/midi_diskstream.h"
+#include "ardour/midi_region.h"
 #include "ardour/plugin.h"
+#include "ardour/plugin_insert.h"
+#include "ardour/profile.h"
 #include "ardour/region_factory.h"
 #include "ardour/route.h"
 #include "ardour/session.h"
@@ -41,7 +46,7 @@ using namespace PBD;
 #include "i18n.h"
 
 Auditioner::Auditioner (Session& s)
-       : Track (s, "auditioner", Route::Auditioner)
+       : Track (s, "auditioner", PresentationInfo::Auditioner)
        , current_frame (0)
        , _auditioning (0)
        , length (0)
@@ -51,7 +56,9 @@ Auditioner::Auditioner (Session& s)
        , via_monitor (false)
        , _midi_audition (false)
        , _synth_added (false)
+       , _synth_changed (false)
        , _queue_panic (false)
+       , _import_position (0)
 {
 }
 
@@ -66,11 +73,31 @@ Auditioner::init ()
                return -1;
        }
 
-       _output->add_port ("Midiaudition", this, DataType::MIDI);
+       _output->add_port ("", this, DataType::MIDI);
+
+       lookup_synth();
+
+       _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
+       Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Auditioner::config_changed, this, _1));
+
+       return 0;
+}
+
+Auditioner::~Auditioner ()
+{
+       if (asynth) {
+               asynth->drop_references ();
+       }
+       asynth.reset ();
+}
 
+void
+Auditioner::lookup_synth ()
+{
        string plugin_id = Config->get_midi_audition_synth_uri();
-       boost::shared_ptr<Plugin> p;
+       asynth.reset ();
        if (!plugin_id.empty()) {
+               boost::shared_ptr<Plugin> p;
                p = find_plugin (_session, plugin_id, ARDOUR::LV2);
                if (!p) {
                        p = find_plugin (_session, "https://community.ardour.org/node/7596", ARDOUR::LV2);
@@ -78,20 +105,21 @@ Auditioner::init ()
                                warning << _("Falling back to Reasonable Synth for Midi Audition") << endmsg;
                        } else {
                                warning << _("No synth for midi-audition found.") << endmsg;
+                               Config->set_midi_audition_synth_uri(""); // Don't check again for Reasonable Synth (ie --no-lv2)
                        }
                }
+               if (p) {
+                       asynth = boost::shared_ptr<Processor> (new PluginInsert (_session, p));
+               }
        }
-       if (p) {
-               asynth = boost::shared_ptr<Processor> (new PluginInsert (_session, p));
-       }
-
-       _output->changed.connect_same_thread (*this, boost::bind (&Auditioner::output_changed, this, _1, _2));
-
-       return 0;
 }
 
-Auditioner::~Auditioner ()
+void
+Auditioner::config_changed (std::string p)
 {
+       if (p == "midi-audition-synth-uri") {
+               _synth_changed = true;
+       }
 }
 
 int
@@ -141,20 +169,20 @@ Auditioner::connect ()
                        /* create (and connect) new ports */
 
                        _main_outs->defer_pan_reset ();
-                       
+
                        if (left.length()) {
                                _output->add_port (left, this, DataType::AUDIO);
                        }
-                       
+
                        if (right.length()) {
                                _output->add_port (right, this, DataType::AUDIO);
                        }
-                       
+
                        _main_outs->allow_pan_reset ();
                        _main_outs->reset_panner ();
 
                } else {
-                       
+
                        /* reconnect existing ports */
 
                        boost::shared_ptr<Port> oleft (_output->nth (0));
@@ -166,7 +194,7 @@ Auditioner::connect ()
                                oright->connect (right);
                        }
                }
-                       
+
        }
 
        return 0;
@@ -304,7 +332,11 @@ Auditioner::set_diskstream (boost::shared_ptr<Diskstream> ds)
        Track::set_diskstream (ds);
 
        _diskstream->set_track (this);
-       _diskstream->set_destructive (_mode == Destructive);
+       if (Profile->get_trx()) {
+               _diskstream->set_destructive (false);
+       } else {
+               _diskstream->set_destructive (_mode == Destructive);
+       }
        _diskstream->set_non_layered (_mode == NonLayered);
        _diskstream->set_record_enabled (false);
        _diskstream->request_input_monitoring (false);
@@ -353,6 +385,7 @@ Auditioner::audition_region (boost::shared_ptr<Region> region)
                        _synth_added = false;
                }
                midi_region.reset();
+               _import_position = 0;
 
                /* copy it */
                the_region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
@@ -382,19 +415,30 @@ Auditioner::audition_region (boost::shared_ptr<Region> region)
                _midi_audition = true;
                set_diskstream(_diskstream_midi);
                the_region.reset();
+               _import_position = region->position();
 
                /* copy it */
                midi_region = (boost::dynamic_pointer_cast<MidiRegion> (RegionFactory::create (region)));
-               midi_region->set_position (0);
+               midi_region->set_position (_import_position);
 
                _diskstream->playlist()->drop_regions ();
-               _diskstream->playlist()->add_region (midi_region, 0, 1);
+               _diskstream->playlist()->add_region (midi_region, _import_position, 1);
                midi_diskstream()->reset_tracker();
 
                ProcessorStreams ps;
 
+               if (_synth_changed && _synth_added) {
+                       remove_processor(asynth);
+                       _synth_added = false;
+               }
+               if (_synth_changed && !_synth_added) {
+                       _synth_added = false;
+                       lookup_synth();
+               }
+
+
                if (!_synth_added && asynth) {
-                       int rv = add_processor_by_index(asynth, PreFader, &ps, true);
+                       int rv = add_processor (asynth, PreFader, &ps, true);
                        if (rv) {
                                error << _("Failed to load synth for MIDI-Audition.") << endmsg;
                        } else {
@@ -430,7 +474,7 @@ Auditioner::audition_region (boost::shared_ptr<Region> region)
 
        if (_midi_audition) {
                length = midi_region->length();
-               offset = midi_region->sync_offset (dir);
+               offset = _import_position + midi_region->sync_offset (dir);
        } else {
                length = the_region->length();
                offset = the_region->sync_offset (dir);
@@ -442,7 +486,7 @@ Auditioner::audition_region (boost::shared_ptr<Region> region)
                offset = 0;
        }
 
-       _diskstream->seek (offset);
+       _diskstream->seek (offset, true);
        current_frame = offset;
 
        g_atomic_int_set (&_auditioning, 1);
@@ -472,12 +516,14 @@ Auditioner::play_audition (framecnt_t nframes)
                _seek_complete = false;
                _seeking = false;
                _seek_frame = -1;
-               midi_diskstream()->reset_tracker();
+               if (_midi_audition && midi_diskstream()) {
+                       midi_diskstream()->reset_tracker();
+               }
        }
 
        if(!_seeking) {
                /* process audio */
-               this_nframes = min (nframes, length - current_frame);
+               this_nframes = min (nframes, length - current_frame + _import_position);
 
                if ((ret = roll (this_nframes, current_frame, current_frame + nframes, false, need_butler)) != 0) {
                        silence (nframes);
@@ -498,10 +544,10 @@ Auditioner::play_audition (framecnt_t nframes)
        }
 
        if (!_seeking) {
-               AuditionProgress(current_frame, length); /* emit */
+               AuditionProgress(current_frame - _import_position, length); /* emit */
        }
 
-       if (current_frame >= length) {
+       if (current_frame >= length + _import_position) {
                _session.cancel_audition ();
                return 0;
        } else {
@@ -570,3 +616,15 @@ Auditioner::monitoring_state () const
 {
        return MonitoringDisk;
 }
+
+boost::shared_ptr<AudioDiskstream>
+Auditioner::audio_diskstream() const
+{
+       return boost::dynamic_pointer_cast<AudioDiskstream> (_diskstream);
+}
+
+boost::shared_ptr<MidiDiskstream>
+Auditioner::midi_diskstream() const
+{
+       return boost::dynamic_pointer_cast<MidiDiskstream> (_diskstream);
+}