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