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/midi_patch_manager.h"
31 #include "ardour/beats_frames_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::BeatsFramesConverter* tc,
46 ARDOUR::Session* session,
47 Evoral::PatchChange<Evoral::MusicalTime> const & patch,
48 ARDOUR::InstrumentInfo& info,
49 const Gtk::BuiltinStockID& ok,
51 : ArdourDialog (_("Patch Change"), true)
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 (*manage (new Adjustment (1, 1, 16384, 1, 64)))
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")));
108 t->attach (*l, 0, 1, r, r + 1);
109 t->attach (_bank, 1, 2, r, r + 1);
112 _bank.set_value (patch.bank() + 1);
113 _bank.signal_changed().connect (sigc::mem_fun (*this, &PatchChangeDialog::bank_changed));
115 get_vbox()->add (*t);
117 add_button (Stock::CANCEL, RESPONSE_CANCEL);
118 add_button (ok, RESPONSE_ACCEPT);
120 add_button (Gtk::StockID(GTK_STOCK_DELETE), RESPONSE_REJECT);
122 set_default_response (RESPONSE_ACCEPT);
125 set_active_bank_combo ();
126 bank_combo_changed ();
128 _info.Changed.connect (_info_changed_connection, invalidator (*this),
129 boost::bind (&PatchChangeDialog::instrument_info_changed, this), gui_context());
135 PatchChangeDialog::instrument_info_changed ()
137 _bank_combo.clear ();
138 _patch_combo.clear ();
143 Evoral::PatchChange<Evoral::MusicalTime>
144 PatchChangeDialog::patch () const
146 Evoral::MusicalTime t = 0;
148 if (_time_converter) {
149 t = _time_converter->from (_time.current_time ());
152 return Evoral::PatchChange<Evoral::MusicalTime> (
154 _channel.get_value_as_int() - 1,
155 _program.get_value_as_int() - 1,
156 _bank.get_value_as_int() - 1
160 /** Fill the bank_combo according to the current _channel */
162 PatchChangeDialog::fill_bank_combo ()
164 _bank_combo.clear ();
166 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
172 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
173 string n = (*i)->name ();
174 boost::replace_all (n, "_", " ");
175 _bank_combo.append_text (n);
179 /** Set the active value of the bank_combo, and _current_patch_bank, from the contents of _bank */
181 PatchChangeDialog::set_active_bank_combo ()
183 _current_patch_bank.reset ();
185 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
191 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
193 string n = (*i)->name ();
194 boost::replace_all (n, "_", " ");
196 if ((*i)->number() == _bank.get_value () - 1) {
197 _current_patch_bank = *i;
198 _ignore_signals = true;
199 _bank_combo.set_active_text (n);
200 _ignore_signals = false;
205 _ignore_signals = true;
206 _bank_combo.set_active (-1);
207 _ignore_signals = false;
210 /** Update _current_patch_bank and reflect the current value of
211 * bank_combo in the rest of the dialog.
214 PatchChangeDialog::bank_combo_changed ()
216 if (_ignore_signals) {
220 _current_patch_bank.reset ();
222 boost::shared_ptr<MIDI::Name::ChannelNameSet> cns = _info.get_patches (_channel.get_value_as_int() - 1);
228 for (MIDI::Name::ChannelNameSet::PatchBanks::const_iterator i = cns->patch_banks().begin(); i != cns->patch_banks().end(); ++i) {
229 string n = (*i)->name ();
230 boost::replace_all (n, "_", " ");
231 if (n == _bank_combo.get_active_text()) {
232 _current_patch_bank = *i;
236 if (_current_patch_bank == 0) {
243 set_active_patch_combo ();
245 _ignore_signals = true;
246 _bank.set_value (_current_patch_bank->number() + 1);
247 _ignore_signals = false;
250 /** Fill the contents of the patch combo */
252 PatchChangeDialog::fill_patch_combo ()
254 _patch_combo.clear ();
256 if (_current_patch_bank == 0) {
260 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
261 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
262 string n = (*j)->name ();
263 boost::replace_all (n, "_", " ");
264 _patch_combo.append_text (n);
268 /** Set the active value of the patch combo from the value of the _program entry */
270 PatchChangeDialog::set_active_patch_combo ()
272 if (_ignore_signals) {
276 if (_current_patch_bank == 0) {
277 _ignore_signals = true;
278 _patch_combo.set_active (-1);
279 _ignore_signals = false;
283 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
284 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
285 string n = (*j)->name ();
286 boost::replace_all (n, "_", " ");
288 MIDI::Name::PatchPrimaryKey const & key = (*j)->patch_primary_key ();
289 if (key.program_number == _program.get_value() - 1) {
290 _ignore_signals = true;
291 _patch_combo.set_active_text (n);
292 _ignore_signals = false;
297 _ignore_signals = true;
298 _patch_combo.set_active (-1);
299 _ignore_signals = false;
302 /** Set _program from the current state of _patch_combo */
304 PatchChangeDialog::patch_combo_changed ()
306 if (_ignore_signals || _current_patch_bank == 0) {
310 const MIDI::Name::PatchNameList& patches = _current_patch_bank->patch_name_list ();
312 for (MIDI::Name::PatchNameList::const_iterator j = patches.begin(); j != patches.end(); ++j) {
313 string n = (*j)->name ();
314 boost::replace_all (n, "_", " ");
316 if (n == _patch_combo.get_active_text ()) {
317 _ignore_signals = true;
318 _program.set_value ((*j)->program_number() + 1);
319 _ignore_signals = false;
326 PatchChangeDialog::channel_changed ()
329 set_active_bank_combo ();
331 set_active_patch_combo ();
335 PatchChangeDialog::program_changed ()
337 if (_ignore_signals) {
341 set_active_patch_combo ();
345 PatchChangeDialog::bank_changed ()
347 if (_ignore_signals) {
351 set_active_bank_combo ();
353 set_active_patch_combo ();