1 /*
2  * Copyright (C) 2012-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2013-2014 David Robillard <d@drobilla.net>
4  * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 
21 #include <algorithm>
22 
23 #include "pbd/compose.h"
24 
25 #include "midi++/midnam_patch.h"
26 
27 #include "ardour/instrument_info.h"
28 #include "ardour/midi_patch_manager.h"
29 #include "ardour/plugin.h"
30 #include "ardour/plugin_insert.h"
31 #include "ardour/processor.h"
32 #include "ardour/rc_configuration.h"
33 
34 #include "pbd/i18n.h"
35 
36 using namespace ARDOUR;
37 using namespace MIDI::Name;
38 using std::string;
39 
InstrumentInfo()40 InstrumentInfo::InstrumentInfo ()
41 	: _external_instrument_model (_("Unknown"))
42 {
43 }
44 
~InstrumentInfo()45 InstrumentInfo::~InstrumentInfo ()
46 {
47 }
48 
49 void
set_external_instrument(const string & model,const string & mode)50 InstrumentInfo::set_external_instrument (const string& model, const string& mode)
51 {
52 	invalidate_cached_plugin_model ();
53 	if (_external_instrument_model == model && _external_instrument_mode == mode) {
54 		//std::cerr << "InstrumentInfo::set_external_instrument '" << model << "' '" << mode << "' -- no change\n";
55 		return;
56 	}
57 	//std::cerr << "InstrumentInfo::set_external_instrument '" << model << "' '" << mode << "'\n";
58 	_external_instrument_model = model;
59 	_external_instrument_mode  = mode;
60 	Changed (); /* EMIT SIGNAL */
61 }
62 
63 void
set_internal_instrument(boost::shared_ptr<Processor> p)64 InstrumentInfo::set_internal_instrument (boost::shared_ptr<Processor> p)
65 {
66 	invalidate_cached_plugin_model ();
67 	if (p == internal_instrument.lock ()) {
68 		//std::cerr << "InstrumentInfo::set_internal_instrument -- NO CHANGE\n";
69 		return;
70 	}
71 	_midnam_changed.disconnect ();
72 	//std::cerr << "InstrumentInfo::set_internal_instrument -> '" << (p ? p->name () : "(NULL)") << "'\n";
73 	internal_instrument = p;
74 	if (_external_instrument_model.empty () || _external_instrument_model == _("Unknown")) {
75 		Changed (); /* EMIT SIGNAL */
76 	}
77 
78 	boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
79 	if (pi && pi->plugin ()->has_midnam ()) {
80 		pi->plugin()->UpdatedMidnam.connect_same_thread (_midnam_changed, boost::bind (&InstrumentInfo::emit_changed, this));
81 	}
82 }
83 
84 void
emit_changed()85 InstrumentInfo::emit_changed () {
86 	if (_external_instrument_model.empty ()) {
87 		Changed ();
88 	}
89 }
90 
91 bool
have_custom_plugin_info() const92 InstrumentInfo::have_custom_plugin_info () const
93 {
94 	boost::shared_ptr<Processor> p = internal_instrument.lock ();
95 
96 	boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
97 	if (pi && pi->plugin ()->has_midnam ()) {
98 		std::string                  model        = pi->plugin ()->midnam_model ();
99 		const std::list<std::string> device_modes = MidiPatchManager::instance ().custom_device_mode_names_by_model (model);
100 		if (device_modes.size () > 0) {
101 			return true;
102 		}
103 	}
104 	return false;
105 }
106 
107 std::string
model() const108 InstrumentInfo::model () const
109 {
110 	if (!_external_instrument_model.empty ()) {
111 		return _external_instrument_model;
112 	}
113 	if (!_plugin_model.empty ()) {
114 		return _plugin_model;
115 	}
116 	boost::shared_ptr<Processor>    p  = internal_instrument.lock ();
117 	boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
118 	if (pi && pi->plugin ()->has_midnam ()) {
119 		_plugin_model = pi->plugin ()->midnam_model ();
120 		return _plugin_model;
121 	}
122 	return "";
123 }
124 
125 std::string
mode() const126 InstrumentInfo::mode () const
127 {
128 	if (!_external_instrument_model.empty ()) {
129 		return _external_instrument_mode;
130 	}
131 	if (!_plugin_mode.empty ()) {
132 		return _plugin_mode;
133 	}
134 	boost::shared_ptr<Processor>    p  = internal_instrument.lock ();
135 	boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (p);
136 	if (pi && pi->plugin ()->has_midnam ()) {
137 		const std::list<std::string> device_modes = MidiPatchManager::instance ().custom_device_mode_names_by_model (model ());
138 		if (device_modes.size () > 0) {
139 			_plugin_mode = device_modes.front ();
140 			return _plugin_mode;
141 		}
142 	}
143 	return "";
144 }
145 
146 string
get_note_name(uint16_t bank,uint8_t program,uint8_t channel,uint8_t note) const147 InstrumentInfo::get_note_name (uint16_t bank, uint8_t program, uint8_t channel, uint8_t note) const
148 {
149 	boost::shared_ptr<MasterDeviceNames> const& dev_names (MidiPatchManager::instance ().master_device_by_model (model ()));
150 	if (dev_names) {
151 		return dev_names->note_name (mode (), channel, bank, program, note);
152 	}
153 	return "";
154 }
155 
156 boost::shared_ptr<const ValueNameList>
value_name_list_by_control(uint8_t channel,uint8_t number) const157 InstrumentInfo::value_name_list_by_control (uint8_t channel, uint8_t number) const
158 {
159 	boost::shared_ptr<MasterDeviceNames> const& dev_names (MidiPatchManager::instance ().master_device_by_model (model ()));
160 	if (dev_names) {
161 		return dev_names->value_name_list_by_control (mode (), channel, number);
162 	}
163 	return boost::shared_ptr<const ValueNameList> ();
164 }
165 
166 boost::shared_ptr<MasterDeviceNames>
master_device_names() const167 InstrumentInfo::master_device_names () const
168 {
169 #if 1
170 	/* this safe if model does not exist */
171 	boost::shared_ptr<MIDINameDocument> midnam = MidiPatchManager::instance ().document_by_model (model ());
172 	if (midnam) {
173 		return midnam->master_device_names (model ());
174 	}
175 	return boost::shared_ptr<MasterDeviceNames> ();
176 #else
177 	return MidiPatchManager::instance ().master_device_by_model (model ());
178 #endif
179 }
180 
181 /* reverse lookup which "ChannelNameSet" use "UsesControlNameList <name>",
182  * then add all channels that the ChannelNameSet is AvailableForChannels.
183  */
184 uint16_t
channels_for_control_list(std::string const & ctrl_name_list) const185 InstrumentInfo::channels_for_control_list (std::string const& ctrl_name_list) const
186 {
187 	boost::shared_ptr<MasterDeviceNames> const& dev_names (MidiPatchManager::instance ().master_device_by_model (model ()));
188 	uint16_t channels = 0;
189 	for (int c = 0; c < 16; ++c) {
190 		boost::shared_ptr<ChannelNameSet> const& chan_names (dev_names->channel_name_set_by_channel (mode (), c));
191 		if (!chan_names || !chan_names->available_for_channel (c + 1)) {
192 			continue;
193 		}
194 		if (chan_names->control_list_name () == ctrl_name_list) {
195 			channels |= 0x0001 << c;
196 		}
197 	}
198 	if (channels == 0) {
199 		channels = 65535;
200 	}
201 	return channels;
202 }
203 
204 boost::shared_ptr<ControlNameList>
control_name_list(uint8_t channel)205 InstrumentInfo::control_name_list (uint8_t channel)
206 {
207 	boost::shared_ptr<MasterDeviceNames> const& dev_names (MidiPatchManager::instance ().master_device_by_model (model ()));
208 	boost::shared_ptr<ChannelNameSet> const&    chan_names (dev_names->channel_name_set_by_channel (mode (), channel));
209 	if (!chan_names) {
210 		return boost::shared_ptr<ControlNameList> ();
211 	}
212 	return dev_names->control_name_list (chan_names->control_list_name ());
213 }
214 
215 size_t
master_controller_count() const216 InstrumentInfo::master_controller_count () const
217 {
218 	boost::shared_ptr<MasterDeviceNames> const& dev_names (MidiPatchManager::instance ().master_device_by_model (model ()));
219 	if (!dev_names) {
220 		return 0;
221 	}
222 	MasterDeviceNames::ControlNameLists const& ctllist (dev_names->controls());
223 
224 	size_t total_ctrls = 0;
225 	for (MasterDeviceNames::ControlNameLists::const_iterator l = ctllist.begin(); l != ctllist.end(); ++l) {
226 		boost::shared_ptr<ControlNameList> const& name_list = l->second;
227 		total_ctrls += name_list->controls().size();
228 	}
229 	return total_ctrls;
230 }
231 
232 #if 0
233 MasterDeviceNames::ControlNameLists const&
234 InstrumentInfo::master_control_names () const
235 {
236 	static MasterDeviceNames::ControlNameLists empty_list;
237 
238 	boost::shared_ptr<MasterDeviceNames> const& dev_names (MidiPatchManager::instance ().master_device_by_model (model ()));
239 	if (dev_names) {
240 		return dev_names->controls();
241 	}
242 	return empty_list;
243 }
244 #endif
245 
246 string
get_patch_name(uint16_t bank,uint8_t program,uint8_t channel) const247 InstrumentInfo::get_patch_name (uint16_t bank, uint8_t program, uint8_t channel) const
248 {
249 	return get_patch_name (bank, program, channel, true);
250 }
251 
252 string
get_patch_name_without(uint16_t bank,uint8_t program,uint8_t channel) const253 InstrumentInfo::get_patch_name_without (uint16_t bank, uint8_t program, uint8_t channel) const
254 {
255 	return get_patch_name (bank, program, channel, false);
256 }
257 
258 string
get_patch_name(uint16_t bank,uint8_t program,uint8_t channel,bool with_extra) const259 InstrumentInfo::get_patch_name (uint16_t bank, uint8_t program, uint8_t channel, bool with_extra) const
260 {
261 	PatchPrimaryKey patch_key (program, bank);
262 
263 	boost::shared_ptr<MIDI::Name::Patch> const& patch (MidiPatchManager::instance ().find_patch (model (), mode (), channel, patch_key));
264 
265 	if (patch) {
266 		return patch->name ();
267 	} else {
268 		/* program and bank numbers are zero-based: convert to one-based: MIDI_BP_ZERO */
269 
270 #define MIDI_BP_ZERO ((Config->get_first_midi_bank_is_zero ()) ? 0 : 1)
271 
272 		if (with_extra) {
273 			return string_compose ("prg %1 bnk %2", program + MIDI_BP_ZERO, bank + MIDI_BP_ZERO);
274 		} else {
275 			return string_compose ("%1", program + MIDI_BP_ZERO);
276 		}
277 	}
278 }
279 
280 string
get_controller_name(Evoral::Parameter param) const281 InstrumentInfo::get_controller_name (Evoral::Parameter param) const
282 {
283 	if (param.type () != MidiCCAutomation) {
284 		return "";
285 	}
286 
287 	boost::shared_ptr<MasterDeviceNames> const& dev_names (MidiPatchManager::instance ().master_device_by_model (model ()));
288 	if (!dev_names) {
289 		return "";
290 	}
291 
292 	boost::shared_ptr<ChannelNameSet> const& chan_names (dev_names->channel_name_set_by_channel (mode (), param.channel ()));
293 	if (!chan_names) {
294 		return "";
295 	}
296 
297 	boost::shared_ptr<ControlNameList> const& control_names (dev_names->control_name_list (chan_names->control_list_name ()));
298 	if (!control_names) {
299 		return "";
300 	}
301 	boost::shared_ptr<const Control> const& c = control_names->control (param.id ());
302 
303 	if (c) {
304 		return string_compose (c->name () + " [%1]", int(param.channel ()) + 1);
305 	}
306 
307 	return "";
308 }
309 
310 boost::shared_ptr<ChannelNameSet>
get_patches(uint8_t channel)311 InstrumentInfo::get_patches (uint8_t channel)
312 {
313 	return MidiPatchManager::instance ().find_channel_name_set (model (), mode (), channel);
314 }
315