annotate src/portaudio/test/patest_in_overflow.c @ 89:8a15ff55d9af

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