2 * Copyright (C) 2014-2016 David Robillard <d@drobilla.net>
3 * Copyright (C) 2014-2017 Paul Davis <paul@linuxaudiosystems.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "evoral/Event.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, samplepos_t when, boost::shared_ptr<MIDISceneChange> msc)
106 MIDIOutputActivity (); /* EMIT SIGNAL */
108 if ((cnt = msc->get_bank_msb_message (buf, sizeof (buf))) > 0) {
109 mbuf.push_back (when, cnt, buf);
111 if ((cnt = msc->get_bank_lsb_message (buf, sizeof (buf))) > 0) {
112 mbuf.push_back (when, cnt, buf);
115 last_delivered_bank = msc->bank();
118 if ((cnt = msc->get_program_message (buf, sizeof (buf))) > 0) {
119 mbuf.push_back (when, cnt, buf);
121 last_delivered_program = msc->program();
126 MIDISceneChanger::non_rt_deliver (boost::shared_ptr<MIDISceneChange> msc)
128 if (!msc->active()) {
134 boost::shared_ptr<AsyncMIDIPort> aport = boost::dynamic_pointer_cast<AsyncMIDIPort>(output_port);
136 /* We use zero as the timestamp for these messages because we are in a
137 non-RT/process context. Using zero means "deliver them as early as
138 possible" (practically speaking, in the next process callback).
141 MIDIOutputActivity (); /* EMIT SIGNAL */
143 if ((cnt = msc->get_bank_msb_message (buf, sizeof (buf))) > 0) {
144 aport->write (buf, cnt, 0);
146 if ((cnt = msc->get_bank_lsb_message (buf, sizeof (buf))) > 0) {
147 aport->write (buf, cnt, 0);
150 last_delivered_bank = msc->bank();
153 if ((cnt = msc->get_program_message (buf, sizeof (buf))) > 0) {
154 aport->write (buf, cnt, 0);
155 last_delivered_program = msc->program();
160 MIDISceneChanger::run (samplepos_t start, samplepos_t end)
162 if (!output_port || recording() || !_session.transport_rolling()) {
166 Glib::Threads::RWLock::ReaderLock lm (scene_lock, Glib::Threads::TRY_LOCK);
172 /* get lower bound of events to consider */
174 Scenes::const_iterator i = scenes.lower_bound (start);
175 MidiBuffer& mbuf (output_port->get_midi_buffer (end-start));
177 while (i != scenes.end()) {
179 if (i->first >= end) {
183 rt_deliver (mbuf, i->first - start, i->second);
190 MIDISceneChanger::locate (samplepos_t pos)
192 boost::shared_ptr<MIDISceneChange> msc;
195 Glib::Threads::RWLock::ReaderLock lm (scene_lock);
197 if (scenes.empty()) {
201 Scenes::const_iterator i = scenes.lower_bound (pos);
203 if (i != scenes.end()) {
205 if (i->first != pos) {
206 /* i points to first scene with position > pos, so back
209 if (i != scenes.begin()) {
216 /* go back to the final scene and use it */
223 if (msc->program() != last_delivered_program || msc->bank() != last_delivered_bank) {
224 non_rt_deliver (msc);
229 MIDISceneChanger::set_input_port (boost::shared_ptr<MidiPort> mp)
231 incoming_connections.drop_connections();
234 boost::shared_ptr<AsyncMIDIPort> async = boost::dynamic_pointer_cast<AsyncMIDIPort> (mp);
240 /* midi port is asynchronous. MIDI parsing will be carried out
241 * by the MIDI UI thread which will emit the relevant signals
242 * and thus invoke our callbacks as necessary.
245 for (int channel = 0; channel < 16; ++channel) {
246 async->parser()->channel_bank_change[channel].connect_same_thread (incoming_connections, boost::bind (&MIDISceneChanger::bank_change_input, this, _1, _2, channel));
247 async->parser()->channel_program_change[channel].connect_same_thread (incoming_connections, boost::bind (&MIDISceneChanger::program_change_input, this, _1, _2, channel));
253 MIDISceneChanger::set_output_port (boost::shared_ptr<MidiPort> mp)
259 MIDISceneChanger::set_recording (bool yn)
265 MIDISceneChanger::recording() const
267 return _session.transport_rolling() && _session.get_record_enabled();
271 MIDISceneChanger::bank_change_input (MIDI::Parser& /*parser*/, unsigned short, int)
274 have_seen_bank_changes = true;
276 MIDIInputActivity (); /* EMIT SIGNAL */
280 MIDISceneChanger::program_change_input (MIDI::Parser& parser, MIDI::byte program, int channel)
282 samplecnt_t time = parser.get_timestamp ();
284 last_program_message_time = time;
288 MIDIInputActivity (); /* EMIT SIGNAL */
291 if (have_seen_bank_changes) {
292 bank = boost::dynamic_pointer_cast<AsyncMIDIPort>(input_port)->channel (channel)->bank();
295 jump_to (bank, program);
299 Locations* locations (_session.locations ());
301 bool new_mark = false;
303 /* check for marker at current location */
305 loc = locations->mark_at (time, Config->get_inter_scene_gap_samples());
308 /* create a new marker at the desired position */
310 std::string new_name;
312 if (!locations->next_available_name (new_name, _("Scene "))) {
313 std::cerr << "No new marker name available\n";
317 loc = new Location (_session, time, time, new_name, Location::IsMark, 0);
322 if (have_seen_bank_changes) {
323 bank = boost::dynamic_pointer_cast<AsyncMIDIPort>(input_port)->channel (channel)->bank();
326 MIDISceneChange* msc =new MIDISceneChange (channel, bank, program & 0x7f);
328 /* check for identical scene change so we can re-use color, if any */
330 Locations::LocationList copy (locations->list());
332 for (Locations::LocationList::const_iterator l = copy.begin(); l != copy.end(); ++l) {
333 boost::shared_ptr<MIDISceneChange> sc = boost::dynamic_pointer_cast<MIDISceneChange>((*l)->scene_change());
335 if (sc && (*sc.get()) == *msc) {
336 msc->set_color (sc->color ());
341 loc->set_scene_change (boost::shared_ptr<MIDISceneChange> (msc));
343 /* this will generate a "changed" signal to be emitted by locations,
344 and we will call ::gather() to update our list of MIDI events.
348 locations->add (loc);
351 MIDIInputActivity (); /* EMIT SIGNAL */
355 MIDISceneChanger::jump_to (int bank, int program)
357 const Locations::LocationList& locations (_session.locations()->list());
358 boost::shared_ptr<SceneChange> sc;
359 samplepos_t where = max_samplepos;
361 for (Locations::LocationList::const_iterator l = locations.begin(); l != locations.end(); ++l) {
363 if ((sc = (*l)->scene_change()) != 0) {
365 boost::shared_ptr<MIDISceneChange> msc = boost::dynamic_pointer_cast<MIDISceneChange> (sc);
367 if (msc->bank() == bank && msc->program() == program && (*l)->start() < where) {
368 where = (*l)->start();
373 if (where != max_samplepos) {
374 _session.request_locate (where);