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>
7 * $Id: patest_maxsines.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $
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
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:
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
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.
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.
39 #include "portaudio.h"
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)
46 #define MIN_PHASE_INC FREQ_TO_PHASE_INC(200.0f)
47 #define MAX_PHASE_INC (MIN_PHASE_INC * (1 << 5))
49 #define FRAMES_PER_BUFFER (512)
51 #define M_PI (3.14159265)
53 #define TWOPI (M_PI * 2.0)
55 #define TABLE_SIZE (512)
57 typedef struct paTestData
60 float sine[TABLE_SIZE + 1]; /* add one for guard point for interpolation */
61 float phases[MAX_SINES];
65 /* Convert phase between and 1.0 to sine value
66 * using linear interpolation.
68 float LookupSine( paTestData *data, float phase );
69 float LookupSine( paTestData *data, float phase )
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);
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().
84 static int patestCallback(const void* inputBuffer,
86 unsigned long framesPerBuffer,
87 const PaStreamCallbackTimeInfo* timeInfo,
88 PaStreamCallbackFlags statusFlags,
91 paTestData *data = (paTestData*)userData;
92 float *out = (float*)outputBuffer;
99 (void) inputBuffer; /* Prevent unused argument warning. */
101 /* Determine amplitude scaling factor */
102 numForScale = data->numSines;
103 if( numForScale < 8 ) numForScale = 8; /* prevent pops at beginning */
104 scaler = 1.0f / numForScale;
106 for( i=0; i<framesPerBuffer; i++ )
109 float phaseInc = MIN_PHASE_INC;
111 for( j=0; j<data->numSines; j++ )
113 /* Advance phase of next oscillator. */
114 phase = data->phases[j];
116 if( phase >= 1.0 ) phase -= 1.0;
118 output += LookupSine(data, phase);
119 data->phases[j] = phase;
122 if( phaseInc > MAX_PHASE_INC ) phaseInc = MIN_PHASE_INC;
125 outSample = (float) (output * scaler);
126 *out++ = outSample; /* Left */
127 *out++ = outSample; /* Right */
132 /*******************************************************************/
138 PaStreamParameters outputParameters;
140 paTestData data = {0};
143 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
145 /* initialise sinusoidal wavetable */
146 for( i=0; i<TABLE_SIZE; i++ )
148 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
150 data.sine[TABLE_SIZE] = data.sine[0]; /* set guard point */
152 err = Pa_Initialize();
153 if( err != paNoError )
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,
166 paClipOff, /* No out of range samples should occur. */
169 if( err != paNoError )
172 err = Pa_StartStream( stream );
173 if( err != paNoError )
176 /* Play an increasing number of sine waves until we hit MAX_USAGE */
180 load = Pa_GetStreamCpuLoad(stream);
181 printf("numSines = %d, CPU load = %f\n", data.numSines, load );
183 } while((load < MAX_USAGE) && (data.numSines < MAX_SINES));
185 Pa_Sleep(2000); /* Stay for 2 seconds around 80% CPU. */
187 err = Pa_StopStream( stream );
188 if( err != paNoError )
191 err = Pa_CloseStream( stream );
192 if( err != paNoError )
196 printf("Test finished.\n");
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 ) );