|
PortAudio 2.0
|
00001 00006 /* 00007 * $Id: $ 00008 * Portable Audio I/O Library 00009 * Windows MME ac3 sound output test 00010 * 00011 * Copyright (c) 2009 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 (20) 00054 #define SAMPLE_RATE (48000) 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 (2) 00064 00065 00066 00067 typedef struct 00068 { 00069 short *buffer; 00070 int bufferSampleCount; 00071 int playbackIndex; 00072 } 00073 paTestData; 00074 00075 /* This routine will be called by the PortAudio engine when audio is needed. 00076 ** It may called at interrupt level on some machines so don't do anything 00077 ** that could mess up the system like calling malloc() or free(). 00078 */ 00079 static int patestCallback( const void *inputBuffer, void *outputBuffer, 00080 unsigned long framesPerBuffer, 00081 const PaStreamCallbackTimeInfo* timeInfo, 00082 PaStreamCallbackFlags statusFlags, 00083 void *userData ) 00084 { 00085 paTestData *data = (paTestData*)userData; 00086 short *out = (short*)outputBuffer; 00087 unsigned long i,j; 00088 00089 (void) timeInfo; /* Prevent unused variable warnings. */ 00090 (void) statusFlags; 00091 (void) inputBuffer; 00092 00093 /* stream out contents of data->buffer looping at end */ 00094 00095 for( i=0; i<framesPerBuffer; i++ ) 00096 { 00097 for( j = 0; j < CHANNEL_COUNT; ++j ){ 00098 *out++ = data->buffer[ data->playbackIndex++ ]; 00099 00100 if( data->playbackIndex >= data->bufferSampleCount ) 00101 data->playbackIndex = 0; /* loop at end of buffer */ 00102 } 00103 } 00104 00105 return paContinue; 00106 } 00107 00108 /*******************************************************************/ 00109 int main(int argc, char* argv[]) 00110 { 00111 PaStreamParameters outputParameters; 00112 PaWinMmeStreamInfo wmmeStreamInfo; 00113 PaStream *stream; 00114 PaError err; 00115 paTestData data; 00116 int deviceIndex; 00117 FILE *fp; 00118 const char *fileName = "c:\\test_48k.ac3.spdif"; 00119 data.buffer = NULL; 00120 00121 printf("usage: patest_wmme_ac3 fileName [paDeviceIndex]\n"); 00122 printf("**IMPORTANT*** The provided file must include the spdif preamble at the start of every AC-3 frame. Using a normal ac3 file won't work.\n"); 00123 printf("PortAudio Test: output a raw spdif ac3 stream. SR = %d, BufSize = %d, Chans = %d\n", 00124 SAMPLE_RATE, FRAMES_PER_BUFFER, CHANNEL_COUNT); 00125 00126 00127 if( argc >= 2 ) 00128 fileName = argv[1]; 00129 00130 printf( "reading spdif ac3 raw stream file %s\n", fileName ); 00131 00132 fp = fopen( fileName, "rb" ); 00133 if( !fp ){ 00134 fprintf( stderr, "error opening spdif ac3 file.\n" ); 00135 return -1; 00136 } 00137 /* get file size */ 00138 fseek( fp, 0, SEEK_END ); 00139 data.bufferSampleCount = ftell( fp ) / sizeof(short); 00140 fseek( fp, 0, SEEK_SET ); 00141 00142 /* allocate buffer, read the whole file into memory */ 00143 data.buffer = (short*)malloc( data.bufferSampleCount * sizeof(short) ); 00144 if( !data.buffer ){ 00145 fprintf( stderr, "error allocating buffer.\n" ); 00146 return -1; 00147 } 00148 00149 fread( data.buffer, sizeof(short), data.bufferSampleCount, fp ); 00150 fclose( fp ); 00151 00152 data.playbackIndex = 0; 00153 00154 err = Pa_Initialize(); 00155 if( err != paNoError ) goto error; 00156 00157 deviceIndex = Pa_GetHostApiInfo( Pa_HostApiTypeIdToHostApiIndex( paMME ) )->defaultOutputDevice; 00158 if( argc >= 3 ){ 00159 sscanf( argv[1], "%d", &deviceIndex ); 00160 } 00161 00162 printf( "using device id %d (%s)\n", deviceIndex, Pa_GetDeviceInfo(deviceIndex)->name ); 00163 00164 00165 outputParameters.device = deviceIndex; 00166 outputParameters.channelCount = CHANNEL_COUNT; 00167 outputParameters.sampleFormat = paInt16; /* IMPORTANT must use paInt16 for WMME AC3 */ 00168 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; 00169 outputParameters.hostApiSpecificStreamInfo = NULL; 00170 00171 wmmeStreamInfo.size = sizeof(PaWinMmeStreamInfo); 00172 wmmeStreamInfo.hostApiType = paMME; 00173 wmmeStreamInfo.version = 1; 00174 wmmeStreamInfo.flags = paWinMmeWaveFormatDolbyAc3Spdif; 00175 outputParameters.hostApiSpecificStreamInfo = &wmmeStreamInfo; 00176 00177 00178 if( Pa_IsFormatSupported( 0, &outputParameters, SAMPLE_RATE ) == paFormatIsSupported ){ 00179 printf( "Pa_IsFormatSupported reports device will support %d channels.\n", CHANNEL_COUNT ); 00180 }else{ 00181 printf( "Pa_IsFormatSupported reports device will not support %d channels.\n", CHANNEL_COUNT ); 00182 } 00183 00184 err = Pa_OpenStream( 00185 &stream, 00186 NULL, /* no input */ 00187 &outputParameters, 00188 SAMPLE_RATE, 00189 FRAMES_PER_BUFFER, 00190 0, 00191 patestCallback, 00192 &data ); 00193 if( err != paNoError ) goto error; 00194 00195 err = Pa_StartStream( stream ); 00196 if( err != paNoError ) goto error; 00197 00198 printf("Play for %d seconds.\n", NUM_SECONDS ); 00199 Pa_Sleep( NUM_SECONDS * 1000 ); 00200 00201 err = Pa_StopStream( stream ); 00202 if( err != paNoError ) goto error; 00203 00204 err = Pa_CloseStream( stream ); 00205 if( err != paNoError ) goto error; 00206 00207 Pa_Terminate(); 00208 free( data.buffer ); 00209 printf("Test finished.\n"); 00210 00211 return err; 00212 error: 00213 Pa_Terminate(); 00214 free( data.buffer ); 00215 00216 fprintf( stderr, "An error occured while using the portaudio stream\n" ); 00217 fprintf( stderr, "Error number: %d\n", err ); 00218 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); 00219 return err; 00220 } 00221
1.7.3