summaryrefslogtreecommitdiff
path: root/libs/ardour/panner_manager.cc
blob: 139101d2d95fa68184417159a95fcbd847879c70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <glibmm/pattern.h>

#include "pbd/error.h"
#include "pbd/compose.h"
#include "pbd/file_utils.h"

#include "ardour/panner_manager.h"
#include "ardour/panner_search_path.h"

using namespace std;
using namespace ARDOUR;
using namespace PBD;

PannerManager* PannerManager::_instance = 0;

PannerManager::PannerManager ()
{
}

PannerManager::~PannerManager ()
{
        for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
                delete *p;
        }
}

PannerManager&
PannerManager::instance ()
{
        if (_instance == 0) {
                _instance = new PannerManager ();
        }

        return *_instance;
}

void
PannerManager::discover_panners ()
{
	vector<sys::path> panner_modules;

	Glib::PatternSpec so_extension_pattern("*.so");
	Glib::PatternSpec dylib_extension_pattern("*.dylib");

	find_matching_files_in_search_path (panner_search_path (),
			so_extension_pattern, panner_modules);

	find_matching_files_in_search_path (panner_search_path (),
			dylib_extension_pattern, panner_modules);

	info << string_compose (_("looking for panners in %1"), panner_search_path().to_string()) << endmsg;

	for (vector<sys::path>::iterator i = panner_modules.begin(); i != panner_modules.end(); ++i) {
		panner_discover ((*i).to_string());
	}
}
int
PannerManager::panner_discover (string path)
{
        PannerInfo* pinfo;

	if ((pinfo = get_descriptor (path)) != 0) {
                panner_info.push_back (pinfo);
                info << string_compose(_("Panner discovered: \"%1\""), pinfo->descriptor.name) << endmsg;
        }

	return 0;
}

PannerInfo*
PannerManager::get_descriptor (string path)
{
	void *module;
        PannerInfo* info = 0;
	PanPluginDescriptor *descriptor = 0;
	PanPluginDescriptor* (*dfunc)(void);
	const char *errstr;

	if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
		error << string_compose(_("PannerManager: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
		return 0;
	}

	dfunc = (PanPluginDescriptor* (*)(void)) dlsym (module, "panner_descriptor");

	if ((errstr = dlerror()) != 0) {
		error << string_compose(_("PannerManager: module \"%1\" has no descriptor function."), path) << endmsg;
		error << errstr << endmsg;
		dlclose (module);
		return 0;
	}

	descriptor = dfunc();
	if (descriptor) {
                info = new PannerInfo (*descriptor, module);
	} else {
		dlclose (module);
	}

	return info;
}

PannerInfo*
PannerManager::select_panner (ChanCount in, ChanCount out)
{
        PanPluginDescriptor* d;
        int32_t nin = in.n_audio();
        int32_t nout = out.n_audio();
        
        /* look for exact match first */

        for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
                d = &(*p)->descriptor;

                if (d->in == nin && d->out == nout) {
                        return *p;
                }
        }

        /* no exact match, look for good fit on inputs and variable on outputs */

        for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
                d = &(*p)->descriptor;
         
                if (d->in == nin && d->out == -1) {
                        return *p;
                }
        }

        /* no exact match, look for good fit on outputs and variable on inputs */

        for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
                d = &(*p)->descriptor;
         
                if (d->in == -1 && d->out == nout) {
                        return *p;
                }
        }

        /* no exact match, look for variable fit on inputs and outputs */

        for (list<PannerInfo*>::iterator p = panner_info.begin(); p != panner_info.end(); ++p) {
                d = &(*p)->descriptor;
         
                if (d->in == -1 && d->out == -1) {
                        return *p;
                }
        }

        warning << string_compose (_("no panner discovered for in/out = %1/%2"), nin, nout) << endmsg;

        return 0;
}