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