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_frames_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::BeatsFramesConverter* tc,
45 ARDOUR::Session* session,
46 Evoral::PatchChange<Evoral::Beats> const & patch,
47 ARDOUR::InstrumentInfo& info,
48 const Gtk::BuiltinStockID& ok,
50 : ArdourDialog (_("Patch Change"), true)
51 , _time_converter (tc)
53 , _time (X_("patchchangetime"), true, "", true, false)
54 , _channel (*manage (new Adjustment (1, 1, 16, 1, 4)))
55 , _program (*manage (new Adjustment (1, 1, 128, 1, 16)))
56 , _bank_msb (*manage (new Adjustment (0, 0, 127, 1, 16)))
57 , _bank_lsb (*manage (new Adjustment (0, 0, 127, 1, 16)))
58 , _ignore_signals (false)
60 Table* t = manage (new Table (4, 2));
65 if (_time_converter) {
67 l = manage (left_aligned_label (_("Time")));
68 t->attach (*l, 0, 1, r, r + 1);
69 t->attach (_time, 1, 2, r, r + 1);
72 _time.set_session (session);
73 _time.set_mode (AudioClock::BBT);
74 _time.set (_time_converter->to (patch.time ()), true);
77 l = manage (left_aligned_label (_("Patch Bank")));
78 t->attach (*l, 0, 1, r, r + 1);
79 t->attach (_bank_combo, 1, 2, r, r + 1);
82 _bank_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_combo_changed));
84 l = manage (left_aligned_label (_("Patch")));
85 t->attach (*l, 0, 1, r, r + 1);
86 t->attach (_patch_combo, 1, 2, r, r + 1);
89 _patch_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::patch_combo_changed));
91 l = manage (left_aligned_label (_("Channel")));
92 t->attach (*l, 0, 1, r, r + 1);
93 t->attach (_channel, 1, 2, r, r + 1);
96 _channel.set_value (patch.channel() + 1);
97 _channel.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::channel_changed));
99 l = manage (left_aligned_label (_("Program")));
100 t->attach (*l, 0, 1, r, r + 1);
101 t->attach (_program, 1, 2, r, r + 1);
104 _program.set_value (patch.program () + 1);
105 _program.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::program_changed));
107 l = manage (left_aligned_label (_("Bank MSB")));
108 t->attach (*l, 0, 1, r, r + 1);
109 t->attach (_bank_msb, 1, 2, r, r + 1);
112 l = manage (left_aligned_label (_("Bank LSB")));
113 t->attach (*l, 0, 1, r, r + 1);
114 t->attach (_bank_lsb, 1, 2, r, r + 1);
117 _bank_msb.set_value ((patch.bank() >> 7));
118 _bank_msb.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_changed));
120 _bank_lsb.set_value ((patch.bank() & 127));
121 _bank_lsb.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_changed));
123 get_vbox()->add (*t);
125 add_button (Stock::CANCEL, RESPONSE_CANCEL);
126 add_button (ok, RESPONSE_ACCEPT);
128 add_button (Gtk::StockID(GTK_STOCK_DELETE), RESPONSE_REJECT);
130 set_default_response (RESPONSE_ACCEPT);
133 set_active_bank_combo ();
134 bank_combo_changed ();
136 _info.Changed.connect (_info_changed_connection, invalidator (*this),
137 boost::bind (&PatchChangeDialog::instrument_info_changed, this), gui_context());
143 PatchChangeDialog::get_14bit_bank () const
145 return (_bank_msb.get_value_as_int() << 7) + _bank_lsb.get_value_as_int();
149 PatchChangeDialog::instrument_info_changed ()
151 _bank_combo.clear ();
152 _patch_combo.clear ();
157 Evoral::PatchChange<Evoral::Beats>
158 PatchChangeDialog::patch () const
160 Evoral::Beats t = Evoral::Beats();
162 if (_time_converter) {
163 t = _time_converter->from (_time.current_time ());
166 return Evoral::PatchChange<Evoral::Beats> (
168 _channel.get_value_as_int() - 1,
169 _program.get_value_as_int() - 1,
174 /** Fill the bank_combo according to the current _channel */
176 PatchChangeDialog::fill_bank_combo ()
178 _bank_combo.clear ();
180 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
186 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
187 string n = (*i)->name ();
188 boost::replace_all (n, "_", " ");
189 _bank_combo.append_text (n);
193 /** Set the active value of the bank_combo, and _current_patch_bank, from the contents of _bank */
195 PatchChangeDialog::set_active_bank_combo ()
197 _current_patch_bank.reset ();
199 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
205 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
207 string n = (*i)->name ();
208 boost::replace_all (n, "_", " ");
210 if ((*i)->number() == get_14bit_bank()) {
211 _current_patch_bank = *i;
212 _ignore_signals = true;
213 _bank_combo.set_active_text (n);
214 _ignore_signals = false;
219 _ignore_signals = true;
220 _bank_combo.set_active (-1);
221 _ignore_signals = false;
224 /** Update _current_patch_bank and reflect the current value of
225 * bank_combo in the rest of the dialog.
228 PatchChangeDialog::bank_combo_changed ()
230 if (_ignore_signals) {
234 _current_patch_bank.reset ();
236 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
242 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
243 string n = (*i)->name ();
244 boost::replace_all (n, "_", " ");
245 if (n == _bank_combo.get_active_text()) {
246 _current_patch_bank = *i;
250 if (_current_patch_bank == 0) {
257 set_active_patch_combo ();
259 _ignore_signals = true;
260 _bank_msb.set_value (_current_patch_bank->number() >> 7);
261 _bank_lsb.set_value (_current_patch_bank->number() & 127);
262 _ignore_signals = false;
265 /** Fill the contents of the patch combo */
267 PatchChangeDialog::fill_patch_combo ()
269 _patch_combo.clear ();
271 if (_current_patch_bank == 0) {
275 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
276 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
277 string n = (*j)->name ();
278 boost::replace_all (n, "_", " ");
279 _patch_combo.append_text (n);
283 /** Set the active value of the patch combo from the value of the _program entry */
285 PatchChangeDialog::set_active_patch_combo ()
287 if (_ignore_signals) {
291 if (_current_patch_bank == 0) {
292 _ignore_signals = true;
293 _patch_combo.set_active (-1);
294 _ignore_signals = false;
298 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
299 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
300 string n = (*j)->name ();
301 boost::replace_all (n, "_", " ");
303 MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
304 if (key.program() == _program.get_value() - 1) {
305 _ignore_signals = true;
306 _patch_combo.set_active_text (n);
307 _ignore_signals = false;
312 _ignore_signals = true;
313 _patch_combo.set_active (-1);
314 _ignore_signals = false;
317 /** Set _program from the current state of _patch_combo */
319 PatchChangeDialog::patch_combo_changed ()
321 if (_ignore_signals || _current_patch_bank == 0) {
325 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
327 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
328 string n = (*j)->name ();
329 boost::replace_all (n, "_", " ");
331 if (n == _patch_combo.get_active_text ()) {
332 _ignore_signals = true;
333 _program.set_value ((*j)->program_number() + 1);
334 _ignore_signals = false;
341 PatchChangeDialog::channel_changed ()
344 set_active_bank_combo ();
346 set_active_patch_combo ();
350 PatchChangeDialog::program_changed ()
352 if (_ignore_signals) {
356 set_active_patch_combo ();
360 PatchChangeDialog::bank_changed ()
362 if (_ignore_signals) {
366 set_active_bank_combo ();
368 set_active_patch_combo ();