merge (squash) with scenechange topic branch to provide MIDI-driven scene change...
[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++/parser.h"
22 #include "midi++/port.h"
23
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"
29
30 #include "i18n.h"
31
32 using namespace ARDOUR;
33
34 MIDISceneChanger::MIDISceneChanger (Session& s)
35         : SceneChanger (s)
36         , _recording (true)
37         , last_bank_message_time (-1)
38         , last_program_message_time (-1)
39         , last_delivered_program (-1)
40         , last_delivered_bank (-1)
41           
42 {
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));
45 }
46
47 MIDISceneChanger::~MIDISceneChanger ()
48 {
49 }
50
51 void
52 MIDISceneChanger::locations_changed (Locations::Change)
53 {
54         gather ();
55 }
56
57 /** Use the session's list of locations to collect all patch changes.
58  * 
59  * This is called whenever the locations change in anyway.
60  */
61 void
62 MIDISceneChanger::gather ()
63 {
64         const Locations::LocationList& locations (_session.locations()->list());
65         boost::shared_ptr<SceneChange> sc;
66
67         scenes.clear ();
68
69         for (Locations::LocationList::const_iterator l = locations.begin(); l != locations.end(); ++l) {
70
71                 if ((sc = (*l)->scene_change()) != 0) {
72
73                         boost::shared_ptr<MIDISceneChange> msc = boost::dynamic_pointer_cast<MIDISceneChange> (sc);
74
75                         if (msc) {
76                                 scenes.insert (std::make_pair (msc->time(), msc));
77                         }
78                 }
79         }
80 }
81
82 void
83 MIDISceneChanger::deliver (MidiBuffer& mbuf, framepos_t when, boost::shared_ptr<MIDISceneChange> msc)
84 {
85         uint8_t buf[4];
86         size_t cnt;
87
88         if ((cnt = msc->get_bank_msb_message (buf, sizeof (buf))) > 0) {
89                 mbuf.push_back (when, cnt, buf);
90
91                 if ((cnt = msc->get_bank_lsb_message (buf, sizeof (buf))) > 0) {
92                         mbuf.push_back (when, cnt, buf);
93                 }
94
95                 last_delivered_bank = msc->bank();
96         }
97
98         if ((cnt = msc->get_program_message (buf, sizeof (buf))) > 0) {
99                 mbuf.push_back (when, cnt, buf);
100
101                 last_delivered_program = msc->program();
102         }
103 }
104                         
105
106 void
107 MIDISceneChanger::run (framepos_t start, framepos_t end)
108 {
109         if (!output_port || recording()) {
110                 return;
111         }
112
113         /* get lower bound of events to consider */
114
115         Scenes::const_iterator i = scenes.lower_bound (start);
116         MidiBuffer& mbuf (output_port->get_midi_buffer (end-start));
117
118         while (i != scenes.end()) {
119
120                 if (i->first >= end) {
121                         break;
122                 }
123                 
124                 deliver (mbuf, i->first - start, i->second);
125
126                 ++i;
127         }
128 }
129
130 void
131 MIDISceneChanger::locate (framepos_t pos)
132 {
133         Scenes::const_iterator i = scenes.upper_bound (pos);
134
135         if (i == scenes.end()) {
136                 return;
137         }
138
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);
142         }
143 }               
144
145 void
146 MIDISceneChanger::set_input_port (MIDI::Port* mp)
147 {
148         input_port = mp;
149
150         incoming_bank_change_connection.disconnect ();
151         incoming_program_change_connection.disconnect ();
152         
153         if (input_port) {
154                 
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.
158                  */
159
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));
162         }
163 }
164
165 void
166 MIDISceneChanger::set_output_port (boost::shared_ptr<MidiPort> mp)
167 {
168         output_port = mp;
169 }
170
171 void
172 MIDISceneChanger::set_recording (bool yn)
173 {
174         _recording = yn;
175 }
176
177 bool
178 MIDISceneChanger::recording() const 
179 {
180         return _session.transport_rolling() && _session.get_record_enabled();
181 }
182
183 void
184 MIDISceneChanger::bank_change_input (MIDI::Parser& parser, unsigned short bank)
185 {
186         if (!recording()) {
187                 return;
188         }
189
190         last_bank_message_time = parser.get_timestamp ();
191         current_bank = bank;
192 }
193
194 void
195 MIDISceneChanger::program_change_input (MIDI::Parser& parser, MIDI::byte program)
196 {
197         framecnt_t time = parser.get_timestamp ();
198         frameoffset_t delta = time - last_program_message_time;
199
200         last_program_message_time = time;
201
202         if (!recording()) {
203                 jump_to (current_bank, program);
204                 return;
205         }
206
207         Locations* locations (_session.locations ());
208         Location* loc;
209         bool new_mark = false;
210         framecnt_t slop = (framecnt_t) floor ((Config->get_inter_scene_gap_msecs() / 1000.0) * _session.frame_rate());
211
212         /* check for marker at current location */
213
214         loc = locations->mark_at (time, slop);
215
216         if (!loc) {
217                 /* create a new marker at the desired position */
218                 
219                 std::string new_name;
220
221                 if (!locations->next_available_name (new_name, _("Scene "))) {
222                         std::cerr << "No new marker name available\n";
223                         return;
224                 }
225                 
226                 loc = new Location (_session, time, time, new_name, Location::IsMark);
227                 new_mark = true;
228         }
229
230         uint8_t bank;
231         uint8_t channel = (program & 0xf0) >> 8;
232
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
235          * that.
236          */
237
238         if (time - last_bank_message_time < (2 * _session.frame_rate() / 1000.0)) {
239                 bank = current_bank;
240         } else {
241                 bank = -1;
242         }
243
244         MIDISceneChange* msc =new MIDISceneChange (loc->start(), channel, bank, program & 0x7f);
245
246         loc->set_scene_change (boost::shared_ptr<MIDISceneChange> (msc));
247         
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.
250         */
251
252         if (new_mark) {
253                 locations->add (loc);
254         }
255 }
256
257 void
258 MIDISceneChanger::jump_to (int bank, int program)
259 {
260         const Locations::LocationList& locations (_session.locations()->list());
261         boost::shared_ptr<SceneChange> sc;
262         framepos_t where = max_framepos;
263
264         for (Locations::LocationList::const_iterator l = locations.begin(); l != locations.end(); ++l) {
265
266                 if ((sc = (*l)->scene_change()) != 0) {
267
268                         boost::shared_ptr<MIDISceneChange> msc = boost::dynamic_pointer_cast<MIDISceneChange> (sc);
269
270                         if (msc->bank() == bank && msc->program() == program && (*l)->start() < where) {
271                                 where = (*l)->start();
272                         }
273                 }
274         }
275
276         if (where != max_framepos) {
277                 _session.request_locate (where);
278         }
279 }