1 /*****************************************************************************
2 * waveout.c : Windows waveOut plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: waveout.c,v 1.8 2002/10/11 10:08:06 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 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;
96 /* Allocate structure */
97 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
99 if( p_aout->output.p_sys == NULL )
101 msg_Err( p_aout, "out of memory" );
105 p_aout->output.pf_play = Play;
106 aout_VolumeSoftInit( p_aout );
109 if ( p_aout->output.output.i_channels > 2 )
111 msg_Warn( p_aout, "only two channels are supported at the moment" );
112 /* Trigger downmixing */
113 p_aout->output.output.i_channels = 2;
116 /* We need to open the device with default values to be sure it is
118 if ( OpenWaveOut( p_aout, WAVE_FORMAT_PCM,
119 p_aout->output.output.i_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,
128 p_aout->output.output.i_channels,
131 msg_Err( p_aout, "cannot open waveout audio device with output "
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 if( p_aout->output.p_sys != NULL )
199 free( p_aout->output.p_sys );
200 p_aout->output.p_sys = NULL;
204 /*****************************************************************************
205 * OpenWaveOut: open the waveout sound device
206 ****************************************************************************/
207 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
208 int i_channels, int i_rate )
212 /* Set sound format */
213 p_aout->output.p_sys->waveformat.wFormatTag = i_format;
214 p_aout->output.p_sys->waveformat.nChannels = i_channels;
215 p_aout->output.p_sys->waveformat.nSamplesPerSec = i_rate;
216 p_aout->output.p_sys->waveformat.wBitsPerSample = 16;
217 p_aout->output.p_sys->waveformat.nBlockAlign =
218 p_aout->output.p_sys->waveformat.wBitsPerSample / 8 * i_channels;
219 p_aout->output.p_sys->waveformat.nAvgBytesPerSec =
220 p_aout->output.p_sys->waveformat.nSamplesPerSec *
221 p_aout->output.p_sys->waveformat.nBlockAlign;
223 /* Open the device */
224 result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
225 &p_aout->output.p_sys->waveformat,
226 (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
228 if( result == WAVERR_BADFORMAT )
230 msg_Err( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
233 if( result != MMSYSERR_NOERROR )
235 msg_Err( p_aout, "waveOutOpen failed" );
242 /*****************************************************************************
243 * PlayWaveOut: play a buffer through the WaveOut device
244 *****************************************************************************/
245 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
246 WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
250 /* Prepare the buffer */
251 if( p_buffer != NULL )
252 p_waveheader->lpData = p_buffer->p_buffer;
254 /* Use silence buffer instead */
255 p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
257 p_waveheader->dwUser = (DWORD_PTR)p_buffer;
258 p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
259 p_waveheader->dwFlags = 0;
261 result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
262 if( result != MMSYSERR_NOERROR )
264 msg_Err( p_aout, "waveOutPrepareHeader failed" );
268 /* Send the buffer to the waveOut queue */
269 result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
270 if( result != MMSYSERR_NOERROR )
272 msg_Err( p_aout, "waveOutWrite failed" );
279 /*****************************************************************************
280 * WaveOutCallback: what to do once WaveOut has played its sound samples
281 *****************************************************************************/
282 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
284 DWORD dwParam1, DWORD dwParam2 )
286 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
287 WAVEHDR *p_waveheader = (WAVEHDR *)dwParam1;
288 aout_buffer_t *p_buffer;
290 if( uMsg != WOM_DONE ) return;
292 /* Unprepare and free the buffer which has just been played */
293 waveOutUnprepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
294 if( p_waveheader->dwUser )
295 aout_BufferFree( (aout_buffer_t *)p_waveheader->dwUser );
297 /* Take into account WaveOut latency instead of just mdate() */
298 p_buffer = aout_OutputNextBuffer( p_aout,
299 mdate() + 1000000 / p_aout->output.output.i_rate * FRAME_SIZE,
302 PlayWaveOut( p_aout, h_waveout, p_waveheader, p_buffer );