Rename SearchPath class Searchpath
[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/file_utils.h"
28
29 #include "ardour/debug.h"
30 #include "ardour/panner_manager.h"
31
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 void
64 PannerManager::discover_panners ()
65 {
66         vector<std::string> panner_modules;
67
68         Glib::PatternSpec so_extension_pattern("*.so");
69         Glib::PatternSpec dylib_extension_pattern("*.dylib");
70         Glib::PatternSpec dll_extension_pattern("*.dll");
71
72         find_matching_files_in_search_path (panner_search_path (),
73                                             so_extension_pattern, panner_modules);
74
75         find_matching_files_in_search_path (panner_search_path (),
76                                             dylib_extension_pattern, panner_modules);
77
78         find_matching_files_in_search_path (panner_search_path (),
79                                             dll_extension_pattern, panner_modules);
80
81         DEBUG_TRACE (DEBUG::Panning, string_compose (_("looking for panners in %1"), panner_search_path().to_string()));
82
83         for (vector<std::string>::iterator i = panner_modules.begin(); i != panner_modules.end(); ++i) {
84                 panner_discover (*i);
85         }
86 }
87 int
88 PannerManager::panner_discover (string path)
89 {
90         PannerInfo* pinfo;
91
92         if ((pinfo = get_descriptor (path)) != 0) {
93
94                 list<PannerInfo*>::iterator i;
95
96                 for (i = panner_info.begin(); i != panner_info.end(); ++i) {
97                         if (pinfo->descriptor.name == (*i)->descriptor.name) {
98                                 break;
99                         }
100                 }
101
102                 if (i == panner_info.end()) {
103                         panner_info.push_back (pinfo);
104                         DEBUG_TRACE (DEBUG::Panning, string_compose(_("Panner discovered: \"%1\" in %2"), pinfo->descriptor.name, path));
105                 }
106         }
107
108         return 0;
109 }
110
111 PannerInfo*
112 PannerManager::get_descriptor (string path)
113 {
114         Glib::Module* module = new Glib::Module(path);
115         PannerInfo* info = 0;
116         PanPluginDescriptor *descriptor = 0;
117         PanPluginDescriptor* (*dfunc)(void);
118         void* func = 0;
119
120         if (!module) {
121                 error << string_compose(_("PannerManager: cannot load module \"%1\" (%2)"), path,
122                                 Glib::Module::get_last_error()) << endmsg;
123                 delete module;
124                 return 0;
125         }
126
127         if (!module->get_symbol("panner_descriptor", func)) {
128                 error << string_compose(_("PannerManager: module \"%1\" has no descriptor function."), path) << endmsg;
129                 error << Glib::Module::get_last_error() << endmsg;
130                 delete module;
131                 return 0;
132         }
133
134         dfunc = (PanPluginDescriptor* (*)(void))func;
135         descriptor = dfunc();
136
137         if (descriptor) {
138                 info = new PannerInfo (*descriptor, module);
139         } else {
140                 delete module;
141         }
142
143         return info;
144 }
145
146 PannerInfo*
147 PannerManager::select_panner (ChanCount in, ChanCount out)
148 {
149         PanPluginDescriptor* d;
150         int32_t nin = in.n_audio();
151         int32_t nout = out.n_audio();
152
153         /* look for exact match first */
154
155         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
156                 d = &(*p)->descriptor;
157
158                 if (d->in == nin && d->out == nout) {
159                         return *p;
160                 }
161         }
162
163         /* no exact match, look for good fit on inputs and variable on outputs */
164
165         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
166                 d = &(*p)->descriptor;
167
168                 if (d->in == nin && d->out == -1) {
169                         return *p;
170                 }
171         }
172
173         /* no exact match, look for good fit on outputs and variable on inputs */
174
175         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
176                 d = &(*p)->descriptor;
177
178                 if (d->in == -1 && d->out == nout) {
179                         return *p;
180                 }
181         }
182
183         /* no exact match, look for variable fit on inputs and outputs */
184
185         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
186                 d = &(*p)->descriptor;
187
188                 if (d->in == -1 && d->out == -1) {
189                         return *p;
190                 }
191         }
192
193         warning << string_compose (_("no panner discovered for in/out = %1/%2"), nin, nout) << endmsg;
194
195         return 0;
196 }