1
/** @file patest_toomanysines.c
3
@brief Play more sine waves than we can handle in real time as a stress test.
4
@author Ross Bencina <rossb@audiomulch.com>
5
@author Phil Burk <philburk@softsynth.com>
8
* $Id: patest_toomanysines.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
10
* This program uses the PortAudio Portable Audio Library.
11
* For more information see: http://www.portaudio.com
12
* Copyright (c) 1999-2000 Ross Bencina and Phil Burk
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:
22
* The above copyright notice and this permission notice shall be
23
* included in all copies or substantial portions of the Software.
25
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
29
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
30
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
* The text above constitutes the entire PortAudio license; however,
36
* the PortAudio community also makes the following non-binding requests:
38
* Any person wishing to distribute modifications to the Software is
39
* requested to send the modifications to the original developer so that
40
* they can be incorporated into the canonical version. It is also
41
* requested that these non-binding requests be included along with the
47
#include "portaudio.h"
49
#define MAX_SINES (500)
50
#define MAX_LOAD (1.2)
51
#define SAMPLE_RATE (44100)
52
#define FRAMES_PER_BUFFER (512)
54
#define M_PI (3.14159265)
56
#define TWOPI (M_PI * 2.0)
58
typedef struct paTestData
61
double phases[MAX_SINES];
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().
69
static int patestCallback( const void *inputBuffer, void *outputBuffer,
70
unsigned long framesPerBuffer,
71
const PaStreamCallbackTimeInfo* timeInfo,
72
PaStreamCallbackFlags statusFlags,
75
paTestData *data = (paTestData*)userData;
76
float *out = (float*)outputBuffer;
80
(void) inputBuffer; /* Prevent unused variable warning. */
82
for( i=0; i<framesPerBuffer; i++ )
85
double phaseInc = 0.02;
87
for( j=0; j<data->numSines; j++ )
89
/* Advance phase of next oscillator. */
90
phase = data->phases[j];
92
if( phase > TWOPI ) phase -= TWOPI;
95
if( phaseInc > 0.5 ) phaseInc *= 0.5;
97
/* This is not a very efficient way to calc sines. */
98
output += (float) sin( phase );
99
data->phases[j] = phase;
101
*out++ = (float) (output / data->numSines);
106
/*******************************************************************/
110
PaStreamParameters outputParameters;
114
paTestData data = {0};
117
printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d. MAX_LOAD = %f\n",
118
SAMPLE_RATE, FRAMES_PER_BUFFER, MAX_LOAD );
120
err = Pa_Initialize();
121
if( err != paNoError ) goto error;
123
outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
124
outputParameters.channelCount = 1; /* mono output */
125
outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
126
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
127
outputParameters.hostApiSpecificStreamInfo = NULL;
135
paClipOff, /* we won't output out of range samples so don't bother clipping them */
138
if( err != paNoError ) goto error;
139
err = Pa_StartStream( stream );
140
if( err != paNoError ) goto error;
142
/* Determine number of sines required to get to 50% */
148
load = Pa_GetStreamCpuLoad( stream );
149
printf("numSines = %d, CPU load = %f\n", data.numSines, load );
153
/* Calculate target stress value then ramp up to that level*/
154
numStress = (int) (2.0 * data.numSines * MAX_LOAD );
155
for( ; data.numSines < numStress; data.numSines++ )
158
load = Pa_GetStreamCpuLoad( stream );
159
printf("STRESSING: numSines = %d, CPU load = %f\n", data.numSines, load );
162
printf("Suffer for 5 seconds.\n");
165
printf("Stop stream.\n");
166
err = Pa_StopStream( stream );
167
if( err != paNoError ) goto error;
169
err = Pa_CloseStream( stream );
170
if( err != paNoError ) goto error;
173
printf("Test finished.\n");
177
fprintf( stderr, "An error occured while using the portaudio stream\n" );
178
fprintf( stderr, "Error number: %d\n", err );
179
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );