1 /*****************************************************************************
2 * waveout.c : Windows waveOut plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: waveout.c,v 1.7 2002/09/18 21:21:23 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"
39 #define FRAME_SIZE 2048 /* The size is in samples, not in bytes */
41 /*****************************************************************************
43 *****************************************************************************/
44 static int Open ( vlc_object_t * );
45 static void Close ( vlc_object_t * );
46 static void Play ( aout_instance_t * );
49 static int OpenWaveOut ( aout_instance_t *p_aout, int i_format,
50 int i_channels, int i_rate );
51 static int PlayWaveOut ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
53 static void CALLBACK WaveOutCallback ( HWAVEOUT h_waveout, UINT uMsg,
55 DWORD dwParam1, DWORD dwParam2 );
57 /*****************************************************************************
59 *****************************************************************************/
61 set_description( _("Win32 waveOut extension module") );
62 set_capability( "audio output", 50 );
63 set_callbacks( Open, Close );
66 /*****************************************************************************
67 * aout_sys_t: waveOut audio output method descriptor
68 *****************************************************************************
69 * This structure is part of the audio output thread descriptor.
70 * It describes the waveOut specific properties of an audio device.
71 *****************************************************************************/
74 HWAVEOUT h_waveout; /* handle to waveout instance */
76 WAVEFORMATEX waveformat; /* audio format */
78 WAVEHDR waveheader[2];
82 byte_t *p_silence_buffer; /* buffer we use to play silence */
85 /*****************************************************************************
86 * Open: open the audio device
87 *****************************************************************************
88 * This function opens and setups Win32 waveOut
89 *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
92 aout_instance_t *p_aout = (aout_instance_t *)p_this;
94 /* Allocate structure */
95 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
97 if( p_aout->output.p_sys == NULL )
99 msg_Err( p_aout, "out of memory" );
103 p_aout->output.pf_play = Play;
104 aout_VolumeSoftInit( p_aout );
106 /* calculate the frame size in bytes */
107 p_aout->output.p_sys->i_buffer_size = FRAME_SIZE * sizeof(s16)
108 * p_aout->output.p_sys->waveformat.nChannels;
109 /* Allocate silence buffer */
110 p_aout->output.p_sys->p_silence_buffer =
111 calloc( p_aout->output.p_sys->i_buffer_size, 1 );
112 if( p_aout->output.p_sys->p_silence_buffer == NULL )
114 msg_Err( p_aout, "out of memory" );
118 /* We need to open the device with default values to be sure it is
120 if ( OpenWaveOut( p_aout, WAVE_FORMAT_PCM, 2, 44100 ) )
122 msg_Err( p_aout, "cannot open waveout" );
126 waveOutReset( p_aout->output.p_sys->h_waveout );
128 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
129 p_aout->output.i_nb_samples = FRAME_SIZE;
131 /* We need to kick off the playback in order to have the callback properly
133 PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
134 &p_aout->output.p_sys->waveheader[0], NULL );
135 PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
136 &p_aout->output.p_sys->waveheader[1], NULL );
141 /*****************************************************************************
142 * Play: play a sound buffer
143 *****************************************************************************
144 * This doesn't actually play the buffer. This just stores the buffer so it
145 * can be played by the callback thread.
146 *****************************************************************************/
147 static void Play( aout_instance_t *p_aout )
151 /*****************************************************************************
152 * Close: close the audio device
153 *****************************************************************************/
154 static void Close( vlc_object_t *p_this )
156 aout_instance_t *p_aout = (aout_instance_t *)p_this;
158 /* Before calling waveOutClose we must reset the device */
159 waveOutReset( p_aout->output.p_sys->h_waveout );
161 /* Close the device */
162 if( waveOutClose( p_aout->output.p_sys->h_waveout ) != MMSYSERR_NOERROR )
164 msg_Err( p_aout, "waveOutClose failed" );
167 /* Free silence buffer */
168 free( p_aout->output.p_sys->p_silence_buffer );
170 if( p_aout->output.p_sys != NULL )
172 free( p_aout->output.p_sys );
173 p_aout->output.p_sys = NULL;
177 /*****************************************************************************
178 * OpenWaveOut: open the waveout sound device
179 ****************************************************************************/
180 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
181 int i_channels, int i_rate )
185 /* Set sound format */
186 p_aout->output.p_sys->waveformat.wFormatTag = i_format;
187 p_aout->output.p_sys->waveformat.nChannels = i_channels;
188 p_aout->output.p_sys->waveformat.nSamplesPerSec = i_rate;
189 p_aout->output.p_sys->waveformat.wBitsPerSample = 16;
190 p_aout->output.p_sys->waveformat.nBlockAlign =
191 p_aout->output.p_sys->waveformat.wBitsPerSample / 8 * i_channels;
192 p_aout->output.p_sys->waveformat.nAvgBytesPerSec =
193 p_aout->output.p_sys->waveformat.nSamplesPerSec *
194 p_aout->output.p_sys->waveformat.nBlockAlign;
196 /* Open the device */
197 result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
198 &p_aout->output.p_sys->waveformat,
199 (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
201 if( result == WAVERR_BADFORMAT )
203 msg_Err( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
206 if( result != MMSYSERR_NOERROR )
208 msg_Err( p_aout, "waveOutOpen failed" );
215 /*****************************************************************************
216 * PlayWaveOut: play a buffer through the WaveOut device
217 *****************************************************************************/
218 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
219 WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
223 /* Prepare the buffer */
224 if( p_buffer != NULL )
225 p_waveheader->lpData = p_buffer->p_buffer;
227 /* Use silence buffer instead */
228 p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
230 p_waveheader->dwUser = (DWORD_PTR)p_buffer;
231 p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
232 p_waveheader->dwFlags = 0;
234 result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
235 if( result != MMSYSERR_NOERROR )
237 msg_Err( p_aout, "waveOutPrepareHeader failed" );
241 /* Send the buffer to the waveOut queue */
242 result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
243 if( result != MMSYSERR_NOERROR )
245 msg_Err( p_aout, "waveOutWrite failed" );
252 /*****************************************************************************
253 * WaveOutCallback: what to do once WaveOut has played its sound samples
254 *****************************************************************************/
255 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
257 DWORD dwParam1, DWORD dwParam2 )
259 aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
260 WAVEHDR *p_waveheader = (WAVEHDR *)dwParam1;
261 aout_buffer_t * p_buffer;
263 if( uMsg != WOM_DONE ) return;
265 /* Unprepare and free the buffer which has just been played */
266 waveOutUnprepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
267 if( p_waveheader->dwUser )
268 aout_BufferFree( (aout_buffer_t *)p_waveheader->dwUser );
270 /* FIXME : take into account WaveOut latency instead of mdate() */
271 p_buffer = aout_OutputNextBuffer( p_aout, mdate(), VLC_FALSE );
273 PlayWaveOut( p_aout, h_waveout, p_waveheader, p_buffer );