comparison src/portaudio/test/patest_out_underflow.c @ 4:e13257ea84a4

Add bzip2, zlib, liblo, portaudio sources
author Chris Cannam
date Wed, 20 Mar 2013 13:59:52 +0000
parents
children
comparison
equal deleted inserted replaced
3:6c505a35919a 4:e13257ea84a4
1 /** @file patest_out_underflow.c
2 @ingroup test_src
3 @brief Count output underflows (using paOutputUnderflow flag)
4 under overloaded and normal conditions.
5 @author Ross Bencina <rossb@audiomulch.com>
6 @author Phil Burk <philburk@softsynth.com>
7 */
8 /*
9 * $Id: patest_out_underflow.c 1609 2011-02-27 00:06:07Z philburk $
10 *
11 * This program uses the PortAudio Portable Audio Library.
12 * For more information see: http://www.portaudio.com
13 * Copyright (c) 1999-2004 Ross Bencina and Phil Burk
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining
16 * a copy of this software and associated documentation files
17 * (the "Software"), to deal in the Software without restriction,
18 * including without limitation the rights to use, copy, modify, merge,
19 * publish, distribute, sublicense, and/or sell copies of the Software,
20 * and to permit persons to whom the Software is furnished to do so,
21 * subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
30 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
31 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 */
34
35 /*
36 * The text above constitutes the entire PortAudio license; however,
37 * the PortAudio community also makes the following non-binding requests:
38 *
39 * Any person wishing to distribute modifications to the Software is
40 * requested to send the modifications to the original developer so that
41 * they can be incorporated into the canonical version. It is also
42 * requested that these non-binding requests be included along with the
43 * license above.
44 */
45
46 #include <stdio.h>
47 #include <math.h>
48 #include "portaudio.h"
49
50 #define MAX_SINES (1000)
51 #define MAX_LOAD (1.2)
52 #define SAMPLE_RATE (44100)
53 #define FRAMES_PER_BUFFER (512)
54 #ifndef M_PI
55 #define M_PI (3.14159265)
56 #endif
57 #define TWOPI (M_PI * 2.0)
58
59 typedef struct paTestData
60 {
61 int sineCount;
62 double phases[MAX_SINES];
63 int countUnderflows;
64 int outputUnderflowCount;
65 }
66 paTestData;
67
68 /* This routine will be called by the PortAudio engine when audio is needed.
69 ** It may called at interrupt level on some machines so don't do anything
70 ** that could mess up the system like calling malloc() or free().
71 */
72 static int patestCallback( const void *inputBuffer, void *outputBuffer,
73 unsigned long framesPerBuffer,
74 const PaStreamCallbackTimeInfo* timeInfo,
75 PaStreamCallbackFlags statusFlags,
76 void *userData )
77 {
78 paTestData *data = (paTestData*)userData;
79 float *out = (float*)outputBuffer;
80 unsigned long i;
81 int j;
82 int finished = paContinue;
83 (void) timeInfo; /* Prevent unused variable warning. */
84 (void) inputBuffer; /* Prevent unused variable warning. */
85
86
87 if( data->countUnderflows && (statusFlags & paOutputUnderflow) )
88 data->outputUnderflowCount++;
89
90 for( i=0; i<framesPerBuffer; i++ )
91 {
92 float output = 0.0;
93 double phaseInc = 0.02;
94 double phase;
95
96 for( j=0; j<data->sineCount; j++ )
97 {
98 /* Advance phase of next oscillator. */
99 phase = data->phases[j];
100 phase += phaseInc;
101 if( phase > TWOPI ) phase -= TWOPI;
102
103 phaseInc *= 1.02;
104 if( phaseInc > 0.5 ) phaseInc *= 0.5;
105
106 /* This is not a very efficient way to calc sines. */
107 output += (float) sin( phase );
108 data->phases[j] = phase;
109 }
110 *out++ = (float) (output / data->sineCount);
111 }
112
113 return finished;
114 }
115
116 /*******************************************************************/
117 int main(void);
118 int main(void)
119 {
120 PaStreamParameters outputParameters;
121 PaStream *stream;
122 PaError err;
123 int safeSineCount, stressedSineCount;
124 int safeUnderflowCount, stressedUnderflowCount;
125 paTestData data = {0};
126 double load;
127
128
129 printf("PortAudio Test: output sine waves, count underflows. SR = %d, BufSize = %d. MAX_LOAD = %f\n",
130 SAMPLE_RATE, FRAMES_PER_BUFFER, (float)MAX_LOAD );
131
132 err = Pa_Initialize();
133 if( err != paNoError ) goto error;
134
135 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
136 if (outputParameters.device == paNoDevice) {
137 fprintf(stderr,"Error: No default output device.\n");
138 goto error;
139 }
140 outputParameters.channelCount = 1; /* mono output */
141 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
142 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
143 outputParameters.hostApiSpecificStreamInfo = NULL;
144
145 err = Pa_OpenStream(
146 &stream,
147 NULL, /* no input */
148 &outputParameters,
149 SAMPLE_RATE,
150 FRAMES_PER_BUFFER,
151 paClipOff, /* we won't output out of range samples so don't bother clipping them */
152 patestCallback,
153 &data );
154 if( err != paNoError ) goto error;
155 err = Pa_StartStream( stream );
156 if( err != paNoError ) goto error;
157
158 printf("Establishing load conditions...\n" );
159
160 /* Determine number of sines required to get to 50% */
161 do
162 {
163 Pa_Sleep( 100 );
164
165 load = Pa_GetStreamCpuLoad( stream );
166 printf("sineCount = %d, CPU load = %f\n", data.sineCount, load );
167
168 if( load < 0.3 )
169 {
170 data.sineCount += 10;
171 }
172 else if( load < 0.4 )
173 {
174 data.sineCount += 2;
175 }
176 else
177 {
178 data.sineCount += 1;
179 }
180
181 }
182 while( load < 0.5 && data.sineCount < (MAX_SINES-1));
183
184 safeSineCount = data.sineCount;
185
186 /* Calculate target stress value then ramp up to that level*/
187 stressedSineCount = (int) (2.0 * data.sineCount * MAX_LOAD );
188 if( stressedSineCount > MAX_SINES )
189 stressedSineCount = MAX_SINES;
190 for( ; data.sineCount < stressedSineCount; data.sineCount+=4 )
191 {
192 Pa_Sleep( 100 );
193 load = Pa_GetStreamCpuLoad( stream );
194 printf("STRESSING: sineCount = %d, CPU load = %f\n", data.sineCount, load );
195 }
196
197 printf("Counting underflows for 5 seconds.\n");
198 data.countUnderflows = 1;
199 Pa_Sleep( 5000 );
200
201 stressedUnderflowCount = data.outputUnderflowCount;
202
203 data.countUnderflows = 0;
204 data.sineCount = safeSineCount;
205
206 printf("Resuming safe load...\n");
207 Pa_Sleep( 1500 );
208 data.outputUnderflowCount = 0;
209 Pa_Sleep( 1500 );
210 load = Pa_GetStreamCpuLoad( stream );
211 printf("sineCount = %d, CPU load = %f\n", data.sineCount, load );
212
213 printf("Counting underflows for 5 seconds.\n");
214 data.countUnderflows = 1;
215 Pa_Sleep( 5000 );
216
217 safeUnderflowCount = data.outputUnderflowCount;
218
219 printf("Stop stream.\n");
220 err = Pa_StopStream( stream );
221 if( err != paNoError ) goto error;
222
223 err = Pa_CloseStream( stream );
224 if( err != paNoError ) goto error;
225
226 Pa_Terminate();
227
228 if( stressedUnderflowCount == 0 )
229 printf("Test failed, no output underflows detected under stress.\n");
230 else if( safeUnderflowCount != 0 )
231 printf("Test failed, %d unexpected underflows detected under safe load.\n", safeUnderflowCount);
232 else
233 printf("Test passed, %d expected output underflows detected under stress, 0 unexpected underflows detected under safe load.\n", stressedUnderflowCount );
234
235 return err;
236 error:
237 Pa_Terminate();
238 fprintf( stderr, "An error occured while using the portaudio stream\n" );
239 fprintf( stderr, "Error number: %d\n", err );
240 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
241 return err;
242 }