lots more work trying to create a common structure for accessing plugin and MIDNAME...
[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::PatchBank::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
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         boost::shared_ptr<Processor> p = internal_instrument.lock();
86
87         if (p) {
88                 return get_plugin_patch_name (p, bank, program, channel);
89         }
90
91         MIDI::Name::PatchPrimaryKey patch_key (bank, program);
92         
93         boost::shared_ptr<MIDI::Name::Patch> patch =
94                 MIDI::Name::MidiPatchManager::instance().find_patch (external_instrument_model, 
95                                                                      external_instrument_mode, channel, patch_key);
96
97         if (patch) {
98                 return patch->name();
99         } else {
100                 /* program and bank numbers are zero-based: convert to one-based: MIDI_BP_ZERO */
101
102 #define MIDI_BP_ZERO ((Config->get_first_midi_bank_is_zero())?0:1)
103
104                 return string_compose ("prg %1 bnk %2",program + MIDI_BP_ZERO , bank + MIDI_BP_ZERO);
105         }
106 }       
107
108 boost::shared_ptr<MIDI::Name::ChannelNameSet>
109 InstrumentInfo::get_patches (uint8_t channel)
110 {
111         boost::shared_ptr<Processor> p = internal_instrument.lock();
112
113         if (p) {
114                 return plugin_programs_to_channel_name_set (p);
115         }
116
117         return MidiPatchManager::instance().find_channel_name_set (external_instrument_model,
118                                                                    external_instrument_mode,
119                                                                    channel);
120                                                                    
121 }
122
123 boost::shared_ptr<MIDI::Name::ChannelNameSet>
124 InstrumentInfo::plugin_programs_to_channel_name_set (boost::shared_ptr<Processor> p)
125 {
126         PatchBank::PatchNameList patch_list;
127
128         boost::shared_ptr<PluginInsert> insert = boost::dynamic_pointer_cast<PluginInsert> (p);
129
130         if (!insert) {
131                 return boost::shared_ptr<ChannelNameSet>();
132         }
133
134         boost::shared_ptr<Plugin> pp = insert->plugin();
135         
136         if (pp->current_preset_uses_general_midi()) {
137
138                 patch_list = InstrumentInfo::general_midi_patches ();
139
140         } else if (pp->presets_are_MIDI_programs()) {
141
142                 std::vector<Plugin::PresetRecord> presets = pp->get_presets ();
143                 std::vector<Plugin::PresetRecord>::iterator i;
144                 int n;
145                 
146                 /* XXX note the assumption that plugin presets start their numbering at
147                  * zero
148                  */
149                 
150                 for (n = 0, i = presets.begin(); i != presets.end(); ++i, ++n) {
151                         if ((*i).number >= 0) {
152                                 patch_list.push_back (boost::shared_ptr<Patch> (new Patch (string_compose ("%1", n), (*i).label)));
153                         } else {
154                                 patch_list.push_back (boost::shared_ptr<Patch> (new Patch (string_compose ("%1", n), 
155                                                                                            string_compose ("program %1", n))));
156                         }
157                 }
158         } else {
159                 for (int n = 0; n < 127; ++n) {
160                         patch_list.push_back (boost::shared_ptr<Patch> (new Patch (string_compose ("%1", n), 
161                                                                                    string_compose ("program %1", n))));
162                 }
163         }
164
165         boost::shared_ptr<PatchBank> pb (new PatchBank (p->name()));
166         pb->set_patch_name_list (patch_list);
167
168         ChannelNameSet::PatchBanks patch_banks;
169         patch_banks.push_back (pb);
170
171         boost::shared_ptr<MIDI::Name::ChannelNameSet> cns (new ChannelNameSet);
172         cns->set_patch_banks (patch_banks);
173
174         return cns;
175 }       
176
177 const MIDI::Name::PatchBank::PatchNameList&
178 InstrumentInfo::general_midi_patches()
179 {
180         if (_gm_patches.empty()) {
181                 for (int n = 0; n < 128; n++) {
182                         _gm_patches.push_back (boost::shared_ptr<Patch> (new Patch (string_compose ("%1", n), general_midi_program_names[n])));
183                 }
184         }
185
186         return _gm_patches;
187 }
188
189 string
190 InstrumentInfo::get_plugin_patch_name (boost::shared_ptr<Processor> p, uint16_t bank, uint8_t program, uint8_t channel) const
191 {
192         boost::shared_ptr<PluginInsert> insert = boost::dynamic_pointer_cast<PluginInsert> (p);
193
194         if (insert) {
195                 boost::shared_ptr<Plugin> pp = insert->plugin();
196                 
197                 if (pp->current_preset_uses_general_midi()) {
198                         return MIDI::Name::general_midi_program_names[std::min((uint8_t) 127,program)];
199                 }
200         }
201
202         return string_compose (_("preset %1 (bank %2)"), (int) program, (int) bank);
203 }