Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/test/patest_pink.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /** @file patest_pink.c
2 @brief Generate Pink Noise using Gardner method.
3
4 Optimization suggested by James McCartney uses a tree
5 to select which random value to replace.
6 <pre>
7 x x x x x x x x x x x x x x x x
8 x x x x x x x x
9 x x x x
10 x x
11 x
12 </pre>
13 Tree is generated by counting trailing zeros in an increasing index.
14 When the index is zero, no random number is selected.
15
16 @author Phil Burk http://www.softsynth.com
17 */
18 /*
19 * $Id: patest_pink.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
20 *
21 * This program uses the PortAudio Portable Audio Library.
22 * For more information see: http://www.portaudio.com
23 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
24 *
25 * Permission is hereby granted, free of charge, to any person obtaining
26 * a copy of this software and associated documentation files
27 * (the "Software"), to deal in the Software without restriction,
28 * including without limitation the rights to use, copy, modify, merge,
29 * publish, distribute, sublicense, and/or sell copies of the Software,
30 * and to permit persons to whom the Software is furnished to do so,
31 * subject to the following conditions:
32 *
33 * The above copyright notice and this permission notice shall be
34 * included in all copies or substantial portions of the Software.
35 *
36 * Any person wishing to distribute modifications to the Software is
37 * requested to send the modifications to the original developer so that
38 * they can be incorporated into the canonical version.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
41 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
42 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
43 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
44 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
45 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
46 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47 *
48 */
49
50 #include <stdio.h>
51 #include <math.h>
52 #include "portaudio.h"
53
54 #define PINK_MAX_RANDOM_ROWS (30)
55 #define PINK_RANDOM_BITS (24)
56 #define PINK_RANDOM_SHIFT ((sizeof(long)*8)-PINK_RANDOM_BITS)
57
58 typedef struct
59 {
60 long pink_Rows[PINK_MAX_RANDOM_ROWS];
61 long pink_RunningSum; /* Used to optimize summing of generators. */
62 int pink_Index; /* Incremented each sample. */
63 int pink_IndexMask; /* Index wrapped by ANDing with this mask. */
64 float pink_Scalar; /* Used to scale within range of -1.0 to +1.0 */
65 }
66 PinkNoise;
67
68 /* Prototypes */
69 static unsigned long GenerateRandomNumber( void );
70 void InitializePinkNoise( PinkNoise *pink, int numRows );
71 float GeneratePinkNoise( PinkNoise *pink );
72
73 /************************************************************/
74 /* Calculate pseudo-random 32 bit number based on linear congruential method. */
75 static unsigned long GenerateRandomNumber( void )
76 {
77 /* Change this seed for different random sequences. */
78 static unsigned long randSeed = 22222;
79 randSeed = (randSeed * 196314165) + 907633515;
80 return randSeed;
81 }
82
83 /************************************************************/
84 /* Setup PinkNoise structure for N rows of generators. */
85 void InitializePinkNoise( PinkNoise *pink, int numRows )
86 {
87 int i;
88 long pmax;
89 pink->pink_Index = 0;
90 pink->pink_IndexMask = (1<<numRows) - 1;
91 /* Calculate maximum possible signed random value. Extra 1 for white noise always added. */
92 pmax = (numRows + 1) * (1<<(PINK_RANDOM_BITS-1));
93 pink->pink_Scalar = 1.0f / pmax;
94 /* Initialize rows. */
95 for( i=0; i<numRows; i++ ) pink->pink_Rows[i] = 0;
96 pink->pink_RunningSum = 0;
97 }
98
99 #define PINK_MEASURE
100 #ifdef PINK_MEASURE
101 float pinkMax = -999.0;
102 float pinkMin = 999.0;
103 #endif
104
105 /* Generate Pink noise values between -1.0 and +1.0 */
106 float GeneratePinkNoise( PinkNoise *pink )
107 {
108 long newRandom;
109 long sum;
110 float output;
111 /* Increment and mask index. */
112 pink->pink_Index = (pink->pink_Index + 1) & pink->pink_IndexMask;
113 /* If index is zero, don't update any random values. */
114 if( pink->pink_Index != 0 )
115 {
116 /* Determine how many trailing zeros in PinkIndex. */
117 /* This algorithm will hang if n==0 so test first. */
118 int numZeros = 0;
119 int n = pink->pink_Index;
120 while( (n & 1) == 0 )
121 {
122 n = n >> 1;
123 numZeros++;
124 }
125 /* Replace the indexed ROWS random value.
126 * Subtract and add back to RunningSum instead of adding all the random
127 * values together. Only one changes each time.
128 */
129 pink->pink_RunningSum -= pink->pink_Rows[numZeros];
130 newRandom = ((long)GenerateRandomNumber()) >> PINK_RANDOM_SHIFT;
131 pink->pink_RunningSum += newRandom;
132 pink->pink_Rows[numZeros] = newRandom;
133 }
134
135 /* Add extra white noise value. */
136 newRandom = ((long)GenerateRandomNumber()) >> PINK_RANDOM_SHIFT;
137 sum = pink->pink_RunningSum + newRandom;
138 /* Scale to range of -1.0 to 0.9999. */
139 output = pink->pink_Scalar * sum;
140 #ifdef PINK_MEASURE
141 /* Check Min/Max */
142 if( output > pinkMax ) pinkMax = output;
143 else if( output < pinkMin ) pinkMin = output;
144 #endif
145 return output;
146 }
147
148 /*******************************************************************/
149 #define PINK_TEST
150 #ifdef PINK_TEST
151
152 /* Context for callback routine. */
153 typedef struct
154 {
155 PinkNoise leftPink;
156 PinkNoise rightPink;
157 unsigned int sampsToGo;
158 }
159 paTestData;
160
161 /* This routine will be called by the PortAudio engine when audio is needed.
162 ** It may called at interrupt level on some machines so don't do anything
163 ** that could mess up the system like calling malloc() or free().
164 */
165 static int patestCallback(const void* inputBuffer,
166 void* outputBuffer,
167 unsigned long framesPerBuffer,
168 const PaStreamCallbackTimeInfo* timeInfo,
169 PaStreamCallbackFlags statusFlags,
170 void* userData)
171 {
172 int finished;
173 int i;
174 int numFrames;
175 paTestData *data = (paTestData*)userData;
176 float *out = (float*)outputBuffer;
177 (void) inputBuffer; /* Prevent "unused variable" warnings. */
178
179 /* Are we almost at end. */
180 if( data->sampsToGo < framesPerBuffer )
181 {
182 numFrames = data->sampsToGo;
183 finished = 1;
184 }
185 else
186 {
187 numFrames = framesPerBuffer;
188 finished = 0;
189 }
190 for( i=0; i<numFrames; i++ )
191 {
192 *out++ = GeneratePinkNoise( &data->leftPink );
193 *out++ = GeneratePinkNoise( &data->rightPink );
194 }
195 data->sampsToGo -= numFrames;
196 return finished;
197 }
198
199 /*******************************************************************/
200 int main(void);
201 int main(void)
202 {
203 PaStream* stream;
204 PaError err;
205 paTestData data;
206 PaStreamParameters outputParameters;
207 int totalSamps;
208 static const double SR = 44100.0;
209 static const int FPB = 2048; /* Frames per buffer: 46 ms buffers. */
210
211 /* Initialize two pink noise signals with different numbers of rows. */
212 InitializePinkNoise( &data.leftPink, 12 );
213 InitializePinkNoise( &data.rightPink, 16 );
214
215 /* Look at a few values. */
216 {
217 int i;
218 float pink;
219 for( i=0; i<20; i++ )
220 {
221 pink = GeneratePinkNoise( &data.leftPink );
222 printf("Pink = %f\n", pink );
223 }
224 }
225
226 data.sampsToGo = totalSamps = (int)(60.0 * SR); /* Play a whole minute. */
227 err = Pa_Initialize();
228 if( err != paNoError ) goto error;
229
230 /* Open a stereo PortAudio stream so we can hear the result. */
231 outputParameters.device = Pa_GetDefaultOutputDevice(); /* Take the default output device. */
232 outputParameters.channelCount = 2; /* Stereo output, most likely supported. */
233 outputParameters.hostApiSpecificStreamInfo = NULL;
234 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output. */
235 outputParameters.suggestedLatency =
236 Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
237 err = Pa_OpenStream(&stream,
238 NULL, /* No input. */
239 &outputParameters,
240 SR, /* Sample rate. */
241 FPB, /* Frames per buffer. */
242 paClipOff, /* we won't output out of range samples so don't bother clipping them */
243 patestCallback,
244 &data);
245 if( err != paNoError ) goto error;
246
247 err = Pa_StartStream( stream );
248 if( err != paNoError ) goto error;
249
250 printf("Stereo pink noise for one minute...\n");
251
252 while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(100);
253 if( err < 0 ) goto error;
254
255 err = Pa_CloseStream( stream );
256 if( err != paNoError ) goto error;
257 #ifdef PINK_MEASURE
258 printf("Pink min = %f, max = %f\n", pinkMin, pinkMax );
259 #endif
260 Pa_Terminate();
261 return 0;
262 error:
263 Pa_Terminate();
•264 fprintf( stderr, "An error occurred while using the portaudio stream\n" );
265 fprintf( stderr, "Error number: %d\n", err );
266 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
267 return 0;
268 }
269 #endif /* PINK_TEST */