2 * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2011 Paul Davis
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.
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.
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.
20 #include <gtkmm/frame.h>
21 #include <boost/algorithm/string.hpp>
23 #include "pbd/unwind.h"
25 #include "evoral/midi_events.h"
26 #include "evoral/PatchChange.hpp"
28 #include "midi++/midnam_patch.h"
30 #include "ardour/instrument_info.h"
31 #include "ardour/midi_track.h"
33 #include "widgets/tooltips.h"
35 #include "gui_thread.h"
36 #include "patch_change_widget.h"
37 #include "ui_config.h"
42 using namespace ARDOUR;
44 PatchChangeWidget::PatchChangeWidget (boost::shared_ptr<ARDOUR::Route> r)
46 , _bank_msb_spin (*manage (new Adjustment (0, 0, 127, 1, 16)))
47 , _bank_lsb_spin (*manage (new Adjustment (0, 0, 127, 1, 16)))
48 , _program_table (/*rows*/ 16, /*cols*/ 8, true)
50 , _ignore_spin_btn_signals (false)
51 , _info (r->instrument_info ())
52 , _audition_enable (_("Audition on Change"), ArdourWidgets::ArdourButton::led_default_elements)
53 , _audition_start_spin (*manage (new Adjustment (48, 0, 127, 1, 16)))
54 , _audition_end_spin (*manage (new Adjustment (60, 0, 127, 1, 16)))
55 , _audition_velocity (*manage (new Adjustment (100, 1, 127, 1, 16)))
56 , _audition_note_on (false)
57 , _piano ((PianoKeyboard*)piano_keyboard_new())
58 , _pianomm (Glib::wrap((GtkWidget*)_piano))
61 box = manage (new HBox ());
62 box->set_border_width (2);
64 box->pack_start (*manage (new Label (_("Channel:"))), false, false);
65 box->pack_start (_channel_select, false, false);
66 box->pack_start (*manage (new Label (_("Bank:"))), false, false);
67 box->pack_start (_bank_select, true, true);
68 box->pack_start (*manage (new Label (_("MSB:"))), false, false);
69 box->pack_start (_bank_msb_spin, false, false);
70 box->pack_start (*manage (new Label (_("LSB:"))), false, false);
71 box->pack_start (_bank_lsb_spin, false, false);
73 pack_start (*box, false, false);
75 _program_table.set_spacings (1);
76 pack_start (_program_table, true, true);
78 if (boost::dynamic_pointer_cast<MidiTrack> (_route)) {
79 box = manage (new HBox ());
81 box->pack_start (_audition_enable, false, false);
82 box->pack_start (*manage (new Label (_("Start Note:"))), false, false);
83 box->pack_start (_audition_start_spin, false, false);
84 box->pack_start (*manage (new Label (_("End Note:"))), false, false);
85 box->pack_start (_audition_end_spin, false, false);
86 box->pack_start (*manage (new Label (_("Velocity:"))), false, false);
87 box->pack_start (_audition_velocity, false, false);
89 Box* box2 = manage (new HBox ());
90 box2->pack_start (*box, true, false);
91 box2->set_border_width (2);
92 pack_start (*box2, false, false);
95 for (uint8_t pgm = 0; pgm < 128; ++pgm) {
96 _program_btn[pgm].set_text_ellipsize (Pango::ELLIPSIZE_END);
97 _program_btn[pgm].set_layout_ellipsize_width (PANGO_SCALE * 112 * UIConfiguration::instance ().get_ui_scale ());
100 _program_table.attach (_program_btn[pgm], col, col + 1, row, row + 1);
101 _program_btn[pgm].signal_clicked.connect (sigc::bind (sigc::mem_fun (*this, &PatchChangeWidget::select_program), pgm));
104 using namespace Menu_Helpers;
105 for (uint32_t chn = 0; chn < 16; ++chn) {
107 snprintf (buf, sizeof(buf), "%d", chn + 1);
108 _channel_select.AddMenuElem (MenuElem (buf, sigc::bind (sigc::mem_fun (*this, &PatchChangeWidget::select_channel), chn)));
111 if (boost::dynamic_pointer_cast<MidiTrack> (_route)) {
112 piano_keyboard_set_monophonic (_piano, TRUE);
113 g_signal_connect (G_OBJECT (_piano), "note-on", G_CALLBACK (PatchChangeWidget::_note_on_event_handler), this);
114 g_signal_connect (G_OBJECT (_piano), "note-off", G_CALLBACK (PatchChangeWidget::_note_off_event_handler), this);
115 _pianomm->set_flags(Gtk::CAN_FOCUS);
116 pack_start (*_pianomm, false, false);
119 _audition_start_spin.set_sensitive (false);
120 _audition_end_spin.set_sensitive (false);
122 _audition_enable.signal_clicked.connect (sigc::mem_fun (*this, &PatchChangeWidget::audition_toggle));
123 _audition_start_spin.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &PatchChangeWidget::check_note_range), false));
124 _audition_end_spin.signal_changed().connect (sigc::bind (sigc::mem_fun (*this, &PatchChangeWidget::check_note_range), true));
125 _bank_msb_spin.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeWidget::select_bank_spin));
126 _bank_lsb_spin.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeWidget::select_bank_spin));
128 _info.Changed.connect (_info_changed_connection, invalidator (*this),
129 boost::bind (&PatchChangeWidget::instrument_info_changed, this), gui_context());
135 PatchChangeWidget::~PatchChangeWidget ()
142 PatchChangeWidget::on_show ()
144 Gtk::VBox::on_show ();
151 PatchChangeWidget::on_hide ()
153 Gtk::VBox::on_hide ();
154 _ac_connections.drop_connections ();
159 PatchChangeWidget::select_channel (uint8_t chn)
164 if (_channel == chn) {
170 _channel_select.set_text (string_compose ("%1", (int)(chn + 1)));
173 _ac_connections.drop_connections ();
175 boost::shared_ptr<AutomationControl> bank_msb = _route->automation_control(Evoral::Parameter (MidiCCAutomation, chn, MIDI_CTL_MSB_BANK), true);
176 boost::shared_ptr<AutomationControl> bank_lsb = _route->automation_control(Evoral::Parameter (MidiCCAutomation, chn, MIDI_CTL_LSB_BANK), true);
177 boost::shared_ptr<AutomationControl> program = _route->automation_control(Evoral::Parameter (MidiPgmChangeAutomation, chn), true);
179 bank_msb->Changed.connect (_ac_connections, invalidator (*this),
180 boost::bind (&PatchChangeWidget::bank_changed, this), gui_context ());
181 bank_lsb->Changed.connect (_ac_connections, invalidator (*this),
182 boost::bind (&PatchChangeWidget::bank_changed, this), gui_context ());
183 program->Changed.connect (_ac_connections, invalidator (*this),
184 boost::bind (&PatchChangeWidget::program_changed, this), gui_context ());
190 PatchChangeWidget::refill_banks ()
193 using namespace Menu_Helpers;
195 _current_patch_bank.reset ();
196 _bank_select.clear_items ();
198 const int b = bank (_channel);
201 PBD::Unwinder<bool> (_ignore_spin_btn_signals, true);
202 _bank_msb_spin.set_value (b >> 7);
203 _bank_lsb_spin.set_value (b & 127);
206 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel);
208 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
209 std::string n = (*i)->name ();
210 boost::replace_all (n, "_", " ");
211 _bank_select.AddMenuElem (MenuElem (n, sigc::bind (sigc::mem_fun (*this, &PatchChangeWidget::select_bank), (*i)->number ())));
212 if ((*i)->number () == b) {
213 _current_patch_bank = *i;
214 _bank_select.set_text (n);
219 if (!_current_patch_bank) {
220 std::string n = string_compose (_("Bank %1"), b);
221 _bank_select.AddMenuElem (MenuElem (n, sigc::bind (sigc::mem_fun (*this, &PatchChangeWidget::select_bank), b)));
222 _bank_select.set_text (n);
225 refill_program_list ();
229 PatchChangeWidget::refill_program_list ()
231 // TODO if _current_patch_bank!=0, only clear/reset unused patches
232 for (uint8_t pgm = 0; pgm < 128; ++pgm) {
233 std::string n = string_compose (_("Pgm-%1"), (int)(pgm +1));
234 _program_btn[pgm].set_text (n);
235 set_tooltip (_program_btn[pgm], n);
238 if (_current_patch_bank) {
239 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
240 for (MIDI::Name::PatchNameList::const_iterator i = patches.begin(); i != patches.end(); ++i) {
241 std::string n = (*i)->name ();
242 boost::replace_all (n, "_", " ");
243 MIDI::Name::PatchPrimaryKey const& key = (*i)->patch_primary_key ();
245 const uint8_t pgm = key.program();
246 _program_btn[pgm].set_text (n);
247 set_tooltip (_program_btn[pgm], string_compose (_("%1 (Pgm-%2)"), n, (int)(pgm +1)));
254 /* ***** user GUI actions *****/
257 PatchChangeWidget::select_bank_spin ()
259 if (_ignore_spin_btn_signals) {
262 const uint32_t b = (_bank_msb_spin.get_value_as_int() << 7) + _bank_lsb_spin.get_value_as_int();
267 PatchChangeWidget::select_bank (uint32_t bank)
271 boost::shared_ptr<AutomationControl> bank_msb = _route->automation_control(Evoral::Parameter (MidiCCAutomation, _channel, MIDI_CTL_MSB_BANK), true);
272 boost::shared_ptr<AutomationControl> bank_lsb = _route->automation_control(Evoral::Parameter (MidiCCAutomation, _channel, MIDI_CTL_LSB_BANK), true);
274 bank_msb->set_value (bank >> 7, PBD::Controllable::NoGroup);
275 bank_lsb->set_value (bank & 127, PBD::Controllable::NoGroup);
279 PatchChangeWidget::select_program (uint8_t pgm)
283 boost::shared_ptr<AutomationControl> program = _route->automation_control(Evoral::Parameter (MidiPgmChangeAutomation, _channel), true);
284 program->set_value (pgm, PBD::Controllable::NoGroup);
289 /* ***** callbacks, external changes *****/
292 PatchChangeWidget::bank_changed ()
294 // TODO optimize, just find the bank, set the text and refill_program_list()
299 PatchChangeWidget::program_changed ()
301 uint8_t p = program (_channel);
302 for (uint8_t pgm = 0; pgm < 128; ++pgm) {
303 _program_btn[pgm].set_active (pgm == p);
308 PatchChangeWidget::instrument_info_changed ()
313 /* ***** play notes *****/
316 PatchChangeWidget::audition_toggle ()
318 _audition_enable.set_active (!_audition_enable.get_active ());
319 if (_audition_enable.get_active()) {
320 _audition_start_spin.set_sensitive (true);
321 _audition_end_spin.set_sensitive (true);
324 _audition_start_spin.set_sensitive (false);
325 _audition_end_spin.set_sensitive (false);
330 PatchChangeWidget::check_note_range (bool upper)
332 int s = _audition_start_spin.get_value_as_int ();
333 int e = _audition_end_spin.get_value_as_int ();
338 _audition_start_spin.set_value (e);
340 _audition_end_spin.set_value (s);
345 PatchChangeWidget::cancel_audition ()
347 _note_queue_connection.disconnect();
349 if (_audition_note_on) {
350 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (_route);
353 event[0] = (MIDI_CMD_NOTE_OFF | _channel);
354 event[1] = _audition_note_num;
356 mt->write_immediate_event (3, event);
357 piano_keyboard_set_note_off (_piano, _audition_note_num);
359 _audition_note_on = false;
363 PatchChangeWidget::audition ()
365 if (!boost::dynamic_pointer_cast<MidiTrack> (_route)) {
372 if (_note_queue_connection.connected ()) {
376 if (!_audition_enable.get_active ()) {
380 assert (!_audition_note_on);
381 _audition_note_num = _audition_start_spin.get_value_as_int ();
383 _note_queue_connection = Glib::signal_timeout().connect (sigc::bind (sigc::mem_fun (&PatchChangeWidget::audition_next), this), 250);
387 PatchChangeWidget::audition_next ()
389 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (_route);
392 if (_audition_note_on) {
393 event[0] = (MIDI_CMD_NOTE_OFF | _channel);
394 event[1] = _audition_note_num;
396 mt->write_immediate_event (3, event);
397 _audition_note_on = false;
398 piano_keyboard_set_note_off (_piano, _audition_note_num);
399 return ++_audition_note_num <= _audition_end_spin.get_value_as_int() && _audition_enable.get_active ();
401 event[0] = (MIDI_CMD_NOTE_ON | _channel);
402 event[1] = _audition_note_num;
403 event[2] = _audition_velocity.get_value_as_int ();
404 mt->write_immediate_event (3, event);
405 _audition_note_on = true;
406 piano_keyboard_set_note_on (_piano, _audition_note_num);
412 PatchChangeWidget::_note_on_event_handler(GtkWidget*, int note, gpointer arg)
414 ((PatchChangeWidget*)arg)->note_on_event_handler(note);
418 PatchChangeWidget::_note_off_event_handler(GtkWidget*, int note, gpointer arg)
420 ((PatchChangeWidget*)arg)->note_off_event_handler(note);
424 PatchChangeWidget::note_on_event_handler (int note)
427 _pianomm->grab_focus ();
428 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (_route);
430 event[0] = (MIDI_CMD_NOTE_ON | _channel);
432 event[2] = _audition_velocity.get_value_as_int ();
433 mt->write_immediate_event (3, event);
434 _audition_note_on = true;
435 _audition_note_num = note;
439 PatchChangeWidget::note_off_event_handler (int note)
441 boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (_route);
443 event[0] = (MIDI_CMD_NOTE_OFF | _channel);
446 mt->write_immediate_event (3, event);
447 _audition_note_on = false;
450 /* ***** query info *****/
453 PatchChangeWidget::bank (uint8_t chn) const
455 boost::shared_ptr<AutomationControl> bank_msb = _route->automation_control(Evoral::Parameter (MidiCCAutomation, chn, MIDI_CTL_MSB_BANK), true);
456 boost::shared_ptr<AutomationControl> bank_lsb = _route->automation_control(Evoral::Parameter (MidiCCAutomation, chn, MIDI_CTL_LSB_BANK), true);
458 return ((int)bank_msb->get_value () << 7) + (int)bank_lsb->get_value();
462 PatchChangeWidget::program (uint8_t chn) const
464 boost::shared_ptr<AutomationControl> program = _route->automation_control(Evoral::Parameter (MidiPgmChangeAutomation, chn), true);
465 return program->get_value();
468 /* ***************************************************************************/
470 PatchChangeGridDialog::PatchChangeGridDialog (std::string const& title, boost::shared_ptr<ARDOUR::Route> r)
471 : ArdourDialog (title, false, false)