Inrease the export "chunk size" to speed it up over 10% at least in some situations
[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 #include "ardour/panner_search_path.h"
32
33 #include "i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 PannerManager* PannerManager::_instance = 0;
40
41 PannerManager::PannerManager ()
42 {
43 }
44
45 PannerManager::~PannerManager ()
46 {
47         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
48                 delete *p;
49         }
50 }
51
52 PannerManager&
53 PannerManager::instance ()
54 {
55         if (_instance == 0) {
56                 _instance = new PannerManager ();
57         }
58
59         return *_instance;
60 }
61
62 void
63 PannerManager::discover_panners ()
64 {
65         vector<std::string> panner_modules;
66
67         Glib::PatternSpec so_extension_pattern("*.so");
68         Glib::PatternSpec dylib_extension_pattern("*.dylib");
69
70         find_matching_files_in_search_path (panner_search_path (),
71                                             so_extension_pattern, panner_modules);
72
73         find_matching_files_in_search_path (panner_search_path (),
74                                             dylib_extension_pattern, panner_modules);
75
76         DEBUG_TRACE (DEBUG::Panning, string_compose (_("looking for panners in %1"), panner_search_path().to_string()));
77
78         for (vector<std::string>::iterator i = panner_modules.begin(); i != panner_modules.end(); ++i) {
79                 panner_discover (*i);
80         }
81 }
82 int
83 PannerManager::panner_discover (string path)
84 {
85         PannerInfo* pinfo;
86
87         if ((pinfo = get_descriptor (path)) != 0) {
88
89                 list<PannerInfo*>::iterator i;
90
91                 for (i = panner_info.begin(); i != panner_info.end(); ++i) {
92                         if (pinfo->descriptor.name == (*i)->descriptor.name) {
93                                 break;
94                         }
95                 }
96
97                 if (i == panner_info.end()) {
98                         panner_info.push_back (pinfo);
99                         DEBUG_TRACE (DEBUG::Panning, string_compose(_("Panner discovered: \"%1\" in %2"), pinfo->descriptor.name, path));
100                 }
101         }
102
103         return 0;
104 }
105
106 PannerInfo*
107 PannerManager::get_descriptor (string path)
108 {
109         void *module;
110         PannerInfo* info = 0;
111         PanPluginDescriptor *descriptor = 0;
112         PanPluginDescriptor* (*dfunc)(void);
113         const char *errstr;
114
115         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
116                 error << string_compose(_("PannerManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
117                 return 0;
118         }
119
120         dfunc = (PanPluginDescriptor* (*)(void)) dlsym (module, "panner_descriptor");
121
122         if ((errstr = dlerror()) != 0) {
123                 error << string_compose(_("PannerManager: module \"%1\" has no descriptor function."), path) << endmsg;
124                 error << errstr << endmsg;
125                 dlclose (module);
126                 return 0;
127         }
128
129         descriptor = dfunc();
130         if (descriptor) {
131                 info = new PannerInfo (*descriptor, module);
132         } else {
133                 dlclose (module);
134         }
135
136         return info;
137 }
138
139 PannerInfo*
140 PannerManager::select_panner (ChanCount in, ChanCount out)
141 {
142         PanPluginDescriptor* d;
143         int32_t nin = in.n_audio();
144         int32_t nout = out.n_audio();
145
146         /* look for exact match first */
147
148         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
149                 d = &(*p)->descriptor;
150
151                 if (d->in == nin && d->out == nout) {
152                         return *p;
153                 }
154         }
155
156         /* no exact match, look for good fit on inputs and variable on outputs */
157
158         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
159                 d = &(*p)->descriptor;
160
161                 if (d->in == nin && d->out == -1) {
162                         return *p;
163                 }
164         }
165
166         /* no exact match, look for good fit on outputs and variable on inputs */
167
168         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
169                 d = &(*p)->descriptor;
170
171                 if (d->in == -1 && d->out == nout) {
172                         return *p;
173                 }
174         }
175
176         /* no exact match, look for variable fit on inputs and outputs */
177
178         for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
179                 d = &(*p)->descriptor;
180
181                 if (d->in == -1 && d->out == -1) {
182                         return *p;
183                 }
184         }
185
186         warning << string_compose (_("no panner discovered for in/out = %1/%2"), nin, nout) << endmsg;
187
188         return 0;
189 }