1 /*****************************************************************************
2 * aout_beos.cpp: BeOS audio output
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: aout_beos.cpp,v 1.10 2001/01/13 12:57:19 sam Exp $
8 * Samuel Hocevar <sam@via.ecp.fr>
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 /*****************************************************************************
27 *****************************************************************************/
31 #include <stdlib.h> /* malloc(), free() */
32 #include <kernel/OS.h>
34 #include <Application.h>
37 #include <media/MediaDefs.h>
38 #include <game/PushGameSound.h>
50 #include "audio_output.h"
58 /*****************************************************************************
59 * aout_sys_t: BeOS audio output method descriptor
60 *****************************************************************************
61 * This structure is part of the audio output thread descriptor.
62 * It describes some BeOS specific variables.
63 *****************************************************************************/
64 typedef struct aout_sys_s
66 BPushGameSound * p_sound;
67 gs_audio_format * p_format;
77 /*****************************************************************************
79 *****************************************************************************/
80 static int aout_Probe ( probedata_t *p_data );
81 static int aout_Open ( aout_thread_t *p_aout );
82 static int aout_SetFormat ( aout_thread_t *p_aout );
83 static long aout_GetBufInfo ( aout_thread_t *p_aout, long l_buffer_info );
84 static void aout_Play ( aout_thread_t *p_aout,
85 byte_t *buffer, int i_size );
86 static void aout_Close ( aout_thread_t *p_aout );
88 /*****************************************************************************
89 * Functions exported as capabilities. They are declared as static so that
90 * we don't pollute the namespace too much.
91 *****************************************************************************/
92 void aout_getfunctions( function_list_t * p_function_list )
94 p_function_list->pf_probe = aout_Probe;
95 p_function_list->functions.aout.pf_open = aout_Open;
96 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
97 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
98 p_function_list->functions.aout.pf_play = aout_Play;
99 p_function_list->functions.aout.pf_close = aout_Close;
102 /*****************************************************************************
103 * aout_Probe: probe the audio device and return a score
104 *****************************************************************************/
105 static int aout_Probe( probedata_t *p_data )
107 /* We don't test anything since I don't know what to test. However
108 * if the module could be loaded it is quite likely to work. */
112 /*****************************************************************************
113 * aout_Open: opens a BPushGameSound
114 *****************************************************************************/
115 static int aout_Open( aout_thread_t *p_aout )
117 /* Allocate structure */
118 p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
119 if( p_aout->p_sys == NULL )
121 intf_ErrMsg("error: %s", strerror(ENOMEM) );
125 /* Allocate gs_audio_format */
126 p_aout->p_sys->p_format = (gs_audio_format *) malloc( sizeof( gs_audio_format ) );
127 if( p_aout->p_sys->p_format == NULL )
129 free( p_aout->p_sys );
130 intf_ErrMsg("error: cannot allocate memory for gs_audio_format" );
134 /* Initialize some variables */
135 p_aout->i_format = AOUT_FORMAT_DEFAULT;
136 p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
137 AOUT_STEREO_DEFAULT );
138 p_aout->l_rate = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
140 p_aout->p_sys->p_format->frame_rate = 44100.0;
141 p_aout->p_sys->p_format->channel_count = p_aout->i_channels;
142 p_aout->p_sys->p_format->format = gs_audio_format::B_GS_S16;
143 p_aout->p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
144 p_aout->p_sys->p_format->buffer_size = 4*8192;
145 p_aout->p_sys->i_buffer_pos = 0;
147 /* Allocate BPushGameSound */
148 p_aout->p_sys->p_sound = new BPushGameSound( 8192,
149 p_aout->p_sys->p_format,
151 if( p_aout->p_sys->p_sound == NULL )
153 free( p_aout->p_sys->p_format );
154 free( p_aout->p_sys );
155 intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
159 if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
161 free( p_aout->p_sys->p_format );
162 free( p_aout->p_sys );
163 intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
167 p_aout->p_sys->p_sound->StartPlaying( );
169 p_aout->p_sys->p_sound->LockForCyclic( &p_aout->p_sys->p_buffer,
170 (size_t *)&p_aout->p_sys->i_buffer_size );
175 /*****************************************************************************
176 * aout_SetFormat: sets the dsp output format
177 *****************************************************************************/
178 static int aout_SetFormat( aout_thread_t *p_aout )
183 /*****************************************************************************
184 * aout_GetBufInfo: buffer status query
185 *****************************************************************************/
186 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
188 /* Each value is 4 bytes long (stereo signed 16 bits) */
189 long i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
191 i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
194 i_hard_pos += p_aout->p_sys->i_buffer_size;
197 return( i_hard_pos );
200 /*****************************************************************************
201 * aout_Play: plays a sound samples buffer
202 *****************************************************************************
203 * This function writes a buffer of i_length bytes in the dsp
204 *****************************************************************************/
205 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
209 if( (i_newbuf_pos = p_aout->p_sys->i_buffer_pos + i_size)
210 > p_aout->p_sys->i_buffer_size )
212 memcpy( (void *)((int)p_aout->p_sys->p_buffer
213 + p_aout->p_sys->i_buffer_pos),
215 p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos );
217 memcpy( (void *)((int)p_aout->p_sys->p_buffer),
218 buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
219 i_size - ( p_aout->p_sys->i_buffer_size
220 - p_aout->p_sys->i_buffer_pos ) );
222 p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
227 memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
230 p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
234 /*****************************************************************************
235 * aout_Close: closes the dsp audio device
236 *****************************************************************************/
237 static void aout_Close( aout_thread_t *p_aout )
239 p_aout->p_sys->p_sound->UnlockCyclic();
240 p_aout->p_sys->p_sound->StopPlaying( );
241 delete p_aout->p_sys->p_sound;
242 free( p_aout->p_sys->p_format );
243 free( p_aout->p_sys );