]> git.sesse.net Git - vlc/blob - modules/gui/beos/AudioOutput.cpp
* AudioOutput.cpp: send zeros to BSoundPlayer if nothing comes from
[vlc] / modules / gui / beos / AudioOutput.cpp
1 /*****************************************************************************
2  * AudioOutput.cpp: BeOS audio output
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id: AudioOutput.cpp,v 1.19 2002/12/09 07:57:04 titer Exp $
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Eric Petit <titer@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdio.h>
30 #include <stdlib.h>                                      /* malloc(), free() */
31 #include <malloc.h>
32 #include <string.h>
33
34 #include <SoundPlayer.h>
35 #include <media/MediaDefs.h>
36
37 #include <vlc/vlc.h>
38 #include <vlc/aout.h>
39 #include <aout_internal.h>
40
41 #define FRAME_SIZE  2048
42 #define BUFFER_SIZE 16384
43
44 /*****************************************************************************
45  * aout_sys_t: BeOS audio output method descriptor
46  *****************************************************************************/
47
48 typedef struct aout_sys_t
49 {
50     BSoundPlayer *p_player;
51     
52 } aout_sys_t;
53
54 /*****************************************************************************
55  * Local prototypes.
56  *****************************************************************************/
57 static void Play         ( void *p_aout, void *p_buffer, size_t size,
58                            const media_raw_audio_format &format );
59 static void DoNothing    ( aout_instance_t *p_aout );
60
61 /*****************************************************************************
62  * OpenAudio
63  *****************************************************************************/
64 int E_(OpenAudio) ( vlc_object_t * p_this )
65 {
66     int i_nb_channels;
67     aout_instance_t *p_aout = (aout_instance_t*) p_this;
68     p_aout->output.p_sys = (aout_sys_t *) malloc( sizeof( aout_sys_t ) );
69
70     aout_sys_t *p_sys = p_aout->output.p_sys;
71
72     aout_VolumeSoftInit( p_aout );
73     
74     media_raw_audio_format *p_format;
75     p_format = (media_raw_audio_format*)
76         malloc( sizeof( media_raw_audio_format ) );
77     
78     p_format->frame_rate = p_aout->output.output.i_rate;
79
80     i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
81     if ( i_nb_channels > 2 )
82     {
83         /* BSoundPlayer does not support more than 2 channels AFAIK */
84         i_nb_channels = 2;
85         p_aout->output.output.i_physical_channels
86             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
87     }
88     p_format->channel_count = i_nb_channels;
89
90     p_format->format = media_raw_audio_format::B_AUDIO_FLOAT;
91 #ifdef WORDS_BIGENDIAN
92     p_format->byte_order = B_MEDIA_BIG_ENDIAN;
93 #else
94     p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
95 #endif
96     p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
97
98     p_format->buffer_size = BUFFER_SIZE;
99     p_aout->output.i_nb_samples = FRAME_SIZE;
100     p_aout->output.pf_play = DoNothing;
101     
102     p_sys->p_player = new BSoundPlayer( p_format, "player",
103                                         Play, NULL, p_aout );
104     p_sys->p_player->Start();
105     p_sys->p_player->SetHasData( true );
106         
107     return 0;
108 }
109
110 /*****************************************************************************
111  * CloseAudio
112  *****************************************************************************/
113 void E_(CloseAudio) ( vlc_object_t *p_this )
114 {
115     aout_instance_t * p_aout = (aout_instance_t *) p_this;
116     aout_sys_t * p_sys = (aout_sys_t *) p_aout->output.p_sys;
117     
118     p_sys->p_player->Stop();
119     delete p_sys->p_player;
120     free( p_sys );
121 }
122
123 /*****************************************************************************
124  * Play
125  *****************************************************************************/
126 static void Play( void *aout, void *p_buffer, size_t i_size,
127                   const media_raw_audio_format &format )
128 {
129     aout_buffer_t * p_aout_buffer;
130     aout_instance_t *p_aout = (aout_instance_t*) aout;
131
132     vlc_mutex_lock( &p_aout->output_fifo_lock );
133     p_aout_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
134     vlc_mutex_unlock( &p_aout->output_fifo_lock );
135
136     if( p_aout_buffer != NULL )
137     {
138         memcpy( (float*)p_buffer,
139                 p_aout_buffer->p_buffer,
140                 MIN( BUFFER_SIZE, p_aout_buffer->i_nb_bytes ) );
141         if( p_aout_buffer->i_nb_bytes < BUFFER_SIZE )
142         {
143             memset( (float*)p_buffer + p_aout_buffer->i_nb_bytes,
144                     0, BUFFER_SIZE - p_aout_buffer->i_nb_bytes );
145         }
146         aout_BufferFree( p_aout_buffer );
147     }
148     else
149     {
150         memset( (float*)p_buffer, 0, BUFFER_SIZE );
151     }
152 }
153
154 /*****************************************************************************
155  * DoNothing 
156  *****************************************************************************/
157 static void DoNothing( aout_instance_t *p_aout)
158 {
159     return;
160 }