]> git.sesse.net Git - vlc/blob - plugins/beos/aout_beos.cpp
Documentation updates.
[vlc] / plugins / beos / aout_beos.cpp
1 /*****************************************************************************
2  * aout_beos.cpp: BeOS audio output
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001 VideoLAN
5  * $Id: aout_beos.cpp,v 1.15 2001/07/12 20:44:52 reno Exp $
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #define MODULE_NAME beos
26 #include "modules_inner.h"
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include "defs.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>                                      /* malloc(), free() */
35 #include <kernel/OS.h>
36 #include <View.h>
37 #include <Application.h>
38 #include <Message.h>
39 #include <Locker.h>
40 #include <media/MediaDefs.h>
41 #include <game/PushGameSound.h>
42 #include <malloc.h>
43 #include <string.h>
44
45 extern "C"
46 {
47 #include "config.h"
48 #include "common.h"
49 #include "threads.h"
50 #include "mtime.h"
51
52 #include "audio_output.h"
53
54 #include "intf_msg.h"
55 #include "main.h"
56
57 #include "modules.h"
58 #include "modules_export.h"
59 }
60
61 /*****************************************************************************
62  * aout_sys_t: BeOS audio output method descriptor
63  *****************************************************************************
64  * This structure is part of the audio output thread descriptor.
65  * It describes some BeOS specific variables.
66  *****************************************************************************/
67 typedef struct aout_sys_s
68 {
69     BPushGameSound * p_sound;
70     gs_audio_format * p_format;
71     void * p_buffer;
72     long i_buffer_size;
73     long i_buffer_pos;
74
75 } aout_sys_t;
76
77 extern "C"
78 {
79
80 /*****************************************************************************
81  * Local prototypes.
82  *****************************************************************************/
83 static int     aout_Probe       ( probedata_t *p_data );
84 static int     aout_Open        ( aout_thread_t *p_aout );
85 static int     aout_SetFormat   ( aout_thread_t *p_aout );
86 static long    aout_GetBufInfo  ( aout_thread_t *p_aout, long l_buffer_info );
87 static void    aout_Play        ( aout_thread_t *p_aout,
88                                   byte_t *buffer, int i_size );
89 static void    aout_Close       ( aout_thread_t *p_aout );
90
91 /*****************************************************************************
92  * Functions exported as capabilities. They are declared as static so that
93  * we don't pollute the namespace too much.
94  *****************************************************************************/
95 void _M( aout_getfunctions )( function_list_t * p_function_list )
96 {
97     p_function_list->pf_probe = aout_Probe;
98     p_function_list->functions.aout.pf_open = aout_Open;
99     p_function_list->functions.aout.pf_setformat = aout_SetFormat;
100     p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
101     p_function_list->functions.aout.pf_play = aout_Play;
102     p_function_list->functions.aout.pf_close = aout_Close;
103 }
104
105 /*****************************************************************************
106  * aout_Probe: probe the audio device and return a score
107  *****************************************************************************/
108 static int aout_Probe( probedata_t *p_data )
109 {
110     /* We don't test anything since I don't know what to test. However
111      * if the module could be loaded it is quite likely to work. */
112     return( 100 );
113 }
114
115 /*****************************************************************************
116  * aout_Open: opens a BPushGameSound
117  *****************************************************************************/
118 static int aout_Open( aout_thread_t *p_aout )
119 {
120     /* Allocate structure */
121     p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
122     if( p_aout->p_sys == NULL )
123     {
124         intf_ErrMsg("error: %s", strerror(ENOMEM) );
125         return( 1 );
126     }
127
128     /* Allocate gs_audio_format */
129     p_aout->p_sys->p_format = (gs_audio_format *) malloc( sizeof( gs_audio_format ) );
130     if( p_aout->p_sys->p_format == NULL )
131     {
132         free( p_aout->p_sys );
133         intf_ErrMsg("error: cannot allocate memory for gs_audio_format" );
134         return( 1 );
135     }
136
137     /* Initialize some variables */
138     p_aout->i_format = AOUT_FORMAT_DEFAULT;
139     p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
140                                                   AOUT_STEREO_DEFAULT );
141     p_aout->l_rate = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
142
143     p_aout->p_sys->p_format->frame_rate = 44100.0;
144     p_aout->p_sys->p_format->channel_count = p_aout->i_channels;
145     p_aout->p_sys->p_format->format = gs_audio_format::B_GS_S16;
146     p_aout->p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
147     p_aout->p_sys->p_format->buffer_size = 4*8192;
148     p_aout->p_sys->i_buffer_pos = 0;
149
150     /* Allocate BPushGameSound */
151     p_aout->p_sys->p_sound = new BPushGameSound( 8192,
152                                                  p_aout->p_sys->p_format,
153                                                  2, NULL );
154     if( p_aout->p_sys->p_sound == NULL )
155     {
156         free( p_aout->p_sys->p_format );
157         free( p_aout->p_sys );
158         intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
159         return( 1 );
160     }
161
162     if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
163     {
164         free( p_aout->p_sys->p_format );
165         free( p_aout->p_sys );
166         intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
167         return( 1 );
168     }
169
170     p_aout->p_sys->p_sound->StartPlaying( );
171
172     p_aout->p_sys->p_sound->LockForCyclic( &p_aout->p_sys->p_buffer,
173                             (size_t *)&p_aout->p_sys->i_buffer_size );
174
175     return( 0 );
176 }
177
178 /*****************************************************************************
179  * aout_SetFormat: sets the dsp output format
180  *****************************************************************************/
181 static int aout_SetFormat( aout_thread_t *p_aout )
182 {
183     p_aout->i_latency = 0;
184
185     return( 0 );
186 }
187
188 /*****************************************************************************
189  * aout_GetBufInfo: buffer status query
190  *****************************************************************************/
191 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
192 {
193     /* Each value is 4 bytes long (stereo signed 16 bits) */
194     long i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
195
196     i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
197     if( i_hard_pos < 0 )
198     {
199          i_hard_pos += p_aout->p_sys->i_buffer_size;
200     }
201
202     return( i_hard_pos );
203 }
204
205 /*****************************************************************************
206  * aout_Play: plays a sound samples buffer
207  *****************************************************************************
208  * This function writes a buffer of i_length bytes in the dsp
209  *****************************************************************************/
210 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
211 {
212     long i_newbuf_pos;
213
214     if( (i_newbuf_pos = p_aout->p_sys->i_buffer_pos + i_size)
215               > p_aout->p_sys->i_buffer_size )
216     {
217         memcpy( (void *)((int)p_aout->p_sys->p_buffer
218                         + p_aout->p_sys->i_buffer_pos),
219                 buffer,
220                 p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos );
221
222         memcpy( (void *)((int)p_aout->p_sys->p_buffer),
223                 buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
224                 i_size - ( p_aout->p_sys->i_buffer_size
225                              - p_aout->p_sys->i_buffer_pos ) );
226         
227         p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
228
229     }
230     else
231     {
232         memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
233                 buffer, i_size );
234
235         p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
236     }
237 }
238
239 /*****************************************************************************
240  * aout_Close: closes the dsp audio device
241  *****************************************************************************/
242 static void aout_Close( aout_thread_t *p_aout )
243 {
244     p_aout->p_sys->p_sound->UnlockCyclic();
245     p_aout->p_sys->p_sound->StopPlaying( );
246     delete p_aout->p_sys->p_sound;
247     free( p_aout->p_sys->p_format );
248     free( p_aout->p_sys );
249 }
250
251 } /* extern "C" */
252