]> git.mxchange.org Git - flightgear.git/blob - 3rdparty/iaxclient/lib/portaudio/test/patest_maxsines.c
Move IAXClient library into 3rdparty directory
[flightgear.git] / 3rdparty / iaxclient / lib / portaudio / test / patest_maxsines.c
1 /** @file patest_maxsines.c
2         @brief How many sine waves can we calculate and play in less than 80% CPU Load.
3         @author Ross Bencina <rossb@audiomulch.com>
4         @author Phil Burk <philburk@softsynth.com>
5 */
6 /*
7  * $Id: patest_maxsines.c,v 1.1 2006/06/10 21:30:56 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_USAGE     (0.8)
43 #define SAMPLE_RATE   (44100)
44 #define FREQ_TO_PHASE_INC(freq)   (freq/(float)SAMPLE_RATE)
45
46 #define MIN_PHASE_INC  FREQ_TO_PHASE_INC(200.0f)
47 #define MAX_PHASE_INC  (MIN_PHASE_INC * (1 << 5))
48
49 #define FRAMES_PER_BUFFER  (512)
50 #ifndef M_PI
51 #define M_PI  (3.14159265)
52 #endif
53 #define TWOPI (M_PI * 2.0)
54
55 #define TABLE_SIZE   (512)
56
57 typedef struct paTestData
58 {
59     int numSines;
60     float sine[TABLE_SIZE + 1]; /* add one for guard point for interpolation */
61     float phases[MAX_SINES];
62 }
63 paTestData;
64
65 /* Convert phase between and 1.0 to sine value
66  * using linear interpolation.
67  */
68 float LookupSine( paTestData *data, float phase );
69 float LookupSine( paTestData *data, float phase )
70 {
71     float fIndex = phase*TABLE_SIZE;
72     int   index = (int) fIndex;
73     float fract = fIndex - index;
74     float lo = data->sine[index];
75     float hi = data->sine[index+1];
76     float val = lo + fract*(hi-lo);
77     return val;
78 }
79
80 /* This routine will be called by the PortAudio engine when audio is needed.
81 ** It may called at interrupt level on some machines so don't do anything
82 ** that could mess up the system like calling malloc() or free().
83 */
84 static int patestCallback(const void*                     inputBuffer,
85                           void*                           outputBuffer,
86                           unsigned long                   framesPerBuffer,
87                           const PaStreamCallbackTimeInfo* timeInfo,
88                           PaStreamCallbackFlags           statusFlags,
89                           void*                           userData )
90 {
91     paTestData *data = (paTestData*)userData;
92     float *out = (float*)outputBuffer;
93     float outSample;
94     float scaler;
95     int numForScale;
96     unsigned long i;
97     int j;
98     int finished = 0;
99     (void) inputBuffer; /* Prevent unused argument warning. */
100
101     /* Determine amplitude scaling factor */
102     numForScale = data->numSines;
103     if( numForScale < 8 ) numForScale = 8;  /* prevent pops at beginning */
104     scaler = 1.0f / numForScale;
105     
106     for( i=0; i<framesPerBuffer; i++ )
107     {
108         float output = 0.0;
109         float phaseInc = MIN_PHASE_INC;
110         float phase;
111         for( j=0; j<data->numSines; j++ )
112         {
113             /* Advance phase of next oscillator. */
114             phase = data->phases[j];
115             phase += phaseInc;
116             if( phase >= 1.0 ) phase -= 1.0;
117
118             output += LookupSine(data, phase); 
119             data->phases[j] = phase;
120             
121             phaseInc *= 1.02f;
122             if( phaseInc > MAX_PHASE_INC ) phaseInc = MIN_PHASE_INC;
123         }
124
125         outSample = (float) (output * scaler);
126         *out++ = outSample; /* Left */
127         *out++ = outSample; /* Right */
128     }
129     return finished;
130 }
131
132 /*******************************************************************/
133 int main(void);
134 int main(void)
135 {
136         int                 i;
137     PaStream*           stream;
138     PaStreamParameters  outputParameters;
139     PaError             err;
140     paTestData          data = {0};
141     double              load;
142
143     printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
144
145     /* initialise sinusoidal wavetable */
146     for( i=0; i<TABLE_SIZE; i++ )
147     {
148         data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
149     }
150     data.sine[TABLE_SIZE] = data.sine[0]; /* set guard point */
151
152     err = Pa_Initialize();
153     if( err != paNoError )
154         goto error;
155     outputParameters.device                    = Pa_GetDefaultOutputDevice(); /* Default output device. */
156     outputParameters.channelCount              = 2;                           /* Stereo output. */
157     outputParameters.sampleFormat              = paFloat32;                   /* 32 bit floating point output. */
158     outputParameters.hostApiSpecificStreamInfo = NULL;
159     outputParameters.suggestedLatency          = Pa_GetDeviceInfo(outputParameters.device)
160                                                  ->defaultHighOutputLatency;
161     err = Pa_OpenStream(&stream,
162                         NULL,               /* no input */
163                         &outputParameters,
164                         SAMPLE_RATE,
165                         FRAMES_PER_BUFFER,
166                         paClipOff,          /* No out of range samples should occur. */
167                         patestCallback,
168                         &data);
169     if( err != paNoError )
170         goto error;
171
172     err = Pa_StartStream( stream );
173     if( err != paNoError )
174         goto error;
175
176     /* Play an increasing number of sine waves until we hit MAX_USAGE */
177     do  {
178         data.numSines++;
179         Pa_Sleep(200);
180         load = Pa_GetStreamCpuLoad(stream);
181         printf("numSines = %d, CPU load = %f\n", data.numSines, load );
182         fflush(stdout);
183         } while((load < MAX_USAGE) && (data.numSines < MAX_SINES));
184
185     Pa_Sleep(2000);     /* Stay for 2 seconds around 80% CPU. */
186
187     err = Pa_StopStream( stream );
188     if( err != paNoError )
189         goto error;
190
191     err = Pa_CloseStream( stream );
192     if( err != paNoError )
193         goto error;
194
195     Pa_Terminate();
196     printf("Test finished.\n");
197     return err;
198 error:
199     Pa_Terminate();
200     fprintf( stderr, "An error occured while using the portaudio stream\n" );
201     fprintf( stderr, "Error number: %d\n", err );
202     fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
203     return err;
204 }