LMMS
Loading...
Searching...
No Matches
pinknoise.h
Go to the documentation of this file.
1/* pinknoise.h
2
3 pink noise generating class using the Voss-McCartney algorithm, as
4 described at www.firstpr.com.au/dsp/pink-noise/
5
6 (c) 2002 Nathaniel Virgo
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#ifndef _PINKNOISE_H
27#define _PINKNOISE_H
28
29#include <cstdlib>
30
31typedef unsigned int CounterType;
32typedef float DataValue;
33
34const int n_generators = 8*sizeof(CounterType);
35
36class PinkNoise {
37 private:
38
42
43 public:
44
47 reset();
48 }
49
50 ~PinkNoise() {delete [] generators;};
51
52 void reset() {
53 counter = 0;
54 last_value = 0;
55 for (int i=0; i<n_generators; ++i) {
56 generators[i] = 2*(rand()/DataValue(RAND_MAX))-1;
58 }
59 }
60
62 if (counter != 0) {
63 // set index to number of trailing zeros in counter.
64 // hangs if counter==0, hence the slightly inefficient
65 // test above.
67 int index = 0;
68 while ( (n & 1) == 0 ) {
69 n >>= 1;
70 index++;
71 // this loop means that the plugins cannot be labelled as
72 // capable of hard real-time performance.
73 }
74
75 last_value -= generators[index];
76 generators[index] = 2*(rand()/DataValue(RAND_MAX))-1;
77 last_value += generators[index];
78 }
79
80 counter++;
81
82 return last_value;
83 }
84
87 }
88
91 }
92
94 // adding some white noise gets rid of some nulls in the frequency spectrum
95 // but makes the signal spikier, so possibly not so good for control signals.
96 return (getUnscaledValue() + rand()/DataValue(RAND_MAX*0.5)-1)/(n_generators+1);
97 }
98
99};
100
101#endif
102
103
104
105
106
107
108
109
110
DataValue getValue2()
Definition pinknoise.h:93
DataValue getValue()
Definition pinknoise.h:85
void reset()
Definition pinknoise.h:52
CounterType counter
Definition pinknoise.h:39
DataValue getLastValue()
Definition pinknoise.h:89
PinkNoise()
Definition pinknoise.h:45
DataValue last_value
Definition pinknoise.h:41
DataValue * generators
Definition pinknoise.h:40
DataValue getUnscaledValue()
Definition pinknoise.h:61
~PinkNoise()
Definition pinknoise.h:50
register unsigned i
Definition inflate.c:1575
unsigned int CounterType
Definition pinknoise.h:31
const int n_generators
Definition pinknoise.h:34
float DataValue
Definition pinknoise.h:32
int n
Definition crypt.c:458