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