some new source
[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         cerr << "Need match for in = " << nin << " out = " << nout << endl;
111
112         /* look for exact match first */
113
114         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
115                 d = &(*p)->descriptor;
116
117                 cerr << "\t1. Check panner with in=" << d->in << " out=" << d->out << endl;
118
119                 if (d->in == nin && d->out == nout) {
120                         return *p;
121                 }
122         }
123
124         /* no exact match, look for good fit on inputs and variable on outputs */
125
126         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
127                 d = &(*p)->descriptor;
128          
129                 cerr << "\t2. Check panner with in=" << d->in << " out=" << d->out << endl;
130
131                 if (d->in == nin && d->out == -1) {
132                         return *p;
133                 }
134         }
135
136         /* no exact match, look for good fit on outputs and variable on inputs */
137
138         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
139                 d = &(*p)->descriptor;
140          
141                 cerr << "\t3. Check panner with in=" << d->in << " out=" << d->out << endl;
142
143                 if (d->in == -1 && d->out == nout) {
144                         return *p;
145                 }
146         }
147
148         /* no exact match, look for variable fit on inputs and outputs */
149
150         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
151                 d = &(*p)->descriptor;
152          
153                 cerr << "\t4. Check panner with in=" << d->in << " out=" << d->out << endl;
154
155                 if (d->in == -1 && d->out == -1) {
156                         return *p;
157                 }
158         }
159
160         warning << string_compose (_("no panner discovered for in/out = %1/%2"), nin, nout) << endmsg;
161
162         return 0;
163 }