Prepare PatchChangeWidget for Midi-Busses (use instrument-plugin)
[ardour.git] / gtk2_ardour / patch_change_widget.cc
1 /*
2  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2011 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include <bitset>
21 #include <gtkmm/frame.h>
22
23 #include "pbd/unwind.h"
24
25 #include "evoral/midi_events.h"
26 #include "evoral/PatchChange.hpp"
27
28 #include "midi++/midnam_patch.h"
29
30 #include "ardour/instrument_info.h"
31 #include "ardour/midi_track.h"
32 #include "ardour/plugin_insert.h"
33
34 #include "gtkmm2ext/menu_elems.h"
35 #include "widgets/tooltips.h"
36
37 #include "gui_thread.h"
38 #include "patch_change_widget.h"
39 #include "ui_config.h"
40
41 #include "pbd/i18n.h"
42
43 using namespace Gtk;
44 using namespace ARDOUR;
45
46 PatchChangeWidget::PatchChangeWidget (boost::shared_ptr<ARDOUR::Route> r)
47         : _route (r)
48         , _bank_msb_spin (*manage (new Adjustment (0, 0, 127, 1, 16)))
49         , _bank_lsb_spin (*manage (new Adjustment (0, 0, 127, 1, 16)))
50         , _program_table (/*rows*/ 16, /*cols*/ 8, true)
51         , _channel (-1)
52         , _ignore_spin_btn_signals (false)
53         , _no_notifications (false)
54         , _info (r->instrument_info ())
55         , _audition_enable (_("Audition on Change"), ArdourWidgets::ArdourButton::led_default_elements)
56         , _audition_start_spin (*manage (new Adjustment (48, 0, 127, 1, 16)))
57         , _audition_end_spin (*manage (new Adjustment (60, 0, 127, 1, 16)))
58         , _audition_velocity (*manage (new Adjustment (100, 1, 127, 1, 16)))
59         , _audition_note_on (false)
60         , _piano ((PianoKeyboard*)piano_keyboard_new())
61         , _pianomm (Glib::wrap((GtkWidget*)_piano))
62 {
63         Box* box;
64         box = manage (new HBox ());
65         box->set_border_width (2);
66         box->set_spacing (4);
67         box->pack_start (*manage (new Label (_("Channel:"))), false, false);
68         box->pack_start (_channel_select, false, false);
69         box->pack_start (*manage (new Label (_("Bank:"))), false, false);
70         box->pack_start (_bank_select, true, true);
71         box->pack_start (*manage (new Label (_("MSB:"))), false, false);
72         box->pack_start (_bank_msb_spin, false, false);
73         box->pack_start (*manage (new Label (_("LSB:"))), false, false);
74         box->pack_start (_bank_lsb_spin, false, false);
75
76         pack_start (*box, false, false);
77
78         _program_table.set_spacings (1);
79         pack_start (_program_table, true, true);
80
81         if (!boost::dynamic_pointer_cast<MidiTrack> (_route)) {
82                 pack_start ( *manage (new Label (_("Note: Patch Selection is volatile (only Midi-Tracks retain bank/patch selection)."))), false, false);
83         }
84
85         box = manage (new HBox ());
86         box->set_spacing (4);
87         box->pack_start (_audition_enable, false, false);
88         box->pack_start (*manage (new Label (_("Start Note:"))), false, false);
89         box->pack_start (_audition_start_spin, false, false);
90         box->pack_start (*manage (new Label (_("End Note:"))), false, false);
91         box->pack_start (_audition_end_spin, false, false);
92         box->pack_start (*manage (new Label (_("Velocity:"))), false, false);
93         box->pack_start (_audition_velocity, false, false);
94
95         Box* box2 = manage (new HBox ());
96         box2->pack_start (*box, true, false);
97         box2->set_border_width (2);
98         pack_start (*box2, false, false);
99
100         for (uint8_t pgm = 0; pgm < 128; ++pgm) {
101                 _program_btn[pgm].set_text_ellipsize (Pango::ELLIPSIZE_END);
102                 _program_btn[pgm].set_layout_ellipsize_width (PANGO_SCALE * 112 * UIConfiguration::instance ().get_ui_scale ());
103                 int row = pgm % 16;
104                 int col = pgm / 16;
105                 _program_table.attach (_program_btn[pgm], col, col + 1, row, row + 1);
106                 _program_btn[pgm].signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PatchChangeWidget::select_program), pgm));
107         }
108
109         for (uint32_t chn = 0; chn < 16; ++chn) {
110                 using namespace Menu_Helpers;
111                 using namespace Gtkmm2ext;
112                 char buf[8];
113                 snprintf (buf, sizeof(buf), "%d", chn + 1);
114                 _channel_select.AddMenuElem (MenuElemNoMnemonic (buf, sigc::bind (sigc::mem_fun (*this, &PatchChangeWidget::select_channel), chn)));
115         }
116
117         piano_keyboard_set_monophonic (_piano, TRUE);
118         g_signal_connect (G_OBJECT (_piano), "note-on", G_CALLBACK (PatchChangeWidget::_note_on_event_handler), this);
119         g_signal_connect (G_OBJECT (_piano), "note-off", G_CALLBACK (PatchChangeWidget::_note_off_event_handler), this);
120         _pianomm->set_flags(Gtk::CAN_FOCUS);
121         pack_start (*_pianomm, false, false);
122
123         _audition_start_spin.set_sensitive (false);
124         _audition_end_spin.set_sensitive (false);
125
126         _audition_enable.signal_clicked.connect (sigc::mem_fun (*this, &PatchChangeWidget::audition_toggle));
127         _audition_start_spin.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &PatchChangeWidget::check_note_range), false));
128         _audition_end_spin.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &PatchChangeWidget::check_note_range), true));
129         _bank_msb_spin.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeWidget::select_bank_spin));
130         _bank_lsb_spin.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeWidget::select_bank_spin));
131
132         _info.Changed.connect (_info_changed_connection, invalidator (*this),
133                         boost::bind (&PatchChangeWidget::instrument_info_changed, this), gui_context());
134
135         if (!boost::dynamic_pointer_cast<MidiTrack> (_route)) {
136                 _route->processors_changed.connect (_route_connection, invalidator (*this),
137                                 boost::bind (&PatchChangeWidget::processors_changed, this), gui_context());
138                 processors_changed ();
139         }
140
141         set_spacing (4);
142         show_all ();
143 }
144
145 PatchChangeWidget::~PatchChangeWidget ()
146 {
147         cancel_audition ();
148         delete _pianomm;
149 }
150
151 void
152 PatchChangeWidget::refresh ()
153 {
154         if (is_visible ()) {
155                 on_show ();
156         }
157 }
158
159 void
160 PatchChangeWidget::on_show ()
161 {
162         Gtk::VBox::on_show ();
163         cancel_audition ();
164         _channel = -1;
165         select_channel (0);
166 }
167
168 void
169 PatchChangeWidget::on_hide ()
170 {
171         Gtk::VBox::on_hide ();
172         _ac_connections.drop_connections ();
173         cancel_audition ();
174 }
175
176 void
177 PatchChangeWidget::select_channel (uint8_t chn)
178 {
179         assert (_route);
180         assert (chn < 16);
181
182         if (_channel == chn) {
183                 return;
184         }
185
186         cancel_audition ();
187
188         _channel_select.set_text (string_compose ("%1", (int)(chn + 1)));
189         _channel = chn;
190         _no_notifications = false;
191
192         _ac_connections.drop_connections ();
193
194         if (boost::dynamic_pointer_cast<MidiTrack> (_route)) {
195                 boost::shared_ptr<AutomationControl> bank_msb = _route->automation_control(Evoral::Parameter (MidiCCAutomation, chn, MIDI_CTL_MSB_BANK), true);
196                 boost::shared_ptr<AutomationControl> bank_lsb = _route->automation_control(Evoral::Parameter (MidiCCAutomation, chn, MIDI_CTL_LSB_BANK), true);
197                 boost::shared_ptr<AutomationControl> program = _route->automation_control(Evoral::Parameter (MidiPgmChangeAutomation, chn), true);
198
199                 bank_msb->Changed.connect (_ac_connections, invalidator (*this),
200                                 boost::bind (&PatchChangeWidget::bank_changed, this), gui_context ());
201                 bank_lsb->Changed.connect (_ac_connections, invalidator (*this),
202                                 boost::bind (&PatchChangeWidget::bank_changed, this), gui_context ());
203                 program->Changed.connect (_ac_connections, invalidator (*this),
204                                 boost::bind (&PatchChangeWidget::program_changed, this), gui_context ());
205         } else if (boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_route->the_instrument())) {
206                 if (pi->plugin()->knows_bank_patch ()) {
207                         pi->plugin ()->BankPatchChange.connect (_ac_connections, invalidator (*this),
208                                         boost::bind (&PatchChangeWidget::bankpatch_changed, this, _1), gui_context ());
209                 } else {
210                         _no_notifications = true;
211                         // TODO add note: instrument does not report changes.
212                 }
213         }
214
215         refill_banks ();
216 }
217
218 void
219 PatchChangeWidget::refill_banks ()
220 {
221         cancel_audition ();
222         using namespace Menu_Helpers;
223         using namespace Gtkmm2ext;
224
225         _current_patch_bank.reset ();
226         _bank_select.clear_items ();
227
228         const int b = bank (_channel);
229
230         {
231                 PBD::Unwinder<bool> (_ignore_spin_btn_signals, true);
232                 _bank_msb_spin.set_value (b >> 7);
233                 _bank_lsb_spin.set_value (b & 127);
234         }
235
236         boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel);
237         if (cns) {
238                 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
239                         std::string n = (*i)->name ();
240                         _bank_select.AddMenuElem (MenuElemNoMnemonic (n, sigc::bind (sigc::mem_fun (*this, &PatchChangeWidget::select_bank), (*i)->number ())));
241                         if ((*i)->number () == b) {
242                                 _current_patch_bank = *i;
243                                 _bank_select.set_text (n);
244                         }
245                 }
246         }
247
248         if (!_current_patch_bank) {
249                 std::string n = string_compose (_("Bank %1"), b);
250                 _bank_select.AddMenuElem (MenuElemNoMnemonic (n, sigc::bind (sigc::mem_fun (*this, &PatchChangeWidget::select_bank), b)));
251                 _bank_select.set_text (n);
252         }
253
254         refill_program_list ();
255 }
256
257 void
258 PatchChangeWidget::refill_program_list ()
259 {
260         std::bitset<128> unset_notes;
261         unset_notes.set ();
262
263         if (_current_patch_bank) {
264                 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
265                 for (MIDI::Name::PatchNameList::const_iterator i = patches.begin(); i != patches.end(); ++i) {
266                         const std::string n = (*i)->name ();
267                         MIDI::Name::PatchPrimaryKey const& key = (*i)->patch_primary_key ();
268
269                         const uint8_t pgm = key.program();
270                         _program_btn[pgm].set_text (n);
271                         set_tooltip (_program_btn[pgm], string_compose (_("%1 (Pgm-%2)"), n, (int)(pgm +1)));
272                         unset_notes.reset (pgm);
273                 }
274         }
275
276         for (uint8_t pgm = 0; pgm < 128; ++pgm) {
277                 if (!unset_notes.test (pgm)) {
278                         _program_btn[pgm].set_name (X_("patch change button"));
279                         continue;
280                 }
281                 std::string n = string_compose (_("Pgm-%1"), (int)(pgm +1));
282                 _program_btn[pgm].set_text (n);
283                 _program_btn[pgm].set_name (X_("patch change dim button"));
284                 set_tooltip (_program_btn[pgm], n);
285         }
286
287         program_changed ();
288 }
289
290 /* ***** user GUI actions *****/
291
292 void
293 PatchChangeWidget::select_bank_spin ()
294 {
295         if (_ignore_spin_btn_signals) {
296                 return;
297         }
298         const uint32_t b = (_bank_msb_spin.get_value_as_int() << 7) + _bank_lsb_spin.get_value_as_int();
299         select_bank (b);
300 }
301
302 void
303 PatchChangeWidget::select_bank (uint32_t bank)
304 {
305         cancel_audition ();
306
307         if (boost::dynamic_pointer_cast<MidiTrack> (_route)) {
308                 boost::shared_ptr<AutomationControl> bank_msb = _route->automation_control(Evoral::Parameter (MidiCCAutomation, _channel, MIDI_CTL_MSB_BANK), true);
309                 boost::shared_ptr<AutomationControl> bank_lsb = _route->automation_control(Evoral::Parameter (MidiCCAutomation, _channel, MIDI_CTL_LSB_BANK), true);
310
311                 bank_msb->set_value (bank >> 7, PBD::Controllable::NoGroup);
312                 bank_lsb->set_value (bank & 127, PBD::Controllable::NoGroup);
313         } else if (boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_route->the_instrument())) {
314                 uint8_t event[3];
315                 event[0] = (MIDI_CMD_CONTROL | _channel);
316                 event[1] = 0x00;
317                 event[2] = bank >> 7;
318                 pi->write_immediate_event (3, event);
319
320                 event[1] = 0x20;
321                 event[2] = bank & 127;
322                 pi->write_immediate_event (3, event);
323         }
324
325         select_program (program (_channel));
326 }
327
328 void
329 PatchChangeWidget::select_program (uint8_t pgm)
330 {
331         cancel_audition ();
332         if (_no_notifications) {
333                 program_changed ();
334         }
335
336         if (pgm > 127) {
337                 return;
338         }
339
340         if (boost::dynamic_pointer_cast<MidiTrack> (_route)) {
341                 boost::shared_ptr<AutomationControl> program = _route->automation_control(Evoral::Parameter (MidiPgmChangeAutomation, _channel), true);
342                 program->set_value (pgm, PBD::Controllable::NoGroup);
343         } else if (boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_route->the_instrument())) {
344                 uint8_t event[2];
345                 event[0] = (MIDI_CMD_PGM_CHANGE | _channel);
346                 event[1] = pgm;
347                 pi->write_immediate_event (2, event);
348         }
349
350         audition ();
351 }
352
353 /* ***** callbacks, external changes *****/
354
355 void
356 PatchChangeWidget::bank_changed ()
357 {
358         refill_banks ();
359 }
360
361 void
362 PatchChangeWidget::bankpatch_changed (uint8_t chn)
363 {
364         if (chn == _channel) {
365                 refill_banks ();
366         }
367 }
368
369 void
370 PatchChangeWidget::program_changed ()
371 {
372         uint8_t p = program (_channel);
373         for (uint8_t pgm = 0; pgm < 128; ++pgm) {
374                 _program_btn[pgm].set_active (pgm == p);
375         }
376 }
377
378 void
379 PatchChangeWidget::instrument_info_changed ()
380 {
381         refill_banks ();
382 }
383
384 void
385 PatchChangeWidget::processors_changed ()
386 {
387         assert (!boost::dynamic_pointer_cast<MidiTrack> (_route));
388         if (_route->the_instrument ()) {
389                 set_sensitive (true);
390         } else {
391                 set_sensitive (false);
392         }
393 }
394
395 /* ***** play notes *****/
396
397 void
398 PatchChangeWidget::audition_toggle ()
399 {
400         _audition_enable.set_active (!_audition_enable.get_active ());
401         if (_audition_enable.get_active()) {
402                 _audition_start_spin.set_sensitive (true);
403                 _audition_end_spin.set_sensitive (true);
404         } else {
405                 cancel_audition ();
406                 _audition_start_spin.set_sensitive (false);
407                 _audition_end_spin.set_sensitive (false);
408         }
409 }
410
411 void
412 PatchChangeWidget::check_note_range (bool upper)
413 {
414         int s = _audition_start_spin.get_value_as_int ();
415         int e = _audition_end_spin.get_value_as_int ();
416         if (s <= e) {
417                 return;
418         }
419         if (upper) {
420                 _audition_start_spin.set_value (e);
421         } else {
422                 _audition_end_spin.set_value (s);
423         }
424 }
425
426 void
427 PatchChangeWidget::cancel_audition ()
428 {
429         _note_queue_connection.disconnect();
430
431         if (_audition_note_on) {
432                 note_off_event_handler (_audition_note_num);
433                 piano_keyboard_set_note_off (_piano, _audition_note_num);
434         }
435 }
436
437 void
438 PatchChangeWidget::audition ()
439 {
440         if (!boost::dynamic_pointer_cast<MidiTrack> (_route) && !boost::dynamic_pointer_cast<PluginInsert> (_route)) {
441                 return;
442         }
443         if (_channel > 16) {
444                 return;
445         }
446
447         if (_note_queue_connection.connected ()) {
448                 cancel_audition ();
449         }
450
451         if (!_audition_enable.get_active ()) {
452                 return;
453         }
454
455         assert (!_audition_note_on);
456         _audition_note_num = _audition_start_spin.get_value_as_int ();
457
458         _note_queue_connection = Glib::signal_timeout().connect (sigc::bind (sigc::mem_fun (&PatchChangeWidget::audition_next), this), 250);
459 }
460
461 bool
462 PatchChangeWidget::audition_next ()
463 {
464         if (_audition_note_on) {
465                 note_off_event_handler (_audition_note_num);
466                 piano_keyboard_set_note_off (_piano, _audition_note_num);
467                 return ++_audition_note_num <= _audition_end_spin.get_value_as_int() && _audition_enable.get_active ();
468         } else {
469                 note_on_event_handler (_audition_note_num);
470                 piano_keyboard_set_note_on (_piano, _audition_note_num);
471                 return true;
472         }
473 }
474
475 void
476 PatchChangeWidget::_note_on_event_handler(GtkWidget*, int note, gpointer arg)
477 {
478         ((PatchChangeWidget*)arg)->note_on_event_handler(note);
479 }
480
481 void
482 PatchChangeWidget::_note_off_event_handler(GtkWidget*, int note, gpointer arg)
483 {
484         ((PatchChangeWidget*)arg)->note_off_event_handler(note);
485 }
486
487 void
488 PatchChangeWidget::note_on_event_handler (int note)
489 {
490         cancel_audition ();
491         _pianomm->grab_focus ();
492         uint8_t event[3];
493         event[0] = (MIDI_CMD_NOTE_ON | _channel);
494         event[1] = note;
495         event[2] = _audition_velocity.get_value_as_int ();
496         _audition_note_on = true;
497         _audition_note_num = note;
498
499         if (boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (_route)) {
500                 mt->write_immediate_event (3, event);
501         } else if (boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_route->the_instrument())) {
502                 pi->write_immediate_event (3, event);
503         }
504 }
505
506 void
507 PatchChangeWidget::note_off_event_handler (int note)
508 {
509         uint8_t event[3];
510         event[0] = (MIDI_CMD_NOTE_OFF | _channel);
511         event[1] = note;
512         event[2] = 0;
513         _audition_note_on = false;
514
515         if (boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (_route)) {
516                 mt->write_immediate_event (3, event);
517         } else if (boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_route->the_instrument())) {
518                 pi->write_immediate_event (3, event);
519         }
520 }
521
522 /* ***** query info *****/
523
524 int
525 PatchChangeWidget::bank (uint8_t chn) const
526 {
527         if (boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (_route)) {
528                 boost::shared_ptr<AutomationControl> bank_msb = _route->automation_control(Evoral::Parameter (MidiCCAutomation, chn, MIDI_CTL_MSB_BANK), true);
529                 boost::shared_ptr<AutomationControl> bank_lsb = _route->automation_control(Evoral::Parameter (MidiCCAutomation, chn, MIDI_CTL_LSB_BANK), true);
530
531                 return ((int)bank_msb->get_value () << 7) + (int)bank_lsb->get_value();
532         } else if (boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_route->the_instrument())) {
533                 uint32_t bankpatch = pi->plugin()->bank_patch (chn);
534                 if (bankpatch != UINT32_MAX) {
535                         return bankpatch >> 7;
536                 }
537         }
538         return 0;
539 }
540
541 uint8_t
542 PatchChangeWidget::program (uint8_t chn) const
543 {
544         if (boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (_route)) {
545                 boost::shared_ptr<AutomationControl> program = _route->automation_control(Evoral::Parameter (MidiPgmChangeAutomation, chn), true);
546                 return program->get_value();
547         } else if (boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (_route->the_instrument())) {
548                 uint32_t bankpatch = pi->plugin()->bank_patch (chn);
549                 if (bankpatch != UINT32_MAX) {
550                         return bankpatch & 127;
551                 }
552         }
553         return 255;
554 }
555
556 /* ***************************************************************************/
557
558 PatchChangeGridDialog::PatchChangeGridDialog (boost::shared_ptr<ARDOUR::Route> r)
559         : ArdourDialog (string_compose (_("Select Patch for '%1"), r->name()), false, false)
560         , w (r)
561 {
562         r->PropertyChanged.connect (_route_connection, invalidator (*this), boost::bind (&PatchChangeGridDialog::route_property_changed, this, _1, boost::weak_ptr<Route>(r)), gui_context());
563         get_vbox()->add (w);
564         w.show ();
565 }
566
567 void
568 PatchChangeGridDialog::route_property_changed (const PBD::PropertyChange& what_changed, boost::weak_ptr<Route> wr)
569 {
570         boost::shared_ptr<ARDOUR::Route> r = wr.lock ();
571         if (r && what_changed.contains (ARDOUR::Properties::name)) {
572                 set_title (string_compose (_("Select Patch for '%1"), r->name()));
573         }
574 }