paex_wmme_ac3.c
Go to the documentation of this file.00001
00006
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 #include <stdio.h>
00045 #include <math.h>
00046
00047 #include <windows.h>
00048 #include <mmsystem.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
00076
00077
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;
00090 (void) statusFlags;
00091 (void) inputBuffer;
00092
00093
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;
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
00139 fseek( fp, 0, SEEK_END );
00140 data.bufferSampleCount = ftell( fp ) / sizeof(short);
00141 fseek( fp, 0, SEEK_SET );
00142
00143
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;
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,
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