75d5581ce5ba842a5da18bf5bb29b1a7d15e16d9
[ardour.git] / libs / ardour / midi_scene_changer.cc
1 /*
2     Copyright (C) 2014 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include "evoral/MIDIEvent.hpp"
21 #include "midi++/channel.h"
22 #include "midi++/parser.h"
23 #include "midi++/port.h"
24
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"
32
33 #include "i18n.h"
34
35 using namespace ARDOUR;
36
37 MIDISceneChanger::MIDISceneChanger (Session& s)
38         : SceneChanger (s)
39         , _recording (true)
40         , have_seen_bank_changes (false)
41         , last_program_message_time (-1)
42         , last_delivered_program (-1)
43         , last_delivered_bank (-1)
44           
45 {
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));
50
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));
53 }
54
55 MIDISceneChanger::~MIDISceneChanger ()
56 {
57 }
58
59 void
60 MIDISceneChanger::locations_changed ()
61 {
62         _session.locations()->apply (*this, &MIDISceneChanger::gather);
63 }
64
65 /** Use the session's list of locations to collect all patch changes.
66  * 
67  * This is called whenever the locations change in anyway.
68  */
69 void
70 MIDISceneChanger::gather (const Locations::LocationList& locations)
71 {
72         boost::shared_ptr<SceneChange> sc;
73
74         Glib::Threads::RWLock::WriterLock lm (scene_lock);
75
76         scenes.clear ();
77
78         for (Locations::LocationList::const_iterator l = locations.begin(); l != locations.end(); ++l) {
79
80                 if ((sc = (*l)->scene_change()) != 0) {
81
82                         boost::shared_ptr<MIDISceneChange> msc = boost::dynamic_pointer_cast<MIDISceneChange> (sc);
83
84                         if (msc) {
85
86                                 if (msc->bank() >= 0) {
87                                         have_seen_bank_changes = true;
88                                 }
89                         
90                                 scenes.insert (std::make_pair ((*l)->start(), msc));
91                         }
92                 }
93         }
94 }
95
96 void
97 MIDISceneChanger::rt_deliver (MidiBuffer& mbuf, framepos_t when, boost::shared_ptr<MIDISceneChange> msc)
98 {
99         if (!msc->active()) {
100                 return;
101         }
102
103         uint8_t buf[4];
104         size_t cnt;
105
106         MIDIOutputActivity (); /* EMIT SIGNAL */
107
108         if ((cnt = msc->get_bank_msb_message (buf, sizeof (buf))) > 0) {
109                 mbuf.push_back (when, cnt, buf);
110
111                 if ((cnt = msc->get_bank_lsb_message (buf, sizeof (buf))) > 0) {
112                         mbuf.push_back (when, cnt, buf);
113                 }
114
115                 last_delivered_bank = msc->bank();
116         }
117
118         if ((cnt = msc->get_program_message (buf, sizeof (buf))) > 0) {
119                 mbuf.push_back (when, cnt, buf);
120
121                 last_delivered_program = msc->program();
122         }
123 }
124
125 void
126 MIDISceneChanger::non_rt_deliver (boost::shared_ptr<MIDISceneChange> msc)
127 {
128         if (!msc->active()) {
129                 return;
130         }
131
132         uint8_t buf[4];
133         size_t cnt;
134         boost::shared_ptr<AsyncMIDIPort> aport = boost::dynamic_pointer_cast<AsyncMIDIPort>(output_port);
135
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).
139         */
140         
141         MIDIOutputActivity (); /* EMIT SIGNAL */
142
143         if ((cnt = msc->get_bank_msb_message (buf, sizeof (buf))) > 0) {
144                 aport->write (buf, cnt, 0);
145
146                 if ((cnt = msc->get_bank_lsb_message (buf, sizeof (buf))) > 0) {
147                         aport->write (buf, cnt, 0);
148                 }
149
150                 last_delivered_bank = msc->bank();
151         }
152
153         if ((cnt = msc->get_program_message (buf, sizeof (buf))) > 0) {
154                 aport->write (buf, cnt, 0);
155                 last_delivered_program = msc->program();
156         }
157 }
158
159 void
160 MIDISceneChanger::run (framepos_t start, framepos_t end)
161 {
162         if (!output_port || recording() || !_session.transport_rolling()) {
163                 return;
164         }
165
166         Glib::Threads::RWLock::ReaderLock lm (scene_lock, Glib::Threads::TRY_LOCK);
167         
168         if (!lm.locked()) {
169                 return;
170         }
171
172         /* get lower bound of events to consider */
173
174         Scenes::const_iterator i = scenes.lower_bound (start);
175         MidiBuffer& mbuf (output_port->get_midi_buffer (end-start));
176
177         while (i != scenes.end()) {
178
179                 if (i->first >= end) {
180                         break;
181                 }
182         
183                 rt_deliver (mbuf, i->first - start, i->second);
184                 
185                 ++i;
186         }
187 }
188
189 void
190 MIDISceneChanger::locate (framepos_t pos)
191 {
192         boost::shared_ptr<MIDISceneChange> msc;
193
194         {
195                 Glib::Threads::RWLock::ReaderLock lm (scene_lock);
196
197                 if (scenes.empty()) {
198                         return;
199                 }
200                 
201                 Scenes::const_iterator i = scenes.lower_bound (pos);
202                 
203                 if (i != scenes.end()) {
204
205                         if (i->first != pos) {
206                                 /* i points to first scene with position > pos, so back
207                                  * up, if possible.
208                                  */
209                                 if (i != scenes.begin()) {
210                                         --i;
211                                 } else {
212                                         return;
213                                 }
214                         }
215                 } else {
216                         /* go back to the final scene and use it */
217                         --i;
218                 }
219
220                 msc = i->second;
221         }
222
223         if (msc->program() != last_delivered_program || msc->bank() != last_delivered_bank) {
224                 non_rt_deliver (msc);
225         }
226 }               
227
228 void
229 MIDISceneChanger::set_input_port (MIDI::Port* mp)
230 {
231         input_port = mp;
232
233         incoming_connections.drop_connections();
234         
235         if (input_port) {
236                 
237                 /* midi port is asynchronous. MIDI parsing will be carried out
238                  * by the MIDI UI thread which will emit the relevant signals
239                  * and thus invoke our callbacks as necessary.
240                  */
241
242                 for (int channel = 0; channel < 16; ++channel) {
243                         input_port->parser()->channel_bank_change[channel].connect_same_thread (incoming_connections, boost::bind (&MIDISceneChanger::bank_change_input, this, _1, _2, channel));
244                         input_port->parser()->channel_program_change[channel].connect_same_thread (incoming_connections, boost::bind (&MIDISceneChanger::program_change_input, this, _1, _2, channel));
245                 }
246         }
247 }
248
249 void
250 MIDISceneChanger::set_output_port (boost::shared_ptr<MidiPort> mp)
251 {
252         output_port = mp;
253 }
254
255 void
256 MIDISceneChanger::set_recording (bool yn)
257 {
258         _recording = yn;
259 }
260
261 bool
262 MIDISceneChanger::recording() const 
263 {
264         return _session.transport_rolling() && _session.get_record_enabled();
265 }
266
267 void
268   MIDISceneChanger::bank_change_input (MIDI::Parser& /*parser*/, unsigned short, int)
269 {
270         if (recording()) {
271                 have_seen_bank_changes = true;
272         }
273         MIDIInputActivity (); /* EMIT SIGNAL */
274 }
275
276 void
277 MIDISceneChanger::program_change_input (MIDI::Parser& parser, MIDI::byte program, int channel)
278 {
279         framecnt_t time = parser.get_timestamp ();
280
281         last_program_message_time = time;
282
283         if (!recording()) {
284
285                 MIDIInputActivity (); /* EMIT SIGNAL */
286         
287                 int bank = -1;
288                 if (have_seen_bank_changes) {
289                         bank = input_port->channel (channel)->bank();
290                 }
291         
292                 jump_to (bank, program);
293                 return;
294         }
295
296         Locations* locations (_session.locations ());
297         Location* loc;
298         bool new_mark = false;
299
300         /* check for marker at current location */
301
302         loc = locations->mark_at (time, Config->get_inter_scene_gap_frames());
303
304         if (!loc) {
305                 /* create a new marker at the desired position */
306                 
307                 std::string new_name;
308
309                 if (!locations->next_available_name (new_name, _("Scene "))) {
310                         std::cerr << "No new marker name available\n";
311                         return;
312                 }
313                 
314                 loc = new Location (_session, time, time, new_name, Location::IsMark);
315                 new_mark = true;
316         }
317
318         int bank = -1;
319         if (have_seen_bank_changes) {
320                 bank = input_port->channel (channel)->bank();
321         }
322
323         MIDISceneChange* msc =new MIDISceneChange (channel, bank, program & 0x7f);
324
325         /* check for identical scene change so we can re-use color, if any */
326
327         Locations::LocationList copy (locations->list());
328
329         for (Locations::LocationList::const_iterator l = copy.begin(); l != copy.end(); ++l) {
330                 boost::shared_ptr<MIDISceneChange> sc = boost::dynamic_pointer_cast<MIDISceneChange>((*l)->scene_change());
331
332                 if (sc && (*sc.get()) == *msc) {
333                         msc->set_color (sc->color ());
334                         break;
335                 }
336         }
337
338         loc->set_scene_change (boost::shared_ptr<MIDISceneChange> (msc));
339         
340         /* this will generate a "changed" signal to be emitted by locations,
341            and we will call ::gather() to update our list of MIDI events.
342         */
343
344         if (new_mark) {
345                 locations->add (loc);
346         }
347
348         MIDIInputActivity (); /* EMIT SIGNAL */
349 }
350
351 void
352 MIDISceneChanger::jump_to (int bank, int program)
353 {
354         const Locations::LocationList& locations (_session.locations()->list());
355         boost::shared_ptr<SceneChange> sc;
356         framepos_t where = max_framepos;
357
358         for (Locations::LocationList::const_iterator l = locations.begin(); l != locations.end(); ++l) {
359
360                 if ((sc = (*l)->scene_change()) != 0) {
361
362                         boost::shared_ptr<MIDISceneChange> msc = boost::dynamic_pointer_cast<MIDISceneChange> (sc);
363
364                         if (msc->bank() == bank && msc->program() == program && (*l)->start() < where) {
365                                 where = (*l)->start();
366                         }
367                 }
368         }
369
370         if (where != max_framepos) {
371                 _session.request_locate (where);
372         }
373 }