]> git.sesse.net Git - vlc/blob - plugins/beos/aout_beos.cpp
Removed flooding debug info :)
[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.12 2001/02/18 03:32:02 polux 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 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include "defs.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>                                      /* malloc(), free() */
32 #include <kernel/OS.h>
33 #include <View.h>
34 #include <Application.h>
35 #include <Message.h>
36 #include <Locker.h>
37 #include <media/MediaDefs.h>
38 #include <game/PushGameSound.h>
39 #include <malloc.h>
40 #include <string.h>
41
42 extern "C"
43 {
44 #include "config.h"
45 #include "common.h"
46 #include "threads.h"
47 #include "mtime.h"
48
49 #include "audio_output.h"
50
51 #include "intf_msg.h"
52 #include "main.h"
53
54 #include "modules.h"
55 }
56
57 /*****************************************************************************
58  * aout_sys_t: BeOS audio output method descriptor
59  *****************************************************************************
60  * This structure is part of the audio output thread descriptor.
61  * It describes some BeOS specific variables.
62  *****************************************************************************/
63 typedef struct aout_sys_s
64 {
65     BPushGameSound * p_sound;
66     gs_audio_format * p_format;
67     void * p_buffer;
68     long i_buffer_size;
69     long i_buffer_pos;
70
71 } aout_sys_t;
72
73 extern "C"
74 {
75
76 /*****************************************************************************
77  * Local prototypes.
78  *****************************************************************************/
79 static int     aout_Probe       ( probedata_t *p_data );
80 static int     aout_Open        ( aout_thread_t *p_aout );
81 static int     aout_SetFormat   ( aout_thread_t *p_aout );
82 static long    aout_GetBufInfo  ( aout_thread_t *p_aout, long l_buffer_info );
83 static void    aout_Play        ( aout_thread_t *p_aout,
84                                   byte_t *buffer, int i_size );
85 static void    aout_Close       ( aout_thread_t *p_aout );
86
87 /*****************************************************************************
88  * Functions exported as capabilities. They are declared as static so that
89  * we don't pollute the namespace too much.
90  *****************************************************************************/
91 void aout_getfunctions( function_list_t * p_function_list )
92 {
93     p_function_list->pf_probe = aout_Probe;
94     p_function_list->functions.aout.pf_open = aout_Open;
95     p_function_list->functions.aout.pf_setformat = aout_SetFormat;
96     p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
97     p_function_list->functions.aout.pf_play = aout_Play;
98     p_function_list->functions.aout.pf_close = aout_Close;
99 }
100
101 /*****************************************************************************
102  * aout_Probe: probe the audio device and return a score
103  *****************************************************************************/
104 static int aout_Probe( probedata_t *p_data )
105 {
106     /* We don't test anything since I don't know what to test. However
107      * if the module could be loaded it is quite likely to work. */
108     return( 100 );
109 }
110
111 /*****************************************************************************
112  * aout_Open: opens a BPushGameSound
113  *****************************************************************************/
114 static int aout_Open( aout_thread_t *p_aout )
115 {
116     /* Allocate structure */
117     p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
118     if( p_aout->p_sys == NULL )
119     {
120         intf_ErrMsg("error: %s", strerror(ENOMEM) );
121         return( 1 );
122     }
123
124     /* Allocate gs_audio_format */
125     p_aout->p_sys->p_format = (gs_audio_format *) malloc( sizeof( gs_audio_format ) );
126     if( p_aout->p_sys->p_format == NULL )
127     {
128         free( p_aout->p_sys );
129         intf_ErrMsg("error: cannot allocate memory for gs_audio_format" );
130         return( 1 );
131     }
132
133     /* Initialize some variables */
134     p_aout->i_format = AOUT_FORMAT_DEFAULT;
135     p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
136                                                   AOUT_STEREO_DEFAULT );
137     p_aout->l_rate = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
138
139     p_aout->p_sys->p_format->frame_rate = 44100.0;
140     p_aout->p_sys->p_format->channel_count = p_aout->i_channels;
141     p_aout->p_sys->p_format->format = gs_audio_format::B_GS_S16;
142     p_aout->p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
143     p_aout->p_sys->p_format->buffer_size = 4*8192;
144     p_aout->p_sys->i_buffer_pos = 0;
145
146     /* Allocate BPushGameSound */
147     p_aout->p_sys->p_sound = new BPushGameSound( 8192,
148                                                  p_aout->p_sys->p_format,
149                                                  2, NULL );
150     if( p_aout->p_sys->p_sound == NULL )
151     {
152         free( p_aout->p_sys->p_format );
153         free( p_aout->p_sys );
154         intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
155         return( 1 );
156     }
157
158     if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
159     {
160         free( p_aout->p_sys->p_format );
161         free( p_aout->p_sys );
162         intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
163         return( 1 );
164     }
165
166     p_aout->p_sys->p_sound->StartPlaying( );
167
168     p_aout->p_sys->p_sound->LockForCyclic( &p_aout->p_sys->p_buffer,
169                             (size_t *)&p_aout->p_sys->i_buffer_size );
170
171     return( 0 );
172 }
173
174 /*****************************************************************************
175  * aout_SetFormat: sets the dsp output format
176  *****************************************************************************/
177 static int aout_SetFormat( aout_thread_t *p_aout )
178 {
179     return( 0 );
180 }
181
182 /*****************************************************************************
183  * aout_GetBufInfo: buffer status query
184  *****************************************************************************/
185 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
186 {
187     /* Each value is 4 bytes long (stereo signed 16 bits) */
188     long i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
189
190     i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
191     if( i_hard_pos < 0 )
192     {
193          i_hard_pos += p_aout->p_sys->i_buffer_size;
194     }
195
196     return( i_hard_pos );
197 }
198
199 /*****************************************************************************
200  * aout_Play: plays a sound samples buffer
201  *****************************************************************************
202  * This function writes a buffer of i_length bytes in the dsp
203  *****************************************************************************/
204 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
205 {
206     long i_newbuf_pos;
207
208     if( (i_newbuf_pos = p_aout->p_sys->i_buffer_pos + i_size)
209               > p_aout->p_sys->i_buffer_size )
210     {
211         memcpy( (void *)((int)p_aout->p_sys->p_buffer
212                         + p_aout->p_sys->i_buffer_pos),
213                 buffer,
214                 p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos );
215
216         memcpy( (void *)((int)p_aout->p_sys->p_buffer),
217                 buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
218                 i_size - ( p_aout->p_sys->i_buffer_size
219                              - p_aout->p_sys->i_buffer_pos ) );
220         
221         p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
222
223     }
224     else
225     {
226         memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
227                 buffer, i_size );
228
229         p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
230     }
231 }
232
233 /*****************************************************************************
234  * aout_Close: closes the dsp audio device
235  *****************************************************************************/
236 static void aout_Close( aout_thread_t *p_aout )
237 {
238     p_aout->p_sys->p_sound->UnlockCyclic();
239     p_aout->p_sys->p_sound->StopPlaying( );
240     delete p_aout->p_sys->p_sound;
241     free( p_aout->p_sys->p_format );
242     free( p_aout->p_sys );
243 }
244
245 } /* extern "C" */
246