Merge branch 'master' into saveas
[ardour.git] / libs / ardour / auditioner.cc
index a0789b3034e98a7f1feeea77f6e128107b463250..30a354aa95210da6db4f9cc6be783d7b1f2d4c1d 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/region_factory.h"
 #include "ardour/route.h"
 #include "ardour/session.h"
@@ -51,6 +55,9 @@ Auditioner::Auditioner (Session& s)
        , via_monitor (false)
        , _midi_audition (false)
        , _synth_added (false)
+       , _synth_changed (false)
+       , _queue_panic (false)
+       , _import_position (0)
 {
 }
 
@@ -65,12 +72,12 @@ Auditioner::init ()
                return -1;
        }
 
-       _output->add_port ("Midiaudition", this, DataType::MIDI);
-       boost::shared_ptr<Plugin> p = find_plugin (_session, "https://community.ardour.org/node/7596", ARDOUR::LV2);
-       assert(p);
-       asynth = boost::shared_ptr<Processor> (new PluginInsert (_session, p));
+       _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;
 }
@@ -79,6 +86,37 @@ Auditioner::~Auditioner ()
 {
 }
 
+void
+Auditioner::lookup_synth ()
+{
+       string plugin_id = Config->get_midi_audition_synth_uri();
+       asynth = boost::shared_ptr<Processor>();
+       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);
+                       if (p) {
+                               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));
+               }
+       }
+}
+
+void
+Auditioner::config_changed (std::string p)
+{
+       if (p == "midi-audition-synth-uri") {
+               _synth_changed = true;
+       }
+}
+
 int
 Auditioner::connect ()
 {
@@ -209,15 +247,35 @@ Auditioner::roll_midi (pframes_t nframes, framepos_t start_frame, framepos_t end
        framecnt_t playback_distance = nframes;
        boost::shared_ptr<MidiDiskstream> diskstream = midi_diskstream();
        BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
-
-       _silent = false;
        MidiBuffer& mbuf (bufs.get_midi (0));
-       diskstream->get_playback (mbuf, nframes);
+       _silent = false;
 
        ChanCount cnt (DataType::MIDI, 1);
        cnt.set (DataType::AUDIO, bufs.count().n_audio());
        bufs.set_count (cnt);
 
+       if (_queue_panic) {
+               _queue_panic = false;
+               for (uint8_t chn = 0; chn < 0xf; ++chn) {
+                       uint8_t buf[3] = { ((uint8_t) (MIDI_CMD_CONTROL | chn)), ((uint8_t) MIDI_CTL_SUSTAIN), 0 };
+                       mbuf.push_back(0, 3, buf);
+                       buf[1] = MIDI_CTL_ALL_NOTES_OFF;
+                       mbuf.push_back(0, 3, buf);
+                       buf[1] = MIDI_CTL_RESET_CONTROLLERS;
+                       mbuf.push_back(0, 3, buf);
+               }
+               process_output_buffers (bufs, start_frame, start_frame+1, 1, false, false);
+
+               for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
+                       boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
+                       if (d) {
+                               d->flush_buffers (nframes);
+                       }
+               }
+       }
+
+       diskstream->get_playback (mbuf, nframes);
+
        process_output_buffers (bufs, start_frame, end_frame, nframes,
                                declick, (!diskstream->record_enabled() && !_session.transport_stopped()));
 
@@ -318,6 +376,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));
@@ -347,27 +406,37 @@ 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_added) {
-                       int rv = add_processor_by_index(asynth, PreFader, &ps, true);
+               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 (asynth, PreFader, &ps, true);
                        if (rv) {
                                error << _("Failed to load synth for MIDI-Audition.") << endmsg;
                        } else {
                                _synth_added = true;
                        }
                } else {
-                       // TODO -- queue midi-panic // reset synth -> all notes off
-                       //  here or in first cycle..
+                       _queue_panic = true;
                }
 
                {
@@ -396,7 +465,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);
@@ -438,12 +507,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);
@@ -457,17 +528,17 @@ Auditioner::play_audition (framecnt_t nframes)
        }
 
        if (_seek_frame >= 0 && _seek_frame < length && !_seeking) {
+               _queue_panic = true;
                _seek_complete = false;
                _seeking = true;
                need_butler = true;
-               // TODO -- send midi-panic. -> all notes off
        }
 
        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 {
@@ -531,8 +602,20 @@ Auditioner::input_streams () const
        return ChanCount ();
 }
 
-MonitorState 
+MonitorState
 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);
+}