Debian Codegraph @

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

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