paex_mono_asio_channel_select.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
00046
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
00073
00074
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
00087 (void) inputBuffer;
00088 (void) timeInfo;
00089 (void) statusFlags;
00090 for( i=0; i<framesPerBuffer; i++ )
00091 {
00092 *out++ = data->sine[data->phase];
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
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;
00123 outputParameters.sampleFormat = paFloat32;
00124 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
00125
00126
00127 asioOutputInfo.size = sizeof(PaAsioStreamInfo);
00128 asioOutputInfo.hostApiType = paASIO;
00129 asioOutputInfo.version = 1;
00130 asioOutputInfo.flags = paAsioUseChannelSelectors;
00131 outputChannelSelectors[0] = 1;
00132 asioOutputInfo.channelSelectors = outputChannelSelectors;
00133 outputParameters.hostApiSpecificStreamInfo = &asioOutputInfo;
00134
00135 err = Pa_OpenStream(
00136 &stream,
00137 NULL,
00138 &outputParameters,
00139 SAMPLE_RATE,
00140 FRAMES_PER_BUFFER,
00141 paClipOff,
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 }