paex_write_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
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 printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
00076
00077
00078 for( i=0; i<TABLE_SIZE; i++ )
00079 {
00080 sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
00081 }
00082
00083
00084 err = Pa_Initialize();
00085 if( err != paNoError ) goto error;
00086
00087 outputParameters.device = Pa_GetDefaultOutputDevice();
00088 if (outputParameters.device == paNoDevice) {
00089 fprintf(stderr,"Error: No default output device.\n");
00090 goto error;
00091 }
00092 outputParameters.channelCount = 2;
00093 outputParameters.sampleFormat = paFloat32;
00094 outputParameters.suggestedLatency = 0.050;
00095 outputParameters.hostApiSpecificStreamInfo = NULL;
00096
00097 err = Pa_OpenStream(
00098 &stream,
00099 NULL,
00100 &outputParameters,
00101 SAMPLE_RATE,
00102 FRAMES_PER_BUFFER,
00103 paClipOff,
00104 NULL,
00105 NULL );
00106 if( err != paNoError ) goto error;
00107
00108
00109 printf( "Play 3 times, higher each time.\n" );
00110
00111 for( k=0; k < 3; ++k )
00112 {
00113 err = Pa_StartStream( stream );
00114 if( err != paNoError ) goto error;
00115
00116 printf("Play for %d seconds.\n", NUM_SECONDS );
00117
00118 bufferCount = ((NUM_SECONDS * SAMPLE_RATE) / FRAMES_PER_BUFFER);
00119
00120 for( i=0; i < bufferCount; i++ )
00121 {
00122 for( j=0; j < FRAMES_PER_BUFFER; j++ )
00123 {
00124 buffer[j][0] = sine[left_phase];
00125 buffer[j][1] = sine[right_phase];
00126 left_phase += left_inc;
00127 if( left_phase >= TABLE_SIZE ) left_phase -= TABLE_SIZE;
00128 right_phase += right_inc;
00129 if( right_phase >= TABLE_SIZE ) right_phase -= TABLE_SIZE;
00130 }
00131
00132 err = Pa_WriteStream( stream, buffer, FRAMES_PER_BUFFER );
00133 if( err != paNoError ) goto error;
00134 }
00135
00136 err = Pa_StopStream( stream );
00137 if( err != paNoError ) goto error;
00138
00139 ++left_inc;
00140 ++right_inc;
00141
00142 Pa_Sleep( 1000 );
00143 }
00144
00145 err = Pa_CloseStream( stream );
00146 if( err != paNoError ) goto error;
00147
00148 Pa_Terminate();
00149 printf("Test finished.\n");
00150
00151 return err;
00152
00153 error:
00154 fprintf( stderr, "An error occured while using the portaudio stream\n" );
00155 fprintf( stderr, "Error number: %d\n", err );
00156 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
00157
00158 if( err == paUnanticipatedHostError )
00159 {
00160 const PaHostErrorInfo *hostErrorInfo = Pa_GetLastHostErrorInfo();
00161 fprintf( stderr, "Host API error = #%ld, hostApiType = %d\n", hostErrorInfo->errorCode, hostErrorInfo->hostApiType );
00162 fprintf( stderr, "Host API error = %s\n", hostErrorInfo->errorText );
00163 }
00164 Pa_Terminate();
00165 return err;
00166 }