Merge branch 'master' into windows
[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 #ifdef SearchPath
33 #undef SearchPath
34 #endif
35
36 #include "ardour/panner_search_path.h"
37
38 #include "i18n.h"
39
40 using namespace std;
41 using namespace ARDOUR;
42 using namespace PBD;
43
44 PannerManager* PannerManager::_instance = 0;
45
46 PannerManager::PannerManager ()
47 {
48 }
49
50 PannerManager::~PannerManager ()
51 {
52         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
53                 delete *p;
54         }
55 }
56
57 PannerManager&
58 PannerManager::instance ()
59 {
60         if (_instance == 0) {
61                 _instance = new PannerManager ();
62         }
63
64         return *_instance;
65 }
66
67 void
68 PannerManager::discover_panners ()
69 {
70         vector<std::string> panner_modules;
71
72         Glib::PatternSpec so_extension_pattern("*.so");
73         Glib::PatternSpec dylib_extension_pattern("*.dylib");
74         Glib::PatternSpec dll_extension_pattern("*.dll");
75
76         find_matching_files_in_search_path (panner_search_path (),
77                                             so_extension_pattern, panner_modules);
78
79         find_matching_files_in_search_path (panner_search_path (),
80                                             dylib_extension_pattern, panner_modules);
81
82         find_matching_files_in_search_path (panner_search_path (),
83                                             dll_extension_pattern, panner_modules);
84
85         DEBUG_TRACE (DEBUG::Panning, string_compose (_("looking for panners in %1"), panner_search_path().to_string()));
86
87         for (vector<std::string>::iterator i = panner_modules.begin(); i != panner_modules.end(); ++i) {
88                 panner_discover (*i);
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"), 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)
152 {
153         PanPluginDescriptor* d;
154         int32_t nin = in.n_audio();
155         int32_t nout = out.n_audio();
156
157         /* look for exact match first */
158
159         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
160                 d = &(*p)->descriptor;
161
162                 if (d->in == nin && d->out == nout) {
163                         return *p;
164                 }
165         }
166
167         /* no exact match, look for good fit on inputs and variable on outputs */
168
169         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
170                 d = &(*p)->descriptor;
171
172                 if (d->in == nin && d->out == -1) {
173                         return *p;
174                 }
175         }
176
177         /* no exact match, look for good fit on outputs and variable on inputs */
178
179         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
180                 d = &(*p)->descriptor;
181
182                 if (d->in == -1 && d->out == nout) {
183                         return *p;
184                 }
185         }
186
187         /* no exact match, look for variable fit on inputs and outputs */
188
189         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
190                 d = &(*p)->descriptor;
191
192                 if (d->in == -1 && d->out == -1) {
193                         return *p;
194                 }
195         }
196
197         warning << string_compose (_("no panner discovered for in/out = %1/%2"), nin, nout) << endmsg;
198
199         return 0;
200 }