~ubuntu-branches/ubuntu/hardy/lmms/hardy

« back to all changes in this revision

Viewing changes to plugins/ladspa_effect/caps/Descriptor.h

  • Committer: Bazaar Package Importer
  • Author(s): Tobias Doerffel
  • Date: 2007-09-17 15:00:24 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070917150024-mo0zk4ks81jawqii
Tags: 0.3.0-1ubuntu1
* Resynchronized with Debian (LP: #139759, LP: #90806, LP: #102639,
  LP: #113447, LP: #121172, LP: #124890)
* reverted changes from 0.2.1-1.1ubuntu1 as upstream merged/included them

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        Descriptor.h
 
3
        
 
4
        Copyright 2004-5 Tim Goetze <tim@quitte.de>
 
5
        
 
6
        http://quitte.de/dsp/
 
7
 
 
8
        creating a LADSPA_Descriptor for a caps plugin via a C++ template,
 
9
        saves us a virtual function call compared to the usual method used
 
10
        for C++ plugins in a C context.
 
11
 
 
12
        Descriptor<P> expects P to declare some common methods, like init(),
 
13
        activate() etc, plus a static port_info[] and LADSPA_Data * ports[]
 
14
        and of course 'adding_gain'.
 
15
        
 
16
        maintaining both port_info[] and ports[] is a bit of a bitch, but,
 
17
        hey, "you only do it once (tm)" .. and then you do it over and over
 
18
        again. particularly bothersome is also the necessary unrolling of our
 
19
        PortInfo array to fit into LADSPA_Descriptor's inconsequential way of 
 
20
        port data structuring, which results in quite a bit of memory holding 
 
21
        duplicated data. oh well.
 
22
*/
 
23
/*
 
24
        This program is free software; you can redistribute it and/or
 
25
        modify it under the terms of the GNU General Public License
 
26
        as published by the Free Software Foundation; either version 2
 
27
        of the License, or (at your option) any later version.
 
28
 
 
29
        This program is distributed in the hope that it will be useful,
 
30
        but WITHOUT ANY WARRANTY; without even the implied warranty of
 
31
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
32
        GNU General Public License for more details.
 
33
 
 
34
        You should have received a copy of the GNU General Public License
 
35
        along with this program; if not, write to the Free Software
 
36
        Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
37
        02111-1307, USA or point your web browser to http://www.gnu.org.
 
38
*/
 
39
 
 
40
#ifndef _DESCRIPTOR_H_
 
41
#define _DESCRIPTOR_H_
 
42
 
 
43
/* common stub for Descriptor makes it possible to delete() without special-
 
44
 * casing for every plugin class.
 
45
 */
 
46
class DescriptorStub
 
47
: public LADSPA_Descriptor
 
48
{
 
49
        public:
 
50
                DescriptorStub()
 
51
                        {
 
52
                                PortCount = 0;
 
53
                        }
 
54
 
 
55
                ~DescriptorStub()
 
56
                        {
 
57
                                if (PortCount)
 
58
                                {
 
59
                                        delete [] PortNames;
 
60
                                        delete [] PortDescriptors;
 
61
                                        delete [] PortRangeHints;
 
62
                                }
 
63
                        }
 
64
};
 
65
                                
 
66
template <class T>
 
67
class Descriptor
 
68
: public DescriptorStub
 
69
{
 
70
        public:
 
71
                /* tom szilyagi reports that hosts exist which call activate() before
 
72
                 * connect_port(). since caps' plugins expect ports to be valid we
 
73
                 * need a safeguard: at instantiation, each port is connected to the
 
74
                 * lower bound. When (If?) LADSPA default values are ever fixed, connecting
 
75
                 * to the default will be preferred. */
 
76
                LADSPA_PortRangeHint * ranges;
 
77
 
 
78
        public:
 
79
                Descriptor()
 
80
                        { setup(); }
 
81
 
 
82
                void setup();
 
83
 
 
84
                void autogen() 
 
85
                        {
 
86
                                PortCount = (sizeof (T::port_info) / sizeof (PortInfo));
 
87
 
 
88
                                /* unroll PortInfo members */
 
89
                                char ** names = new char * [PortCount];
 
90
                                LADSPA_PortDescriptor * desc = new LADSPA_PortDescriptor [PortCount];
 
91
                                ranges = new LADSPA_PortRangeHint [PortCount];
 
92
 
 
93
                                /* could also assign directly but const_cast is ugly. */
 
94
                                for (int i = 0; i < (int) PortCount; ++i)
 
95
                                {
 
96
                                        names[i] = T::port_info[i].name;
 
97
                                        desc[i] = T::port_info[i].descriptor;
 
98
                                        ranges[i] = T::port_info[i].range;
 
99
                                }
 
100
 
 
101
                                PortNames = names;
 
102
                                PortDescriptors = desc;
 
103
                                PortRangeHints = ranges;
 
104
                                
 
105
                                /* LADSPA_Descriptor vtable entries */
 
106
                                instantiate = _instantiate;
 
107
                                connect_port = _connect_port;
 
108
                                activate = _activate;
 
109
                                run = _run;
 
110
                                run_adding = _run_adding;
 
111
                                set_run_adding_gain = _set_run_adding_gain;
 
112
                                deactivate = 0;
 
113
                                cleanup = _cleanup;
 
114
                        }                       
 
115
 
 
116
                static LADSPA_Handle _instantiate (
 
117
                                const struct _LADSPA_Descriptor * d, ulong fs)
 
118
                        { 
 
119
                                T * plugin = new T();
 
120
                                
 
121
                                /* see comment above at 'ranges' member */
 
122
                                for (int i = 0; i < (int) d->PortCount; ++i)
 
123
                                        plugin->ports[i] = &((Descriptor *) d)->ranges[i].LowerBound;
 
124
 
 
125
                                plugin->init (fs);
 
126
 
 
127
                                return plugin;
 
128
                        }
 
129
                
 
130
                static void _connect_port (LADSPA_Handle h, ulong i, LADSPA_Data * p)
 
131
                        { 
 
132
                                ((T *) h)->ports[i] = p;
 
133
                        }
 
134
 
 
135
                static void _activate (LADSPA_Handle h)
 
136
                        {
 
137
                                ((T *) h)->activate();
 
138
                        }
 
139
 
 
140
                static void _run (LADSPA_Handle h, ulong n)
 
141
                        {
 
142
                                /* cannot call template here (g++ 2.95), sigh. */
 
143
                                ((T *) h)->run (n);
 
144
                        }
 
145
                
 
146
                static void _run_adding (LADSPA_Handle h, ulong n)
 
147
                        {
 
148
                                /* cannot call a template here (g++ 2.95), sigh. */
 
149
                                ((T *) h)->run_adding (n);
 
150
                        }
 
151
                
 
152
                static void _set_run_adding_gain (LADSPA_Handle h, LADSPA_Data g)
 
153
                        {
 
154
                                ((T *) h)->adding_gain = g;
 
155
                        }
 
156
 
 
157
                static void _cleanup (LADSPA_Handle h)
 
158
                        {
 
159
                                delete (T *) h;
 
160
                        }
 
161
};
 
162
 
 
163
#endif /* _DESCRIPTOR_H_ */