Add option to limit automatable control parmaters
[ardour.git] / libs / ardour / instrument_info.cc
1 /*
2     Copyright (C) 2012 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <algorithm>
20
21 #include "pbd/compose.h"
22
23 #include "midi++/midnam_patch.h"
24
25 #include "ardour/instrument_info.h"
26 #include "ardour/midi_patch_manager.h"
27 #include "ardour/processor.h"
28 #include "ardour/plugin.h"
29 #include "ardour/plugin_insert.h"
30 #include "ardour/rc_configuration.h"
31
32 #include "pbd/i18n.h"
33
34 using namespace ARDOUR;
35 using namespace MIDI::Name;
36 using std::string;
37
38 MIDI::Name::PatchNameList InstrumentInfo::_gm_patches;
39
40 InstrumentInfo::InstrumentInfo ()
41         : external_instrument_model (_("Unknown"))
42 {
43 }
44
45 InstrumentInfo::~InstrumentInfo ()
46 {
47 }
48
49 void
50 InstrumentInfo::set_external_instrument (const string& model, const string& mode)
51 {
52         if (external_instrument_model == model && external_instrument_mode == mode && internal_instrument.expired ()) {
53                 return;
54         }
55         external_instrument_model = model;
56         external_instrument_mode = mode;
57         internal_instrument.reset ();
58         Changed(); /* EMIT SIGNAL */
59 }
60
61 void
62 InstrumentInfo::set_internal_instrument (boost::shared_ptr<Processor> p)
63 {
64         bool changed = !external_instrument_mode.empty ();
65         external_instrument_mode = "";
66
67         boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert>(p);
68         if (pi && pi->plugin ()->has_midnam ()) {
69                 /* really back hack, following MidiTimeAxisView::model_changed()
70                  *
71                  * InstrumentInfo::get_plugin_patch_name() needs to be overhauled,
72                  * it limits all PluginInsert to generic-midi or only numbers.
73                  */
74                 changed |= !internal_instrument.expired ();
75                 changed |= external_instrument_model != pi->plugin ()->midnam_model ();
76
77                 internal_instrument.reset ();
78                 external_instrument_model = pi->plugin ()->midnam_model ();
79                 const std::list<std::string> device_modes = MIDI::Name::MidiPatchManager::instance().custom_device_mode_names_by_model (external_instrument_model);
80                 if (device_modes.size() > 0) {
81                         changed |= external_instrument_mode != device_modes.front();
82                         external_instrument_mode = device_modes.front();
83                 }
84         } else {
85                 changed |= internal_instrument.lock () != p || external_instrument_model != _("Unknown");
86                 internal_instrument = p;
87                 external_instrument_model = _("Unknown");
88         }
89         if (changed) {
90                 Changed(); /* EMIT SIGNAL */
91         }
92 }
93
94 string
95 InstrumentInfo::get_instrument_name () const
96 {
97         boost::shared_ptr<Processor> p = internal_instrument.lock();
98         if (p) {
99                 return p->name();
100         }
101
102         if (external_instrument_mode.empty()) {
103                 return external_instrument_model;
104         } else {
105                 return string_compose ("%1 (%2)", external_instrument_model, external_instrument_mode);
106         }
107 }
108
109 string
110 InstrumentInfo::get_patch_name (uint16_t bank, uint8_t program, uint8_t channel) const
111 {
112         return get_patch_name (bank, program, channel, true);
113 }
114
115 string
116 InstrumentInfo::get_patch_name_without (uint16_t bank, uint8_t program, uint8_t channel) const
117 {
118         return get_patch_name (bank, program, channel, false);
119 }
120
121 string
122 InstrumentInfo::get_patch_name (uint16_t bank, uint8_t program, uint8_t channel, bool with_extra) const
123 {
124         boost::shared_ptr<Processor> p = internal_instrument.lock();
125         if (p) {
126                 return get_plugin_patch_name (p, bank, program, channel);
127         }
128
129         MIDI::Name::PatchPrimaryKey patch_key (program, bank);
130
131         boost::shared_ptr<MIDI::Name::Patch> patch =
132                 MIDI::Name::MidiPatchManager::instance().find_patch (external_instrument_model,
133                                                                      external_instrument_mode, channel, patch_key);
134
135         if (patch) {
136                 return patch->name();
137         } else {
138                 /* program and bank numbers are zero-based: convert to one-based: MIDI_BP_ZERO */
139
140 #define MIDI_BP_ZERO ((Config->get_first_midi_bank_is_zero())?0:1)
141
142                 if (with_extra) {
143                         return string_compose ("prg %1 bnk %2",program + MIDI_BP_ZERO , bank + MIDI_BP_ZERO);
144                 } else {
145                         return string_compose ("%1", program + MIDI_BP_ZERO);
146                 }
147         }
148 }
149
150 string
151 InstrumentInfo::get_controller_name (Evoral::Parameter param) const
152 {
153         boost::shared_ptr<Processor> p = internal_instrument.lock();
154         if (param.type() != MidiCCAutomation) {
155                 return "";
156         }
157         if (p) {
158                 return get_plugin_controller_name (p, param);
159         }
160
161         boost::shared_ptr<MIDI::Name::MasterDeviceNames> dev_names(
162                 MIDI::Name::MidiPatchManager::instance().master_device_by_model(
163                         external_instrument_model));
164         if (!dev_names) {
165                 return "";
166         }
167
168         boost::shared_ptr<ChannelNameSet> chan_names(
169                 dev_names->channel_name_set_by_channel(
170                         external_instrument_mode, param.channel()));
171         if (!chan_names) {
172                 return "";
173         }
174
175         boost::shared_ptr<ControlNameList> control_names(
176                 dev_names->control_name_list(chan_names->control_list_name()));
177         if (!control_names) {
178                 return "";
179         }
180         boost::shared_ptr<const Control> c = control_names->control(param.id());
181
182         if (c) {
183                 return string_compose(c->name() + " [%1]", int(param.channel()) + 1);
184         }
185
186         return "";
187 }
188
189 boost::shared_ptr<MIDI::Name::ChannelNameSet>
190 InstrumentInfo::get_patches (uint8_t channel)
191 {
192         boost::shared_ptr<Processor> p = internal_instrument.lock();
193         if (p) {
194                 return plugin_programs_to_channel_name_set (p);
195         }
196
197         boost::shared_ptr<MIDI::Name::ChannelNameSet> channel_name_set =
198                 MidiPatchManager::instance().find_channel_name_set (external_instrument_model,
199                                                                                                                     external_instrument_mode,
200                                                                                                                     channel);
201
202         //std::cerr << "got channel name set with name '" << channel_name_set->name() << std::endl;
203
204         return channel_name_set;
205 }
206
207 boost::shared_ptr<MIDI::Name::ChannelNameSet>
208 InstrumentInfo::plugin_programs_to_channel_name_set (boost::shared_ptr<Processor> p)
209 {
210         PatchNameList patch_list;
211
212         boost::shared_ptr<PluginInsert> insert = boost::dynamic_pointer_cast<PluginInsert> (p);
213         if (!insert) {
214                 return boost::shared_ptr<ChannelNameSet>();
215         }
216
217         boost::shared_ptr<Plugin> pp = insert->plugin();
218
219         if (pp->current_preset_uses_general_midi()) {
220
221                 patch_list = InstrumentInfo::general_midi_patches ();
222
223         } else if (pp->presets_are_MIDI_programs()) {
224
225                 std::vector<Plugin::PresetRecord> presets = pp->get_presets ();
226                 std::vector<Plugin::PresetRecord>::iterator i;
227                 int n;
228
229                 for (n = 0, i = presets.begin(); i != presets.end(); ++i, ++n) {
230                         if ((*i).valid) {
231                                 patch_list.push_back (boost::shared_ptr<Patch> (new Patch ((*i).label, n)));
232                         } else {
233                                 patch_list.push_back (boost::shared_ptr<Patch> (new Patch (string_compose ("program %1", n), n)));
234                         }
235                 }
236         } else {
237                 for (int n = 0; n < 127; ++n) {
238                         patch_list.push_back (boost::shared_ptr<Patch> (new Patch (string_compose ("program %1", n), n)));
239                 }
240         }
241
242         boost::shared_ptr<PatchBank> pb (new PatchBank (0, p->name()));
243         pb->set_patch_name_list (patch_list);
244
245         ChannelNameSet::PatchBanks patch_banks;
246         patch_banks.push_back (pb);
247
248         boost::shared_ptr<MIDI::Name::ChannelNameSet> cns (new ChannelNameSet);
249         cns->set_patch_banks (patch_banks);
250
251         return cns;
252 }
253
254 const MIDI::Name::PatchNameList&
255 InstrumentInfo::general_midi_patches()
256 {
257         if (_gm_patches.empty()) {
258                 for (int n = 0; n < 128; n++) {
259                         _gm_patches.push_back (boost::shared_ptr<Patch> (new Patch (general_midi_program_names[n], n)));
260                 }
261         }
262
263         return _gm_patches;
264 }
265
266 string
267 InstrumentInfo::get_plugin_controller_name (boost::shared_ptr<ARDOUR::Processor>, Evoral::Parameter param) const
268 {
269         return "";
270 }
271
272 string
273 InstrumentInfo::get_plugin_patch_name (boost::shared_ptr<Processor> p, uint16_t bank, uint8_t program, uint8_t /*channel*/) const
274 {
275         boost::shared_ptr<PluginInsert> insert = boost::dynamic_pointer_cast<PluginInsert> (p);
276         if (insert) {
277                 boost::shared_ptr<Plugin> pp = insert->plugin();
278
279                 if (pp->current_preset_uses_general_midi()) {
280                         return MIDI::Name::general_midi_program_names[std::min((uint8_t) 127,program)];
281                 }
282         }
283
284         return string_compose (_("preset %1 (bank %2)"), (int) program, (int) bank);
285 }