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
00045 #include <stdio.h>
00046 #include <math.h>
00047 #include "portaudio.h"
00048
00049 #define NUM_SECONDS (5)
00050 #define SAMPLE_RATE (44100)
00051 #define FRAMES_PER_BUFFER (1024)
00052
00053 #ifndef M_PI
00054 #define M_PI (3.14159265)
00055 #endif
00056
00057 #define TABLE_SIZE (200)
00058
00059
00060 int main(void);
00061 int main(void)
00062 {
00063 PaStreamParameters outputParameters;
00064 PaStream *stream;
00065 PaError err;
00066 float buffer[FRAMES_PER_BUFFER][2];
00067 float sine[TABLE_SIZE];
00068 int left_phase = 0;
00069 int right_phase = 0;
00070 int left_inc = 1;
00071 int right_inc = 3;
00072 int i, j, k;
00073 int bufferCount;
00074
00075
00076 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
00077
00078
00079 for( i=0; i<TABLE_SIZE; i++ )
00080 {
00081 sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
00082 }
00083
00084
00085 err = Pa_Initialize();
00086 if( err != paNoError ) goto error;
00087
00088 outputParameters.device = Pa_GetDefaultOutputDevice();
00089 if (outputParameters.device == paNoDevice) {
00090 fprintf(stderr,"Error: No default output device.\n");
00091 goto error;
00092 }
00093 outputParameters.channelCount = 2;
00094 outputParameters.sampleFormat = paFloat32;
00095 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
00096 outputParameters.hostApiSpecificStreamInfo = NULL;
00097
00098 err = Pa_OpenStream(
00099 &stream,
00100 NULL,
00101 &outputParameters,
00102 SAMPLE_RATE,
00103 FRAMES_PER_BUFFER,
00104 paClipOff,
00105 NULL,
00106 NULL );
00107 if( err != paNoError ) goto error;
00108
00109
00110 printf( "Play 3 times, higher each time.\n" );
00111
00112 for( k=0; k < 3; ++k )
00113 {
00114 err = Pa_StartStream( stream );
00115 if( err != paNoError ) goto error;
00116
00117 printf("Play for %d seconds.\n", NUM_SECONDS );
00118
00119 bufferCount = ((NUM_SECONDS * SAMPLE_RATE) / FRAMES_PER_BUFFER);
00120
00121 for( i=0; i < bufferCount; i++ )
00122 {
00123 for( j=0; j < FRAMES_PER_BUFFER; j++ )
00124 {
00125 buffer[j][0] = sine[left_phase];
00126 buffer[j][1] = sine[right_phase];
00127 left_phase += left_inc;
00128 if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE;
00129 right_phase += right_inc;
00130 if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE;
00131 }
00132
00133 err = Pa_WriteStream( stream, buffer, FRAMES_PER_BUFFER );
00134 if( err != paNoError ) goto error;
00135 }
00136
00137 err = Pa_StopStream( stream );
00138 if( err != paNoError ) goto error;
00139
00140 ++left_inc;
00141 ++right_inc;
00142
00143 Pa_Sleep( 1000 );
00144 }
00145
00146 err = Pa_CloseStream( stream );
00147 if( err != paNoError ) goto error;
00148
00149 Pa_Terminate();
00150 printf("Test finished.\n");
00151
00152 return err;
00153 error:
00154 Pa_Terminate();
00155 fprintf( stderr, "An error occured while using the portaudio stream\n" );
00156 fprintf( stderr, "Error number: %d\n", err );
00157 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
00158 return err;
00159 }