1
/** @file patest_in_overflow.c
3
@brief Count input overflows (using paInputOverflow flag) under
4
overloaded and normal conditions.
5
This test uses the same method to overload the stream as does
6
patest_out_underflow.c -- it generates sine waves until the cpu load
7
exceeds a certain level. However this test is only concerned with
8
input and so doesn't ouput any sound.
10
@author Ross Bencina <rossb@audiomulch.com>
11
@author Phil Burk <philburk@softsynth.com>
14
* $Id: patest_in_overflow.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
16
* This program uses the PortAudio Portable Audio Library.
17
* For more information see: http://www.portaudio.com
18
* Copyright (c) 1999-2004 Ross Bencina and Phil Burk
20
* Permission is hereby granted, free of charge, to any person obtaining
21
* a copy of this software and associated documentation files
22
* (the "Software"), to deal in the Software without restriction,
23
* including without limitation the rights to use, copy, modify, merge,
24
* publish, distribute, sublicense, and/or sell copies of the Software,
25
* and to permit persons to whom the Software is furnished to do so,
26
* subject to the following conditions:
28
* The above copyright notice and this permission notice shall be
29
* included in all copies or substantial portions of the Software.
31
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
34
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
35
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
36
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41
* The text above constitutes the entire PortAudio license; however,
42
* the PortAudio community also makes the following non-binding requests:
44
* Any person wishing to distribute modifications to the Software is
45
* requested to send the modifications to the original developer so that
46
* they can be incorporated into the canonical version. It is also
47
* requested that these non-binding requests be included along with the
53
#include "portaudio.h"
55
#define MAX_SINES (500)
56
#define MAX_LOAD (1.2)
57
#define SAMPLE_RATE (44100)
58
#define FRAMES_PER_BUFFER (512)
60
#define M_PI (3.14159265)
62
#define TWOPI (M_PI * 2.0)
64
typedef struct paTestData
67
double phases[MAX_SINES];
69
int inputOverflowCount;
73
/* This routine will be called by the PortAudio engine when audio is needed.
74
** It may called at interrupt level on some machines so don't do anything
75
** that could mess up the system like calling malloc() or free().
77
static int patestCallback( const void *inputBuffer, void *outputBuffer,
78
unsigned long framesPerBuffer,
79
const PaStreamCallbackTimeInfo* timeInfo,
80
PaStreamCallbackFlags statusFlags,
83
paTestData *data = (paTestData*)userData;
84
float out; /* variable to hold dummy output */
87
int finished = paContinue;
88
(void) timeInfo; /* Prevent unused variable warning. */
89
(void) inputBuffer; /* Prevent unused variable warning. */
90
(void) outputBuffer; /* Prevent unused variable warning. */
92
if( data->countOverflows && (statusFlags & paInputOverflow) )
93
data->inputOverflowCount++;
95
for( i=0; i<framesPerBuffer; i++ )
98
double phaseInc = 0.02;
101
for( j=0; j<data->sineCount; j++ )
103
/* Advance phase of next oscillator. */
104
phase = data->phases[j];
106
if( phase > TWOPI ) phase -= TWOPI;
109
if( phaseInc > 0.5 ) phaseInc *= 0.5;
111
/* This is not a very efficient way to calc sines. */
112
output += (float) sin( phase );
113
data->phases[j] = phase;
115
/* this is an input-only stream so we don't actually use the output */
116
out = (float) (output / data->sineCount);
117
(void) out; /* suppress unused variable warning*/
123
/*******************************************************************/
127
PaStreamParameters inputParameters;
130
int safeSineCount, stressedSineCount;
131
int safeOverflowCount, stressedOverflowCount;
132
paTestData data = {0};
136
printf("PortAudio Test: input only, no sound output. Load callback by performing calculations, count input overflows. SR = %d, BufSize = %d. MAX_LOAD = %f\n",
137
SAMPLE_RATE, FRAMES_PER_BUFFER, (float)MAX_LOAD );
139
err = Pa_Initialize();
140
if( err != paNoError ) goto error;
142
inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
143
inputParameters.channelCount = 1; /* mono output */
144
inputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
145
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
146
inputParameters.hostApiSpecificStreamInfo = NULL;
151
NULL, /* no output */
154
paClipOff, /* we won't output out of range samples so don't bother clipping them */
157
if( err != paNoError ) goto error;
158
err = Pa_StartStream( stream );
159
if( err != paNoError ) goto error;
161
printf("Establishing load conditions...\n" );
163
/* Determine number of sines required to get to 50% */
169
load = Pa_GetStreamCpuLoad( stream );
170
printf("sineCount = %d, CPU load = %f\n", data.sineCount, load );
172
while( load < 0.5 && data.sineCount < (MAX_SINES-1));
174
safeSineCount = data.sineCount;
176
/* Calculate target stress value then ramp up to that level*/
177
stressedSineCount = (int) (2.0 * data.sineCount * MAX_LOAD );
178
if( stressedSineCount > MAX_SINES )
179
stressedSineCount = MAX_SINES;
180
for( ; data.sineCount < stressedSineCount; data.sineCount++ )
183
load = Pa_GetStreamCpuLoad( stream );
184
printf("STRESSING: sineCount = %d, CPU load = %f\n", data.sineCount, load );
187
printf("Counting overflows for 5 seconds.\n");
188
data.countOverflows = 1;
191
stressedOverflowCount = data.inputOverflowCount;
193
data.countOverflows = 0;
194
data.sineCount = safeSineCount;
196
printf("Resuming safe load...\n");
198
data.inputOverflowCount = 0;
200
load = Pa_GetStreamCpuLoad( stream );
201
printf("sineCount = %d, CPU load = %f\n", data.sineCount, load );
203
printf("Counting overflows for 5 seconds.\n");
204
data.countOverflows = 1;
207
safeOverflowCount = data.inputOverflowCount;
209
printf("Stop stream.\n");
210
err = Pa_StopStream( stream );
211
if( err != paNoError ) goto error;
213
err = Pa_CloseStream( stream );
214
if( err != paNoError ) goto error;
218
if( stressedOverflowCount == 0 )
219
printf("Test failed, no input overflows detected under stress.\n");
220
else if( safeOverflowCount != 0 )
221
printf("Test failed, %d unexpected overflows detected under safe load.\n", safeOverflowCount);
223
printf("Test passed, %d expected input overflows detected under stress, 0 unexpected overflows detected under safe load.\n", stressedOverflowCount );
228
fprintf( stderr, "An error occured while using the portaudio stream\n" );
229
fprintf( stderr, "Error number: %d\n", err );
230
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );