remove debug output
[ardour.git] / libs / ardour / panner_manager.cc
1 #include <glibmm/pattern.h>
2
3 #include "pbd/error.h"
4 #include "pbd/compose.h"
5 #include "pbd/file_utils.h"
6
7 #include "ardour/panner_manager.h"
8 #include "ardour/panner_search_path.h"
9
10 using namespace std;
11 using namespace ARDOUR;
12 using namespace PBD;
13
14 PannerManager* PannerManager::_instance = 0;
15
16 PannerManager::PannerManager ()
17 {
18 }
19
20 PannerManager::~PannerManager ()
21 {
22         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
23                 delete *p;
24         }
25 }
26
27 PannerManager&
28 PannerManager::instance ()
29 {
30         if (_instance == 0) {
31                 _instance = new PannerManager ();
32         }
33
34         return *_instance;
35 }
36
37 void
38 PannerManager::discover_panners ()
39 {
40         vector<sys::path> panner_modules;
41
42         Glib::PatternSpec so_extension_pattern("*.so");
43         Glib::PatternSpec dylib_extension_pattern("*.dylib");
44
45         find_matching_files_in_search_path (panner_search_path (),
46                         so_extension_pattern, panner_modules);
47
48         find_matching_files_in_search_path (panner_search_path (),
49                         dylib_extension_pattern, panner_modules);
50
51         info << string_compose (_("looking for panners in %1"), panner_search_path().to_string()) << endmsg;
52
53         for (vector<sys::path>::iterator i = panner_modules.begin(); i != panner_modules.end(); ++i) {
54                 panner_discover ((*i).to_string());
55         }
56 }
57 int
58 PannerManager::panner_discover (string path)
59 {
60         PannerInfo* pinfo;
61
62         if ((pinfo = get_descriptor (path)) != 0) {
63                 panner_info.push_back (pinfo);
64                 info << string_compose(_("Panner discovered: \"%1\""), pinfo->descriptor.name) << endmsg;
65         }
66
67         return 0;
68 }
69
70 PannerInfo*
71 PannerManager::get_descriptor (string path)
72 {
73         void *module;
74         PannerInfo* info = 0;
75         PanPluginDescriptor *descriptor = 0;
76         PanPluginDescriptor* (*dfunc)(void);
77         const char *errstr;
78
79         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
80                 error << string_compose(_("PannerManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
81                 return 0;
82         }
83
84         dfunc = (PanPluginDescriptor* (*)(void)) dlsym (module, "panner_descriptor");
85
86         if ((errstr = dlerror()) != 0) {
87                 error << string_compose(_("PannerManager: module \"%1\" has no descriptor function."), path) << endmsg;
88                 error << errstr << endmsg;
89                 dlclose (module);
90                 return 0;
91         }
92
93         descriptor = dfunc();
94         if (descriptor) {
95                 info = new PannerInfo (*descriptor, module);
96         } else {
97                 dlclose (module);
98         }
99
100         return info;
101 }
102
103 PannerInfo*
104 PannerManager::select_panner (ChanCount in, ChanCount out)
105 {
106         PanPluginDescriptor* d;
107         int32_t nin = in.n_audio();
108         int32_t nout = out.n_audio();
109         
110         /* look for exact match first */
111
112         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
113                 d = &(*p)->descriptor;
114
115                 if (d->in == nin && d->out == nout) {
116                         return *p;
117                 }
118         }
119
120         /* no exact match, look for good fit on inputs and variable on outputs */
121
122         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
123                 d = &(*p)->descriptor;
124          
125                 if (d->in == nin && d->out == -1) {
126                         return *p;
127                 }
128         }
129
130         /* no exact match, look for good fit on outputs and variable on inputs */
131
132         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
133                 d = &(*p)->descriptor;
134          
135                 if (d->in == -1 && d->out == nout) {
136                         return *p;
137                 }
138         }
139
140         /* no exact match, look for variable fit on inputs and outputs */
141
142         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
143                 d = &(*p)->descriptor;
144          
145                 if (d->in == -1 && d->out == -1) {
146                         return *p;
147                 }
148         }
149
150         warning << string_compose (_("no panner discovered for in/out = %1/%2"), nin, nout) << endmsg;
151
152         return 0;
153 }