PortAudio 2.0

paex_sine.c

Go to the documentation of this file.
00001 
00007 /*
00008  * $Id: paex_sine.c 1752 2011-09-08 03:21:55Z philburk $
00009  *
00010  * This program uses the PortAudio Portable Audio Library.
00011  * For more information see: http://www.portaudio.com/
00012  * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
00013  *
00014  * Permission is hereby granted, free of charge, to any person obtaining
00015  * a copy of this software and associated documentation files
00016  * (the "Software"), to deal in the Software without restriction,
00017  * including without limitation the rights to use, copy, modify, merge,
00018  * publish, distribute, sublicense, and/or sell copies of the Software,
00019  * and to permit persons to whom the Software is furnished to do so,
00020  * subject to the following conditions:
00021  *
00022  * The above copyright notice and this permission notice shall be
00023  * included in all copies or substantial portions of the Software.
00024  *
00025  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00026  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00027  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00028  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
00029  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00030  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00031  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00032  */
00033 
00034 /*
00035  * The text above constitutes the entire PortAudio license; however, 
00036  * the PortAudio community also makes the following non-binding requests:
00037  *
00038  * Any person wishing to distribute modifications to the Software is
00039  * requested to send the modifications to the original developer so that
00040  * they can be incorporated into the canonical version. It is also 
00041  * requested that these non-binding requests be included along with the 
00042  * license above.
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 /* This routine will be called by the PortAudio engine when audio is needed.
00067 ** It may called at interrupt level on some machines so don't do anything
00068 ** that could mess up the system like calling malloc() or free().
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; /* Prevent unused variable warnings. */
00081     (void) statusFlags;
00082     (void) inputBuffer;
00083     
00084     for( i=0; i<framesPerBuffer; i++ )
00085     {
00086         *out++ = data->sine[data->left_phase];  /* left */
00087         *out++ = data->sine[data->right_phase];  /* right */
00088         data->left_phase += 1;
00089         if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
00090         data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
00091         if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
00092     }
00093     
00094     return paContinue;
00095 }
00096 
00097 /*
00098  * This routine is called by portaudio when playback is done.
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     
00117     printf("PortAudio Test: output sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
00118     
00119     /* initialise sinusoidal wavetable */
00120     for( i=0; i<TABLE_SIZE; i++ )
00121     {
00122         data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
00123     }
00124     data.left_phase = data.right_phase = 0;
00125     
00126     err = Pa_Initialize();
00127     if( err != paNoError ) goto error;
00128 
00129     outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
00130     if (outputParameters.device == paNoDevice) {
00131       fprintf(stderr,"Error: No default output device.\n");
00132       goto error;
00133     }
00134     outputParameters.channelCount = 2;       /* stereo output */
00135     outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
00136     outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
00137     outputParameters.hostApiSpecificStreamInfo = NULL;
00138 
00139     err = Pa_OpenStream(
00140               &stream,
00141               NULL, /* no input */
00142               &outputParameters,
00143               SAMPLE_RATE,
00144               FRAMES_PER_BUFFER,
00145               paClipOff,      /* we won't output out of range samples so don't bother clipping them */
00146               patestCallback,
00147               &data );
00148     if( err != paNoError ) goto error;
00149 
00150     sprintf( data.message, "No Message" );
00151     err = Pa_SetStreamFinishedCallback( stream, &StreamFinished );
00152     if( err != paNoError ) goto error;
00153 
00154     err = Pa_StartStream( stream );
00155     if( err != paNoError ) goto error;
00156 
00157     printf("Play for %d seconds.\n", NUM_SECONDS );
00158     Pa_Sleep( NUM_SECONDS * 1000 );
00159 
00160     err = Pa_StopStream( stream );
00161     if( err != paNoError ) goto error;
00162 
00163     err = Pa_CloseStream( stream );
00164     if( err != paNoError ) goto error;
00165 
00166     Pa_Terminate();
00167     printf("Test finished.\n");
00168     
00169     return err;
00170 error:
00171     Pa_Terminate();
00172     fprintf( stderr, "An error occured while using the portaudio stream\n" );
00173     fprintf( stderr, "Error number: %d\n", err );
00174     fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
00175     return err;
00176 }