1 /*****************************************************************************
2 * waveout.c : Windows waveOut plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: waveout.c,v 1.10 2002/10/28 22:31:49 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 )
119 /* Waveout doesn't support more than two channels. */
121 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
124 /* We need to open the device with default values to be sure it is
126 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
127 if ( OpenWaveOut( p_aout, WAVE_FORMAT_IEEE_FLOAT, i_nb_channels,
128 p_aout->output.output.i_rate ) )
130 msg_Err( p_aout, "Audio device doesn't allow WAVE_FORMAT_IEEE_FLOAT" );
132 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
133 if ( OpenWaveOut( p_aout, WAVE_FORMAT_PCM, i_nb_channels,
134 p_aout->output.output.i_rate ) )
136 msg_Err( p_aout, "cannot open waveout audio device" );
141 waveOutReset( p_aout->output.p_sys->h_waveout );
143 /* Calculate the frame size in bytes */
144 p_aout->output.i_nb_samples = FRAME_SIZE;
145 aout_FormatPrepare( &p_aout->output.output );
146 p_aout->output.p_sys->i_buffer_size = FRAME_SIZE *
147 p_aout->output.output.i_bytes_per_frame;
148 /* Allocate silence buffer */
149 p_aout->output.p_sys->p_silence_buffer =
150 calloc( p_aout->output.p_sys->i_buffer_size, 1 );
151 if( p_aout->output.p_sys->p_silence_buffer == NULL )
153 msg_Err( p_aout, "out of memory" );
157 /* We need to kick off the playback in order to have the callback properly
159 for( i = 0; i < FRAMES_NUM; i++ )
161 PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
162 &p_aout->output.p_sys->waveheader[i], NULL );
167 /*****************************************************************************
168 * Play: play a sound buffer
169 *****************************************************************************
170 * This doesn't actually play the buffer. This just stores the buffer so it
171 * can be played by the callback thread.
172 *****************************************************************************/
173 static void Play( aout_instance_t *p_aout )
177 /*****************************************************************************
178 * Close: close the audio device
179 *****************************************************************************/
180 static void Close( vlc_object_t *p_this )
182 aout_instance_t *p_aout = (aout_instance_t *)p_this;
184 /* Before calling waveOutClose we must reset the device */
185 p_aout->b_die = VLC_TRUE;
186 //Hmmm, waveOutReset never seems to return... why ???
187 //waveOutReset( p_aout->output.p_sys->h_waveout );
189 /* Wait for the waveout buffers to be freed */
190 while( !(p_aout->output.p_sys->waveheader[0].dwFlags & WHDR_DONE) ||
191 !(p_aout->output.p_sys->waveheader[1].dwFlags & WHDR_DONE) )
196 /* Close the device */
197 if( waveOutClose( p_aout->output.p_sys->h_waveout ) != MMSYSERR_NOERROR )
199 msg_Err( p_aout, "waveOutClose failed" );
202 /* Free silence buffer */
203 free( p_aout->output.p_sys->p_silence_buffer );
205 free( p_aout->output.p_sys );
208 /*****************************************************************************
209 * OpenWaveOut: open the waveout sound device
210 ****************************************************************************/
211 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
212 int i_channels, int i_rate )
216 /* Set sound format */
217 p_aout->output.p_sys->waveformat.wFormatTag = i_format;
218 p_aout->output.p_sys->waveformat.nChannels = i_channels;
219 p_aout->output.p_sys->waveformat.nSamplesPerSec = i_rate;
223 case WAVE_FORMAT_PCM:
224 p_aout->output.p_sys->waveformat.wBitsPerSample = 16;
226 case WAVE_FORMAT_IEEE_FLOAT:
227 p_aout->output.p_sys->waveformat.wBitsPerSample = sizeof(float) * 8;
231 p_aout->output.p_sys->waveformat.nBlockAlign =
232 p_aout->output.p_sys->waveformat.wBitsPerSample / 8 * i_channels;
233 p_aout->output.p_sys->waveformat.nAvgBytesPerSec =
234 p_aout->output.p_sys->waveformat.nSamplesPerSec *
235 p_aout->output.p_sys->waveformat.nBlockAlign;
237 /* Open the device */
238 result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
239 &p_aout->output.p_sys->waveformat,
240 (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
242 if( result == WAVERR_BADFORMAT )
244 msg_Err( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
247 if( result != MMSYSERR_NOERROR )
249 msg_Err( p_aout, "waveOutOpen failed" );
256 /*****************************************************************************
257 * PlayWaveOut: play a buffer through the WaveOut device
258 *****************************************************************************/
259 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
260 WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
264 /* Prepare the buffer */
265 if( p_buffer != NULL )
266 p_waveheader->lpData = p_buffer->p_buffer;
268 /* Use silence buffer instead */
269 p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
271 p_waveheader->dwUser = (DWORD_PTR)p_buffer;
272 p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
273 p_waveheader->dwFlags = 0;
275 result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
276 if( result != MMSYSERR_NOERROR )
278 msg_Err( p_aout, "waveOutPrepareHeader failed" );
282 /* Send the buffer to the waveOut queue */
283 result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
284 if( result != MMSYSERR_NOERROR )
286 msg_Err( p_aout, "waveOutWrite failed" );
293 /*****************************************************************************
294 * WaveOutCallback: what to do once WaveOut has played its sound samples
295 *****************************************************************************/
296 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
298 DWORD dwParam1, DWORD dwParam2 )
300 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
301 WAVEHDR *p_waveheader = (WAVEHDR *)dwParam1;
302 aout_buffer_t *p_buffer;
304 if( uMsg != WOM_DONE ) return;
306 /* Unprepare and free the buffer which has just been played */
307 waveOutUnprepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
308 if( p_waveheader->dwUser )
309 aout_BufferFree( (aout_buffer_t *)p_waveheader->dwUser );
311 if( p_aout->b_die ) return;
313 /* Take into account WaveOut latency ( 1 FRAME ) */
314 p_buffer = aout_OutputNextBuffer( p_aout,
315 mdate() + 1000000 / p_aout->output.output.i_rate * FRAME_SIZE,
318 PlayWaveOut( p_aout, h_waveout, p_waveheader, p_buffer );