2 * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2011-2015 David Robillard <d@drobilla.net>
4 * Copyright (C) 2011-2017 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <gtkmm/stock.h>
23 #include <gtkmm/table.h>
25 #include <boost/algorithm/string.hpp>
27 #include "gtkmm2ext/utils.h"
29 #include "midi++/midnam_patch.h"
31 #include "ardour/beats_samples_converter.h"
32 #include "ardour/instrument_info.h"
34 #include "patch_change_dialog.h"
35 #include "gui_thread.h"
41 using namespace Gtkmm2ext;
43 /** @param tc If non-0, a time converter for this patch change. If 0, time control will be desensitized */
44 PatchChangeDialog::PatchChangeDialog (
45 const ARDOUR::BeatsSamplesConverter* tc,
46 ARDOUR::Session* session,
47 Evoral::PatchChange<Temporal::Beats> const& patch,
48 ARDOUR::InstrumentInfo& info,
49 const Gtk::BuiltinStockID& ok,
52 : ArdourDialog (_("Patch Change"), modal)
53 , _time_converter (tc)
55 , _time (X_("patchchangetime"), true, "", true, false)
56 , _channel (*manage (new Adjustment (1, 1, 16, 1, 4)))
57 , _program (*manage (new Adjustment (1, 1, 128, 1, 16)))
58 , _bank_msb (*manage (new Adjustment (0, 0, 127, 1, 16)))
59 , _bank_lsb (*manage (new Adjustment (0, 0, 127, 1, 16)))
60 , _ignore_signals (false)
63 Table* t = manage (new Table (4, 2));
68 if (_time_converter) {
70 l = manage (left_aligned_label (_("Time")));
71 t->attach (*l, 0, 1, r, r + 1);
72 t->attach (_time, 1, 2, r, r + 1);
75 _time.set_session (session);
76 _time.set_mode (AudioClock::BBT);
77 _time.set (_time_converter->to (patch.time ()), true);
80 l = manage (left_aligned_label (_("Patch Bank")));
81 t->attach (*l, 0, 1, r, r + 1);
82 t->attach (_bank_combo, 1, 2, r, r + 1);
85 _bank_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_combo_changed));
87 l = manage (left_aligned_label (_("Patch")));
88 t->attach (*l, 0, 1, r, r + 1);
89 t->attach (_patch_combo, 1, 2, r, r + 1);
92 _patch_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::patch_combo_changed));
94 l = manage (left_aligned_label (_("Channel")));
95 t->attach (*l, 0, 1, r, r + 1);
96 t->attach (_channel, 1, 2, r, r + 1);
99 _channel.set_value (patch.channel() + 1);
100 _channel.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::channel_changed));
102 l = manage (left_aligned_label (_("Program")));
103 t->attach (*l, 0, 1, r, r + 1);
104 t->attach (_program, 1, 2, r, r + 1);
107 _program.set_value (patch.program () + 1);
108 _program.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::program_changed));
110 l = manage (left_aligned_label (_("Bank MSB")));
111 t->attach (*l, 0, 1, r, r + 1);
112 t->attach (_bank_msb, 1, 2, r, r + 1);
115 l = manage (left_aligned_label (_("Bank LSB")));
116 t->attach (*l, 0, 1, r, r + 1);
117 t->attach (_bank_lsb, 1, 2, r, r + 1);
120 assert (patch.bank() != UINT16_MAX);
122 _bank_msb.set_value ((patch.bank() >> 7));
123 _bank_msb.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_changed));
124 _bank_lsb.set_value ((patch.bank() & 127));
125 _bank_lsb.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_changed));
127 get_vbox()->add (*t);
130 add_button (Stock::CANCEL, RESPONSE_CANCEL);
132 add_button (ok, RESPONSE_ACCEPT);
134 add_button (Gtk::StockID(GTK_STOCK_DELETE), RESPONSE_REJECT);
136 set_default_response (RESPONSE_ACCEPT);
139 set_active_bank_combo ();
140 bank_combo_changed ();
142 _info.Changed.connect (_info_changed_connection, invalidator (*this),
143 boost::bind (&PatchChangeDialog::instrument_info_changed, this), gui_context());
149 PatchChangeDialog::on_response (int response_id)
152 Gtk::Dialog::on_response (response_id);
154 ArdourDialog::on_response (response_id);
159 PatchChangeDialog::get_14bit_bank () const
161 return (_bank_msb.get_value_as_int() << 7) + _bank_lsb.get_value_as_int();
165 PatchChangeDialog::instrument_info_changed ()
167 _bank_combo.clear ();
168 _patch_combo.clear ();
173 Evoral::PatchChange<Temporal::Beats>
174 PatchChangeDialog::patch () const
176 Temporal::Beats t = Temporal::Beats();
178 if (_time_converter) {
179 t = _time_converter->from (_time.current_time ());
182 return Evoral::PatchChange<Temporal::Beats> (
184 _channel.get_value_as_int() - 1,
185 _program.get_value_as_int() - 1,
190 /** Fill the bank_combo according to the current _channel */
192 PatchChangeDialog::fill_bank_combo ()
194 _bank_combo.clear ();
196 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
202 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
203 string n = (*i)->name ();
204 boost::replace_all (n, "_", " ");
205 _bank_combo.append_text (n);
209 /** Set the active value of the bank_combo, and _current_patch_bank, from the contents of _bank */
211 PatchChangeDialog::set_active_bank_combo ()
213 _current_patch_bank.reset ();
215 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
221 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
223 string n = (*i)->name ();
224 boost::replace_all (n, "_", " ");
226 if ((*i)->number() == get_14bit_bank()) {
227 _current_patch_bank = *i;
228 _ignore_signals = true;
229 _bank_combo.set_active_text (n);
230 _ignore_signals = false;
235 _ignore_signals = true;
236 _bank_combo.set_active (-1);
237 _ignore_signals = false;
240 /** Update _current_patch_bank and reflect the current value of
241 * bank_combo in the rest of the dialog.
244 PatchChangeDialog::bank_combo_changed ()
246 if (_ignore_signals) {
250 _current_patch_bank.reset ();
252 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
258 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
259 string n = (*i)->name ();
260 boost::replace_all (n, "_", " ");
261 if (n == _bank_combo.get_active_text()) {
262 _current_patch_bank = *i;
266 if (_current_patch_bank == 0) {
273 set_active_patch_combo ();
275 if (_current_patch_bank->number() != UINT16_MAX) {
276 _ignore_signals = true;
277 _bank_msb.set_value (_current_patch_bank->number() >> 7);
278 _bank_lsb.set_value (_current_patch_bank->number() & 127);
279 _ignore_signals = false;
283 /** Fill the contents of the patch combo */
285 PatchChangeDialog::fill_patch_combo ()
287 _patch_combo.clear ();
289 if (_current_patch_bank == 0) {
293 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
294 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
295 string n = (*j)->name ();
296 boost::replace_all (n, "_", " ");
297 _patch_combo.append_text (n);
301 /** Set the active value of the patch combo from the value of the _program entry */
303 PatchChangeDialog::set_active_patch_combo ()
305 if (_ignore_signals) {
309 if (_current_patch_bank == 0) {
310 _ignore_signals = true;
311 _patch_combo.set_active (-1);
312 _ignore_signals = false;
316 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
317 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
318 string n = (*j)->name ();
319 boost::replace_all (n, "_", " ");
321 MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
322 if (key.program() == _program.get_value() - 1) {
323 _ignore_signals = true;
324 _patch_combo.set_active_text (n);
325 _ignore_signals = false;
330 _ignore_signals = true;
331 _patch_combo.set_active (-1);
332 _ignore_signals = false;
335 /** Set _program from the current state of _patch_combo */
337 PatchChangeDialog::patch_combo_changed ()
339 if (_ignore_signals || _current_patch_bank == 0) {
343 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
345 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
346 string n = (*j)->name ();
347 boost::replace_all (n, "_", " ");
349 if (n == _patch_combo.get_active_text ()) {
350 _ignore_signals = true;
351 _program.set_value ((*j)->program_number() + 1);
352 _bank_msb.set_value ((*j)->bank_number() >> 7);
353 _bank_lsb.set_value ((*j)->bank_number() & 127);
354 _ignore_signals = false;
361 PatchChangeDialog::channel_changed ()
364 set_active_bank_combo ();
366 set_active_patch_combo ();
370 PatchChangeDialog::program_changed ()
372 if (_ignore_signals) {
376 set_active_patch_combo ();
380 PatchChangeDialog::bank_changed ()
382 if (_ignore_signals) {
386 set_active_bank_combo ();
388 set_active_patch_combo ();