ignore negative value locates and MMC locate commands
[ardour.git] / libs / ardour / midi_scene_changer.cc
index 49e835ca80e8eaafd94966fd91dd39a3312f756a..88a9e8db3577e79dfb4ae07b1d9a2f9599d60f87 100644 (file)
 */
 
 #include "evoral/MIDIEvent.hpp"
+#include "midi++/channel.h"
 #include "midi++/parser.h"
 #include "midi++/port.h"
 
+#include "ardour/async_midi_port.h"
 #include "ardour/event_type_map.h"
 #include "ardour/midi_port.h"
 #include "ardour/midi_scene_change.h"
@@ -64,6 +66,8 @@ MIDISceneChanger::gather ()
        const Locations::LocationList& locations (_session.locations()->list());
        boost::shared_ptr<SceneChange> sc;
 
+       Glib::Threads::RWLock::WriterLock lm (scene_lock);
+
        scenes.clear ();
 
        for (Locations::LocationList::const_iterator l = locations.begin(); l != locations.end(); ++l) {
@@ -71,16 +75,16 @@ MIDISceneChanger::gather ()
                if ((sc = (*l)->scene_change()) != 0) {
 
                        boost::shared_ptr<MIDISceneChange> msc = boost::dynamic_pointer_cast<MIDISceneChange> (sc);
-
+                       
                        if (msc) {
-                               scenes.insert (std::make_pair (msc->time(), msc));
+                               scenes.insert (std::make_pair ((*l)->start(), msc));
                        }
                }
        }
 }
 
 void
-MIDISceneChanger::deliver (MidiBuffer& mbuf, framepos_t when, boost::shared_ptr<MIDISceneChange> msc)
+MIDISceneChanger::rt_deliver (MidiBuffer& mbuf, framepos_t when, boost::shared_ptr<MIDISceneChange> msc)
 {
        uint8_t buf[4];
        size_t cnt;
@@ -101,12 +105,45 @@ MIDISceneChanger::deliver (MidiBuffer& mbuf, framepos_t when, boost::shared_ptr<
                last_delivered_program = msc->program();
        }
 }
-                       
+
+void
+MIDISceneChanger::non_rt_deliver (boost::shared_ptr<MIDISceneChange> msc)
+{
+       uint8_t buf[4];
+       size_t cnt;
+       boost::shared_ptr<AsyncMIDIPort> aport = boost::dynamic_pointer_cast<AsyncMIDIPort>(output_port);
+
+       /* We use zero as the timestamp for these messages because we are in a
+          non-RT/process context. Using zero means "deliver them as early as
+          possible" (practically speaking, in the next process callback).
+       */
+
+       if ((cnt = msc->get_bank_msb_message (buf, sizeof (buf))) > 0) {
+               aport->write (buf, cnt, 0);
+
+               if ((cnt = msc->get_bank_lsb_message (buf, sizeof (buf))) > 0) {
+                       aport->write (buf, cnt, 0);
+               }
+
+               last_delivered_bank = msc->bank();
+       }
+
+       if ((cnt = msc->get_program_message (buf, sizeof (buf))) > 0) {
+               aport->write (buf, cnt, 0);
+               last_delivered_program = msc->program();
+       }
+}
 
 void
 MIDISceneChanger::run (framepos_t start, framepos_t end)
 {
-       if (!output_port || recording()) {
+       if (!output_port || recording() || !_session.transport_rolling()) {
+               return;
+       }
+
+       Glib::Threads::RWLock::ReaderLock lm (scene_lock, Glib::Threads::TRY_LOCK);
+       
+       if (!lm.locked()) {
                return;
        }
 
@@ -120,9 +157,9 @@ MIDISceneChanger::run (framepos_t start, framepos_t end)
                if (i->first >= end) {
                        break;
                }
+       
+               rt_deliver (mbuf, i->first - start, i->second);
                
-               deliver (mbuf, i->first - start, i->second);
-
                ++i;
        }
 }
