1 /*****************************************************************************
2 * waveout.c : Windows waveOut plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: waveout.c,v 1.9 2002/10/20 12:23:47 massiot 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 2048 /* The size is in samples, not in bytes */
42 /*****************************************************************************
44 *****************************************************************************/
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
47 static void Play ( aout_instance_t * );
50 static int OpenWaveOut ( aout_instance_t *p_aout, int i_format,
51 int i_channels, int i_rate );
52 static int PlayWaveOut ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
54 static void CALLBACK WaveOutCallback ( HWAVEOUT h_waveout, UINT uMsg,
56 DWORD dwParam1, DWORD dwParam2 );
58 /*****************************************************************************
60 *****************************************************************************/
62 set_description( _("Win32 waveOut extension module") );
63 set_capability( "audio output", 50 );
64 set_callbacks( Open, Close );
67 /*****************************************************************************
68 * aout_sys_t: waveOut audio output method descriptor
69 *****************************************************************************
70 * This structure is part of the audio output thread descriptor.
71 * It describes the waveOut specific properties of an audio device.
72 *****************************************************************************/
75 HWAVEOUT h_waveout; /* handle to waveout instance */
77 WAVEFORMATEX waveformat; /* audio format */
79 WAVEHDR waveheader[2];
83 byte_t *p_silence_buffer; /* buffer we use to play silence */
86 /*****************************************************************************
87 * Open: open the audio device
88 *****************************************************************************
89 * This function opens and setups Win32 waveOut
90 *****************************************************************************/
91 static int Open( vlc_object_t *p_this )
93 aout_instance_t *p_aout = (aout_instance_t *)p_this;
94 aout_buffer_t *p_buffer;
97 /* Allocate structure */
98 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
100 if( p_aout->output.p_sys == NULL )
102 msg_Err( p_aout, "out of memory" );
106 p_aout->output.pf_play = Play;
107 aout_VolumeSoftInit( p_aout );
109 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
110 if ( i_nb_channels > 2 )
112 /* Waveout doesn't support more than two channels. */
114 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
117 /* We need to open the device with default values to be sure it is
119 if ( OpenWaveOut( p_aout, WAVE_FORMAT_PCM, i_nb_channels,
120 p_aout->output.output.i_rate ) )
122 msg_Err( p_aout, "cannot open waveout audio device with output "
124 p_aout->output.output.i_rate );
127 if ( OpenWaveOut( p_aout, WAVE_FORMAT_PCM, i_nb_channels,
130 msg_Err( p_aout, "cannot open waveout audio device with output "
134 p_aout->output.output.i_rate = 44100;
137 waveOutReset( p_aout->output.p_sys->h_waveout );
139 /* Calculate the frame size in bytes */
140 p_aout->output.p_sys->i_buffer_size = FRAME_SIZE * sizeof(s16)
141 * p_aout->output.p_sys->waveformat.nChannels;
142 /* Allocate silence buffer */
143 p_aout->output.p_sys->p_silence_buffer =
144 calloc( p_aout->output.p_sys->i_buffer_size, 1 );
145 if( p_aout->output.p_sys->p_silence_buffer == NULL )
147 msg_Err( p_aout, "out of memory" );
151 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
152 p_aout->output.i_nb_samples = FRAME_SIZE;
154 /* We need to kick off the playback in order to have the callback properly
156 PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
157 &p_aout->output.p_sys->waveheader[0], NULL );
159 p_buffer = aout_OutputNextBuffer( p_aout,
160 mdate() + 1000000 / p_aout->output.output.i_rate * FRAME_SIZE,
162 PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
163 &p_aout->output.p_sys->waveheader[1], p_buffer );
168 /*****************************************************************************
169 * Play: play a sound buffer
170 *****************************************************************************
171 * This doesn't actually play the buffer. This just stores the buffer so it
172 * can be played by the callback thread.
173 *****************************************************************************/
174 static void Play( aout_instance_t *p_aout )
178 /*****************************************************************************
179 * Close: close the audio device
180 *****************************************************************************/
181 static void Close( vlc_object_t *p_this )
183 aout_instance_t *p_aout = (aout_instance_t *)p_this;
185 /* Before calling waveOutClose we must reset the device */
186 waveOutReset( p_aout->output.p_sys->h_waveout );
188 /* Close the device */
189 if( waveOutClose( p_aout->output.p_sys->h_waveout ) != MMSYSERR_NOERROR )
191 msg_Err( p_aout, "waveOutClose failed" );
194 /* Free silence buffer */
195 free( p_aout->output.p_sys->p_silence_buffer );
197 free( p_aout->output.p_sys );
200 /*****************************************************************************
201 * OpenWaveOut: open the waveout sound device
202 ****************************************************************************/
203 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
204 int i_channels, int i_rate )
208 /* Set sound format */
209 p_aout->output.p_sys->waveformat.wFormatTag = i_format;
210 p_aout->output.p_sys->waveformat.nChannels = i_channels;
211 p_aout->output.p_sys->waveformat.nSamplesPerSec = i_rate;
212 p_aout->output.p_sys->waveformat.wBitsPerSample = 16;
213 p_aout->output.p_sys->waveformat.nBlockAlign =
214 p_aout->output.p_sys->waveformat.wBitsPerSample / 8 * i_channels;
215 p_aout->output.p_sys->waveformat.nAvgBytesPerSec =
216 p_aout->output.p_sys->waveformat.nSamplesPerSec *
217 p_aout->output.p_sys->waveformat.nBlockAlign;
219 /* Open the device */
220 result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
221 &p_aout->output.p_sys->waveformat,
222 (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
224 if( result == WAVERR_BADFORMAT )
226 msg_Err( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
229 if( result != MMSYSERR_NOERROR )
231 msg_Err( p_aout, "waveOutOpen failed" );
238 /*****************************************************************************
239 * PlayWaveOut: play a buffer through the WaveOut device
240 *****************************************************************************/
241 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
242 WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
246 /* Prepare the buffer */
247 if( p_buffer != NULL )
248 p_waveheader->lpData = p_buffer->p_buffer;
250 /* Use silence buffer instead */
251 p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
253 p_waveheader->dwUser = (DWORD_PTR)p_buffer;
254 p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
255 p_waveheader->dwFlags = 0;
257 result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
258 if( result != MMSYSERR_NOERROR )
260 msg_Err( p_aout, "waveOutPrepareHeader failed" );
264 /* Send the buffer to the waveOut queue */
265 result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
266 if( result != MMSYSERR_NOERROR )
268 msg_Err( p_aout, "waveOutWrite failed" );
275 /*****************************************************************************
276 * WaveOutCallback: what to do once WaveOut has played its sound samples
277 *****************************************************************************/
278 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
280 DWORD dwParam1, DWORD dwParam2 )
282 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
283 WAVEHDR *p_waveheader = (WAVEHDR *)dwParam1;
284 aout_buffer_t *p_buffer;
286 if( uMsg != WOM_DONE ) return;
288 /* Unprepare and free the buffer which has just been played */
289 waveOutUnprepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
290 if( p_waveheader->dwUser )
291 aout_BufferFree( (aout_buffer_t *)p_waveheader->dwUser );
293 /* Take into account WaveOut latency instead of just mdate() */
294 p_buffer = aout_OutputNextBuffer( p_aout,
295 mdate() + 1000000 / p_aout->output.output.i_rate * FRAME_SIZE,
298 PlayWaveOut( p_aout, h_waveout, p_waveheader, p_buffer );