add a fixed priority to panner modules
[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 #include "ardour/panner_search_path.h"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
39
40 PannerManager* PannerManager::_instance = 0;
41
42 PannerManager::PannerManager ()
43 {
44 }
45
46 PannerManager::~PannerManager ()
47 {
48         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
49                 delete *p;
50         }
51 }
52
53 PannerManager&
54 PannerManager::instance ()
55 {
56         if (_instance == 0) {
57                 _instance = new PannerManager ();
58         }
59
60         return *_instance;
61 }
62
63 static bool panner_filter (const string& str, void */*arg*/)
64 {
65 #ifdef __APPLE__
66         return str[0] != '.' && (str.length() > 6 && str.find (".dylib") == (str.length() - 6));
67 #else
68         return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
69 #endif
70 }
71
72 void
73 PannerManager::discover_panners ()
74 {
75         PathScanner scanner;
76         std::vector<std::string *> *panner_modules;
77         std::string search_path = panner_search_path().to_string();
78
79         DEBUG_TRACE (DEBUG::Panning, string_compose (_("looking for panners in %1\n"), search_path));
80
81         panner_modules = scanner (search_path, panner_filter, 0, false, true, 1, true);
82
83         for (vector<std::string *>::iterator i = panner_modules->begin(); i != panner_modules->end(); ++i) {
84                 panner_discover (**i);
85         }
86         vector_delete (panner_modules);
87 }
88
89 int
90 PannerManager::panner_discover (string path)
91 {
92         PannerInfo* pinfo;
93
94         if ((pinfo = get_descriptor (path)) != 0) {
95
96                 list<PannerInfo*>::iterator i;
97
98                 for (i = panner_info.begin(); i != panner_info.end(); ++i) {
99                         if (pinfo->descriptor.name == (*i)->descriptor.name) {
100                                 break;
101                         }
102                 }
103
104                 if (i == panner_info.end()) {
105                         panner_info.push_back (pinfo);
106                         DEBUG_TRACE (DEBUG::Panning, string_compose(_("Panner discovered: \"%1\" in %2\n"), pinfo->descriptor.name, path));
107                 }
108         }
109
110         return 0;
111 }
112
113 PannerInfo*
114 PannerManager::get_descriptor (string path)
115 {
116         void *module;
117         PannerInfo* info = 0;
118         PanPluginDescriptor *descriptor = 0;
119         PanPluginDescriptor* (*dfunc)(void);
120         const char *errstr;
121
122         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
123                 error << string_compose(_("PannerManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
124                 return 0;
125         }
126
127         dfunc = (PanPluginDescriptor* (*)(void)) dlsym (module, "panner_descriptor");
128
129         if ((errstr = dlerror()) != 0) {
130                 error << string_compose(_("PannerManager: module \"%1\" has no descriptor function."), path) << endmsg;
131                 error << errstr << endmsg;
132                 dlclose (module);
133                 return 0;
134         }
135
136         descriptor = dfunc();
137         if (descriptor) {
138                 info = new PannerInfo (*descriptor, module);
139         } else {
140                 dlclose (module);
141         }
142
143         return info;
144 }
145
146 PannerInfo*
147 PannerManager::select_panner (ChanCount in, ChanCount out, std::string const uri)
148 {
149         PannerInfo* rv = NULL;
150         PanPluginDescriptor* d;
151         int32_t nin = in.n_audio();
152         int32_t nout = out.n_audio();
153         uint32_t priority = 0;
154
155         /* look for user-preference -- check if channels match */
156         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
157                 d = &(*p)->descriptor;
158                 if (d->panner_uri != uri) continue;
159                 if (d->in != nin && d->in != -1) continue;
160                 if (d->out != nout && d->out != -1) continue;
161                 return *p;
162         }
163
164         /* look for exact match first */
165
166         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
167                 d = &(*p)->descriptor;
168
169                 if (d->in == nin && d->out == nout && d->priority > priority) {
170                         priority = d->priority;
171                         rv = *p;
172                 }
173         }
174         if (rv) { return rv; }
175
176         /* no exact match, look for good fit on inputs and variable on outputs */
177
178         priority = 0;
179         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
180                 d = &(*p)->descriptor;
181
182                 if (d->in == nin && d->out == -1 && d->priority > priority) {
183                         priority = d->priority;
184                         rv = *p;
185                 }
186         }
187         if (rv) { return rv; }
188
189         /* no exact match, look for good fit on outputs and variable on inputs */
190
191         priority = 0;
192         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
193                 d = &(*p)->descriptor;
194
195                 if (d->in == -1 && d->out == nout && d->priority > priority) {
196                         priority = d->priority;
197                         rv = *p;
198                 }
199         }
200         if (rv) { return rv; }
201
202         /* no exact match, look for variable fit on inputs and outputs */
203
204         priority = 0;
205         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
206                 d = &(*p)->descriptor;
207
208                 if (d->in == -1 && d->out == -1 && d->priority > priority) {
209                         priority = d->priority;
210                         rv = *p;
211                 }
212         }
213         if (rv) { return rv; }
214
215         warning << string_compose (_("no panner discovered for in/out = %1/%2"), nin, nout) << endmsg;
216
217         return 0;
218 }
219
220 PannerInfo*
221 PannerManager::get_by_uri (std::string uri)
222 {
223         PannerInfo* pi = NULL;
224         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
225                 if ((*p)->descriptor.panner_uri != uri) continue;
226                 pi = (*p);
227                 break;
228         }
229         return pi;
230 }