paex_sine.c
Go to the documentation of this file.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 typedef struct
00058 {
00059 float sine[TABLE_SIZE];
00060 int left_phase;
00061 int right_phase;
00062 char message[20];
00063 }
00064 paTestData;
00065
00066
00067
00068
00069
00070 static int patestCallback( const void *inputBuffer, void *outputBuffer,
00071 unsigned long framesPerBuffer,
00072 const PaStreamCallbackTimeInfo* timeInfo,
00073 PaStreamCallbackFlags statusFlags,
00074 void *userData )
00075 {
00076 paTestData *data = (paTestData*)userData;
00077 float *out = (float*)outputBuffer;
00078 unsigned long i;
00079
00080 (void) timeInfo;
00081 (void) statusFlags;
00082 (void) inputBuffer;
00083
00084 for( i=0; i<framesPerBuffer; i++ )
00085 {
00086 *out++ = data->sine[data->left_phase];
00087 *out++ = data->sine[data->right_phase];
00088 data->left_phase += 1;
00089 if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
00090 data->right_phase += 3;
00091 if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
00092 }
00093
00094 return paContinue;
00095 }
00096
00097
00098
00099
00100 static void StreamFinished( void* userData )
00101 {
00102 paTestData *data = (paTestData *) userData;
00103 printf( "Stream Completed: %s\n", data->message );
00104 }
00105
00106
00107 int main(void);
00108 int main(void)
00109 {
00110 PaStreamParameters outputParameters;
00111 PaStream *stream;
00112 PaError err;
00113 paTestData data;
00114 int i;
00115
00116 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
00117
00118
00119 for( i=0; i<TABLE_SIZE; i++ )
00120 {
00121 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
00122 }
00123 data.left_phase = data.right_phase = 0;
00124
00125 err = Pa_Initialize();
00126 if( err != paNoError ) goto error;
00127
00128 outputParameters.device = Pa_GetDefaultOutputDevice();
00129 if (outputParameters.device == paNoDevice) {
00130 fprintf(stderr,"Error: No default output device.\n");
00131 goto error;
00132 }
00133 outputParameters.channelCount = 2;
00134 outputParameters.sampleFormat = paFloat32;
00135 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
00136 outputParameters.hostApiSpecificStreamInfo = NULL;
00137
00138 err = Pa_OpenStream(
00139 &stream,
00140 NULL,
00141 &outputParameters,
00142 SAMPLE_RATE,
00143 FRAMES_PER_BUFFER,
00144 paClipOff,
00145 patestCallback,
00146 &data );
00147 if( err != paNoError ) goto error;
00148
00149 sprintf( data.message, "No Message" );
00150 err = Pa_SetStreamFinishedCallback( stream, &StreamFinished );
00151 if( err != paNoError ) goto error;
00152
00153 err = Pa_StartStream( stream );
00154 if( err != paNoError ) goto error;
00155
00156 printf("Play for %d seconds.\n", NUM_SECONDS );
00157 Pa_Sleep( NUM_SECONDS * 1000 );
00158
00159 err = Pa_StopStream( stream );
00160 if( err != paNoError ) goto error;
00161
00162 err = Pa_CloseStream( stream );
00163 if( err != paNoError ) goto error;
00164
00165 Pa_Terminate();
00166 printf("Test finished.\n");
00167
00168 return err;
00169 error:
00170 Pa_Terminate();
00171 fprintf( stderr, "An error occured while using the portaudio stream\n" );
00172 fprintf( stderr, "Error number: %d\n", err );
00173 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
00174 return err;
00175 }