paex_sine_c++.cpp
00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include <stdio.h>
00045 #include <math.h>
00046 #include "portaudio.h"
00047
00048 #define NUM_SECONDS (5)
00049 #define SAMPLE_RATE (44100)
00050 #define FRAMES_PER_BUFFER (64)
00051
00052 #ifndef M_PI
00053 #define M_PI (3.14159265)
00054 #endif
00055
00056 #define TABLE_SIZE (200)
00057
00058 class Sine
00059 {
00060 public:
00061 Sine() : stream(0), left_phase(0), right_phase(0)
00062 {
00063
00064 for( int i=0; i<TABLE_SIZE; i++ )
00065 {
00066 sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
00067 }
00068
00069 sprintf( message, "No Message" );
00070 }
00071
00072 bool open(PaDeviceIndex index)
00073 {
00074 PaStreamParameters outputParameters;
00075
00076 outputParameters.device = index;
00077 if (outputParameters.device == paNoDevice) {
00078 return false;
00079 }
00080
00081 const PaDeviceInfo* pInfo = Pa_GetDeviceInfo(index);
00082 if (pInfo != 0)
00083 {
00084 printf("Output device name: '%s'\r", pInfo->name);
00085 }
00086
00087 outputParameters.channelCount = 2;
00088 outputParameters.sampleFormat = paFloat32;
00089 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
00090 outputParameters.hostApiSpecificStreamInfo = NULL;
00091
00092 PaError err = Pa_OpenStream(
00093 &stream,
00094 NULL,
00095 &outputParameters,
00096 SAMPLE_RATE,
00097 paFramesPerBufferUnspecified,
00098 paClipOff,
00099 &Sine::paCallback,
00100 this
00101 );
00102
00103 if (err != paNoError)
00104 {
00105
00106 return false;
00107 }
00108
00109 err = Pa_SetStreamFinishedCallback( stream, &Sine::paStreamFinished );
00110
00111 if (err != paNoError)
00112 {
00113 Pa_CloseStream( stream );
00114 stream = 0;
00115
00116 return false;
00117 }
00118
00119 return true;
00120 }
00121
00122 bool close()
00123 {
00124 if (stream == 0)
00125 return false;
00126
00127 PaError err = Pa_CloseStream( stream );
00128 stream = 0;
00129
00130 return (err == paNoError);
00131 }
00132
00133
00134 bool start()
00135 {
00136 if (stream == 0)
00137 return false;
00138
00139 PaError err = Pa_StartStream( stream );
00140
00141 return (err == paNoError);
00142 }
00143
00144 bool stop()
00145 {
00146 if (stream == 0)
00147 return false;
00148
00149 PaError err = Pa_StopStream( stream );
00150
00151 return (err == paNoError);
00152 }
00153
00154 private:
00155
00156 int paCallbackMethod(const void *inputBuffer, void *outputBuffer,
00157 unsigned long framesPerBuffer,
00158 const PaStreamCallbackTimeInfo* timeInfo,
00159 PaStreamCallbackFlags statusFlags)
00160 {
00161 float *out = (float*)outputBuffer;
00162 unsigned long i;
00163
00164 (void) timeInfo;
00165 (void) statusFlags;
00166 (void) inputBuffer;
00167
00168 for( i=0; i<framesPerBuffer; i++ )
00169 {
00170 *out++ = sine[left_phase];
00171 *out++ = sine[right_phase];
00172 left_phase += 1;
00173 if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE;
00174 right_phase += 3;
00175 if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE;
00176 }
00177
00178 return paContinue;
00179
00180 }
00181
00182
00183
00184
00185
00186 static int paCallback( const void *inputBuffer, void *outputBuffer,
00187 unsigned long framesPerBuffer,
00188 const PaStreamCallbackTimeInfo* timeInfo,
00189 PaStreamCallbackFlags statusFlags,
00190 void *userData )
00191 {
00192
00193
00194 return ((Sine*)userData)->paCallbackMethod(inputBuffer, outputBuffer,
00195 framesPerBuffer,
00196 timeInfo,
00197 statusFlags);
00198 }
00199
00200
00201 void paStreamFinishedMethod()
00202 {
00203 printf( "Stream Completed: %s\n", message );
00204 }
00205
00206
00207
00208
00209 static void paStreamFinished(void* userData)
00210 {
00211 return ((Sine*)userData)->paStreamFinishedMethod();
00212 }
00213
00214 PaStream *stream;
00215 float sine[TABLE_SIZE];
00216 int left_phase;
00217 int right_phase;
00218 char message[20];
00219 };
00220
00221 class ScopedPaHandler
00222 {
00223 public:
00224 ScopedPaHandler()
00225 : _result(Pa_Initialize())
00226 {
00227 }
00228 ~ScopedPaHandler()
00229 {
00230 if (_result == paNoError)
00231 {
00232 Pa_Terminate();
00233 }
00234 }
00235
00236 PaError result() const { return _result; }
00237
00238 private:
00239 PaError _result;
00240 };
00241
00242
00243
00244 int main(void);
00245 int main(void)
00246 {
00247 Sine sine;
00248
00249 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
00250
00251 ScopedPaHandler paInit;
00252 if( paInit.result() != paNoError ) goto error;
00253
00254 if (sine.open(Pa_GetDefaultOutputDevice()))
00255 {
00256 if (sine.start())
00257 {
00258 printf("Play for %d seconds.\n", NUM_SECONDS );
00259 Pa_Sleep( NUM_SECONDS * 1000 );
00260
00261 sine.stop();
00262 }
00263
00264 sine.close();
00265 }
00266
00267 printf("Test finished.\n");
00268 return paNoError;
00269
00270 error:
00271 fprintf( stderr, "An error occured while using the portaudio stream\n" );
00272 fprintf( stderr, "Error number: %d\n", paInit.result() );
00273 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( paInit.result() ) );
00274 return 1;
00275 }