1 /*****************************************************************************
2 * aout_beos.cpp: BeOS audio output
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001 VideoLAN
5 * $Id: aout_beos.cpp,v 1.13 2001/02/20 07:49:12 sam Exp $
7 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
25 #define MODULE_NAME beos
26 #include "modules_inner.h"
28 /*****************************************************************************
30 *****************************************************************************/
34 #include <stdlib.h> /* malloc(), free() */
35 #include <kernel/OS.h>
37 #include <Application.h>
40 #include <media/MediaDefs.h>
41 #include <game/PushGameSound.h>
52 #include "audio_output.h"
60 /*****************************************************************************
61 * aout_sys_t: BeOS audio output method descriptor
62 *****************************************************************************
63 * This structure is part of the audio output thread descriptor.
64 * It describes some BeOS specific variables.
65 *****************************************************************************/
66 typedef struct aout_sys_s
68 BPushGameSound * p_sound;
69 gs_audio_format * p_format;
79 /*****************************************************************************
81 *****************************************************************************/
82 static int aout_Probe ( probedata_t *p_data );
83 static int aout_Open ( aout_thread_t *p_aout );
84 static int aout_SetFormat ( aout_thread_t *p_aout );
85 static long aout_GetBufInfo ( aout_thread_t *p_aout, long l_buffer_info );
86 static void aout_Play ( aout_thread_t *p_aout,
87 byte_t *buffer, int i_size );
88 static void aout_Close ( aout_thread_t *p_aout );
90 /*****************************************************************************
91 * Functions exported as capabilities. They are declared as static so that
92 * we don't pollute the namespace too much.
93 *****************************************************************************/
94 void _M( aout_getfunctions )( function_list_t * p_function_list )
96 p_function_list->pf_probe = aout_Probe;
97 p_function_list->functions.aout.pf_open = aout_Open;
98 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
99 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
100 p_function_list->functions.aout.pf_play = aout_Play;
101 p_function_list->functions.aout.pf_close = aout_Close;
104 /*****************************************************************************
105 * aout_Probe: probe the audio device and return a score
106 *****************************************************************************/
107 static int aout_Probe( probedata_t *p_data )
109 /* We don't test anything since I don't know what to test. However
110 * if the module could be loaded it is quite likely to work. */
114 /*****************************************************************************
115 * aout_Open: opens a BPushGameSound
116 *****************************************************************************/
117 static int aout_Open( aout_thread_t *p_aout )
119 /* Allocate structure */
120 p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
121 if( p_aout->p_sys == NULL )
123 intf_ErrMsg("error: %s", strerror(ENOMEM) );
127 /* Allocate gs_audio_format */
128 p_aout->p_sys->p_format = (gs_audio_format *) malloc( sizeof( gs_audio_format ) );
129 if( p_aout->p_sys->p_format == NULL )
131 free( p_aout->p_sys );
132 intf_ErrMsg("error: cannot allocate memory for gs_audio_format" );
136 /* Initialize some variables */
137 p_aout->i_format = AOUT_FORMAT_DEFAULT;
138 p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
139 AOUT_STEREO_DEFAULT );
140 p_aout->l_rate = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
142 p_aout->p_sys->p_format->frame_rate = 44100.0;
143 p_aout->p_sys->p_format->channel_count = p_aout->i_channels;
144 p_aout->p_sys->p_format->format = gs_audio_format::B_GS_S16;
145 p_aout->p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
146 p_aout->p_sys->p_format->buffer_size = 4*8192;
147 p_aout->p_sys->i_buffer_pos = 0;
149 /* Allocate BPushGameSound */
150 p_aout->p_sys->p_sound = new BPushGameSound( 8192,
151 p_aout->p_sys->p_format,
153 if( p_aout->p_sys->p_sound == NULL )
155 free( p_aout->p_sys->p_format );
156 free( p_aout->p_sys );
157 intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
161 if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
163 free( p_aout->p_sys->p_format );
164 free( p_aout->p_sys );
165 intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
169 p_aout->p_sys->p_sound->StartPlaying( );
171 p_aout->p_sys->p_sound->LockForCyclic( &p_aout->p_sys->p_buffer,
172 (size_t *)&p_aout->p_sys->i_buffer_size );
177 /*****************************************************************************
178 * aout_SetFormat: sets the dsp output format
179 *****************************************************************************/
180 static int aout_SetFormat( aout_thread_t *p_aout )
185 /*****************************************************************************
186 * aout_GetBufInfo: buffer status query
187 *****************************************************************************/
188 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
190 /* Each value is 4 bytes long (stereo signed 16 bits) */
191 long i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
193 i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
196 i_hard_pos += p_aout->p_sys->i_buffer_size;
199 return( i_hard_pos );
202 /*****************************************************************************
203 * aout_Play: plays a sound samples buffer
204 *****************************************************************************
205 * This function writes a buffer of i_length bytes in the dsp
206 *****************************************************************************/
207 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
211 if( (i_newbuf_pos = p_aout->p_sys->i_buffer_pos + i_size)
212 > p_aout->p_sys->i_buffer_size )
214 memcpy( (void *)((int)p_aout->p_sys->p_buffer
215 + p_aout->p_sys->i_buffer_pos),
217 p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos );
219 memcpy( (void *)((int)p_aout->p_sys->p_buffer),
220 buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
221 i_size - ( p_aout->p_sys->i_buffer_size
222 - p_aout->p_sys->i_buffer_pos ) );
224 p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
229 memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
232 p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
236 /*****************************************************************************
237 * aout_Close: closes the dsp audio device
238 *****************************************************************************/
239 static void aout_Close( aout_thread_t *p_aout )
241 p_aout->p_sys->p_sound->UnlockCyclic();
242 p_aout->p_sys->p_sound->StopPlaying( );
243 delete p_aout->p_sys->p_sound;
244 free( p_aout->p_sys->p_format );
245 free( p_aout->p_sys );