Debian Codegraph @

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