1 /* noise.cpp
2 
3    Computer Music Toolkit - a library of LADSPA plugins. Copyright (C)
4    2000-2002 Richard W.E. Furse. The author may be contacted at
5    richard@muse.demon.co.uk.
6 
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public Licence as
9    published by the Free Software Foundation; either version 2 of the
10    Licence, or (at your option) any later version.
11 
12    This library is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this library; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20    02111-1307, USA. */
21 
22 /*****************************************************************************/
23 
24 #include <stdlib.h>
25 
26 /*****************************************************************************/
27 
28 #include "cmt.h"
29 
30 /*****************************************************************************/
31 
32 /* The port numbers for the plugin: */
33 
34 #define NOISE_AMPLITUDE 0
35 #define NOISE_OUTPUT    1
36 
37 /** Plugin that provides white noise output. This is provided by
38     calling rand() repeatedly. */
39 class WhiteNoise : public CMT_PluginInstance {
40 private:
41 
42   LADSPA_Data m_fRunAddingGain;
43 
44 public:
45 
WhiteNoise(const LADSPA_Descriptor *,unsigned long)46   WhiteNoise(const LADSPA_Descriptor *,
47 	      unsigned long)
48     : CMT_PluginInstance(2) {
49   }
50 
51   friend void runWhiteNoise(LADSPA_Handle Instance,
52 			    unsigned long SampleCount);
53   friend void runWhiteNoiseAdding(LADSPA_Handle Instance,
54 				  unsigned long SampleCount);
55   friend void setWhiteNoiseRunAddingGain(LADSPA_Handle Instance,
56 					 LADSPA_Data   Gain);
57 
58 };
59 
60 /*****************************************************************************/
61 
62 void
runWhiteNoise(LADSPA_Handle Instance,unsigned long SampleCount)63 runWhiteNoise(LADSPA_Handle Instance,
64 	      unsigned long SampleCount) {
65 
66   WhiteNoise * poNoise = (WhiteNoise *)Instance;
67 
68   LADSPA_Data fAmplitude = *(poNoise->m_ppfPorts[NOISE_AMPLITUDE]);
69   LADSPA_Data fScalar = fAmplitude * LADSPA_Data(2.0 / RAND_MAX);
70 
71   LADSPA_Data * pfOutput = poNoise->m_ppfPorts[NOISE_OUTPUT];
72 
73   for (unsigned long lSampleIndex = 0;
74        lSampleIndex < SampleCount;
75        lSampleIndex++)
76     *(pfOutput++) = rand() * fScalar - fAmplitude;
77 }
78 
79 void
runWhiteNoiseAdding(LADSPA_Handle Instance,unsigned long SampleCount)80 runWhiteNoiseAdding(LADSPA_Handle Instance,
81 		    unsigned long SampleCount) {
82 
83   WhiteNoise * poNoise = (WhiteNoise *)Instance;
84 
85   LADSPA_Data fAmplitude
86     = *(poNoise->m_ppfPorts[NOISE_AMPLITUDE]);
87   LADSPA_Data fScalar
88     = poNoise->m_fRunAddingGain * fAmplitude * LADSPA_Data(2.0 / RAND_MAX);
89 
90   LADSPA_Data * pfOutput = poNoise->m_ppfPorts[NOISE_OUTPUT];
91 
92   for (unsigned long lSampleIndex = 0;
93        lSampleIndex < SampleCount;
94        lSampleIndex++)
95     *(pfOutput++) += rand() * fScalar - fAmplitude;
96 
97 }
98 
99 void
setWhiteNoiseRunAddingGain(LADSPA_Handle Instance,LADSPA_Data Gain)100 setWhiteNoiseRunAddingGain(LADSPA_Handle Instance,
101 			   LADSPA_Data   Gain) {
102 }
103 
104 /*****************************************************************************/
105 
106 void
initialise_noise()107 initialise_noise() {
108 
109   CMT_Descriptor * psDescriptor;
110 
111   psDescriptor = new CMT_Descriptor
112     (1069,
113      "noise_source_white",
114      LADSPA_PROPERTY_HARD_RT_CAPABLE,
115      "Noise Source (White)",
116      CMT_MAKER("Richard W.E. Furse"),
117      CMT_COPYRIGHT("2000-2002", "Richard W.E. Furse"),
118      NULL,
119      CMT_Instantiate<WhiteNoise>,
120      NULL,
121      runWhiteNoise,
122      runWhiteNoiseAdding,
123      setWhiteNoiseRunAddingGain,
124      NULL);
125   psDescriptor->addPort
126     (LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL,
127      "Amplitude",
128      (LADSPA_HINT_BOUNDED_BELOW
129       | LADSPA_HINT_LOGARITHMIC
130       | LADSPA_HINT_DEFAULT_1),
131      0,
132      0);
133   psDescriptor->addPort
134     (LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO,
135      "Output");
136   registerNewPluginDescriptor(psDescriptor);
137 }
138 
139 /*****************************************************************************/
140 
141 /* EOF */
142