1 /*****************************************************************************
2 * waveout.c : Windows waveOut plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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 <string.h> /* strerror() */
28 #include <stdlib.h> /* calloc(), malloc(), free() */
29 #include <math.h> /* roundf() */
33 #include "aout_internal.h"
38 #define FRAME_SIZE 4096 /* The size is in samples, not in bytes */
41 /*****************************************************************************
43 *****************************************************************************/
45 # define DWORD_PTR DWORD
48 #ifndef WAVE_FORMAT_IEEE_FLOAT
49 # define WAVE_FORMAT_IEEE_FLOAT 0x0003
52 #ifndef WAVE_FORMAT_DOLBY_AC3_SPDIF
53 # define WAVE_FORMAT_DOLBY_AC3_SPDIF 0x0092
56 #ifndef WAVE_FORMAT_EXTENSIBLE
57 #define WAVE_FORMAT_EXTENSIBLE 0xFFFE
60 #ifndef SPEAKER_FRONT_LEFT
61 # define SPEAKER_FRONT_LEFT 0x1
62 # define SPEAKER_FRONT_RIGHT 0x2
63 # define SPEAKER_FRONT_CENTER 0x4
64 # define SPEAKER_LOW_FREQUENCY 0x8
65 # define SPEAKER_BACK_LEFT 0x10
66 # define SPEAKER_BACK_RIGHT 0x20
67 # define SPEAKER_FRONT_LEFT_OF_CENTER 0x40
68 # define SPEAKER_FRONT_RIGHT_OF_CENTER 0x80
69 # define SPEAKER_BACK_CENTER 0x100
70 # define SPEAKER_SIDE_LEFT 0x200
71 # define SPEAKER_SIDE_RIGHT 0x400
72 # define SPEAKER_TOP_CENTER 0x800
73 # define SPEAKER_TOP_FRONT_LEFT 0x1000
74 # define SPEAKER_TOP_FRONT_CENTER 0x2000
75 # define SPEAKER_TOP_FRONT_RIGHT 0x4000
76 # define SPEAKER_TOP_BACK_LEFT 0x8000
77 # define SPEAKER_TOP_BACK_CENTER 0x10000
78 # define SPEAKER_TOP_BACK_RIGHT 0x20000
79 # define SPEAKER_RESERVED 0x80000000
82 #ifndef _WAVEFORMATEXTENSIBLE_
86 WORD wValidBitsPerSample; /* bits of precision */
87 WORD wSamplesPerBlock; /* valid if wBitsPerSample==0 */
88 WORD wReserved; /* If neither applies, set to zero. */
90 DWORD dwChannelMask; /* which channels are */
91 /* present in stream */
93 } WAVEFORMATEXTENSIBLE, *PWAVEFORMATEXTENSIBLE;
96 static const GUID __KSDATAFORMAT_SUBTYPE_IEEE_FLOAT = {WAVE_FORMAT_IEEE_FLOAT, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
97 static const GUID __KSDATAFORMAT_SUBTYPE_PCM = {WAVE_FORMAT_PCM, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
98 static const GUID __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF = {WAVE_FORMAT_DOLBY_AC3_SPDIF, 0x0000, 0x0010, {0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71}};
100 /*****************************************************************************
102 *****************************************************************************/
103 static int Open ( vlc_object_t * );
104 static void Close ( vlc_object_t * );
105 static void Play ( aout_instance_t * );
107 /*****************************************************************************
108 * notification_thread_t: waveOut event thread
109 *****************************************************************************/
110 typedef struct notification_thread_t
113 aout_instance_t *p_aout;
115 } notification_thread_t;
117 /* local functions */
118 static void Probe ( aout_instance_t * );
119 static int OpenWaveOut ( aout_instance_t *, int, int, int, int, vlc_bool_t );
120 static int OpenWaveOutPCM( aout_instance_t *, int*, int, int, int, vlc_bool_t );
121 static int PlayWaveOut ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
124 static void CALLBACK WaveOutCallback ( HWAVEOUT, UINT, DWORD, DWORD, DWORD );
125 static void WaveOutThread( notification_thread_t * );
127 static int VolumeInfos( aout_instance_t *, audio_volume_t * );
128 static int VolumeGet( aout_instance_t *, audio_volume_t * );
129 static int VolumeSet( aout_instance_t *, audio_volume_t );
131 /*****************************************************************************
133 *****************************************************************************/
134 #define FLOAT_TEXT N_("Use float32 output")
135 #define FLOAT_LONGTEXT N_( \
136 "The option allows you to enable or disable the high-quality float32 " \
137 "audio output mode (which is not well supported by some soundcards)." )
140 set_shortname( "WaveOut" );
141 set_description( _("Win32 waveOut extension output") );
142 set_capability( "audio output", 50 );
143 set_category( CAT_AUDIO );
144 set_subcategory( SUBCAT_AUDIO_AOUT );
145 add_bool( "waveout-float32", 1, 0, FLOAT_TEXT, FLOAT_LONGTEXT, VLC_TRUE );
146 set_callbacks( Open, Close );
149 /*****************************************************************************
150 * aout_sys_t: waveOut audio output method descriptor
151 *****************************************************************************
152 * This structure is part of the audio output thread descriptor.
153 * It describes the waveOut specific properties of an audio device.
154 *****************************************************************************/
157 HWAVEOUT h_waveout; /* handle to waveout instance */
159 WAVEFORMATEXTENSIBLE waveformat; /* audio format */
161 WAVEHDR waveheader[FRAMES_NUM];
163 notification_thread_t *p_notif; /* WaveOutThread id */
168 byte_t *p_silence_buffer; /* buffer we use to play silence */
170 vlc_bool_t b_chan_reorder; /* do we need channel reordering */
171 int pi_chan_table[AOUT_CHAN_MAX];
174 static const uint32_t pi_channels_src[] =
175 { AOUT_CHAN_LEFT, AOUT_CHAN_RIGHT,
176 AOUT_CHAN_MIDDLELEFT, AOUT_CHAN_MIDDLERIGHT,
177 AOUT_CHAN_REARLEFT, AOUT_CHAN_REARRIGHT,
178 AOUT_CHAN_CENTER, AOUT_CHAN_LFE, 0 };
179 static const uint32_t pi_channels_in[] =
180 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
181 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT,
182 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
183 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY, 0 };
184 static const uint32_t pi_channels_out[] =
185 { SPEAKER_FRONT_LEFT, SPEAKER_FRONT_RIGHT,
186 SPEAKER_FRONT_CENTER, SPEAKER_LOW_FREQUENCY,
187 SPEAKER_BACK_LEFT, SPEAKER_BACK_RIGHT,
188 SPEAKER_SIDE_LEFT, SPEAKER_SIDE_RIGHT, 0 };
190 /*****************************************************************************
191 * Open: open the audio device
192 *****************************************************************************
193 * This function opens and setups Win32 waveOut
194 *****************************************************************************/
195 static int Open( vlc_object_t *p_this )
197 aout_instance_t *p_aout = (aout_instance_t *)p_this;
201 /* Allocate structure */
202 p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
204 if( p_aout->output.p_sys == NULL )
206 msg_Err( p_aout, "out of memory" );
210 p_aout->output.pf_play = Play;
211 p_aout->b_die = VLC_FALSE;
213 if( var_Type( p_aout, "audio-device" ) == 0 )
218 if( var_Get( p_aout, "audio-device", &val ) < 0 )
220 /* Probe() has failed. */
221 free( p_aout->output.p_sys );
225 var_Create( p_aout, "waveout-float32", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
227 /* Open the device */
228 if( val.i_int == AOUT_VAR_SPDIF )
230 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
232 if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
233 p_aout->output.output.i_physical_channels,
234 aout_FormatNbChannels( &p_aout->output.output ),
235 p_aout->output.output.i_rate, VLC_FALSE )
238 msg_Err( p_aout, "cannot open waveout audio device" );
239 free( p_aout->output.p_sys );
243 /* Calculate the frame size in bytes */
244 p_aout->output.i_nb_samples = A52_FRAME_NB;
245 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
246 p_aout->output.output.i_frame_length = A52_FRAME_NB;
247 p_aout->output.p_sys->i_buffer_size =
248 p_aout->output.output.i_bytes_per_frame;
250 aout_VolumeNoneInit( p_aout );
254 if( val.i_int == AOUT_VAR_5_1 )
256 p_aout->output.output.i_physical_channels
257 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
258 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
261 else if( val.i_int == AOUT_VAR_2F2R )
263 p_aout->output.output.i_physical_channels
264 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
265 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
267 else if( val.i_int == AOUT_VAR_MONO )
269 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
273 p_aout->output.output.i_physical_channels
274 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
277 if( OpenWaveOutPCM( p_aout, &p_aout->output.output.i_format,
278 p_aout->output.output.i_physical_channels,
279 aout_FormatNbChannels( &p_aout->output.output ),
280 p_aout->output.output.i_rate, VLC_FALSE )
283 msg_Err( p_aout, "cannot open waveout audio device" );
284 free( p_aout->output.p_sys );
288 /* Calculate the frame size in bytes */
289 p_aout->output.i_nb_samples = FRAME_SIZE;
290 aout_FormatPrepare( &p_aout->output.output );
291 p_aout->output.p_sys->i_buffer_size = FRAME_SIZE *
292 p_aout->output.output.i_bytes_per_frame;
294 aout_VolumeSoftInit( p_aout );
296 p_aout->output.pf_volume_infos = VolumeInfos;
297 p_aout->output.pf_volume_get = VolumeGet;
298 p_aout->output.pf_volume_set = VolumeSet;
302 waveOutReset( p_aout->output.p_sys->h_waveout );
304 /* Allocate silence buffer */
305 p_aout->output.p_sys->p_silence_buffer =
306 malloc( p_aout->output.p_sys->i_buffer_size );
307 if( p_aout->output.p_sys->p_silence_buffer == NULL )
309 free( p_aout->output.p_sys );
310 msg_Err( p_aout, "out of memory" );
314 /* Zero the buffer. WinCE doesn't have calloc(). */
315 memset( p_aout->output.p_sys->p_silence_buffer, 0,
316 p_aout->output.p_sys->i_buffer_size );
318 /* Now we need to setup our waveOut play notification structure */
319 p_aout->output.p_sys->p_notif =
320 vlc_object_create( p_aout, sizeof(notification_thread_t) );
321 p_aout->output.p_sys->p_notif->p_aout = p_aout;
322 p_aout->output.p_sys->event = CreateEvent( NULL, FALSE, FALSE, NULL );
324 /* Then launch the notification thread */
325 if( vlc_thread_create( p_aout->output.p_sys->p_notif,
326 "waveOut Notification Thread", WaveOutThread,
327 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
329 msg_Err( p_aout, "cannot create WaveOutThread" );
332 /* We need to kick off the playback in order to have the callback properly
334 for( i = 0; i < FRAMES_NUM; i++ )
336 p_aout->output.p_sys->waveheader[i].dwFlags = WHDR_DONE;
337 p_aout->output.p_sys->waveheader[i].dwUser = 0;
339 PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
340 &p_aout->output.p_sys->waveheader[0], NULL );
345 /*****************************************************************************
346 * Probe: probe the audio device for available formats and channels
347 *****************************************************************************/
348 static void Probe( aout_instance_t * p_aout )
350 vlc_value_t val, text;
352 unsigned int i_physical_channels;
354 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
355 text.psz_string = _("Audio Device");
356 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
358 /* Test for 5.1 support */
359 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
360 AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT |
361 AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE;
362 if( p_aout->output.output.i_physical_channels == i_physical_channels )
364 if( OpenWaveOutPCM( p_aout, &i_format,
365 i_physical_channels, 6,
366 p_aout->output.output.i_rate, VLC_TRUE )
369 val.i_int = AOUT_VAR_5_1;
370 text.psz_string = N_("5.1");
371 var_Change( p_aout, "audio-device",
372 VLC_VAR_ADDCHOICE, &val, &text );
373 msg_Dbg( p_aout, "device supports 5.1 channels" );
377 /* Test for 2 Front 2 Rear support */
378 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT |
379 AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
380 if( ( p_aout->output.output.i_physical_channels & i_physical_channels )
381 == i_physical_channels )
383 if( OpenWaveOutPCM( p_aout, &i_format,
384 i_physical_channels, 4,
385 p_aout->output.output.i_rate, VLC_TRUE )
388 val.i_int = AOUT_VAR_2F2R;
389 text.psz_string = N_("2 Front 2 Rear");
390 var_Change( p_aout, "audio-device",
391 VLC_VAR_ADDCHOICE, &val, &text );
392 msg_Dbg( p_aout, "device supports 4 channels" );
396 /* Test for stereo support */
397 i_physical_channels = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
398 if( OpenWaveOutPCM( p_aout, &i_format,
399 i_physical_channels, 2,
400 p_aout->output.output.i_rate, VLC_TRUE )
403 val.i_int = AOUT_VAR_STEREO;
404 text.psz_string = N_("Stereo");
405 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
406 msg_Dbg( p_aout, "device supports 2 channels" );
409 /* Test for mono support */
410 i_physical_channels = AOUT_CHAN_CENTER;
411 if( OpenWaveOutPCM( p_aout, &i_format,
412 i_physical_channels, 1,
413 p_aout->output.output.i_rate, VLC_TRUE )
416 val.i_int = AOUT_VAR_MONO;
417 text.psz_string = N_("Mono");
418 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
419 msg_Dbg( p_aout, "device supports 1 channel" );
422 /* Test for SPDIF support */
423 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
425 if( OpenWaveOut( p_aout, VLC_FOURCC('s','p','d','i'),
426 p_aout->output.output.i_physical_channels,
427 aout_FormatNbChannels( &p_aout->output.output ),
428 p_aout->output.output.i_rate, VLC_TRUE )
431 msg_Dbg( p_aout, "device supports A/52 over S/PDIF" );
432 val.i_int = AOUT_VAR_SPDIF;
433 text.psz_string = N_("A/52 over S/PDIF");
434 var_Change( p_aout, "audio-device",
435 VLC_VAR_ADDCHOICE, &val, &text );
436 if( config_GetInt( p_aout, "spdif" ) )
437 var_Set( p_aout, "audio-device", val );
441 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
444 /* Probe() has failed. */
445 var_Destroy( p_aout, "audio-device" );
449 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
451 val.b_bool = VLC_TRUE;
452 var_Set( p_aout, "intf-change", val );
455 /*****************************************************************************
456 * Play: play a sound buffer
457 *****************************************************************************
458 * This doesn't actually play the buffer. This just stores the buffer so it
459 * can be played by the callback thread.
460 *****************************************************************************/
461 static void Play( aout_instance_t *_p_aout )
465 /*****************************************************************************
466 * Close: close the audio device
467 *****************************************************************************/
468 static void Close( vlc_object_t *p_this )
470 aout_instance_t *p_aout = (aout_instance_t *)p_this;
471 aout_sys_t *p_sys = p_aout->output.p_sys;
473 /* Before calling waveOutClose we must reset the device */
474 p_aout->b_die = VLC_TRUE;
476 waveOutReset( p_sys->h_waveout );
478 /* wake up the audio thread */
479 SetEvent( p_sys->event );
480 vlc_thread_join( p_sys->p_notif );
481 vlc_object_destroy( p_sys->p_notif );
482 CloseHandle( p_sys->event );
484 /* Close the device */
485 if( waveOutClose( p_sys->h_waveout ) != MMSYSERR_NOERROR )
487 msg_Err( p_aout, "waveOutClose failed" );
490 free( p_sys->p_silence_buffer );
494 /*****************************************************************************
495 * OpenWaveOut: open the waveout sound device
496 ****************************************************************************/
497 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
498 int i_channels, int i_nb_channels, int i_rate,
504 /* Set sound format */
506 #define waveformat p_aout->output.p_sys->waveformat
508 waveformat.dwChannelMask = 0;
509 for( i = 0; i < sizeof(pi_channels_src)/sizeof(uint32_t); i++ )
511 if( i_channels & pi_channels_src[i] )
512 waveformat.dwChannelMask |= pi_channels_in[i];
517 case VLC_FOURCC('s','p','d','i'):
519 /* To prevent channel re-ordering */
520 waveformat.dwChannelMask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
521 waveformat.Format.wBitsPerSample = 16;
522 waveformat.Samples.wValidBitsPerSample =
523 waveformat.Format.wBitsPerSample;
524 waveformat.Format.wFormatTag = WAVE_FORMAT_DOLBY_AC3_SPDIF;
525 waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_DOLBY_AC3_SPDIF;
528 case VLC_FOURCC('f','l','3','2'):
529 waveformat.Format.wBitsPerSample = sizeof(float) * 8;
530 waveformat.Samples.wValidBitsPerSample =
531 waveformat.Format.wBitsPerSample;
532 waveformat.Format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
533 waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
536 case VLC_FOURCC('s','1','6','l'):
537 waveformat.Format.wBitsPerSample = 16;
538 waveformat.Samples.wValidBitsPerSample =
539 waveformat.Format.wBitsPerSample;
540 waveformat.Format.wFormatTag = WAVE_FORMAT_PCM;
541 waveformat.SubFormat = __KSDATAFORMAT_SUBTYPE_PCM;
545 waveformat.Format.nChannels = i_nb_channels;
546 waveformat.Format.nSamplesPerSec = i_rate;
547 waveformat.Format.nBlockAlign =
548 waveformat.Format.wBitsPerSample / 8 * i_nb_channels;
549 waveformat.Format.nAvgBytesPerSec =
550 waveformat.Format.nSamplesPerSec * waveformat.Format.nBlockAlign;
552 /* Only use the new WAVE_FORMAT_EXTENSIBLE format for multichannel audio */
553 if( i_nb_channels <= 2 )
555 waveformat.Format.cbSize = 0;
559 waveformat.Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
560 waveformat.Format.cbSize =
561 sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX);
564 /* Open the device */
565 result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
566 (WAVEFORMATEX *)&waveformat,
567 (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
568 CALLBACK_FUNCTION | (b_probe?WAVE_FORMAT_QUERY:0) );
569 if( result == WAVERR_BADFORMAT )
571 msg_Warn( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
574 if( result == MMSYSERR_ALLOCATED )
576 msg_Warn( p_aout, "waveOutOpen failed WAVERR_ALLOCATED" );
579 if( result != MMSYSERR_NOERROR )
581 msg_Warn( p_aout, "waveOutOpen failed" );
585 p_aout->output.p_sys->b_chan_reorder =
586 aout_CheckChannelReorder( pi_channels_in, pi_channels_out,
587 waveformat.dwChannelMask, i_nb_channels,
588 p_aout->output.p_sys->pi_chan_table );
590 if( p_aout->output.p_sys->b_chan_reorder )
592 msg_Dbg( p_aout, "channel reordering needed" );
601 /*****************************************************************************
602 * OpenWaveOutPCM: open a PCM waveout sound device
603 ****************************************************************************/
604 static int OpenWaveOutPCM( aout_instance_t *p_aout, int *i_format,
605 int i_channels, int i_nb_channels, int i_rate,
610 var_Get( p_aout, "waveout-float32", &val );
612 if( !val.b_bool || OpenWaveOut( p_aout, VLC_FOURCC('f','l','3','2'),
613 i_channels, i_nb_channels, i_rate, b_probe )
616 if ( OpenWaveOut( p_aout, VLC_FOURCC('s','1','6','l'),
617 i_channels, i_nb_channels, i_rate, b_probe )
624 *i_format = VLC_FOURCC('s','1','6','l');
630 *i_format = VLC_FOURCC('f','l','3','2');
635 /*****************************************************************************
636 * PlayWaveOut: play a buffer through the WaveOut device
637 *****************************************************************************/
638 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
639 WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
643 /* Prepare the buffer */
644 if( p_buffer != NULL )
645 p_waveheader->lpData = p_buffer->p_buffer;
647 /* Use silence buffer instead */
648 p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
650 p_waveheader->dwUser = p_buffer ? (DWORD_PTR)p_buffer : (DWORD_PTR)1;
651 p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
652 p_waveheader->dwFlags = 0;
654 result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
655 if( result != MMSYSERR_NOERROR )
657 msg_Err( p_aout, "waveOutPrepareHeader failed" );
661 /* Send the buffer to the waveOut queue */
662 result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
663 if( result != MMSYSERR_NOERROR )
665 msg_Err( p_aout, "waveOutWrite failed" );
672 /*****************************************************************************
673 * WaveOutCallback: what to do once WaveOut has played its sound samples
674 *****************************************************************************/
675 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
677 DWORD dwParam1, DWORD dwParam2 )
679 aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
680 int i, i_queued_frames = 0;
682 if( uMsg != WOM_DONE ) return;
684 if( p_aout->b_die ) return;
686 /* Find out the current latency */
687 for( i = 0; i < FRAMES_NUM; i++ )
689 /* Check if frame buf is available */
690 if( !(p_aout->output.p_sys->waveheader[i].dwFlags & WHDR_DONE) )
696 /* Don't wake up the thread too much */
697 if( i_queued_frames < FRAMES_NUM / 2 )
698 SetEvent( p_aout->output.p_sys->event );
701 /*****************************************************************************
702 * WaveOutThread: this thread will capture play notification events.
703 *****************************************************************************
704 * We use this thread to feed new audio samples to the sound card because
705 * we are not authorized to use waveOutWrite() directly in the waveout
707 *****************************************************************************/
708 static void WaveOutThread( notification_thread_t *p_notif )
710 aout_instance_t *p_aout = p_notif->p_aout;
711 aout_sys_t *p_sys = p_aout->output.p_sys;
712 aout_buffer_t *p_buffer = NULL;
713 WAVEHDR *p_waveheader = p_sys->waveheader;
714 int i, i_queued_frames;
717 /* We don't want any resampling when using S/PDIF */
718 b_sleek = p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i');
722 WaitForSingleObject( p_sys->event, INFINITE );
724 /* Cleanup and find out the current latency */
726 for( i = 0; i < FRAMES_NUM; i++ )
728 if( (p_waveheader[i].dwFlags & WHDR_DONE) &&
729 p_waveheader[i].dwUser )
731 /* Unprepare and free the buffers which has just been played */
732 waveOutUnprepareHeader( p_sys->h_waveout, &p_waveheader[i],
735 if( p_waveheader[i].dwUser != 1 )
736 aout_BufferFree( (aout_buffer_t *)p_waveheader[i].dwUser );
738 p_waveheader[i].dwUser = 0;
741 /* Check if frame buf is available */
742 if( !(p_waveheader[i].dwFlags & WHDR_DONE) )
748 if( p_aout->b_die ) return;
750 /* Try to fill in as many frame buffers as possible */
751 for( i = 0; i < FRAMES_NUM; i++ )
753 /* Check if frame buf is available */
754 if( p_waveheader[i].dwFlags & WHDR_DONE )
756 /* Take into account the latency */
757 p_buffer = aout_OutputNextBuffer( p_aout,
758 mdate() + 1000000 * i_queued_frames /
759 p_aout->output.output.i_rate * p_aout->output.i_nb_samples,
762 if( !p_buffer && i_queued_frames )
764 /* We aren't late so no need to play a blank sample */
768 /* Do the channel reordering */
769 if( p_buffer && p_sys->b_chan_reorder )
771 aout_ChannelReorder( p_buffer->p_buffer,
772 p_buffer->i_nb_bytes,
773 p_sys->waveformat.Format.nChannels,
774 p_sys->pi_chan_table,
775 p_sys->waveformat.Format.wBitsPerSample );
778 PlayWaveOut( p_aout, p_sys->h_waveout,
779 &p_waveheader[i], p_buffer );
787 static int VolumeInfos( aout_instance_t * p_aout, audio_volume_t * pi_soft )
789 *pi_soft = AOUT_VOLUME_MAX / 2;
793 static int VolumeGet( aout_instance_t * p_aout, audio_volume_t * pi_volume )
798 waveOutGetVolume( 0, &i_waveout_vol );
800 waveOutGetVolume( p_aout->output.p_sys->h_waveout, &i_waveout_vol );
803 i_waveout_vol &= 0xFFFF;
804 /* Force float computation, otherwise VolumeGet does not return the value
805 * which was set with VolumeSet, because of rounding issues */
806 *pi_volume = p_aout->output.i_volume =
807 (audio_volume_t)roundf((float)i_waveout_vol * AOUT_VOLUME_MAX
812 static int VolumeSet( aout_instance_t * p_aout, audio_volume_t i_volume )
814 unsigned long i_waveout_vol = i_volume * 0xFFFF * 2 / AOUT_VOLUME_MAX;
815 i_waveout_vol |= (i_waveout_vol << 16);
818 waveOutSetVolume( 0, i_waveout_vol );
820 waveOutSetVolume( p_aout->output.p_sys->h_waveout, i_waveout_vol );
823 p_aout->output.i_volume = i_volume;