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 "ardour/midi_patch_manager.h"
29 #include "ardour/beats_frames_converter.h"
30 #include "ardour/instrument_info.h"
32 #include "patch_change_dialog.h"
38 using namespace Gtkmm2ext;
40 /** @param tc If non-0, a time converter for this patch change. If 0, time control will be desensitized */
41 PatchChangeDialog::PatchChangeDialog (
42 const ARDOUR::BeatsFramesConverter* tc,
43 ARDOUR::Session* session,
44 Evoral::PatchChange<Evoral::MusicalTime> const & patch,
45 ARDOUR::InstrumentInfo& info,
46 const Gtk::BuiltinStockID& ok
48 : ArdourDialog (_("Patch Change"), true)
49 , _time_converter (tc)
51 , _time (X_("patchchangetime"), true, "", true, false)
52 , _channel (*manage (new Adjustment (1, 1, 16, 1, 4)))
53 , _program (*manage (new Adjustment (1, 1, 128, 1, 16)))
54 , _bank (*manage (new Adjustment (1, 1, 16384, 1, 64)))
55 , _ignore_signals (false)
57 Table* t = manage (new Table (4, 2));
62 if (_time_converter) {
64 l = manage (left_aligned_label (_("Time")));
65 t->attach (*l, 0, 1, r, r + 1);
66 t->attach (_time, 1, 2, r, r + 1);
69 _time.set_session (session);
70 _time.set_mode (AudioClock::BBT);
71 _time.set (_time_converter->to (patch.time ()), true);
74 l = manage (left_aligned_label (_("Patch Bank")));
75 t->attach (*l, 0, 1, r, r + 1);
76 t->attach (_bank_combo, 1, 2, r, r + 1);
79 _bank_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_combo_changed));
81 l = manage (left_aligned_label (_("Patch")));
82 t->attach (*l, 0, 1, r, r + 1);
83 t->attach (_patch_combo, 1, 2, r, r + 1);
86 _patch_combo.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::patch_combo_changed));
88 l = manage (left_aligned_label (_("Channel")));
89 t->attach (*l, 0, 1, r, r + 1);
90 t->attach (_channel, 1, 2, r, r + 1);
93 _channel.set_value (patch.channel() + 1);
94 _channel.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::channel_changed));
96 l = manage (left_aligned_label (_("Program")));
97 t->attach (*l, 0, 1, r, r + 1);
98 t->attach (_program, 1, 2, r, r + 1);
101 _program.set_value (patch.program () + 1);
102 _program.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::program_changed));
104 l = manage (left_aligned_label (_("Bank")));
105 t->attach (*l, 0, 1, r, r + 1);
106 t->attach (_bank, 1, 2, r, r + 1);
109 _bank.set_value (patch.bank() + 1);
110 _bank.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_changed));
112 get_vbox()->add (*t);
114 add_button (Stock::CANCEL, RESPONSE_CANCEL);
115 add_button (ok, RESPONSE_ACCEPT);
116 set_default_response (RESPONSE_ACCEPT);
119 set_active_bank_combo ();
120 bank_combo_changed ();
125 Evoral::PatchChange<Evoral::MusicalTime>
126 PatchChangeDialog::patch () const
128 Evoral::MusicalTime t = 0;
130 if (_time_converter) {
131 t = _time_converter->from (_time.current_time ());
134 return Evoral::PatchChange<Evoral::MusicalTime> (
136 _channel.get_value_as_int() - 1,
137 _program.get_value_as_int() - 1,
138 _bank.get_value_as_int() - 1
142 /** Fill the bank_combo according to the current _channel */
144 PatchChangeDialog::fill_bank_combo ()
146 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
148 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
149 string n = (*i)->name ();
150 boost::replace_all (n, "_", " ");
151 _bank_combo.append_text (n);
155 /** Set the active value of the bank_combo, and _current_patch_bank, from the contents of _bank */
157 PatchChangeDialog::set_active_bank_combo ()
159 _current_patch_bank.reset ();
161 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
163 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
165 string n = (*i)->name ();
166 boost::replace_all (n, "_", " ");
168 MIDI::Name::PatchPrimaryKey const * key = (*i)->patch_primary_key ();
169 if (key && (key->bank_number == _bank.get_value () - 1)) {
170 _current_patch_bank = *i;
171 _ignore_signals = true;
172 _bank_combo.set_active_text (n);
173 _ignore_signals = false;
178 _ignore_signals = true;
179 _bank_combo.set_active (-1);
180 _ignore_signals = false;
183 /** Update _current_patch_bank and reflect the current value of
184 * bank_combo in the rest of the dialog.
187 PatchChangeDialog::bank_combo_changed ()
189 if (_ignore_signals) {
193 _current_patch_bank.reset ();
195 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
197 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
198 string n = (*i)->name ();
199 boost::replace_all (n, "_", " ");
200 if (n == _bank_combo.get_active_text()) {
201 _current_patch_bank = *i;
205 if (_current_patch_bank == 0) {
212 set_active_patch_combo ();
214 MIDI::Name::PatchPrimaryKey const * key = _current_patch_bank->patch_primary_key ();
216 _ignore_signals = true;
217 _bank.set_value (key->bank_number + 1);
218 _ignore_signals = false;
222 /** Fill the contents of the patch combo */
224 PatchChangeDialog::fill_patch_combo ()
226 _patch_combo.clear ();
228 if (_current_patch_bank == 0) {
232 const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
233 for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
234 string n = (*j)->name ();
235 boost::replace_all (n, "_", " ");
236 _patch_combo.append_text (n);
240 /** Set the active value of the patch combo from the value of the _program entry */
242 PatchChangeDialog::set_active_patch_combo ()
244 if (_ignore_signals) {
248 if (_current_patch_bank == 0) {
249 _ignore_signals = true;
250 _patch_combo.set_active (-1);
251 _ignore_signals = false;
255 const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
256 for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
257 string n = (*j)->name ();
258 boost::replace_all (n, "_", " ");
260 MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
261 if (key.program_number == _program.get_value() - 1) {
262 _ignore_signals = true;
263 _patch_combo.set_active_text (n);
264 _ignore_signals = false;
269 _ignore_signals = true;
270 _patch_combo.set_active (-1);
271 _ignore_signals = false;
274 /** Set _program from the current state of _patch_combo */
276 PatchChangeDialog::patch_combo_changed ()
278 if (_ignore_signals || _current_patch_bank == 0) {
282 const MIDI::Name::PatchBank::PatchNameList& patches = _current_patch_bank->patch_name_list ();
283 for (MIDI::Name::PatchBank::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
284 string n = (*j)->name ();
285 boost::replace_all (n, "_", " ");
286 if (n == _patch_combo.get_active_text ()) {
287 MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
288 _ignore_signals = true;
289 _program.set_value (key.program_number + 1);
290 _ignore_signals = false;
296 PatchChangeDialog::channel_changed ()
299 set_active_bank_combo ();
301 set_active_patch_combo ();
305 PatchChangeDialog::program_changed ()
307 if (_ignore_signals) {
311 set_active_patch_combo ();
315 PatchChangeDialog::bank_changed ()
317 if (_ignore_signals) {
321 set_active_bank_combo ();
323 set_active_patch_combo ();