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>
23 #include <boost/algorithm/string.hpp>
24 #include "gtkmm2ext/utils.h"
25 #include "ardour/midi_patch_manager.h"
26 #include "ardour/beats_frames_converter.h"
27 #include "patch_change_dialog.h"
32 using namespace Gtkmm2ext;
34 /** @param tc If non-0, a time converter for this patch change. If 0, time control will be desensitized */
35 PatchChangeDialog::PatchChangeDialog (
36 const ARDOUR::BeatsFramesConverter* tc,
37 ARDOUR::Session* session,
38 Evoral::PatchChange<Evoral::MusicalTime> const & patch,
39 string const & model_name,
40 string const & custom_device_node,
41 const Gtk::BuiltinStockID& ok
43 : ArdourDialog (_("Patch Change"), true)
44 , _time_converter (tc)
45 , _model_name (model_name)
46 , _custom_device_mode (custom_device_node)
47 , _time (X_("patchchangetime"), true, "", true, false)
48 , _channel (*manage (new Adjustment (1, 1, 16, 1, 4)))
49 , _program (*manage (new Adjustment (1, 1, 128, 1, 16)))
50 , _bank (*manage (new Adjustment (1, 1, 16384, 1, 64)))
51 , _ignore_signals (false)
53 Table* t = manage (new Table (4, 2));
58 if (_time_converter) {
60 l = manage (left_aligned_label (_("Time")));
61 t->attach (*l, 0, 1, r, r + 1);
62 t->attach (_time, 1, 2, r, r + 1);
65 _time.set_session (session);
66 _time.set_mode (AudioClock::BBT);
67 _time.set (_time_converter->to (patch.time ()), true);
70 l = manage (left_aligned_label (_("Patch Bank")));
71 t->attach (*l, 0, 1, r, r + 1);
72 t->attach (_bank_combo, 1, 2, r, r + 1);
75 _bank_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_combo_changed));
77 l = manage (left_aligned_label (_("Patch")));
78 t->attach (*l, 0, 1, r, r + 1);
79 t->attach (_patch_combo, 1, 2, r, r + 1);
82 _patch_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::patch_combo_changed));
84 l = manage (left_aligned_label (_("Channel")));
85 t->attach (*l, 0, 1, r, r + 1);
86 t->attach (_channel, 1, 2, r, r + 1);
89 _channel.set_value (patch.channel() + 1);
90 _channel.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::channel_changed));
92 l = manage (left_aligned_label (_("Program")));
93 t->attach (*l, 0, 1, r, r + 1);
94 t->attach (_program, 1, 2, r, r + 1);
97 _program.set_value (patch.program () + 1);
98 _program.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::program_changed));
100 l = manage (left_aligned_label (_("Bank")));
101 t->attach (*l, 0, 1, r, r + 1);
102 t->attach (_bank, 1, 2, r, r + 1);
105 _bank.set_value (patch.bank() + 1);
106 _bank.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_changed));
108 get_vbox()->add (*t);
110 add_button (Stock::CANCEL, RESPONSE_CANCEL);
111 add_button (ok, RESPONSE_ACCEPT);
112 set_default_response (RESPONSE_ACCEPT);
115 set_active_bank_combo ();
116 bank_combo_changed ();
121 Evoral::PatchChange<Evoral::MusicalTime>
122 PatchChangeDialog::patch () const
124 Evoral::MusicalTime t = 0;
126 if (_time_converter) {
127 t = _time_converter->from (_time.current_time ());
130 return Evoral::PatchChange<Evoral::MusicalTime> (
132 _channel.get_value_as_int() - 1,
133 _program.get_value_as_int() - 1,
134 _bank.get_value_as_int() - 1
138 /** Fill the bank_combo according to the current _channel */
140 PatchChangeDialog::fill_bank_combo ()
142 MIDI::Name::ChannelNameSet::PatchBanks const * banks = get_banks ();
147 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = banks->begin(); i != banks->end(); ++i) {
148 string n = (*i)->name ();
149 boost::replace_all (n, "_", " ");
150 _bank_combo.append_text (n);
154 /** Set the active value of the bank_combo, and _current_patch_bank, from the contents of _bank */
156 PatchChangeDialog::set_active_bank_combo ()
158 _current_patch_bank.reset ();
160 MIDI::Name::ChannelNameSet::PatchBanks const * banks = get_banks ();
165 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = banks->begin(); i != banks->end(); ++i) {
166 string n = (*i)->name ();
167 boost::replace_all (n, "_", " ");
169 MIDI::Name::PatchPrimaryKey const * key = (*i)->patch_primary_key ();
170 if (key && ((key->msb << 7) | key->lsb) == _bank.get_value () - 1) {
171 _current_patch_bank = *i;
172 _ignore_signals = true;
173 _bank_combo.set_active_text (n);
174 _ignore_signals = false;
179 _ignore_signals = true;
180 _bank_combo.set_active (-1);
181 _ignore_signals = false;
184 /** Update _current_patch_bank and reflect the current value of
185 * bank_combo in the rest of the dialog.
188 PatchChangeDialog::bank_combo_changed ()
190 if (_ignore_signals) {
194 _current_patch_bank.reset ();
196 MIDI::Name::ChannelNameSet::PatchBanks const * banks = get_banks ();
201 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = banks->begin(); i != banks->end(); ++i) {
202 string n = (*i)->name ();
203 boost::replace_all (n, "_", " ");
204 if (n == _bank_combo.get_active_text()) {
205 _current_patch_bank = *i;
209 if (_current_patch_bank == 0) {
216 set_active_patch_combo ();
218 MIDI::Name::PatchPrimaryKey const * key = _current_patch_bank->patch_primary_key ();
220 _ignore_signals = true;
221 _bank.set_value (((key->msb << 7) | key->lsb) + 1);
222 _ignore_signals = false;
226 /** Fill the contents of the patch combo */
228 PatchChangeDialog::fill_patch_combo ()
230 _patch_combo.clear ();
232 if (_current_patch_bank == 0) {
236 const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
237 for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
238 string n = (*j)->name ();
239 boost::replace_all (n, "_", " ");
240 _patch_combo.append_text (n);
244 /** Set the active value of the patch combo from the value of the _program entry */
246 PatchChangeDialog::set_active_patch_combo ()
248 if (_ignore_signals) {
252 if (_current_patch_bank == 0) {
253 _ignore_signals = true;
254 _patch_combo.set_active (-1);
255 _ignore_signals = false;
259 const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
260 for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
261 string n = (*j)->name ();
262 boost::replace_all (n, "_", " ");
264 MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
265 if (key.program_number == _program.get_value() - 1) {
266 _ignore_signals = true;
267 _patch_combo.set_active_text (n);
268 _ignore_signals = false;
273 _ignore_signals = true;
274 _patch_combo.set_active (-1);
275 _ignore_signals = false;
278 /** Set _program from the current state of _patch_combo */
280 PatchChangeDialog::patch_combo_changed ()
282 if (_ignore_signals || _current_patch_bank == 0) {
286 const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
287 for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
288 string n = (*j)->name ();
289 boost::replace_all (n, "_", " ");
290 if (n == _patch_combo.get_active_text ()) {
291 MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
292 _ignore_signals = true;
293 _program.set_value (key.program_number + 1);
294 _ignore_signals = false;
300 PatchChangeDialog::channel_changed ()
303 set_active_bank_combo ();
305 set_active_patch_combo ();
309 PatchChangeDialog::program_changed ()
311 if (_ignore_signals) {
315 set_active_patch_combo ();
319 PatchChangeDialog::bank_changed ()
321 if (_ignore_signals) {
325 set_active_bank_combo ();
327 set_active_patch_combo ();
330 MIDI::Name::ChannelNameSet::PatchBanks const *
331 PatchChangeDialog::get_banks ()
333 MIDI::Name::MidiPatchManager& mpm = MIDI::Name::MidiPatchManager::instance ();
334 boost::shared_ptr<MIDI::Name::ChannelNameSet> channel_name_set = mpm.find_channel_name_set (
335 _model_name, _custom_device_mode, _channel.get_value_as_int() - 1
338 if (!channel_name_set) {
342 return &channel_name_set->patch_banks ();