1 /*****************************************************************************
2 * waveout.c : Windows waveOut plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: waveout.c,v 1.10 2002/07/20 18:01:43 sam Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <errno.h> /* ENOMEM */
28 #include <fcntl.h> /* open(), O_WRONLY */
29 #include <string.h> /* strerror() */
31 #include <stdlib.h> /* calloc(), malloc(), free() */
38 /*****************************************************************************
40 *****************************************************************************/
41 static void aout_getfunctions( function_list_t * p_function_list );
43 static int aout_Open ( aout_thread_t *p_aout );
44 static int aout_SetFormat ( aout_thread_t *p_aout );
45 static int aout_GetBufInfo ( aout_thread_t *p_aout, int i_buffer_info );
46 static void aout_Play ( aout_thread_t *p_aout,
47 byte_t *buffer, int i_size );
48 static void aout_Close ( aout_thread_t *p_aout );
51 static int OpenWaveOutDevice( aout_thread_t *p_aout );
53 /*****************************************************************************
54 * Building configuration tree
55 *****************************************************************************/
60 SET_DESCRIPTION( _("Win32 waveOut extension module") )
61 ADD_CAPABILITY( AOUT, 250 )
65 aout_getfunctions( &p_module->p_functions->aout );
68 MODULE_DEACTIVATE_START
69 MODULE_DEACTIVATE_STOP
71 /*****************************************************************************
72 * aout_sys_t: waveOut audio output method descriptor
73 *****************************************************************************
74 * This structure is part of the audio output thread descriptor.
75 * It describes the waveOut specific properties of an audio device.
76 *****************************************************************************/
78 #define NUMBUF 3 /* We use triple buffering to be on the safe side */
82 HWAVEOUT h_waveout; /* handle to waveout instance */
84 WAVEFORMATEX waveformat; /* Audio format */
86 WAVEHDR waveheader[NUMBUF];
90 DWORD dw_counter; /* Number of bytes played since beginning */
93 /*****************************************************************************
94 * Functions exported as capabilities. They are declared as static so that
95 * we don't pollute the namespace too much.
96 *****************************************************************************/
97 static void aout_getfunctions( function_list_t * p_function_list )
99 p_function_list->functions.aout.pf_open = aout_Open;
100 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
101 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
102 p_function_list->functions.aout.pf_play = aout_Play;
103 p_function_list->functions.aout.pf_close = aout_Close;
106 /*****************************************************************************
107 * aout_Open: open the audio device
108 *****************************************************************************
109 * This function opens and setups Win32 waveOut
110 *****************************************************************************/
111 static int aout_Open( aout_thread_t *p_aout )
115 /* Allocate structure */
116 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
118 if( p_aout->p_sys == NULL )
120 msg_Err( p_aout, "out of memory" );
125 /* Initialize some variables */
126 p_aout->p_sys->i_current_buffer = 0;
127 for( i=0; i<NUMBUF; i++)
128 p_aout->p_sys->waveheader[i].lpData = malloc( 1 );
130 return OpenWaveOutDevice( p_aout );
134 /*****************************************************************************
135 * aout_SetFormat: reset the audio device and sets its format
136 *****************************************************************************
137 * This functions set a new audio format.
138 * For this we need to close the current device and create another
139 * one with the desired format.
140 *****************************************************************************/
141 static int aout_SetFormat( aout_thread_t *p_aout )
143 msg_Dbg( p_aout, "aout_SetFormat" );
145 /* Check if the format has changed */
147 if( (p_aout->p_sys->waveformat.nChannels != p_aout->i_channels) ||
148 (p_aout->p_sys->waveformat.nSamplesPerSec != p_aout->i_rate) )
150 /* Before calling waveOutClose we must reset the device */
151 waveOutReset( p_aout->p_sys->h_waveout );
153 if( waveOutClose( p_aout->p_sys->h_waveout ) != MMSYSERR_NOERROR )
155 msg_Err( p_aout, "waveOutClose failed" );
158 return OpenWaveOutDevice( p_aout );
164 /*****************************************************************************
165 * aout_GetBufInfo: buffer status query
166 *****************************************************************************
167 * returns the number of bytes in the audio buffer that have not yet been
168 * sent to the sound device.
169 *****************************************************************************/
170 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
174 mmtime.wType = TIME_BYTES;
175 if( (waveOutGetPosition(p_aout->p_sys->h_waveout, &mmtime, sizeof(MMTIME)))
176 != MMSYSERR_NOERROR || (mmtime.wType != TIME_BYTES) )
178 msg_Warn( p_aout, "waveOutGetPosition failed" );
179 return i_buffer_limit;
184 msg_Dbg( p_aout, "aout_GetBufInfo: %i",
185 p_aout->p_sys->dw_counter - mmtime.u.cb );
188 return (p_aout->p_sys->dw_counter - mmtime.u.cb);
191 /*****************************************************************************
192 * aout_Play: play a sound buffer
193 *****************************************************************************
194 * This function writes a buffer of i_length bytes
195 *****************************************************************************/
196 static void aout_Play( aout_thread_t *p_aout, byte_t *p_buffer, int i_size )
199 int current_buffer = p_aout->p_sys->i_current_buffer;
201 p_aout->p_sys->i_current_buffer = (current_buffer + 1) % NUMBUF;
203 /* Unprepare the old buffer */
204 waveOutUnprepareHeader( p_aout->p_sys->h_waveout,
205 &p_aout->p_sys->waveheader[current_buffer],
208 /* Prepare the buffer */
209 p_aout->p_sys->waveheader[current_buffer].lpData =
210 realloc( p_aout->p_sys->waveheader[current_buffer].lpData, i_size );
211 if( !p_aout->p_sys->waveheader[current_buffer].lpData )
213 msg_Err( p_aout, "could not allocate buffer" );
216 p_aout->p_sys->waveheader[current_buffer].dwBufferLength = i_size;
217 p_aout->p_sys->waveheader[current_buffer].dwFlags = 0;
219 result = waveOutPrepareHeader( p_aout->p_sys->h_waveout,
220 &p_aout->p_sys->waveheader[current_buffer],
222 if( result != MMSYSERR_NOERROR )
224 msg_Err( p_aout, "waveOutPrepareHeader failed" );
228 /* Send the buffer the waveOut queue */
229 p_aout->p_vlc->pf_memcpy( p_aout->p_sys->waveheader[current_buffer].lpData,
231 result = waveOutWrite( p_aout->p_sys->h_waveout,
232 &p_aout->p_sys->waveheader[current_buffer],
234 if( result != MMSYSERR_NOERROR )
236 msg_Err( p_aout, "waveOutWrite failed" );
240 /* keep track of number of bytes played */
241 p_aout->p_sys->dw_counter += i_size;
245 /*****************************************************************************
246 * aout_Close: close the audio device
247 *****************************************************************************/
248 static void aout_Close( aout_thread_t *p_aout )
252 /* Before calling waveOutClose we must reset the device */
253 waveOutReset( p_aout->p_sys->h_waveout );
255 /* Close the device */
256 if( waveOutClose( p_aout->p_sys->h_waveout ) != MMSYSERR_NOERROR )
258 msg_Err( p_aout, "waveOutClose failed" );
261 /* Deallocate memory */
262 for( i=0; i<NUMBUF; i++ )
263 free( p_aout->p_sys->waveheader[i].lpData );
265 if( p_aout->p_sys != NULL )
267 free( p_aout->p_sys );
268 p_aout->p_sys = NULL;
272 /*****************************************************************************
273 * OpenWaveOutDevice: open the sound device
274 ****************************************************************************/
275 static int OpenWaveOutDevice( aout_thread_t *p_aout )
279 /* initialize played bytes counter */
280 p_aout->p_sys->dw_counter = 0;
282 /* Set sound format */
283 p_aout->p_sys->waveformat.wFormatTag = WAVE_FORMAT_PCM;
284 p_aout->p_sys->waveformat.nChannels = p_aout->i_channels;
285 p_aout->p_sys->waveformat.nSamplesPerSec = p_aout->i_rate;
286 p_aout->p_sys->waveformat.wBitsPerSample = 16;
287 p_aout->p_sys->waveformat.nBlockAlign =
288 p_aout->p_sys->waveformat.wBitsPerSample / 8 * p_aout->i_channels;
289 p_aout->p_sys->waveformat.nAvgBytesPerSec =
290 p_aout->p_sys->waveformat.nSamplesPerSec *
291 p_aout->p_sys->waveformat.nBlockAlign;
294 /* Open the device */
295 result = waveOutOpen( &p_aout->p_sys->h_waveout, WAVE_MAPPER,
296 &p_aout->p_sys->waveformat,
297 0 /*callback*/, 0 /*callback data*/, CALLBACK_NULL );
298 if( result != MMSYSERR_NOERROR )
300 msg_Err( p_aout, "waveOutOpen failed" );