Support midnam controller value labels.
[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/rc_configuration.h"
30
31 #include "i18n.h"
32
33 using namespace ARDOUR;
34 using namespace MIDI::Name;
35 using std::string;
36
37 MIDI::Name::PatchNameList InstrumentInfo::_gm_patches;
38
39 InstrumentInfo::InstrumentInfo ()
40         : external_instrument_model (_("Unknown"))
41 {
42 }
43
44 InstrumentInfo::~InstrumentInfo ()
45 {
46 }
47
48 void
49 InstrumentInfo::set_external_instrument (const string& model, const string& mode)
50 {
51         external_instrument_model = model;
52         external_instrument_mode = mode;
53         internal_instrument.reset ();
54         Changed(); /* EMIT SIGNAL */
55 }
56
57 void
58 InstrumentInfo::set_internal_instrument (boost::shared_ptr<Processor> p)
59 {
60         internal_instrument = p;
61         external_instrument_model = (_("Unknown"));
62         external_instrument_mode = "";
63         Changed(); /* EMIT SIGNAL */
64 }
65
66 string
67 InstrumentInfo::get_instrument_name () const
68 {
69         boost::shared_ptr<Processor> p = internal_instrument.lock();
70         if (p) {
71                 return p->name();
72         }
73
74         if (external_instrument_mode.empty()) {
75                 return external_instrument_model;
76         } else {
77                 return string_compose ("%1 (%2)", external_instrument_model, external_instrument_mode);
78         }
79 }
80
81 string
82 InstrumentInfo::get_patch_name (uint16_t bank, uint8_t program, uint8_t channel) const
83 {
84         boost::shared_ptr<Processor> p = internal_instrument.lock();
85         if (p) {
86                 return get_plugin_patch_name (p, bank, program, channel);
87         }
88
89         MIDI::Name::PatchPrimaryKey patch_key (program, bank);
90         
91         boost::shared_ptr<MIDI::Name::Patch> patch =
92                 MIDI::Name::MidiPatchManager::instance().find_patch (external_instrument_model, 
93                                                                      external_instrument_mode, channel, patch_key);
94
95         if (patch) {
96                 return patch->name();
97         } else {
98                 /* program and bank numbers are zero-based: convert to one-based: MIDI_BP_ZERO */
99
100 #define MIDI_BP_ZERO ((Config->get_first_midi_bank_is_zero())?0:1)
101
102                 return string_compose ("prg %1 bnk %2",program + MIDI_BP_ZERO , bank + MIDI_BP_ZERO);
103         }
104 }       
105
106 string
107 InstrumentInfo::get_controller_name (Evoral::Parameter param) const
108 {
109         boost::shared_ptr<Processor> p = internal_instrument.lock();
110         if (p || param.type() != MidiCCAutomation) {
111                 return "";
112         }
113
114         boost::shared_ptr<MIDI::Name::MasterDeviceNames> dev_names(
115                 MIDI::Name::MidiPatchManager::instance().master_device_by_model(
116                         external_instrument_model));
117         if (!dev_names) {
118                 return "";
119         }
120         
121         boost::shared_ptr<ChannelNameSet> chan_names(
122                 dev_names->channel_name_set_by_channel(
123                         external_instrument_mode, param.channel()));
124         if (!chan_names) {
125                 return "";
126         }
127
128         boost::shared_ptr<ControlNameList> control_names(
129                 dev_names->control_name_list(chan_names->control_list_name()));
130         if (!control_names) {
131                 return "";
132         }
133
134         return control_names->control(param.id())->name();
135 }       
136
137 boost::shared_ptr<MIDI::Name::ChannelNameSet>
138 InstrumentInfo::get_patches (uint8_t channel)
139 {
140         boost::shared_ptr<Processor> p = internal_instrument.lock();
141         if (p) {
142                 return plugin_programs_to_channel_name_set (p);
143         }
144
145         boost::shared_ptr<MIDI::Name::ChannelNameSet> channel_name_set =
146                 MidiPatchManager::instance().find_channel_name_set (external_instrument_model,
147                                                                                                                     external_instrument_mode,
148                                                                                                                     channel);
149
150         //std::cerr << "got channel name set with name '" << channel_name_set->name() << std::endl;
151
152         return channel_name_set;
153 }
154
155 boost::shared_ptr<MIDI::Name::ChannelNameSet>
156 InstrumentInfo::plugin_programs_to_channel_name_set (boost::shared_ptr<Processor> p)
157 {
158         PatchNameList patch_list;
159
160         boost::shared_ptr<PluginInsert> insert = boost::dynamic_pointer_cast<PluginInsert> (p);
161         if (!insert) {
162                 return boost::shared_ptr<ChannelNameSet>();
163         }
164
165         boost::shared_ptr<Plugin> pp = insert->plugin();
166         
167         if (pp->current_preset_uses_general_midi()) {
168
169                 patch_list = InstrumentInfo::general_midi_patches ();
170
171         } else if (pp->presets_are_MIDI_programs()) {
172
173                 std::vector<Plugin::PresetRecord> presets = pp->get_presets ();
174                 std::vector<Plugin::PresetRecord>::iterator i;
175                 int n;
176                 
177                 /* XXX note the assumption that plugin presets start their numbering at
178                  * zero
179                  */
180                 
181                 for (n = 0, i = presets.begin(); i != presets.end(); ++i, ++n) {
182                         if ((*i).number >= 0) {
183                                 patch_list.push_back (boost::shared_ptr<Patch> (new Patch ((*i).label, n)));
184                         } else {
185                                 patch_list.push_back (boost::shared_ptr<Patch> (new Patch (string_compose ("program %1", n), n)));
186                         }
187                 }
188         } else {
189                 for (int n = 0; n < 127; ++n) {
190                         patch_list.push_back (boost::shared_ptr<Patch> (new Patch (string_compose ("program %1", n), n)));
191                 }
192         }
193
194         boost::shared_ptr<PatchBank> pb (new PatchBank (0, p->name()));
195         pb->set_patch_name_list (patch_list);
196
197         ChannelNameSet::PatchBanks patch_banks;
198         patch_banks.push_back (pb);
199
200         boost::shared_ptr<MIDI::Name::ChannelNameSet> cns (new ChannelNameSet);
201         cns->set_patch_banks (patch_banks);
202
203         return cns;
204 }       
205
206 const MIDI::Name::PatchNameList&
207 InstrumentInfo::general_midi_patches()
208 {
209         if (_gm_patches.empty()) {
210                 for (int n = 0; n < 128; n++) {
211                         _gm_patches.push_back (boost::shared_ptr<Patch> (new Patch (general_midi_program_names[n], n))); 
212                 }
213         }
214
215         return _gm_patches;
216 }
217
218 string
219 InstrumentInfo::get_plugin_patch_name (boost::shared_ptr<Processor> p, uint16_t bank, uint8_t program, uint8_t /*channel*/) const
220 {
221         boost::shared_ptr<PluginInsert> insert = boost::dynamic_pointer_cast<PluginInsert> (p);
222         if (insert) {
223                 boost::shared_ptr<Plugin> pp = insert->plugin();
224                 
225                 if (pp->current_preset_uses_general_midi()) {
226                         return MIDI::Name::general_midi_program_names[std::min((uint8_t) 127,program)];
227                 }
228         }
229
230         return string_compose (_("preset %1 (bank %2)"), (int) program, (int) bank);
231 }