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++/parser.h"
22 #include "midi++/port.h"
24 #include "ardour/event_type_map.h"
25 #include "ardour/midi_port.h"
26 #include "ardour/midi_scene_change.h"
27 #include "ardour/midi_scene_changer.h"
28 #include "ardour/session.h"
32 using namespace ARDOUR;
34 MIDISceneChanger::MIDISceneChanger (Session& s)
37 , last_bank_message_time (-1)
38 , last_program_message_time (-1)
39 , last_delivered_program (-1)
40 , last_delivered_bank (-1)
43 _session.locations()->changed.connect_same_thread (*this, boost::bind (&MIDISceneChanger::locations_changed, this, _1));
44 Location::scene_changed.connect_same_thread (*this, boost::bind (&MIDISceneChanger::gather, this));
47 MIDISceneChanger::~MIDISceneChanger ()
52 MIDISceneChanger::locations_changed (Locations::Change)
57 /** Use the session's list of locations to collect all patch changes.
59 * This is called whenever the locations change in anyway.
62 MIDISceneChanger::gather ()
64 const Locations::LocationList& locations (_session.locations()->list());
65 boost::shared_ptr<SceneChange> sc;
69 for (Locations::LocationList::const_iterator l = locations.begin(); l != locations.end(); ++l) {
71 if ((sc = (*l)->scene_change()) != 0) {
73 boost::shared_ptr<MIDISceneChange> msc = boost::dynamic_pointer_cast<MIDISceneChange> (sc);
76 scenes.insert (std::make_pair (msc->time(), msc));
83 MIDISceneChanger::deliver (MidiBuffer& mbuf, framepos_t when, boost::shared_ptr<MIDISceneChange> msc)
88 if ((cnt = msc->get_bank_msb_message (buf, sizeof (buf))) > 0) {
89 mbuf.push_back (when, cnt, buf);
91 if ((cnt = msc->get_bank_lsb_message (buf, sizeof (buf))) > 0) {
92 mbuf.push_back (when, cnt, buf);
95 last_delivered_bank = msc->bank();
98 if ((cnt = msc->get_program_message (buf, sizeof (buf))) > 0) {
99 mbuf.push_back (when, cnt, buf);
101 last_delivered_program = msc->program();
107 MIDISceneChanger::run (framepos_t start, framepos_t end)
109 if (!output_port || recording()) {
113 /* get lower bound of events to consider */
115 Scenes::const_iterator i = scenes.lower_bound (start);
116 MidiBuffer& mbuf (output_port->get_midi_buffer (end-start));
118 while (i != scenes.end()) {
120 if (i->first >= end) {
124 deliver (mbuf, i->first - start, i->second);
131 MIDISceneChanger::locate (framepos_t pos)
133 Scenes::const_iterator i = scenes.upper_bound (pos);
135 if (i == scenes.end()) {
139 if (i->second->program() != last_delivered_program || i->second->bank() != last_delivered_bank) {
140 // MidiBuffer& mbuf (output_port->get_midi_buffer (end-start));
141 // deliver (mbuf, i->first, i->second);
146 MIDISceneChanger::set_input_port (MIDI::Port* mp)
150 incoming_bank_change_connection.disconnect ();
151 incoming_program_change_connection.disconnect ();
155 /* midi port is asynchronous. MIDI parsing will be carried out
156 * by the MIDI UI thread which will emit the relevant signals
157 * and thus invoke our callbacks as necessary.
160 input_port->parser()->bank_change.connect_same_thread (incoming_bank_change_connection, boost::bind (&MIDISceneChanger::bank_change_input, this, _1, _2));
161 input_port->parser()->program_change.connect_same_thread (incoming_program_change_connection, boost::bind (&MIDISceneChanger::program_change_input, this, _1, _2));
166 MIDISceneChanger::set_output_port (boost::shared_ptr<MidiPort> mp)
172 MIDISceneChanger::set_recording (bool yn)
178 MIDISceneChanger::recording() const
180 return _session.transport_rolling() && _session.get_record_enabled();
184 MIDISceneChanger::bank_change_input (MIDI::Parser& parser, unsigned short bank)
190 last_bank_message_time = parser.get_timestamp ();
195 MIDISceneChanger::program_change_input (MIDI::Parser& parser, MIDI::byte program)
197 framecnt_t time = parser.get_timestamp ();
198 frameoffset_t delta = time - last_program_message_time;
200 last_program_message_time = time;
203 jump_to (current_bank, program);
207 Locations* locations (_session.locations ());
209 bool new_mark = false;
210 framecnt_t slop = (framecnt_t) floor ((Config->get_inter_scene_gap_msecs() / 1000.0) * _session.frame_rate());
212 /* check for marker at current location */
214 loc = locations->mark_at (time, slop);
217 /* create a new marker at the desired position */
219 std::string new_name;
221 if (!locations->next_available_name (new_name, _("Scene "))) {
222 std::cerr << "No new marker name available\n";
226 loc = new Location (_session, time, time, new_name, Location::IsMark);
231 uint8_t channel = (program & 0xf0) >> 8;
233 /* if we received a bank change message within the last 2 msec, use the
234 * current bank value, otherwise lookup the current bank number and use
238 if (time - last_bank_message_time < (2 * _session.frame_rate() / 1000.0)) {
244 MIDISceneChange* msc =new MIDISceneChange (loc->start(), channel, bank, program & 0x7f);
246 loc->set_scene_change (boost::shared_ptr<MIDISceneChange> (msc));
248 /* this will generate a "changed" signal to be emitted by locations,
249 and we will call ::gather() to update our list of MIDI events.
253 locations->add (loc);
258 MIDISceneChanger::jump_to (int bank, int program)
260 const Locations::LocationList& locations (_session.locations()->list());
261 boost::shared_ptr<SceneChange> sc;
262 framepos_t where = max_framepos;
264 for (Locations::LocationList::const_iterator l = locations.begin(); l != locations.end(); ++l) {
266 if ((sc = (*l)->scene_change()) != 0) {
268 boost::shared_ptr<MIDISceneChange> msc = boost::dynamic_pointer_cast<MIDISceneChange> (sc);
270 if (msc->bank() == bank && msc->program() == program && (*l)->start() < where) {
271 where = (*l)->start();
276 if (where != max_framepos) {
277 _session.request_locate (where);