2 Copyright (C) 2010 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <gtkmm/stock.h>
22 #include <gtkmm/table.h>
24 #include <boost/algorithm/string.hpp>
26 #include "gtkmm2ext/utils.h"
28 #include "midi++/midnam_patch.h"
30 #include "ardour/beats_samples_converter.h"
31 #include "ardour/instrument_info.h"
33 #include "patch_change_dialog.h"
34 #include "gui_thread.h"
40 using namespace Gtkmm2ext;
42 /** @param tc If non-0, a time converter for this patch change. If 0, time control will be desensitized */
43 PatchChangeDialog::PatchChangeDialog (
44 const ARDOUR::BeatsSamplesConverter* tc,
45 ARDOUR::Session* session,
46 Evoral::PatchChange<Temporal::Beats> const & patch,
47 ARDOUR::InstrumentInfo& info,
48 const Gtk::BuiltinStockID& ok,
51 : ArdourDialog (_("Patch Change"), modal)
52 , _time_converter (tc)
54 , _time (X_("patchchangetime"), true, "", true, false)
55 , _channel (*manage (new Adjustment (1, 1, 16, 1, 4)))
56 , _program (*manage (new Adjustment (1, 1, 128, 1, 16)))
57 , _bank_msb (*manage (new Adjustment (0, 0, 127, 1, 16)))
58 , _bank_lsb (*manage (new Adjustment (0, 0, 127, 1, 16)))
59 , _ignore_signals (false)
62 Table* t = manage (new Table (4, 2));
67 if (_time_converter) {
69 l = manage (left_aligned_label (_("Time")));
70 t->attach (*l, 0, 1, r, r + 1);
71 t->attach (_time, 1, 2, r, r + 1);
74 _time.set_session (session);
75 _time.set_mode (AudioClock::BBT);
76 _time.set (_time_converter->to (patch.time ()), true);
79 l = manage (left_aligned_label (_("Patch Bank")));
80 t->attach (*l, 0, 1, r, r + 1);
81 t->attach (_bank_combo, 1, 2, r, r + 1);
84 _bank_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_combo_changed));
86 l = manage (left_aligned_label (_("Patch")));
87 t->attach (*l, 0, 1, r, r + 1);
88 t->attach (_patch_combo, 1, 2, r, r + 1);
91 _patch_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::patch_combo_changed));
93 l = manage (left_aligned_label (_("Channel")));
94 t->attach (*l, 0, 1, r, r + 1);
95 t->attach (_channel, 1, 2, r, r + 1);
98 _channel.set_value (patch.channel() + 1);
99 _channel.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::channel_changed));
101 l = manage (left_aligned_label (_("Program")));
102 t->attach (*l, 0, 1, r, r + 1);
103 t->attach (_program, 1, 2, r, r + 1);
106 _program.set_value (patch.program () + 1);
107 _program.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::program_changed));
109 l = manage (left_aligned_label (_("Bank MSB")));
110 t->attach (*l, 0, 1, r, r + 1);
111 t->attach (_bank_msb, 1, 2, r, r + 1);
114 l = manage (left_aligned_label (_("Bank LSB")));
115 t->attach (*l, 0, 1, r, r + 1);
116 t->attach (_bank_lsb, 1, 2, r, r + 1);
119 assert (patch.bank() != UINT16_MAX);
121 _bank_msb.set_value ((patch.bank() >> 7));
122 _bank_msb.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_changed));
123 _bank_lsb.set_value ((patch.bank() & 127));
124 _bank_lsb.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_changed));
126 get_vbox()->add (*t);
129 add_button (Stock::CANCEL, RESPONSE_CANCEL);
131 add_button (ok, RESPONSE_ACCEPT);
133 add_button (Gtk::StockID(GTK_STOCK_DELETE), RESPONSE_REJECT);
135 set_default_response (RESPONSE_ACCEPT);
138 set_active_bank_combo ();
139 bank_combo_changed ();
141 _info.Changed.connect (_info_changed_connection, invalidator (*this),
142 boost::bind (&PatchChangeDialog::instrument_info_changed, this), gui_context());
148 PatchChangeDialog::on_response (int response_id)
151 Gtk::Dialog::on_response (response_id);
153 ArdourDialog::on_response (response_id);
158 PatchChangeDialog::get_14bit_bank () const
160 return (_bank_msb.get_value_as_int() << 7) + _bank_lsb.get_value_as_int();
164 PatchChangeDialog::instrument_info_changed ()
166 _bank_combo.clear ();
167 _patch_combo.clear ();
172 Evoral::PatchChange<Temporal::Beats>
173 PatchChangeDialog::patch () const
175 Temporal::Beats t = Temporal::Beats();
177 if (_time_converter) {
178 t = _time_converter->from (_time.current_time ());
181 return Evoral::PatchChange<Temporal::Beats> (
183 _channel.get_value_as_int() - 1,
184 _program.get_value_as_int() - 1,
189 /** Fill the bank_combo according to the current _channel */
191 PatchChangeDialog::fill_bank_combo ()
193 _bank_combo.clear ();
195 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
201 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
202 string n = (*i)->name ();
203 boost::replace_all (n, "_", " ");
204 _bank_combo.append_text (n);
208 /** Set the active value of the bank_combo, and _current_patch_bank, from the contents of _bank */
210 PatchChangeDialog::set_active_bank_combo ()
212 _current_patch_bank.reset ();
214 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
220 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
222 string n = (*i)->name ();
223 boost::replace_all (n, "_", " ");
225 if ((*i)->number() == get_14bit_bank()) {
226 _current_patch_bank = *i;
227 _ignore_signals = true;
228 _bank_combo.set_active_text (n);
229 _ignore_signals = false;
234 _ignore_signals = true;
235 _bank_combo.set_active (-1);
236 _ignore_signals = false;
239 /** Update _current_patch_bank and reflect the current value of
240 * bank_combo in the rest of the dialog.
243 PatchChangeDialog::bank_combo_changed ()
245 if (_ignore_signals) {
249 _current_patch_bank.reset ();
251 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
257 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
258 string n = (*i)->name ();
259 boost::replace_all (n, "_", " ");
260 if (n == _bank_combo.get_active_text()) {
261 _current_patch_bank = *i;
265 if (_current_patch_bank == 0) {
272 set_active_patch_combo ();
274 if (_current_patch_bank->number() != UINT16_MAX) {
275 _ignore_signals = true;
276 _bank_msb.set_value (_current_patch_bank->number() >> 7);
277 _bank_lsb.set_value (_current_patch_bank->number() & 127);
278 _ignore_signals = false;
282 /** Fill the contents of the patch combo */
284 PatchChangeDialog::fill_patch_combo ()
286 _patch_combo.clear ();
288 if (_current_patch_bank == 0) {
292 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
293 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
294 string n = (*j)->name ();
295 boost::replace_all (n, "_", " ");
296 _patch_combo.append_text (n);
300 /** Set the active value of the patch combo from the value of the _program entry */
302 PatchChangeDialog::set_active_patch_combo ()
304 if (_ignore_signals) {
308 if (_current_patch_bank == 0) {
309 _ignore_signals = true;
310 _patch_combo.set_active (-1);
311 _ignore_signals = false;
315 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
316 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
317 string n = (*j)->name ();
318 boost::replace_all (n, "_", " ");
320 MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
321 if (key.program() == _program.get_value() - 1) {
322 _ignore_signals = true;
323 _patch_combo.set_active_text (n);
324 _ignore_signals = false;
329 _ignore_signals = true;
330 _patch_combo.set_active (-1);
331 _ignore_signals = false;
334 /** Set _program from the current state of _patch_combo */
336 PatchChangeDialog::patch_combo_changed ()
338 if (_ignore_signals || _current_patch_bank == 0) {
342 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
344 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
345 string n = (*j)->name ();
346 boost::replace_all (n, "_", " ");
348 if (n == _patch_combo.get_active_text ()) {
349 _ignore_signals = true;
350 _program.set_value ((*j)->program_number() + 1);
351 _bank_msb.set_value ((*j)->bank_number() >> 7);
352 _bank_lsb.set_value ((*j)->bank_number() & 127);
353 _ignore_signals = false;
360 PatchChangeDialog::channel_changed ()
363 set_active_bank_combo ();
365 set_active_patch_combo ();
369 PatchChangeDialog::program_changed ()
371 if (_ignore_signals) {
375 set_active_patch_combo ();
379 PatchChangeDialog::bank_changed ()
381 if (_ignore_signals) {
385 set_active_bank_combo ();
387 set_active_patch_combo ();