Disambiguate instrument plugin names:
[ardour.git] / gtk2_ardour / instrument_selector.cc
1 /*
2  * Copyright (C) 2016-2018 Robin Gareus <robin@gareus.org>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include "pbd/convert.h"
20 #include "pbd/enumwriter.h"
21 #include "ardour/chan_count.h"
22 #include "ardour/plugin_manager.h"
23 #include "gtkmm2ext/gui_thread.h"
24 #include "instrument_selector.h"
25
26 #include "pbd/i18n.h"
27
28 using namespace Gtk;
29 using namespace ARDOUR;
30
31 InstrumentSelector::InstrumentSelector()
32         : _reasonable_synth_id(0)
33         , _gmsynth_id(UINT32_MAX)
34 {
35         refill ();
36
37         PluginManager::instance ().PluginListChanged.connect (_update_connection, invalidator (*this), boost::bind (&InstrumentSelector::refill, this), gui_context());
38 }
39
40 void
41 InstrumentSelector::refill()
42 {
43         TreeModel::iterator iter = get_active();
44         std::string selected;
45         if (iter) {
46                 const TreeModel::Row& row = (*iter);
47                 selected = row[_instrument_list_columns.name];
48         }
49
50         unset_model ();
51         clear ();
52         build_instrument_list();
53         set_model(_instrument_list);
54         pack_start(_instrument_list_columns.name);
55         if (selected.empty ()) {
56                 if (_gmsynth_id != UINT32_MAX) {
57                         set_active(_gmsynth_id);
58                 } else {
59                         set_active(_reasonable_synth_id);
60                 }
61         } else {
62                 TreeModel::Children rows = _instrument_list->children();
63                 TreeModel::Children::iterator i;
64                 for (i = rows.begin(); i != rows.end(); ++i) {
65                         std::string cn = (*i)[_instrument_list_columns.name];
66                         if (cn == selected) {
67                                 set_active(*i);
68                                 break;
69                         }
70                 }
71         }
72         set_button_sensitivity(Gtk::SENSITIVITY_AUTO);
73 }
74
75 static bool
76 pluginsort (const PluginInfoPtr& a, const PluginInfoPtr& b)
77 {
78         return PBD::downcase(a->name) < PBD::downcase(b->name);
79 }
80
81 static bool
82 invalid_instrument (PluginInfoPtr p) {
83         const PluginManager& manager = PluginManager::instance();
84         if (manager.get_status(p) == PluginManager::Hidden) {
85                 return true;
86         }
87         return !p->is_instrument();
88 }
89
90 void
91 InstrumentSelector::build_instrument_list()
92 {
93         PluginManager& manager = PluginManager::instance();
94
95         PluginInfoList all_plugs;
96         all_plugs.insert(all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
97         all_plugs.insert(all_plugs.end(), manager.lua_plugin_info().begin(), manager.lua_plugin_info().end());
98 #ifdef WINDOWS_VST_SUPPORT
99         all_plugs.insert(all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
100 #endif
101 #ifdef LXVST_SUPPORT
102         all_plugs.insert(all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
103 #endif
104 #ifdef MACVST_SUPPORT
105         all_plugs.insert(all_plugs.end(), manager.mac_vst_plugin_info().begin(), manager.mac_vst_plugin_info().end());
106 #endif
107 #ifdef AUDIOUNIT_SUPPORT
108         all_plugs.insert(all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
109 #endif
110 #ifdef LV2_SUPPORT
111         all_plugs.insert(all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
112 #endif
113
114         all_plugs.remove_if (invalid_instrument);
115         all_plugs.sort (pluginsort);
116
117         _instrument_list = ListStore::create(_instrument_list_columns);
118
119         TreeModel::Row row = *(_instrument_list->append());
120         row[_instrument_list_columns.info_ptr] = PluginInfoPtr();
121         row[_instrument_list_columns.name]     = _("-none-");
122
123         uint32_t n = 1;
124         std::string prev;
125         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i, ++n) {
126                 PluginInfoPtr p = *i;
127                 
128                 std::string suffix;
129
130 #ifdef MIXBUS
131                 if (n_outs > 2) {
132                         suffix = string_compose(_("%1 outs"), n_outs);
133                 }
134 #else
135                 if (p->multichannel_name_ambiguity) {
136                         int n_outs = p->n_outputs.n_audio();
137                         if (n_outs > 2) {
138                                 suffix = string_compose(_("%1 outs"), n_outs);
139                         } else if (n_outs == 2) {
140                                 suffix = _("stereo");
141                         }
142                 }
143 #endif
144
145                 if (p->plugintype_name_ambiguity) {
146                         std::string pt;
147                         switch (p->type) {
148                                 case AudioUnit:
149                                         pt = "AU";
150                                         break;
151                                 case Windows_VST:
152                                 case LXVST:
153                                 case MacVST:
154                                         pt = "VST";
155                                         break;
156                                 default:
157                                         pt = enum_2_string (p->type);
158                         }
159                         if (!suffix.empty ()) {
160                                 suffix += ", ";
161                         }
162                         suffix += pt;
163                 }
164
165                 std::string name = p->name;
166                 if (!suffix.empty ()) {
167                         name += " (" + suffix + ")";
168                 }
169
170                 row = *(_instrument_list->append());
171                 row[_instrument_list_columns.name] = name;
172
173                 row[_instrument_list_columns.info_ptr] = p;
174                 if (p->unique_id == "https://community.ardour.org/node/7596") {
175                         _reasonable_synth_id = n;
176                 }
177                 if (p->unique_id == "http://gareus.org/oss/lv2/gmsynth") {
178                         _gmsynth_id = n;
179                 }
180                 prev = p->name;
181         }
182 }
183
184 PluginInfoPtr
185 InstrumentSelector::selected_instrument()
186 {
187         TreeModel::iterator iter = get_active();
188         if (!iter) {
189                 return PluginInfoPtr();
190         }
191
192         const TreeModel::Row& row = (*iter);
193         return row[_instrument_list_columns.info_ptr];
194 }