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