1 /*****************************************************************************
2 * waveout.c : Windows waveOut plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: waveout.c,v 1.11 2002/11/15 16:27:10 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 <errno.h> /* ENOMEM */
28 #include <fcntl.h> /* open(), O_WRONLY */
29 #include <string.h> /* strerror() */
31 #include <stdlib.h> /* calloc(), malloc(), free() */
35 #include "aout_internal.h"
40 #define FRAME_SIZE 1024 /* The size is in samples, not in bytes */
43 /*****************************************************************************
45 *****************************************************************************/
46 #ifndef WAVE_FORMAT_IEEE_FLOAT
47 # define WAVE_FORMAT_IEEE_FLOAT 0x0003
50 /*****************************************************************************
52 *****************************************************************************/
53 static int Open ( vlc_object_t * );
54 static void Close ( vlc_object_t * );
55 static void Play ( aout_instance_t * );
58 static int OpenWaveOut ( aout_instance_t *p_aout, int i_format,
59 int i_channels, int i_rate );
60 static int PlayWaveOut ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
62 static void CALLBACK WaveOutCallback ( HWAVEOUT h_waveout, UINT uMsg,
64 DWORD dwParam1, DWORD dwParam2 );
66 /*****************************************************************************
68 *****************************************************************************/
70 set_description( _("Win32 waveOut extension module") );
71 set_capability( "audio output", 50 );
72 set_callbacks( Open, Close );
75 /*****************************************************************************
76 * aout_sys_t: waveOut audio output method descriptor
77 *****************************************************************************
78 * This structure is part of the audio output thread descriptor.
79 * It describes the waveOut specific properties of an audio device.
80 *****************************************************************************/
83 HWAVEOUT h_waveout; /* handle to waveout instance */
85 WAVEFORMATEX waveformat; /* audio format */
87 WAVEHDR waveheader[FRAMES_NUM];
91 byte_t *p_silence_buffer; /* buffer we use to play silence */
94 /*****************************************************************************
95 * Open: open the audio device
96 *****************************************************************************
97 * This function opens and setups Win32 waveOut
98 *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
101 aout_instance_t *p_aout = (aout_instance_t *)p_this;
102 int i_nb_channels, i;
104 /* Allocate structure */
105 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
107 if( p_aout->output.p_sys == NULL )
109 msg_Err( p_aout, "out of memory" );
113 p_aout->output.pf_play = Play;
114 aout_VolumeSoftInit( p_aout );
116 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
117 if ( i_nb_channels >= 2 )
120 p_aout->output.output.i_physical_channels =
121 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
126 p_aout->output.output.i_physical_channels =
130 /* We need to open the device with default values to be sure it is
132 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
133 if ( OpenWaveOut( p_aout, WAVE_FORMAT_IEEE_FLOAT, i_nb_channels,
134 p_aout->output.output.i_rate ) )
136 msg_Err( p_aout, "Audio device doesn't allow WAVE_FORMAT_IEEE_FLOAT" );
138 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
139 if ( OpenWaveOut( p_aout, WAVE_FORMAT_PCM, i_nb_channels,
140 p_aout->output.output.i_rate ) )
142 msg_Err( p_aout, "cannot open waveout audio device" );
147 waveOutReset( p_aout->output.p_sys->h_waveout );
149 /* Calculate the frame size in bytes */
150 p_aout->output.i_nb_samples = FRAME_SIZE;
151 aout_FormatPrepare( &p_aout->output.output );
152 p_aout->output.p_sys->i_buffer_size = FRAME_SIZE *
153 p_aout->output.output.i_bytes_per_frame;
154 /* Allocate silence buffer */
155 p_aout->output.p_sys->p_silence_buffer =
156 calloc( p_aout->output.p_sys->i_buffer_size, 1 );
157 if( p_aout->output.p_sys->p_silence_buffer == NULL )
159 msg_Err( p_aout, "out of memory" );
163 /* We need to kick off the playback in order to have the callback properly
165 for( i = 0; i < FRAMES_NUM; i++ )
167 PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
168 &p_aout->output.p_sys->waveheader[i], NULL );
173 /*****************************************************************************
174 * Play: play a sound buffer
175 *****************************************************************************
176 * This doesn't actually play the buffer. This just stores the buffer so it
177 * can be played by the callback thread.
178 *****************************************************************************/
179 static void Play( aout_instance_t *p_aout )
183 /*****************************************************************************
184 * Close: close the audio device
185 *****************************************************************************/
186 static void Close( vlc_object_t *p_this )
188 aout_instance_t *p_aout = (aout_instance_t *)p_this;
190 /* Before calling waveOutClose we must reset the device */
191 p_aout->b_die = VLC_TRUE;
192 //Hmmm, waveOutReset never seems to return... why ???
193 //waveOutReset( p_aout->output.p_sys->h_waveout );
195 /* Wait for the waveout buffers to be freed */
196 while( !(p_aout->output.p_sys->waveheader[0].dwFlags & WHDR_DONE) ||
197 !(p_aout->output.p_sys->waveheader[1].dwFlags & WHDR_DONE) )
202 /* Close the device */
203 if( waveOutClose( p_aout->output.p_sys->h_waveout ) != MMSYSERR_NOERROR )
205 msg_Err( p_aout, "waveOutClose failed" );
208 /* Free silence buffer */
209 free( p_aout->output.p_sys->p_silence_buffer );
211 free( p_aout->output.p_sys );
214 /*****************************************************************************
215 * OpenWaveOut: open the waveout sound device
216 ****************************************************************************/
217 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
218 int i_channels, int i_rate )
222 /* Set sound format */
223 p_aout->output.p_sys->waveformat.wFormatTag = i_format;
224 p_aout->output.p_sys->waveformat.nChannels = i_channels;
225 p_aout->output.p_sys->waveformat.nSamplesPerSec = i_rate;
229 case WAVE_FORMAT_PCM:
230 p_aout->output.p_sys->waveformat.wBitsPerSample = 16;
232 case WAVE_FORMAT_IEEE_FLOAT:
233 p_aout->output.p_sys->waveformat.wBitsPerSample = sizeof(float) * 8;
237 p_aout->output.p_sys->waveformat.nBlockAlign =
238 p_aout->output.p_sys->waveformat.wBitsPerSample / 8 * i_channels;
239 p_aout->output.p_sys->waveformat.nAvgBytesPerSec =
240 p_aout->output.p_sys->waveformat.nSamplesPerSec *
241 p_aout->output.p_sys->waveformat.nBlockAlign;
243 /* Open the device */
244 result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
245 &p_aout->output.p_sys->waveformat,
246 (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
248 if( result == WAVERR_BADFORMAT )
250 msg_Err( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
253 if( result != MMSYSERR_NOERROR )
255 msg_Err( p_aout, "waveOutOpen failed" );
262 /*****************************************************************************
263 * PlayWaveOut: play a buffer through the WaveOut device
264 *****************************************************************************/
265 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
266 WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
270 /* Prepare the buffer */
271 if( p_buffer != NULL )
272 p_waveheader->lpData = p_buffer->p_buffer;
274 /* Use silence buffer instead */
275 p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
277 p_waveheader->dwUser = (DWORD_PTR)p_buffer;
278 p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
279 p_waveheader->dwFlags = 0;
281 result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
282 if( result != MMSYSERR_NOERROR )
284 msg_Err( p_aout, "waveOutPrepareHeader failed" );
288 /* Send the buffer to the waveOut queue */
289 result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
290 if( result != MMSYSERR_NOERROR )
292 msg_Err( p_aout, "waveOutWrite failed" );
299 /*****************************************************************************
300 * WaveOutCallback: what to do once WaveOut has played its sound samples
301 *****************************************************************************/
302 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
304 DWORD dwParam1, DWORD dwParam2 )
306 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
307 WAVEHDR *p_waveheader = (WAVEHDR *)dwParam1;
308 aout_buffer_t *p_buffer;
310 if( uMsg != WOM_DONE ) return;
312 /* Unprepare and free the buffer which has just been played */
313 waveOutUnprepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
314 if( p_waveheader->dwUser )
315 aout_BufferFree( (aout_buffer_t *)p_waveheader->dwUser );
317 if( p_aout->b_die ) return;
319 /* Take into account WaveOut latency ( 1 FRAME ) */
320 p_buffer = aout_OutputNextBuffer( p_aout,
321 mdate() + 1000000 / p_aout->output.output.i_rate * FRAME_SIZE,
324 PlayWaveOut( p_aout, h_waveout, p_waveheader, p_buffer );