LMMS
Loading...
Searching...
No Matches
run_adding.h
Go to the documentation of this file.
1/* run_adding.h
2
3 (c) 2002 Nathaniel Virgo
4
5 a few simple inline functions that can be used with templates
6 to get run_adding for free.
7
8 Computer Music Toolkit - a library of LADSPA plugins. Copyright (C)
9 2000-2002 Richard W.E. Furse.
10
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public Licence as
13 published by the Free Software Foundation; either version 2 of the
14 Licence, or (at your option) any later version.
15
16 This library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this library; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24 02111-1307, USA. */
25
26/*****************************************************************************/
27
28/*
29 How to use this
30 ---------------
31
32 Templates can be used to automatically generate code for both the run and
33 run_adding LADSPA functions. Simply define the plugin's run function as
34
35 template<OutputFunction write_output>
36 void run_foo(LADSPA_Handle Instance, unsigned long SampleCount);
37
38 and in the body of the function use
39
40 write_output(pfOutput, fValue, poFoo->m_fRunAddingGain);
41
42 instead of
43
44 *(pfOutput++) = fValue.
45
46 and be sure to include a set_run_adding_gain function.
47 then set the LADSPA run function as
48
49 run_foo<write_output_normal>;
50
51 and the run_adding function as
52
53 run_foo<write_output_adding>;
54
55 With -O1 or greater g++ will inline the write_output function, and
56 although the code ends up slightly bigger there is no overhead compared to
57 having two seperate functions.
58
59 Sometimes the run_adding_gain function can be made more efficient than this
60 - for instance, if the output is multiplied by a gain already then you are
61 doing one more multiplication than necessary on every sample. It's a lot
62 less code to maintain, though, and it should still save some work for the
63 host compared to not having a run_adding function.
64*/
65
66/*****************************************************************************/
67
68#include <ladspa.h>
69
70/*****************************************************************************/
71
72typedef void OutputFunction(LADSPA_Data *&, const LADSPA_Data &,
73 const LADSPA_Data &);
74
76 const LADSPA_Data &run_adding_gain)
77{
78 *(out++) = value;
79}
80
82 const LADSPA_Data &run_adding_gain)
83{
84 *(out++) += value*run_adding_gain;
85}
86
87/*****************************************************************************/
88
89/*
90 If the plugin has a control-rate ouput then you don't want the write_output
91 function to try to increment the pointer. To achieve this, use
92
93 write_control<write_output>(pfOutput, fValue, poFoo->m_fRunAddingGain);
94
95 instead of just
96
97 write_output(...);
98
99 I realise this feels a bit hacky, but it works.
100*/
101
102template <OutputFunction ouput_mode>
103inline void write_control(LADSPA_Data *const,
104 const LADSPA_Data &, const LADSPA_Data &);
105
106template <>
108 const LADSPA_Data &value,
109 const LADSPA_Data &run_adding_gain)
110{
111 *out = value;
112}
113
114template <>
116 const LADSPA_Data &value,
117 const LADSPA_Data &run_adding_gain)
118{
119 *out += value*run_adding_gain;
120}
121
122
123/*****************************************************************************/
124
125/*
126 This next bit is an attempt to facilitate the writing of slightly
127 more efficent run_adding functions without writing two seperate pieces of
128 code. You can say something like
129
130 LADSPA_Data fOutputGain = ... ;
131 ...
132 fOutputGain *= get_gain<write_output>(poFoo->m_fRunAddingGain);
133 ...
134 write_output(pfOutput, fValue*fOutputGain, 1.0f);
135
136 in run_foo. With -O1 or greater g++ should inline the functions and
137 optimise away the multiplies by 1.0f, so in run_foo<write_output_adding>
138 fOutputGain will be multiplied by m_fRunAddingGain and in
139 run_foo<write_output_normal> it will be left alone.
140
141 This does not make for very clear code, sorry about that. See disintegrator.cpp
142 for an example.
143*/
144
145template <OutputFunction output_mode>
146inline float get_gain(const LADSPA_Data &);
147
148template <>
150{
151 return 1.0f;
152}
153
154template <>
155inline float get_gain<write_output_adding>(const LADSPA_Data &run_adding_gain)
156{
157 return run_adding_gain;
158}
static PuglViewHint int value
Definition pugl.h:1708
float LADSPA_Data
Definition ladspa.h:84
float out
Definition lilv_test.c:1461
void write_output_normal(LADSPA_Data *&out, const LADSPA_Data &value, const LADSPA_Data &run_adding_gain)
Definition run_adding.h:75
void OutputFunction(LADSPA_Data *&, const LADSPA_Data &, const LADSPA_Data &)
Definition run_adding.h:72
void write_control< write_output_normal >(LADSPA_Data *const out, const LADSPA_Data &value, const LADSPA_Data &run_adding_gain)
Definition run_adding.h:107
void write_control(LADSPA_Data *const, const LADSPA_Data &, const LADSPA_Data &)
float get_gain< write_output_normal >(const LADSPA_Data &)
Definition run_adding.h:149
void write_control< write_output_adding >(LADSPA_Data *const out, const LADSPA_Data &value, const LADSPA_Data &run_adding_gain)
Definition run_adding.h:115
float get_gain< write_output_adding >(const LADSPA_Data &run_adding_gain)
Definition run_adding.h:155
void write_output_adding(LADSPA_Data *&out, const LADSPA_Data &value, const LADSPA_Data &run_adding_gain)
Definition run_adding.h:81
float get_gain(const LADSPA_Data &)