95136c29518ceeb0663b6826c30544c550e46df6
[ardour.git] / libs / ardour / panner_manager.cc
1 /*
2     Copyright (C) 2011 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
20 #include <unistd.h>
21
22 #include <glibmm/pattern.h>
23 #include <glibmm/fileutils.h>
24
25 #include "pbd/error.h"
26 #include "pbd/compose.h"
27 #include "pbd/pathscanner.h"
28 #include "pbd/stl_delete.h"
29
30 #include "ardour/debug.h"
31 #include "ardour/panner_manager.h"
32
33 #include "ardour/search_paths.h"
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40
41 PannerManager* PannerManager::_instance = 0;
42
43 PannerManager::PannerManager ()
44 {
45 }
46
47 PannerManager::~PannerManager ()
48 {
49         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
50                 delete *p;
51         }
52 }
53
54 PannerManager&
55 PannerManager::instance ()
56 {
57         if (_instance == 0) {
58                 _instance = new PannerManager ();
59         }
60
61         return *_instance;
62 }
63
64 static bool panner_filter (const string& str, void */*arg*/)
65 {
66 #ifdef COMPILER_MSVC
67    /**
68     * Different build targets (Debug / Release etc) use different versions
69     * of the 'C' runtime (which can't be 'mixed & matched'). Therefore, in
70     * case the supplied search path contains multiple version(s) of a given
71     * panner module, only select the one(s) which match the current build
72     * target (otherwise, all hell will break loose !!)
73     */
74         #if defined (_DEBUG)
75                 return str.length() > 12 && (str.find ("panner_") == 0) && (str.find ("D.dll") == (str.length() - 5));
76         #elif defined (RDC_BUILD)
77                 return str.length() > 14 && (str.find ("panner_") == 0) && (str.find ("RDC.dll") == (str.length() - 7));
78         #elif defined (_WIN64)
79                 return str.length() > 13 && (str.find ("panner_") == 0) && (str.find ("64.dll") == (str.length() - 6));
80         #else
81                 return str.length() > 13 && (str.find ("panner_") == 0) && (str.find ("32.dll") == (str.length() - 6));
82         #endif
83 #elif defined (__APPLE__)
84         return str[0] != '.' && (str.length() > 6 && str.find (".dylib") == (str.length() - 6));
85 #else
86         return str[0] != '.' && (str.length() > 3 && (str.find (".so") == (str.length() - 3) || str.find (".dll") == (str.length() - 4)));
87 #endif
88 }
89
90 void
91 PannerManager::discover_panners ()
92 {
93         PathScanner scanner;
94         std::vector<std::string *> *panner_modules;
95         std::string search_path = panner_search_path().to_string();
96
97         DEBUG_TRACE (DEBUG::Panning, string_compose (_("looking for panners in %1\n"), search_path));
98
99         panner_modules = scanner (search_path, panner_filter, 0, false, true, 1, true);
100
101         for (vector<std::string *>::iterator i = panner_modules->begin(); i != panner_modules->end(); ++i) {
102                 panner_discover (**i);
103         }
104
105         vector_delete (panner_modules);
106         delete (panner_modules);
107 }
108
109 int
110 PannerManager::panner_discover (string path)
111 {
112         PannerInfo* pinfo;
113
114         if ((pinfo = get_descriptor (path)) != 0) {
115
116                 list<PannerInfo*>::iterator i;
117
118                 for (i = panner_info.begin(); i != panner_info.end(); ++i) {
119                         if (pinfo->descriptor.name == (*i)->descriptor.name) {
120                                 break;
121                         }
122                 }
123
124                 if (i == panner_info.end()) {
125                         panner_info.push_back (pinfo);
126                         DEBUG_TRACE (DEBUG::Panning, string_compose(_("Panner discovered: \"%1\" in %2\n"), pinfo->descriptor.name, path));
127                 } else {
128                         delete pinfo;
129                 }
130         }
131
132         return 0;
133 }
134
135 PannerInfo*
136 PannerManager::get_descriptor (string path)
137 {
138         Glib::Module* module = new Glib::Module(path);
139         PannerInfo* info = 0;
140         PanPluginDescriptor *descriptor = 0;
141         PanPluginDescriptor* (*dfunc)(void);
142         void* func = 0;
143
144         if (!module) {
145                 error << string_compose(_("PannerManager: cannot load module \"%1\" (%2)"), path,
146                                 Glib::Module::get_last_error()) << endmsg;
147                 delete module;
148                 return 0;
149         }
150
151         if (!module->get_symbol("panner_descriptor", func)) {
152                 error << string_compose(_("PannerManager: module \"%1\" has no descriptor function."), path) << endmsg;
153                 error << Glib::Module::get_last_error() << endmsg;
154                 delete module;
155                 return 0;
156         }
157
158         dfunc = (PanPluginDescriptor* (*)(void))func;
159         descriptor = dfunc();
160
161         if (descriptor) {
162                 info = new PannerInfo (*descriptor, module);
163         } else {
164                 delete module;
165         }
166
167         return info;
168 }
169
170 PannerInfo*
171 PannerManager::select_panner (ChanCount in, ChanCount out, std::string const uri)
172 {
173         PannerInfo* rv = NULL;
174         PanPluginDescriptor* d;
175         int32_t nin = in.n_audio();
176         int32_t nout = out.n_audio();
177         uint32_t priority = 0;
178
179         /* look for user-preference -- check if channels match */
180         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
181                 d = &(*p)->descriptor;
182                 if (d->panner_uri != uri) continue;
183                 if (d->in != nin && d->in != -1) continue;
184                 if (d->out != nout && d->out != -1) continue;
185                 return *p;
186         }
187
188         /* look for exact match first */
189
190         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
191                 d = &(*p)->descriptor;
192
193                 if (d->in == nin && d->out == nout && d->priority > priority) {
194                         priority = d->priority;
195                         rv = *p;
196                 }
197         }
198         if (rv) { return rv; }
199
200         /* no exact match, look for good fit on inputs and variable on outputs */
201
202         priority = 0;
203         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
204                 d = &(*p)->descriptor;
205
206                 if (d->in == nin && d->out == -1 && d->priority > priority) {
207                         priority = d->priority;
208                         rv = *p;
209                 }
210         }
211         if (rv) { return rv; }
212
213         /* no exact match, look for good fit on outputs and variable on inputs */
214
215         priority = 0;
216         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
217                 d = &(*p)->descriptor;
218
219                 if (d->in == -1 && d->out == nout && d->priority > priority) {
220                         priority = d->priority;
221                         rv = *p;
222                 }
223         }
224         if (rv) { return rv; }
225
226         /* no exact match, look for variable fit on inputs and outputs */
227
228         priority = 0;
229         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
230                 d = &(*p)->descriptor;
231
232                 if (d->in == -1 && d->out == -1 && d->priority > priority) {
233                         priority = d->priority;
234                         rv = *p;
235                 }
236         }
237         if (rv) { return rv; }
238
239         warning << string_compose (_("no panner discovered for in/out = %1/%2"), nin, nout) << endmsg;
240
241         return 0;
242 }
243
244 PannerInfo*
245 PannerManager::get_by_uri (std::string uri) const
246 {
247         PannerInfo* pi = NULL;
248         for (list<PannerInfo*>::const_iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
249                 if ((*p)->descriptor.panner_uri != uri) continue;
250                 pi = (*p);
251                 break;
252         }
253         return pi;
254 }
255
256 PannerUriMap
257 PannerManager::get_available_panners(uint32_t const a_in, uint32_t const a_out) const
258 {
259         int const in = a_in;
260         int const out = a_out;
261         PannerUriMap panner_list;
262
263         if (out < 2 || in == 0) {
264                 return panner_list;
265         }
266
267         /* get available panners for current configuration. */
268         for (list<PannerInfo*>::const_iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
269                  PanPluginDescriptor* d = &(*p)->descriptor;
270                  if (d->in != -1 && d->in != in) continue;
271                  if (d->out != -1 && d->out != out) continue;
272                  if (d->in == -1 && d->out == -1 && out <= 2) continue;
273                  panner_list.insert(std::pair<std::string,std::string>(d->panner_uri,d->name));
274         }
275         return panner_list;
276 }