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 i; 00117 int deviceIndex; 00118 FILE *fp; 00119 const char *fileName = "c:\\test_48k.ac3.spdif"; 00120 data.buffer = NULL; 00121 00122 printf("usage: patest_wmme_ac3 fileName [paDeviceIndex]\n"); 00123 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"); 00124 printf("PortAudio Test: output a raw spdif ac3 stream. SR = %d, BufSize = %d, Chans = %d\n", 00125 SAMPLE_RATE, FRAMES_PER_BUFFER, CHANNEL_COUNT); 00126 00127 00128 if( argc >= 2 ) 00129 fileName = argv[1]; 00130 00131 printf( "reading spdif ac3 raw stream file %s\n", fileName ); 00132 00133 fp = fopen( fileName, "rb" ); 00134 if( !fp ){ 00135 fprintf( stderr, "error opening spdif ac3 file.\n" ); 00136 return -1; 00137 } 00138 /* get file size */ 00139 fseek( fp, 0, SEEK_END ); 00140 data.bufferSampleCount = ftell( fp ) / sizeof(short); 00141 fseek( fp, 0, SEEK_SET ); 00142 00143 /* allocate buffer, read the whole file into memory */ 00144 data.buffer = (short*)malloc( data.bufferSampleCount * sizeof(short) ); 00145 if( !data.buffer ){ 00146 fprintf( stderr, "error allocating buffer.\n" ); 00147 return -1; 00148 } 00149 00150 fread( data.buffer, sizeof(short), data.bufferSampleCount, fp ); 00151 fclose( fp ); 00152 00153 data.playbackIndex = 0; 00154 00155 err = Pa_Initialize(); 00156 if( err != paNoError ) goto error; 00157 00158 deviceIndex = Pa_GetHostApiInfo( Pa_HostApiTypeIdToHostApiIndex( paMME ) )->defaultOutputDevice; 00159 if( argc >= 3 ){ 00160 sscanf( argv[1], "%d", &deviceIndex ); 00161 } 00162 00163 printf( "using device id %d (%s)\n", deviceIndex, Pa_GetDeviceInfo(deviceIndex)->name ); 00164 00165 00166 outputParameters.device = deviceIndex; 00167 outputParameters.channelCount = CHANNEL_COUNT; 00168 outputParameters.sampleFormat = paInt16; /* IMPORTANT must use paInt16 for WMME AC3 */ 00169 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; 00170 outputParameters.hostApiSpecificStreamInfo = NULL; 00171 00172 wmmeStreamInfo.size = sizeof(PaWinMmeStreamInfo); 00173 wmmeStreamInfo.hostApiType = paMME; 00174 wmmeStreamInfo.version = 1; 00175 wmmeStreamInfo.flags = paWinMmeWaveFormatDolbyAc3Spdif; 00176 outputParameters.hostApiSpecificStreamInfo = &wmmeStreamInfo; 00177 00178 00179 if( Pa_IsFormatSupported( 0, &outputParameters, SAMPLE_RATE ) == paFormatIsSupported ){ 00180 printf( "Pa_IsFormatSupported reports device will support %d channels.\n", CHANNEL_COUNT ); 00181 }else{ 00182 printf( "Pa_IsFormatSupported reports device will not support %d channels.\n", CHANNEL_COUNT ); 00183 } 00184 00185 err = Pa_OpenStream( 00186 &stream, 00187 NULL, /* no input */ 00188 &outputParameters, 00189 SAMPLE_RATE, 00190 FRAMES_PER_BUFFER, 00191 0, 00192 patestCallback, 00193 &data ); 00194 if( err != paNoError ) goto error; 00195 00196 err = Pa_StartStream( stream ); 00197 if( err != paNoError ) goto error; 00198 00199 printf("Play for %d seconds.\n", NUM_SECONDS ); 00200 Pa_Sleep( NUM_SECONDS * 1000 ); 00201 00202 err = Pa_StopStream( stream ); 00203 if( err != paNoError ) goto error; 00204 00205 err = Pa_CloseStream( stream ); 00206 if( err != paNoError ) goto error; 00207 00208 Pa_Terminate(); 00209 free( data.buffer ); 00210 printf("Test finished.\n"); 00211 00212 return err; 00213 error: 00214 Pa_Terminate(); 00215 free( data.buffer ); 00216 00217 fprintf( stderr, "An error occured while using the portaudio stream\n" ); 00218 fprintf( stderr, "Error number: %d\n", err ); 00219 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); 00220 return err; 00221 } 00222