Debian Codegraph @

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

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