]> git.sesse.net Git - vlc/blob - modules/audio_output/waveout.c
* configure.ac.in, modules/audio_output/directx.c, modules/audio_output/waveout.c:
[vlc] / modules / audio_output / waveout.c
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 $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *      
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>                                                 /* ENOMEM */
28 #include <fcntl.h>                                       /* open(), O_WRONLY */
29 #include <string.h>                                            /* strerror() */
30
31 #include <stdlib.h>                            /* calloc(), malloc(), free() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/aout.h>
35 #include "aout_internal.h"
36
37 #include <windows.h>
38 #include <mmsystem.h>
39
40 #define FRAME_SIZE 1024              /* The size is in samples, not in bytes */
41 #define FRAMES_NUM 4
42
43 /*****************************************************************************
44  * Useful macros
45  *****************************************************************************/
46 #ifndef WAVE_FORMAT_IEEE_FLOAT
47 #   define WAVE_FORMAT_IEEE_FLOAT 0x0003
48 #endif
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static int  Open         ( vlc_object_t * );             
54 static void Close        ( vlc_object_t * );                   
55 static void Play         ( aout_instance_t * );
56
57 /* local functions */
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 *,
61                            aout_buffer_t * );
62 static void CALLBACK WaveOutCallback ( HWAVEOUT h_waveout, UINT uMsg,
63                                        DWORD _p_aout,
64                                        DWORD dwParam1, DWORD dwParam2 );
65
66 /*****************************************************************************
67  * Module descriptor
68  *****************************************************************************/
69 vlc_module_begin();
70     set_description( _("Win32 waveOut extension module") ); 
71     set_capability( "audio output", 50 );
72     set_callbacks( Open, Close );
73 vlc_module_end();
74
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  *****************************************************************************/
81 struct aout_sys_t
82 {
83     HWAVEOUT h_waveout;                        /* handle to waveout instance */
84
85     WAVEFORMATEX waveformat;                                 /* audio format */
86
87     WAVEHDR waveheader[FRAMES_NUM];
88
89     int i_buffer_size;
90
91     byte_t *p_silence_buffer;               /* buffer we use to play silence */
92 };
93
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 )
100 {   
101     aout_instance_t *p_aout = (aout_instance_t *)p_this;
102     int i_nb_channels, i;
103
104     /* Allocate structure */
105     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
106
107     if( p_aout->output.p_sys == NULL )
108     {
109         msg_Err( p_aout, "out of memory" );
110         return VLC_EGENERIC;
111     }
112
113     p_aout->output.pf_play = Play;
114     aout_VolumeSoftInit( p_aout );
115
116     i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
117     if ( i_nb_channels >= 2 )
118     {
119         i_nb_channels = 2;
120         p_aout->output.output.i_physical_channels =
121             AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
122     }
123     else
124     {
125         i_nb_channels = 1;
126         p_aout->output.output.i_physical_channels =
127             AOUT_CHAN_CENTER;
128     }
129
130     /* We need to open the device with default values to be sure it is
131      * available */
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 ) )
135     {
136         msg_Err( p_aout, "Audio device doesn't allow WAVE_FORMAT_IEEE_FLOAT" );
137
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 ) )
141         {
142             msg_Err( p_aout, "cannot open waveout audio device" );
143             return VLC_EGENERIC;
144         }
145     }
146
147     waveOutReset( p_aout->output.p_sys->h_waveout );
148
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 )
158     {
159         msg_Err( p_aout, "out of memory" );
160         return 1;
161     }
162
163     /* We need to kick off the playback in order to have the callback properly
164      * working */
165     for( i = 0; i < FRAMES_NUM; i++ )
166     {
167         PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
168                      &p_aout->output.p_sys->waveheader[i], NULL );
169     }
170     return 0;
171 }
172
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 )
180 {
181 }
182
183 /*****************************************************************************
184  * Close: close the audio device
185  *****************************************************************************/
186 static void Close( vlc_object_t *p_this )
187 {       
188     aout_instance_t *p_aout = (aout_instance_t *)p_this;
189
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 );
194
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) )
198     {
199         msleep( 1000 );
200     }
201
202     /* Close the device */
203     if( waveOutClose( p_aout->output.p_sys->h_waveout ) != MMSYSERR_NOERROR )
204     {
205         msg_Err( p_aout, "waveOutClose failed" );
206     }
207
208     /* Free silence buffer */
209     free( p_aout->output.p_sys->p_silence_buffer );
210
211     free( p_aout->output.p_sys );
212 }
213
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 )
219 {
220     MMRESULT result;
221
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;
226
227     switch( i_format )
228     {
229     case WAVE_FORMAT_PCM:
230         p_aout->output.p_sys->waveformat.wBitsPerSample = 16;
231         break;
232     case WAVE_FORMAT_IEEE_FLOAT:
233         p_aout->output.p_sys->waveformat.wBitsPerSample = sizeof(float) * 8;
234         break;
235     }
236
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;
242
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,
247                           CALLBACK_FUNCTION );
248     if( result == WAVERR_BADFORMAT )
249     {
250         msg_Err( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
251         return( 1 );
252     }
253     if( result != MMSYSERR_NOERROR )
254     {
255         msg_Err( p_aout, "waveOutOpen failed" );
256         return 1;
257     }
258
259     return 0;
260 }
261
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 )
267 {
268     MMRESULT result;
269
270     /* Prepare the buffer */
271     if( p_buffer != NULL )
272         p_waveheader->lpData = p_buffer->p_buffer;
273     else
274         /* Use silence buffer instead */
275         p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
276
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;
280
281     result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
282     if( result != MMSYSERR_NOERROR )
283     {
284         msg_Err( p_aout, "waveOutPrepareHeader failed" );
285         return 1;
286     }
287
288     /* Send the buffer to the waveOut queue */
289     result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
290     if( result != MMSYSERR_NOERROR )
291     {
292         msg_Err( p_aout, "waveOutWrite failed" );
293         return 1;
294     }
295
296     return 0;
297 }
298
299 /*****************************************************************************
300  * WaveOutCallback: what to do once WaveOut has played its sound samples
301  *****************************************************************************/
302 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
303                                       DWORD _p_aout,
304                                       DWORD dwParam1, DWORD dwParam2 )
305 {
306     aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
307     WAVEHDR *p_waveheader = (WAVEHDR *)dwParam1;
308     aout_buffer_t *p_buffer;
309
310     if( uMsg != WOM_DONE ) return;
311
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 );
316
317     if( p_aout->b_die ) return;
318
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,
322         VLC_FALSE );
323
324     PlayWaveOut( p_aout, h_waveout, p_waveheader, p_buffer );
325 }