PortAudio
2.0
|
00001 00006 /* 00007 * $Id: $ 00008 * Portable Audio I/O Library 00009 * Windows MME surround sound output test 00010 * 00011 * Copyright (c) 2007 Ross Bencina 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 00047 #include <windows.h> /* required when using pa_win_wmme.h */ 00048 #include <mmsystem.h> /* required when using pa_win_wmme.h */ 00049 00050 #include "portaudio.h" 00051 #include "pa_win_wmme.h" 00052 00053 #define NUM_SECONDS (12) 00054 #define SAMPLE_RATE (44100) 00055 #define FRAMES_PER_BUFFER (64) 00056 00057 #ifndef M_PI 00058 #define M_PI (3.14159265) 00059 #endif 00060 00061 #define TABLE_SIZE (100) 00062 00063 #define CHANNEL_COUNT (6) 00064 00065 00066 00067 typedef struct 00068 { 00069 float sine[TABLE_SIZE]; 00070 int phase; 00071 int currentChannel; 00072 int cycleCount; 00073 } 00074 paTestData; 00075 00076 /* This routine will be called by the PortAudio engine when audio is needed. 00077 ** It may called at interrupt level on some machines so don't do anything 00078 ** that could mess up the system like calling malloc() or free(). 00079 */ 00080 static int patestCallback( const void *inputBuffer, void *outputBuffer, 00081 unsigned long framesPerBuffer, 00082 const PaStreamCallbackTimeInfo* timeInfo, 00083 PaStreamCallbackFlags statusFlags, 00084 void *userData ) 00085 { 00086 paTestData *data = (paTestData*)userData; 00087 float *out = (float*)outputBuffer; 00088 unsigned long i,j; 00089 00090 (void) timeInfo; /* Prevent unused variable warnings. */ 00091 (void) statusFlags; 00092 (void) inputBuffer; 00093 00094 for( i=0; i<framesPerBuffer; i++ ) 00095 { 00096 for( j = 0; j < CHANNEL_COUNT; ++j ){ 00097 if( j == data->currentChannel && data->cycleCount < 4410 ){ 00098 *out++ = data->sine[data->phase]; 00099 data->phase += 1 + j; // play each channel at a different pitch so they can be distinguished 00100 if( data->phase >= TABLE_SIZE ){ 00101 data->phase -= TABLE_SIZE; 00102 } 00103 }else{ 00104 *out++ = 0; 00105 } 00106 } 00107 00108 data->cycleCount++; 00109 if( data->cycleCount > 44100 ){ 00110 data->cycleCount = 0; 00111 00112 ++data->currentChannel; 00113 if( data->currentChannel >= CHANNEL_COUNT ) 00114 data->currentChannel -= CHANNEL_COUNT; 00115 } 00116 } 00117 00118 return paContinue; 00119 } 00120 00121 /*******************************************************************/ 00122 int main(int argc, char* argv[]) 00123 { 00124 PaStreamParameters outputParameters; 00125 PaWinMmeStreamInfo wmmeStreamInfo; 00126 PaStream *stream; 00127 PaError err; 00128 paTestData data; 00129 int i; 00130 int deviceIndex; 00131 00132 printf("PortAudio Test: output a sine blip on each channel. SR = %d, BufSize = %d, Chans = %d\n", SAMPLE_RATE, FRAMES_PER_BUFFER, CHANNEL_COUNT); 00133 00134 err = Pa_Initialize(); 00135 if( err != paNoError ) goto error; 00136 00137 deviceIndex = Pa_GetHostApiInfo( Pa_HostApiTypeIdToHostApiIndex( paMME ) )->defaultOutputDevice; 00138 if( argc == 2 ){ 00139 sscanf( argv[1], "%d", &deviceIndex ); 00140 } 00141 00142 printf( "using device id %d (%s)\n", deviceIndex, Pa_GetDeviceInfo(deviceIndex)->name ); 00143 00144 /* initialise sinusoidal wavetable */ 00145 for( i=0; i<TABLE_SIZE; i++ ) 00146 { 00147 data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ); 00148 } 00149 00150 data.phase = 0; 00151 data.currentChannel = 0; 00152 data.cycleCount = 0; 00153 00154 outputParameters.device = deviceIndex; 00155 outputParameters.channelCount = CHANNEL_COUNT; 00156 outputParameters.sampleFormat = paFloat32; /* 32 bit floating point processing */ 00157 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; 00158 outputParameters.hostApiSpecificStreamInfo = NULL; 00159 00160 /* it's not strictly necessary to provide a channelMask for surround sound 00161 output. But if you want to be sure which channel mask PortAudio will use 00162 then you should supply one */ 00163 wmmeStreamInfo.size = sizeof(PaWinMmeStreamInfo); 00164 wmmeStreamInfo.hostApiType = paMME; 00165 wmmeStreamInfo.version = 1; 00166 wmmeStreamInfo.flags = paWinMmeUseChannelMask; 00167 wmmeStreamInfo.channelMask = PAWIN_SPEAKER_5POINT1; /* request 5.1 output format */ 00168 outputParameters.hostApiSpecificStreamInfo = &wmmeStreamInfo; 00169 00170 00171 if( Pa_IsFormatSupported( 0, &outputParameters, SAMPLE_RATE ) == paFormatIsSupported ){ 00172 printf( "Pa_IsFormatSupported reports device will support %d channels.\n", CHANNEL_COUNT ); 00173 }else{ 00174 printf( "Pa_IsFormatSupported reports device will not support %d channels.\n", CHANNEL_COUNT ); 00175 } 00176 00177 err = Pa_OpenStream( 00178 &stream, 00179 NULL, /* no input */ 00180 &outputParameters, 00181 SAMPLE_RATE, 00182 FRAMES_PER_BUFFER, 00183 paClipOff, /* we won't output out of range samples so don't bother clipping them */ 00184 patestCallback, 00185 &data ); 00186 if( err != paNoError ) goto error; 00187 00188 err = Pa_StartStream( stream ); 00189 if( err != paNoError ) goto error; 00190 00191 printf("Play for %d seconds.\n", NUM_SECONDS ); 00192 Pa_Sleep( NUM_SECONDS * 1000 ); 00193 00194 err = Pa_StopStream( stream ); 00195 if( err != paNoError ) goto error; 00196 00197 err = Pa_CloseStream( stream ); 00198 if( err != paNoError ) goto error; 00199 00200 Pa_Terminate(); 00201 printf("Test finished.\n"); 00202 00203 return err; 00204 error: 00205 Pa_Terminate(); 00206 fprintf( stderr, "An error occured while using the portaudio stream\n" ); 00207 fprintf( stderr, "Error number: %d\n", err ); 00208 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); 00209 return err; 00210 } 00211