2 Copyright (C) 2014 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "evoral/MIDIEvent.hpp"
21 #include "midi++/channel.h"
22 #include "midi++/parser.h"
23 #include "midi++/port.h"
25 #include "ardour/async_midi_port.h"
26 #include "ardour/event_type_map.h"
27 #include "ardour/midi_buffer.h"
28 #include "ardour/midi_port.h"
29 #include "ardour/midi_scene_change.h"
30 #include "ardour/midi_scene_changer.h"
31 #include "ardour/session.h"
35 using namespace ARDOUR;
37 MIDISceneChanger::MIDISceneChanger (Session& s)
40 , have_seen_bank_changes (false)
41 , last_program_message_time (-1)
42 , last_delivered_program (-1)
43 , last_delivered_bank (-1)
46 /* catch any add/remove/clear etc. for all Locations */
47 _session.locations()->changed.connect_same_thread (*this, boost::bind (&MIDISceneChanger::locations_changed, this));
48 _session.locations()->added.connect_same_thread (*this, boost::bind (&MIDISceneChanger::locations_changed, this));
49 _session.locations()->removed.connect_same_thread (*this, boost::bind (&MIDISceneChanger::locations_changed, this));
51 /* catch class-based signal that notifies of us changes in the scene change state of any Location */
52 Location::scene_changed.connect_same_thread (*this, boost::bind (&MIDISceneChanger::locations_changed, this));
55 MIDISceneChanger::~MIDISceneChanger ()
60 MIDISceneChanger::locations_changed ()
62 _session.locations()->apply (*this, &MIDISceneChanger::gather);
65 /** Use the session's list of locations to collect all patch changes.
67 * This is called whenever the locations change in anyway.
70 MIDISceneChanger::gather (const Locations::LocationList& locations)
72 boost::shared_ptr<SceneChange> sc;
74 Glib::Threads::RWLock::WriterLock lm (scene_lock);
78 for (Locations::LocationList::const_iterator l = locations.begin(); l != locations.end(); ++l) {
80 if ((sc = (*l)->scene_change()) != 0) {
82 boost::shared_ptr<MIDISceneChange> msc = boost::dynamic_pointer_cast<MIDISceneChange> (sc);
86 if (msc->bank() >= 0) {
87 have_seen_bank_changes = true;
90 scenes.insert (std::make_pair ((*l)->start(), msc));
97 MIDISceneChanger::rt_deliver (MidiBuffer& mbuf, framepos_t when, boost::shared_ptr<MIDISceneChange> msc)
102 MIDIOutputActivity (); /* EMIT SIGNAL */
104 if ((cnt = msc->get_bank_msb_message (buf, sizeof (buf))) > 0) {
105 mbuf.push_back (when, cnt, buf);
107 if ((cnt = msc->get_bank_lsb_message (buf, sizeof (buf))) > 0) {
108 mbuf.push_back (when, cnt, buf);
111 last_delivered_bank = msc->bank();
114 if ((cnt = msc->get_program_message (buf, sizeof (buf))) > 0) {
115 mbuf.push_back (when, cnt, buf);
117 last_delivered_program = msc->program();
122 MIDISceneChanger::non_rt_deliver (boost::shared_ptr<MIDISceneChange> msc)
126 boost::shared_ptr<AsyncMIDIPort> aport = boost::dynamic_pointer_cast<AsyncMIDIPort>(output_port);
128 /* We use zero as the timestamp for these messages because we are in a
129 non-RT/process context. Using zero means "deliver them as early as
130 possible" (practically speaking, in the next process callback).
133 MIDIOutputActivity (); /* EMIT SIGNAL */
135 if ((cnt = msc->get_bank_msb_message (buf, sizeof (buf))) > 0) {
136 aport->write (buf, cnt, 0);
138 if ((cnt = msc->get_bank_lsb_message (buf, sizeof (buf))) > 0) {
139 aport->write (buf, cnt, 0);
142 last_delivered_bank = msc->bank();
145 if ((cnt = msc->get_program_message (buf, sizeof (buf))) > 0) {
146 aport->write (buf, cnt, 0);
147 last_delivered_program = msc->program();
152 MIDISceneChanger::run (framepos_t start, framepos_t end)
154 if (!output_port || recording() || !_session.transport_rolling()) {
158 Glib::Threads::RWLock::ReaderLock lm (scene_lock, Glib::Threads::TRY_LOCK);
164 /* get lower bound of events to consider */
166 Scenes::const_iterator i = scenes.lower_bound (start);
167 MidiBuffer& mbuf (output_port->get_midi_buffer (end-start));
169 while (i != scenes.end()) {
171 if (i->first >= end) {
175 rt_deliver (mbuf, i->first - start, i->second);
182 MIDISceneChanger::locate (framepos_t pos)
184 boost::shared_ptr<MIDISceneChange> msc;
187 Glib::Threads::RWLock::ReaderLock lm (scene_lock);
189 if (scenes.empty()) {
193 Scenes::const_iterator i = scenes.lower_bound (pos);
195 if (i != scenes.end()) {
197 if (i->first != pos) {
198 /* i points to first scene with position > pos, so back
201 if (i != scenes.begin()) {
208 /* go back to the final scene and use it */
215 if (msc->program() != last_delivered_program || msc->bank() != last_delivered_bank) {
216 non_rt_deliver (msc);
221 MIDISceneChanger::set_input_port (MIDI::Port* mp)
225 incoming_connections.drop_connections();
229 /* midi port is asynchronous. MIDI parsing will be carried out
230 * by the MIDI UI thread which will emit the relevant signals
231 * and thus invoke our callbacks as necessary.
234 for (int channel = 0; channel < 16; ++channel) {
235 input_port->parser()->channel_bank_change[channel].connect_same_thread (incoming_connections, boost::bind (&MIDISceneChanger::bank_change_input, this, _1, _2, channel));
236 input_port->parser()->channel_program_change[channel].connect_same_thread (incoming_connections, boost::bind (&MIDISceneChanger::program_change_input, this, _1, _2, channel));
242 MIDISceneChanger::set_output_port (boost::shared_ptr<MidiPort> mp)
248 MIDISceneChanger::set_recording (bool yn)
254 MIDISceneChanger::recording() const
256 return _session.transport_rolling() && _session.get_record_enabled();
260 MIDISceneChanger::bank_change_input (MIDI::Parser& /*parser*/, unsigned short, int)
263 have_seen_bank_changes = true;
265 MIDIInputActivity (); /* EMIT SIGNAL */
269 MIDISceneChanger::program_change_input (MIDI::Parser& parser, MIDI::byte program, int channel)
271 framecnt_t time = parser.get_timestamp ();
273 last_program_message_time = time;
277 MIDIInputActivity (); /* EMIT SIGNAL */
280 if (have_seen_bank_changes) {
281 bank = input_port->channel (channel)->bank();
284 jump_to (input_port->channel (channel)->bank(), program);
288 Locations* locations (_session.locations ());
290 bool new_mark = false;
291 framecnt_t slop = (framecnt_t) floor ((Config->get_inter_scene_gap_msecs() / 1000.0) * _session.frame_rate());
293 /* check for marker at current location */
295 loc = locations->mark_at (time, slop);
298 /* create a new marker at the desired position */
300 std::string new_name;
302 if (!locations->next_available_name (new_name, _("Scene "))) {
303 std::cerr << "No new marker name available\n";
307 loc = new Location (_session, time, time, new_name, Location::IsMark);
312 if (have_seen_bank_changes) {
313 bank = input_port->channel (channel)->bank();
316 MIDISceneChange* msc =new MIDISceneChange (channel, bank, program & 0x7f);
318 /* check for identical scene change so we can re-use color, if any */
320 Locations::LocationList copy (locations->list());
322 for (Locations::LocationList::const_iterator l = copy.begin(); l != copy.end(); ++l) {
323 boost::shared_ptr<MIDISceneChange> sc = boost::dynamic_pointer_cast<MIDISceneChange>((*l)->scene_change());
325 if (sc && (*sc.get()) == *msc) {
326 msc->set_color (sc->color ());
331 loc->set_scene_change (boost::shared_ptr<MIDISceneChange> (msc));
333 /* this will generate a "changed" signal to be emitted by locations,
334 and we will call ::gather() to update our list of MIDI events.
338 locations->add (loc);
341 MIDIInputActivity (); /* EMIT SIGNAL */
345 MIDISceneChanger::jump_to (int bank, int program)
347 const Locations::LocationList& locations (_session.locations()->list());
348 boost::shared_ptr<SceneChange> sc;
349 framepos_t where = max_framepos;
351 for (Locations::LocationList::const_iterator l = locations.begin(); l != locations.end(); ++l) {
353 if ((sc = (*l)->scene_change()) != 0) {
355 boost::shared_ptr<MIDISceneChange> msc = boost::dynamic_pointer_cast<MIDISceneChange> (sc);
357 if (msc->bank() == bank && msc->program() == program && (*l)->start() < where) {
358 where = (*l)->start();
363 if (where != max_framepos) {
364 _session.request_locate (where);