@@ -130,15 +167,39 @@ MIDISceneChanger::run (framepos_t start, framepos_t end)
 void
 MIDISceneChanger::locate (framepos_t pos)
 {
-       Scenes::const_iterator i = scenes.upper_bound (pos);
+       boost::shared_ptr<MIDISceneChange> msc;
 
-       if (i == scenes.end()) {
-               return;
+       {
+               Glib::Threads::RWLock::ReaderLock lm (scene_lock);
+
+               if (scenes.empty()) {
+                       return;
+               }
+               
+               Scenes::const_iterator i = scenes.lower_bound (pos);
+               
+               if (i != scenes.end()) {
+
+                       if (i->first != pos) {
+                               /* i points to first scene with position > pos, so back
+                                * up, if possible.
+                                */
+                               if (i != scenes.begin()) {
+                                       --i;
+                               } else {
+                                       return;
+                               }
+                       }
+               } else {
+                       /* go back to the final scene and use it */
+                       --i;
+               }
+
+               msc = i->second;
        }
 
-       if (i->second->program() != last_delivered_program || i->second->bank() != last_delivered_bank) {
-               // MidiBuffer& mbuf (output_port->get_midi_buffer (end-start));
-               // deliver (mbuf, i->first, i->second);
+       if (msc->program() != last_delivered_program || msc->bank() != last_delivered_bank) {
+               non_rt_deliver (msc);
        }
 }              
 
@@ -147,8 +208,7 @@ MIDISceneChanger::set_input_port (MIDI::Port* mp)
 {
        input_port = mp;
 
-       incoming_bank_change_connection.disconnect ();
-       incoming_program_change_connection.disconnect ();
+       incoming_connections.drop_connections();
        
        if (input_port) {
                
@@ -157,8 +217,10 @@ MIDISceneChanger::set_input_port (MIDI::Port* mp)
                 * and thus invoke our callbacks as necessary.
                 */
 
-               input_port->parser()->bank_change.connect_same_thread (incoming_bank_change_connection, boost::bind (&MIDISceneChanger::bank_change_input, this, _1, _2));
-               input_port->parser()->program_change.connect_same_thread (incoming_program_change_connection, boost::bind (&MIDISceneChanger::program_change_input, this, _1, _2));
+               for (int channel = 0; channel < 16; ++channel) {
+                       input_port->parser()->channel_bank_change[channel].connect_same_thread (incoming_connections, boost::bind (&MIDISceneChanger::bank_change_input, this, _1, _2, channel));
+                       input_port->parser()->channel_program_change[channel].connect_same_thread (incoming_connections, boost::bind (&MIDISceneChanger::program_change_input, this, _1, _2, channel));
+               }
        }
 }
 
@@ -181,26 +243,24 @@ MIDISceneChanger::recording() const
 }
 
 void
-MIDISceneChanger::bank_change_input (MIDI::Parser& parser, unsigned short bank)
+MIDISceneChanger::bank_change_input (MIDI::Parser& parser, unsigned short, int)
 {
        if (!recording()) {
                return;
        }
 
        last_bank_message_time = parser.get_timestamp ();
-       current_bank = bank;
 }
 
 void
-MIDISceneChanger::program_change_input (MIDI::Parser& parser, MIDI::byte program)
+MIDISceneChanger::program_change_input (MIDI::Parser& parser, MIDI::byte program, int channel)
 {
        framecnt_t time = parser.get_timestamp ();
-       frameoffset_t delta = time - last_program_message_time;
 
        last_program_message_time = time;
 
        if (!recording()) {
-               jump_to (current_bank, program);
+               jump_to (input_port->channel (channel)->bank(), program);
                return;
        }
 
@@ -227,21 +287,9 @@ MIDISceneChanger::program_change_input (MIDI::Parser& parser, MIDI::byte program
                new_mark = true;
        }
 
-       uint8_t bank;
-       uint8_t channel = (program & 0xf0) >> 8;
-
-       /* if we received a bank change message within the last 2 msec, use the
-        * current bank value, otherwise lookup the current bank number and use
-        * that.
-        */
-
-       if (time - last_bank_message_time < (2 * _session.frame_rate() / 1000.0)) {
-               bank = current_bank;
-       } else {
-               bank = -1;
-       }
+       unsigned short bank = input_port->channel (channel)->bank();
 
-       MIDISceneChange* msc =new MIDISceneChange (loc->start(), channel, bank, program & 0x7f);
+       MIDISceneChange* msc =new MIDISceneChange (channel, bank, program & 0x7f);
 
        loc->set_scene_change (boost::shared_ptr<MIDISceneChange> (msc));