PortAudio  2.0
paex_saw.c
Go to the documentation of this file.
00001 
00006 /*
00007  * $Id: paex_saw.c 1752 2011-09-08 03:21:55Z philburk $
00008  *
00009  * This program uses the PortAudio Portable Audio Library.
00010  * For more information see: http://www.portaudio.com
00011  * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
00012  *
00013  * Permission is hereby granted, free of charge, to any person obtaining
00014  * a copy of this software and associated documentation files
00015  * (the "Software"), to deal in the Software without restriction,
00016  * including without limitation the rights to use, copy, modify, merge,
00017  * publish, distribute, sublicense, and/or sell copies of the Software,
00018  * and to permit persons to whom the Software is furnished to do so,
00019  * subject to the following conditions:
00020  *
00021  * The above copyright notice and this permission notice shall be
00022  * included in all copies or substantial portions of the Software.
00023  *
00024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00027  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
00028  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00029  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  */
00032 
00033 /*
00034  * The text above constitutes the entire PortAudio license; however, 
00035  * the PortAudio community also makes the following non-binding requests:
00036  *
00037  * Any person wishing to distribute modifications to the Software is
00038  * requested to send the modifications to the original developer so that
00039  * they can be incorporated into the canonical version. It is also 
00040  * requested that these non-binding requests be included along with the 
00041  * license above.
00042  */
00043 
00044 #include <stdio.h>
00045 #include <math.h>
00046 #include "portaudio.h"
00047 #define NUM_SECONDS   (4)
00048 #define SAMPLE_RATE   (44100)
00049 
00050 typedef struct
00051 {
00052     float left_phase;
00053     float right_phase;
00054 }
00055 paTestData;
00056 
00057 /* This routine will be called by the PortAudio engine when audio is needed.
00058 ** It may called at interrupt level on some machines so don't do anything
00059 ** that could mess up the system like calling malloc() or free().
00060 */
00061 static int patestCallback( const void *inputBuffer, void *outputBuffer,
00062                            unsigned long framesPerBuffer,
00063                            const PaStreamCallbackTimeInfo* timeInfo,
00064                            PaStreamCallbackFlags statusFlags,
00065                            void *userData )
00066 {
00067     /* Cast data passed through stream to our structure. */
00068     paTestData *data = (paTestData*)userData;
00069     float *out = (float*)outputBuffer;
00070     unsigned int i;
00071     (void) inputBuffer; /* Prevent unused variable warning. */
00072 
00073     for( i=0; i<framesPerBuffer; i++ )
00074     {
00075         *out++ = data->left_phase;  /* left */
00076         *out++ = data->right_phase;  /* right */
00077         /* Generate simple sawtooth phaser that ranges between -1.0 and 1.0. */
00078         data->left_phase += 0.01f;
00079         /* When signal reaches top, drop back down. */
00080         if( data->left_phase >= 1.0f ) data->left_phase -= 2.0f;
00081         /* higher pitch so we can distinguish left and right. */
00082         data->right_phase += 0.03f;
00083         if( data->right_phase >= 1.0f ) data->right_phase -= 2.0f;
00084     }
00085     return 0;
00086 }
00087 
00088 /*******************************************************************/
00089 static paTestData data;
00090 int main(void);
00091 int main(void)
00092 {
00093     PaStream *stream;
00094     PaError err;
00095     
00096     printf("PortAudio Test: output sawtooth wave.\n");
00097     /* Initialize our data for use by callback. */
00098     data.left_phase = data.right_phase = 0.0;
00099     /* Initialize library before making any other calls. */
00100     err = Pa_Initialize();
00101     if( err != paNoError ) goto error;
00102     
00103     /* Open an audio I/O stream. */
00104     err = Pa_OpenDefaultStream( &stream,
00105                                 0,          /* no input channels */
00106                                 2,          /* stereo output */
00107                                 paFloat32,  /* 32 bit floating point output */
00108                                 SAMPLE_RATE,
00109                                 256,        /* frames per buffer */
00110                                 patestCallback,
00111                                 &data );
00112     if( err != paNoError ) goto error;
00113 
00114     err = Pa_StartStream( stream );
00115     if( err != paNoError ) goto error;
00116 
00117     /* Sleep for several seconds. */
00118     Pa_Sleep(NUM_SECONDS*1000);
00119 
00120     err = Pa_StopStream( stream );
00121     if( err != paNoError ) goto error;
00122     err = Pa_CloseStream( stream );
00123     if( err != paNoError ) goto error;
00124     Pa_Terminate();
00125     printf("Test finished.\n");
00126     return err;
00127 error:
00128     Pa_Terminate();
00129     fprintf( stderr, "An error occured while using the portaudio stream\n" );
00130     fprintf( stderr, "Error number: %d\n", err );
00131     fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
00132     return err;
00133 }