PBD::Signal<...>::connect() is already thread safe, so drop intermediate proxy/call_s...
[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         external_instrument_model = model;
53         external_instrument_mode = mode;
54         internal_instrument.reset ();
55         Changed(); /* EMIT SIGNAL */
56 }
57
58 void
59 InstrumentInfo::set_internal_instrument (boost::shared_ptr<Processor> p)
60 {
61         internal_instrument = p;
62         external_instrument_model = (_("Unknown"));
63         external_instrument_mode = "";
64         Changed(); /* EMIT SIGNAL */
65 }
66
67 string
68 InstrumentInfo::get_instrument_name () const
69 {
70         boost::shared_ptr<Processor> p = internal_instrument.lock();
71         if (p) {
72                 return p->name();
73         }
74
75         if (external_instrument_mode.empty()) {
76                 return external_instrument_model;
77         } else {
78                 return string_compose ("%1 (%2)", external_instrument_model, external_instrument_mode);
79         }
80 }
81
82 string
83 InstrumentInfo::get_patch_name (uint16_t bank, uint8_t program, uint8_t channel) const
84 {
85         return get_patch_name (bank, program, channel, true);
86 }
87
88 string
89 InstrumentInfo::get_patch_name_without (uint16_t bank, uint8_t program, uint8_t channel) const
90 {
91         return get_patch_name (bank, program, channel, false);
92 }
93
94 string
95 InstrumentInfo::get_patch_name (uint16_t bank, uint8_t program, uint8_t channel, bool with_extra) const
96 {
97         boost::shared_ptr<Processor> p = internal_instrument.lock();
98         if (p) {
99                 return get_plugin_patch_name (p, bank, program, channel);
100         }
101
102         MIDI::Name::PatchPrimaryKey patch_key (program, bank);
103
104         boost::shared_ptr<MIDI::Name::Patch> patch =
105                 MIDI::Name::MidiPatchManager::instance().find_patch (external_instrument_model,
106                                                                      external_instrument_mode, channel, patch_key);
107
108         if (patch) {
109                 return patch->name();
110         } else {
111                 /* program and bank numbers are zero-based: convert to one-based: MIDI_BP_ZERO */
112
113 #define MIDI_BP_ZERO ((Config->get_first_midi_bank_is_zero())?0:1)
114
115                 if (with_extra) {
116                         return string_compose ("prg %1 bnk %2",program + MIDI_BP_ZERO , bank + MIDI_BP_ZERO);
117                 } else {
118                         return string_compose ("%1", program + MIDI_BP_ZERO);
119                 }
120         }
121 }
122
123 string
124 InstrumentInfo::get_controller_name (Evoral::Parameter param) const
125 {
126         boost::shared_ptr<Processor> p = internal_instrument.lock();
127         if (param.type() != MidiCCAutomation) {
128                 return "";
129         }
130         if (p) {
131                 return get_plugin_controller_name (p, param);
132         }
133
134         boost::shared_ptr<MIDI::Name::MasterDeviceNames> dev_names(
135                 MIDI::Name::MidiPatchManager::instance().master_device_by_model(
136                         external_instrument_model));
137         if (!dev_names) {
138                 return "";
139         }
140
141         boost::shared_ptr<ChannelNameSet> chan_names(
142                 dev_names->channel_name_set_by_channel(
143                         external_instrument_mode, param.channel()));
144         if (!chan_names) {
145                 return "";
146         }
147
148         boost::shared_ptr<ControlNameList> control_names(
149                 dev_names->control_name_list(chan_names->control_list_name()));
150         if (!control_names) {
151                 return "";
152         }
153         boost::shared_ptr<const Control> c = control_names->control(param.id());
154
155         if (c) {
156                 return string_compose(c->name() + " [%1]", int(param.channel()) + 1);
157         }
158
159         return "";
160 }
161
162 boost::shared_ptr<MIDI::Name::ChannelNameSet>
163 InstrumentInfo::get_patches (uint8_t channel)
164 {
165         boost::shared_ptr<Processor> p = internal_instrument.lock();
166         if (p) {
167                 return plugin_programs_to_channel_name_set (p);
168         }
169
170         boost::shared_ptr<MIDI::Name::ChannelNameSet> channel_name_set =
171                 MidiPatchManager::instance().find_channel_name_set (external_instrument_model,
172                                                                                                                     external_instrument_mode,
173                                                                                                                     channel);
174
175         //std::cerr << "got channel name set with name '" << channel_name_set->name() << std::endl;
176
177         return channel_name_set;
178 }
179
180 boost::shared_ptr<MIDI::Name::ChannelNameSet>
181 InstrumentInfo::plugin_programs_to_channel_name_set (boost::shared_ptr<Processor> p)
182 {
183         PatchNameList patch_list;
184
185         boost::shared_ptr<PluginInsert> insert = boost::dynamic_pointer_cast<PluginInsert> (p);
186         if (!insert) {
187                 return boost::shared_ptr<ChannelNameSet>();
188         }
189
190         boost::shared_ptr<Plugin> pp = insert->plugin();
191
192         if (pp->current_preset_uses_general_midi()) {
193
194                 patch_list = InstrumentInfo::general_midi_patches ();
195
196         } else if (pp->presets_are_MIDI_programs()) {
197
198                 std::vector<Plugin::PresetRecord> presets = pp->get_presets ();
199                 std::vector<Plugin::PresetRecord>::iterator i;
200                 int n;
201
202                 for (n = 0, i = presets.begin(); i != presets.end(); ++i, ++n) {
203                         if ((*i).valid) {
204                                 patch_list.push_back (boost::shared_ptr<Patch> (new Patch ((*i).label, n)));
205                         } else {
206                                 patch_list.push_back (boost::shared_ptr<Patch> (new Patch (string_compose ("program %1", n), n)));
207                         }
208                 }
209         } else {
210                 for (int n = 0; n < 127; ++n) {
211                         patch_list.push_back (boost::shared_ptr<Patch> (new Patch (string_compose ("program %1", n), n)));
212                 }
213         }
214
215         boost::shared_ptr<PatchBank> pb (new PatchBank (0, p->name()));
216         pb->set_patch_name_list (patch_list);
217
218         ChannelNameSet::PatchBanks patch_banks;
219         patch_banks.push_back (pb);
220
221         boost::shared_ptr<MIDI::Name::ChannelNameSet> cns (new ChannelNameSet);
222         cns->set_patch_banks (patch_banks);
223
224         return cns;
225 }
226
227 const MIDI::Name::PatchNameList&
228 InstrumentInfo::general_midi_patches()
229 {
230         if (_gm_patches.empty()) {
231                 for (int n = 0; n < 128; n++) {
232                         _gm_patches.push_back (boost::shared_ptr<Patch> (new Patch (general_midi_program_names[n], n)));
233                 }
234         }
235
236         return _gm_patches;
237 }
238
239 string
240 InstrumentInfo::get_plugin_controller_name (boost::shared_ptr<ARDOUR::Processor>, Evoral::Parameter param) const
241 {
242         return "";
243 }
244
245 string
246 InstrumentInfo::get_plugin_patch_name (boost::shared_ptr<Processor> p, uint16_t bank, uint8_t program, uint8_t /*channel*/) const
247 {
248         boost::shared_ptr<PluginInsert> insert = boost::dynamic_pointer_cast<PluginInsert> (p);
249         if (insert) {
250                 boost::shared_ptr<Plugin> pp = insert->plugin();
251
252                 if (pp->current_preset_uses_general_midi()) {
253                         return MIDI::Name::general_midi_program_names[std::min((uint8_t) 127,program)];
254                 }
255         }
256
257         return string_compose (_("preset %1 (bank %2)"), (int) program, (int) bank);
258 }