PortAudio: paex_wmme_ac3.c Source File

PortAudio 2.0

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

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 }
73 paTestData;
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 i;
117 int deviceIndex;
118 FILE *fp;
119 const char *fileName = "c:\\test_48k.ac3.spdif";
120 data.buffer = NULL;
121
122 printf("usage: patest_wmme_ac3 fileName [paDeviceIndex]\n");
123 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");
124 printf("PortAudio Test: output a raw spdif ac3 stream. SR = %d, BufSize = %d, Chans = %d\n",
125 SAMPLE_RATE, FRAMES_PER_BUFFER, CHANNEL_COUNT);
126
127
128 if( argc >= 2 )
129 fileName = argv[1];
130
131 printf( "reading spdif ac3 raw stream file %s\n", fileName );
132
133 fp = fopen( fileName, "rb" );
134 if( !fp ){
135 fprintf( stderr, "error opening spdif ac3 file.\n" );
136 return -1;
137 }
138 /* get file size */
139 fseek( fp, 0, SEEK_END );
140 data.bufferSampleCount = ftell( fp ) / sizeof(short);
141 fseek( fp, 0, SEEK_SET );
142
143 /* allocate buffer, read the whole file into memory */
144 data.buffer = (short*)malloc( data.bufferSampleCount * sizeof(short) );
145 if( !data.buffer ){
146 fprintf( stderr, "error allocating buffer.\n" );
147 return -1;
148 }
149
150 fread( data.buffer, sizeof(short), data.bufferSampleCount, fp );
151 fclose( fp );
152
153 data.playbackIndex = 0;
154
155 err = Pa_Initialize();
156 if( err != paNoError ) goto error;
157
158 deviceIndex = Pa_GetHostApiInfo( Pa_HostApiTypeIdToHostApiIndex( paMME ) )->defaultOutputDevice;
159 if( argc >= 3 ){
160 sscanf( argv[1], "%d", &deviceIndex );
161 }
162
163 printf( "using device id %d (%s)\n", deviceIndex, Pa_GetDeviceInfo(deviceIndex)->name );
164
165
166 outputParameters.device = deviceIndex;
167 outputParameters.channelCount = CHANNEL_COUNT;
168 outputParameters.sampleFormat = paInt16; /* IMPORTANT must use paInt16 for WMME AC3 */
169 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
170 outputParameters.hostApiSpecificStreamInfo = NULL;
171
172 wmmeStreamInfo.size = sizeof(PaWinMmeStreamInfo);
173 wmmeStreamInfo.hostApiType = paMME;
174 wmmeStreamInfo.version = 1;
175 wmmeStreamInfo.flags = paWinMmeWaveFormatDolbyAc3Spdif;
176 outputParameters.hostApiSpecificStreamInfo = &wmmeStreamInfo;
177
178
179 if( Pa_IsFormatSupported( 0, &outputParameters, SAMPLE_RATE ) == paFormatIsSupported ){
180 printf( "Pa_IsFormatSupported reports device will support %d channels.\n", CHANNEL_COUNT );
181 }else{
182 printf( "Pa_IsFormatSupported reports device will not support %d channels.\n", CHANNEL_COUNT );
183 }
184
185 err = Pa_OpenStream(
186 &stream,
187 NULL, /* no input */
188 &outputParameters,
189 SAMPLE_RATE,
190 FRAMES_PER_BUFFER,
191 0,
192 patestCallback,
193 &data );
194 if( err != paNoError ) goto error;
195
196 err = Pa_StartStream( stream );
197 if( err != paNoError ) goto error;
198
199 printf("Play for %d seconds.\n", NUM_SECONDS );
200 Pa_Sleep( NUM_SECONDS * 1000 );
201
202 err = Pa_StopStream( stream );
203 if( err != paNoError ) goto error;
204
205 err = Pa_CloseStream( stream );
206 if( err != paNoError ) goto error;
207
208 Pa_Terminate();
209 free( data.buffer );
210 printf("Test finished.\n");
211
212 return err;
213 error:
214 Pa_Terminate();
215 free( data.buffer );
216
217 fprintf( stderr, "An error occured while using the portaudio stream\n" );
218 fprintf( stderr, "Error number: %d\n", err );
219 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
220 return err;
221 }
222