PortAudio 2.0
paex_mono_asio_channel_select.c
Go to the documentation of this file.
00001 
00007 /*
00008  * $Id: paex_mono_asio_channel_select.c 1756 2011-09-08 06:09:29Z philburk $
00009  *
00010  * Authors:
00011  *    Ross Bencina <rossb@audiomulch.com>
00012  *    Phil Burk <philburk@softsynth.com>
00013  *
00014  * This program uses the PortAudio Portable Audio Library.
00015  * For more information see: http://www.portaudio.com
00016  * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
00017  *
00018  * Permission is hereby granted, free of charge, to any person obtaining
00019  * a copy of this software and associated documentation files
00020  * (the "Software"), to deal in the Software without restriction,
00021  * including without limitation the rights to use, copy, modify, merge,
00022  * publish, distribute, sublicense, and/or sell copies of the Software,
00023  * and to permit persons to whom the Software is furnished to do so,
00024  * subject to the following conditions:
00025  *
00026  * The above copyright notice and this permission notice shall be
00027  * included in all copies or substantial portions of the Software.
00028  *
00029  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00030  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00031  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00032  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
00033  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00034  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00035  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00036  */
00037 
00038 /*
00039  * The text above constitutes the entire PortAudio license; however, 
00040  * the PortAudio community also makes the following non-binding requests:
00041  *
00042  * Any person wishing to distribute modifications to the Software is
00043  * requested to send the modifications to the original developer so that
00044  * they can be incorporated into the canonical version. It is also 
00045  * requested that these non-binding requests be included along with the 
00046  * license above.
00047  */
00048 
00049 #include <stdio.h>
00050 #include <math.h>
00051 #include "portaudio.h"
00052 #include "pa_asio.h"
00053 
00054 #define NUM_SECONDS   (10)
00055 #define SAMPLE_RATE   (44100)
00056 #define AMPLITUDE     (0.8)
00057 #define FRAMES_PER_BUFFER  (64)
00058 #define OUTPUT_DEVICE Pa_GetDefaultOutputDevice()
00059 
00060 #ifndef M_PI
00061 #define M_PI  (3.14159265)
00062 #endif
00063 
00064 #define TABLE_SIZE   (200)
00065 typedef struct
00066 {
00067     float sine[TABLE_SIZE];
00068     int phase;
00069 }
00070 paTestData;
00071 
00072 /* This routine will be called by the PortAudio engine when audio is needed.
00073 ** It may called at interrupt level on some machines so don't do anything
00074 ** that could mess up the system like calling malloc() or free().
00075 */
00076 static int patestCallback( const void *inputBuffer, void *outputBuffer,
00077                             unsigned long framesPerBuffer,
00078                             const PaStreamCallbackTimeInfo* timeInfo,
00079                             PaStreamCallbackFlags statusFlags,
00080                             void *userData )
00081 {
00082     paTestData *data = (paTestData*)userData;
00083     float *out = (float*)outputBuffer;
00084     unsigned long i;
00085     int finished = 0;
00086     /* avoid unused variable warnings */
00087     (void) inputBuffer;
00088     (void) timeInfo;
00089     (void) statusFlags;
00090     for( i=0; i<framesPerBuffer; i++ )
00091     {
00092         *out++ = data->sine[data->phase];  /* left */
00093         data->phase += 1;
00094         if( data->phase >= TABLE_SIZE ) data->phase -= TABLE_SIZE;
00095     }
00096     return finished;
00097 }
00098 
00099 /*******************************************************************/
00100 int main(void);
00101 int main(void)
00102 {
00103     PaStreamParameters outputParameters;
00104     PaAsioStreamInfo asioOutputInfo;
00105     PaStream *stream;
00106     PaError err;
00107     paTestData data;
00108     int outputChannelSelectors[1];
00109     int i;
00110     printf("PortAudio Test: output MONO sine wave. SR = %d, BufSize = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER);
00111     /* initialise sinusoidal wavetable */
00112     for( i=0; i<TABLE_SIZE; i++ )
00113     {
00114         data.sine[i] = (float) (AMPLITUDE * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
00115     }
00116     data.phase = 0;
00117     
00118     err = Pa_Initialize();
00119     if( err != paNoError ) goto error;
00120 
00121     outputParameters.device = OUTPUT_DEVICE;
00122     outputParameters.channelCount = 1;       /* MONO output */
00123     outputParameters.sampleFormat = paFloat32; /* 32 bit floating point output */
00124     outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
00125 
00126         /* Use an ASIO specific structure. WARNING - this is not portable. */
00127     asioOutputInfo.size = sizeof(PaAsioStreamInfo);
00128     asioOutputInfo.hostApiType = paASIO;
00129     asioOutputInfo.version = 1;
00130     asioOutputInfo.flags = paAsioUseChannelSelectors;
00131     outputChannelSelectors[0] = 1; /* skip channel 0 and use the second (right) ASIO device channel */
00132     asioOutputInfo.channelSelectors = outputChannelSelectors;
00133     outputParameters.hostApiSpecificStreamInfo = &asioOutputInfo;
00134 
00135     err = Pa_OpenStream(
00136               &stream,
00137               NULL, /* no input */
00138               &outputParameters,
00139               SAMPLE_RATE,
00140               FRAMES_PER_BUFFER,
00141               paClipOff,      /* we won't output out of range samples so don't bother clipping them */
00142               patestCallback,
00143               &data );
00144     if( err != paNoError ) goto error;
00145 
00146     err = Pa_StartStream( stream );
00147     if( err != paNoError ) goto error;
00148     
00149     printf("Play for %d seconds.\n", NUM_SECONDS ); fflush(stdout);
00150     Pa_Sleep( NUM_SECONDS * 1000 );
00151 
00152     err = Pa_StopStream( stream );
00153     if( err != paNoError ) goto error;
00154     
00155     err = Pa_CloseStream( stream );
00156     if( err != paNoError ) goto error;
00157     
00158     Pa_Terminate();
00159     printf("Test finished.\n");
00160     return err;
00161 error:
00162     Pa_Terminate();
00163     fprintf( stderr, "An error occured while using the portaudio stream\n" );
00164     fprintf( stderr, "Error number: %d\n", err );
00165     fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
00166     return err;
00167 }