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 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
00138 fseek( fp, 0, SEEK_END );
00139 data.bufferSampleCount = ftell( fp ) / sizeof(short);
00140 fseek( fp, 0, SEEK_SET );
00141
00142
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;
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,
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