1 /*****************************************************************************
2 * waveout.c : Windows waveOut plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: waveout.c,v 1.13 2002/12/10 18:22:01 gbazin 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 <string.h> /* strerror() */
28 #include <stdlib.h> /* calloc(), malloc(), free() */
32 #include "aout_internal.h"
37 #define FRAME_SIZE 1024 /* The size is in samples, not in bytes */
40 /*****************************************************************************
42 *****************************************************************************/
43 #ifndef WAVE_FORMAT_IEEE_FLOAT
44 # define WAVE_FORMAT_IEEE_FLOAT 0x0003
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close ( vlc_object_t * );
52 static void Play ( aout_instance_t * );
55 static int OpenWaveOut ( aout_instance_t *p_aout, int i_format,
56 int i_channels, int i_rate );
57 static int PlayWaveOut ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
59 static void CALLBACK WaveOutCallback ( HWAVEOUT h_waveout, UINT uMsg,
61 DWORD dwParam1, DWORD dwParam2 );
63 /*****************************************************************************
65 *****************************************************************************/
67 set_description( _("Win32 waveOut extension module") );
68 set_capability( "audio output", 50 );
69 set_callbacks( Open, Close );
72 /*****************************************************************************
73 * aout_sys_t: waveOut audio output method descriptor
74 *****************************************************************************
75 * This structure is part of the audio output thread descriptor.
76 * It describes the waveOut specific properties of an audio device.
77 *****************************************************************************/
80 HWAVEOUT h_waveout; /* handle to waveout instance */
82 WAVEFORMATEX waveformat; /* audio format */
84 WAVEHDR waveheader[FRAMES_NUM];
88 byte_t *p_silence_buffer; /* buffer we use to play silence */
91 /*****************************************************************************
92 * Open: open the audio device
93 *****************************************************************************
94 * This function opens and setups Win32 waveOut
95 *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
98 aout_instance_t *p_aout = (aout_instance_t *)p_this;
101 /* Allocate structure */
102 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
104 if( p_aout->output.p_sys == NULL )
106 msg_Err( p_aout, "out of memory" );
110 p_aout->output.pf_play = Play;
111 aout_VolumeSoftInit( p_aout );
113 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
114 if ( i_nb_channels >= 2 )
117 p_aout->output.output.i_physical_channels =
118 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
123 p_aout->output.output.i_physical_channels =
127 /* We need to open the device with default values to be sure it is
129 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
130 if ( OpenWaveOut( p_aout, WAVE_FORMAT_IEEE_FLOAT, i_nb_channels,
131 p_aout->output.output.i_rate ) )
133 msg_Warn( p_aout, "Audio device can't use WAVE_FORMAT_IEEE_FLOAT" );
135 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
136 if ( OpenWaveOut( p_aout, WAVE_FORMAT_PCM, i_nb_channels,
137 p_aout->output.output.i_rate ) )
139 msg_Err( p_aout, "cannot open waveout audio device" );
144 waveOutReset( p_aout->output.p_sys->h_waveout );
146 /* Calculate the frame size in bytes */
147 p_aout->output.i_nb_samples = FRAME_SIZE;
148 aout_FormatPrepare( &p_aout->output.output );
149 p_aout->output.p_sys->i_buffer_size = FRAME_SIZE *
150 p_aout->output.output.i_bytes_per_frame;
151 /* Allocate silence buffer */
153 p_aout->output.p_sys->p_silence_buffer =
154 calloc( p_aout->output.p_sys->i_buffer_size, 1 );
156 p_aout->output.p_sys->p_silence_buffer =
157 malloc( p_aout->output.p_sys->i_buffer_size );
159 if( p_aout->output.p_sys->p_silence_buffer == NULL )
161 msg_Err( p_aout, "out of memory" );
165 memset( p_aout->output.p_sys->p_silence_buffer, 0,
166 p_aout->output.p_sys->i_buffer_size );
169 /* We need to kick off the playback in order to have the callback properly
171 for( i = 0; i < FRAMES_NUM; i++ )
173 PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
174 &p_aout->output.p_sys->waveheader[i], NULL );
179 /*****************************************************************************
180 * Play: play a sound buffer
181 *****************************************************************************
182 * This doesn't actually play the buffer. This just stores the buffer so it
183 * can be played by the callback thread.
184 *****************************************************************************/
185 static void Play( aout_instance_t *p_aout )
189 /*****************************************************************************
190 * Close: close the audio device
191 *****************************************************************************/
192 static void Close( vlc_object_t *p_this )
194 aout_instance_t *p_aout = (aout_instance_t *)p_this;
196 /* Before calling waveOutClose we must reset the device */
197 p_aout->b_die = VLC_TRUE;
198 //Hmmm, waveOutReset never seems to return... why ???
199 //waveOutReset( p_aout->output.p_sys->h_waveout );
201 /* Wait for the waveout buffers to be freed */
202 while( !(p_aout->output.p_sys->waveheader[0].dwFlags & WHDR_DONE) ||
203 !(p_aout->output.p_sys->waveheader[1].dwFlags & WHDR_DONE) )
208 /* Close the device */
209 if( waveOutClose( p_aout->output.p_sys->h_waveout ) != MMSYSERR_NOERROR )
211 msg_Err( p_aout, "waveOutClose failed" );
214 /* Free silence buffer */
215 free( p_aout->output.p_sys->p_silence_buffer );
217 free( p_aout->output.p_sys );
220 /*****************************************************************************
221 * OpenWaveOut: open the waveout sound device
222 ****************************************************************************/
223 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
224 int i_channels, int i_rate )
228 /* Set sound format */
229 p_aout->output.p_sys->waveformat.wFormatTag = i_format;
230 p_aout->output.p_sys->waveformat.nChannels = i_channels;
231 p_aout->output.p_sys->waveformat.nSamplesPerSec = i_rate;
235 case WAVE_FORMAT_PCM:
236 p_aout->output.p_sys->waveformat.wBitsPerSample = 16;
238 case WAVE_FORMAT_IEEE_FLOAT:
239 p_aout->output.p_sys->waveformat.wBitsPerSample = sizeof(float) * 8;
243 p_aout->output.p_sys->waveformat.nBlockAlign =
244 p_aout->output.p_sys->waveformat.wBitsPerSample / 8 * i_channels;
245 p_aout->output.p_sys->waveformat.nAvgBytesPerSec =
246 p_aout->output.p_sys->waveformat.nSamplesPerSec *
247 p_aout->output.p_sys->waveformat.nBlockAlign;
249 /* Open the device */
250 result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
251 &p_aout->output.p_sys->waveformat,
253 (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
255 (DWORD)WaveOutCallback, (DWORD)p_aout,
258 if( result == WAVERR_BADFORMAT )
260 msg_Warn( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
263 if( result != MMSYSERR_NOERROR )
265 msg_Warn( p_aout, "waveOutOpen failed" );
272 /*****************************************************************************
273 * PlayWaveOut: play a buffer through the WaveOut device
274 *****************************************************************************/
275 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
276 WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
280 /* Prepare the buffer */
281 if( p_buffer != NULL )
282 p_waveheader->lpData = p_buffer->p_buffer;
284 /* Use silence buffer instead */
285 p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
288 p_waveheader->dwUser = (DWORD_PTR)p_buffer;
290 p_waveheader->dwUser = (DWORD)p_buffer;
292 p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
293 p_waveheader->dwFlags = 0;
295 result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
296 if( result != MMSYSERR_NOERROR )
298 msg_Err( p_aout, "waveOutPrepareHeader failed" );
302 /* Send the buffer to the waveOut queue */
303 result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
304 if( result != MMSYSERR_NOERROR )
306 msg_Err( p_aout, "waveOutWrite failed" );
313 /*****************************************************************************
314 * WaveOutCallback: what to do once WaveOut has played its sound samples
315 *****************************************************************************/
316 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
318 DWORD dwParam1, DWORD dwParam2 )
320 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
321 WAVEHDR *p_waveheader = (WAVEHDR *)dwParam1;
322 aout_buffer_t *p_buffer;
324 if( uMsg != WOM_DONE ) return;
326 /* Unprepare and free the buffer which has just been played */
327 waveOutUnprepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
328 if( p_waveheader->dwUser )
329 aout_BufferFree( (aout_buffer_t *)p_waveheader->dwUser );
331 if( p_aout->b_die ) return;
333 /* Take into account WaveOut latency ( 1 FRAME ) */
334 p_buffer = aout_OutputNextBuffer( p_aout,
335 mdate() + 1000000 / p_aout->output.output.i_rate * FRAME_SIZE,
338 PlayWaveOut( p_aout, h_waveout, p_waveheader, p_buffer );