PortAudio  2.0
paex_wmme_ac3.c
Go to the documentation of this file.
1 
6 /*
7  * $Id: $
8  * Portable Audio I/O Library
9  * Windows MME ac3 sound output test
10  *
11  * Copyright (c) 2009 Ross Bencina
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files
15  * (the "Software"), to deal in the Software without restriction,
16  * including without limitation the rights to use, copy, modify, merge,
17  * publish, distribute, sublicense, and/or sell copies of the Software,
18  * and to permit persons to whom the Software is furnished to do so,
19  * subject to the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
28  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  */
32 
33 /*
34  * The text above constitutes the entire PortAudio license; however,
35  * the PortAudio community also makes the following non-binding requests:
36  *
37  * Any person wishing to distribute modifications to the Software is
38  * requested to send the modifications to the original developer so that
39  * they can be incorporated into the canonical version. It is also
40  * requested that these non-binding requests be included along with the
41  * license above.
42  */
43 
44 #include <stdio.h>
45 #include <math.h>
46 
47 #include <windows.h> /* required when using pa_win_wmme.h */
48 #include <mmsystem.h> /* required when using pa_win_wmme.h */
49 
50 #include "portaudio.h"
51 #include "pa_win_wmme.h"
52 
53 #define NUM_SECONDS (20)
54 #define SAMPLE_RATE (48000)
55 #define FRAMES_PER_BUFFER (64)
56 
57 #ifndef M_PI
58 #define M_PI (3.14159265)
59 #endif
60 
61 #define TABLE_SIZE (100)
62 
63 #define CHANNEL_COUNT (2)
64 
65 
66 
67 typedef struct
68 {
69  short *buffer;
70  int bufferSampleCount;
71  int playbackIndex;
72 }
74 
75 /* This routine will be called by the PortAudio engine when audio is needed.
76 ** It may called at interrupt level on some machines so don't do anything
77 ** that could mess up the system like calling malloc() or free().
78 */
79 static int patestCallback( const void *inputBuffer, void *outputBuffer,
80  unsigned long framesPerBuffer,
81  const PaStreamCallbackTimeInfo* timeInfo,
82  PaStreamCallbackFlags statusFlags,
83  void *userData )
84 {
85  paTestData *data = (paTestData*)userData;
86  short *out = (short*)outputBuffer;
87  unsigned long i,j;
88 
89  (void) timeInfo; /* Prevent unused variable warnings. */
90  (void) statusFlags;
91  (void) inputBuffer;
92 
93  /* stream out contents of data->buffer looping at end */
94 
95  for( i=0; i<framesPerBuffer; i++ )
96  {
97  for( j = 0; j < CHANNEL_COUNT; ++j ){
98  *out++ = data->buffer[ data->playbackIndex++ ];
99 
100  if( data->playbackIndex >= data->bufferSampleCount )
101  data->playbackIndex = 0; /* loop at end of buffer */
102  }
103  }
104 
105  return paContinue;
106 }
107 
108 /*******************************************************************/
109 int main(int argc, char* argv[])
110 {
111  PaStreamParameters outputParameters;
112  PaWinMmeStreamInfo wmmeStreamInfo;
113  PaStream *stream;
114  PaError err;
115  paTestData data;
116  int deviceIndex;
117  FILE *fp;
118  const char *fileName = "c:\\test_48k.ac3.spdif";
119  data.buffer = NULL;
120 
121  printf("usage: patest_wmme_ac3 fileName [paDeviceIndex]\n");
122  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");
123  printf("PortAudio Test: output a raw spdif ac3 stream. SR = %d, BufSize = %d, Chans = %d\n",
124  SAMPLE_RATE, FRAMES_PER_BUFFER, CHANNEL_COUNT);
125 
126 
127  if( argc >= 2 )
128  fileName = argv[1];
129 
130  printf( "reading spdif ac3 raw stream file %s\n", fileName );
131 
132  fp = fopen( fileName, "rb" );
133  if( !fp ){
134  fprintf( stderr, "error opening spdif ac3 file.\n" );
135  return -1;
136  }
137  /* get file size */
138  fseek( fp, 0, SEEK_END );
139  data.bufferSampleCount = ftell( fp ) / sizeof(short);
140  fseek( fp, 0, SEEK_SET );
141 
142  /* allocate buffer, read the whole file into memory */
143  data.buffer = (short*)malloc( data.bufferSampleCount * sizeof(short) );
144  if( !data.buffer ){
145  fprintf( stderr, "error allocating buffer.\n" );
146  return -1;
147  }
148 
149  fread( data.buffer, sizeof(short), data.bufferSampleCount, fp );
150  fclose( fp );
151 
152  data.playbackIndex = 0;
153 
154  err = Pa_Initialize();
155  if( err != paNoError ) goto error;
156 
158  if( argc >= 3 ){
159  sscanf( argv[1], "%d", &deviceIndex );
160  }
161 
162  printf( "using device id %d (%s)\n", deviceIndex, Pa_GetDeviceInfo(deviceIndex)->name );
163 
164 
165  outputParameters.device = deviceIndex;
166  outputParameters.channelCount = CHANNEL_COUNT;
167  outputParameters.sampleFormat = paInt16; /* IMPORTANT must use paInt16 for WMME AC3 */
168  outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
169  outputParameters.hostApiSpecificStreamInfo = NULL;
170 
171  wmmeStreamInfo.size = sizeof(PaWinMmeStreamInfo);
172  wmmeStreamInfo.hostApiType = paMME;
173  wmmeStreamInfo.version = 1;
174  wmmeStreamInfo.flags = paWinMmeWaveFormatDolbyAc3Spdif;
175  outputParameters.hostApiSpecificStreamInfo = &wmmeStreamInfo;
176 
177 
178  if( Pa_IsFormatSupported( 0, &outputParameters, SAMPLE_RATE ) == paFormatIsSupported ){
179  printf( "Pa_IsFormatSupported reports device will support %d channels.\n", CHANNEL_COUNT );
180  }else{
181  printf( "Pa_IsFormatSupported reports device will not support %d channels.\n", CHANNEL_COUNT );
182  }
183 
184  err = Pa_OpenStream(
185  &stream,
186  NULL, /* no input */
187  &outputParameters,
188  SAMPLE_RATE,
189  FRAMES_PER_BUFFER,
190  0,
191  patestCallback,
192  &data );
193  if( err != paNoError ) goto error;
194 
195  err = Pa_StartStream( stream );
196  if( err != paNoError ) goto error;
197 
198  printf("Play for %d seconds.\n", NUM_SECONDS );
199  Pa_Sleep( NUM_SECONDS * 1000 );
200 
201  err = Pa_StopStream( stream );
202  if( err != paNoError ) goto error;
203 
204  err = Pa_CloseStream( stream );
205  if( err != paNoError ) goto error;
206 
207  Pa_Terminate();
208  free( data.buffer );
209  printf("Test finished.\n");
210 
211  return err;
212 error:
213  Pa_Terminate();
214  free( data.buffer );
215 
216  fprintf( stderr, "An error occurred while using the portaudio stream\n" );
217  fprintf( stderr, "Error number: %d\n", err );
218  fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
219  return err;
220 }
PaError Pa_Initialize(void)
PaHostApiTypeId hostApiType
Definition: pa_win_wmme.h:84
PaError Pa_StopStream(PaStream *stream)
void PaStream
Definition: portaudio.h:644
PaError Pa_OpenStream(PaStream **stream, const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, double sampleRate, unsigned long framesPerBuffer, PaStreamFlags streamFlags, PaStreamCallback *streamCallback, void *userData)
unsigned long version
Definition: pa_win_wmme.h:85
PaError Pa_IsFormatSupported(const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, double sampleRate)
PaError Pa_StartStream(PaStream *stream)
void * hostApiSpecificStreamInfo
Definition: portaudio.h:588
The portable PortAudio API.
WMME-specific PortAudio API extension header file.
PaSampleFormat sampleFormat
Definition: portaudio.h:568
int PaError
Definition: portaudio.h:122
#define paInt16
Definition: portaudio.h:495
PaTime suggestedLatency
Definition: portaudio.h:581
unsigned long PaStreamCallbackFlags
Definition: portaudio.h:721
PaDeviceIndex defaultOutputDevice
Definition: portaudio.h:327
const PaHostApiInfo * Pa_GetHostApiInfo(PaHostApiIndex hostApi)
const PaDeviceInfo * Pa_GetDeviceInfo(PaDeviceIndex device)
PaDeviceIndex device
Definition: portaudio.h:555
void Pa_Sleep(long msec)
#define paFormatIsSupported
Definition: portaudio.h:594
const char * Pa_GetErrorText(PaError errorCode)
unsigned long size
Definition: pa_win_wmme.h:83
PaHostApiIndex Pa_HostApiTypeIdToHostApiIndex(PaHostApiTypeId type)
PaError Pa_CloseStream(PaStream *stream)
PaError Pa_Terminate(void)