1 /*****************************************************************************
2 * aout_beos.cpp: BeOS audio output
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001 VideoLAN
5 * $Id: aout_beos.cpp,v 1.25 2002/07/20 18:01:42 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 /*****************************************************************************
27 *****************************************************************************/
29 #include <stdlib.h> /* malloc(), free() */
30 #include <kernel/OS.h>
32 #include <Application.h>
35 #include <media/MediaDefs.h>
36 #include <game/PushGameSound.h>
46 /*****************************************************************************
47 * aout_sys_t: BeOS audio output method descriptor
48 *****************************************************************************
49 * This structure is part of the audio output thread descriptor.
50 * It describes some BeOS specific variables.
51 *****************************************************************************/
54 BPushGameSound * p_sound;
55 gs_audio_format * p_format;
64 /*****************************************************************************
66 *****************************************************************************/
67 static int aout_Open ( aout_thread_t *p_aout );
68 static int aout_SetFormat ( aout_thread_t *p_aout );
69 static int aout_GetBufInfo ( aout_thread_t *p_aout, int i_buffer_info );
70 static void aout_Play ( aout_thread_t *p_aout,
71 byte_t *buffer, int i_size );
72 static void aout_Close ( aout_thread_t *p_aout );
74 /*****************************************************************************
75 * Functions exported as capabilities. They are declared as static so that
76 * we don't pollute the namespace too much.
77 *****************************************************************************/
78 void _M( aout_getfunctions )( function_list_t * p_function_list )
80 p_function_list->functions.aout.pf_open = aout_Open;
81 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
82 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
83 p_function_list->functions.aout.pf_play = aout_Play;
84 p_function_list->functions.aout.pf_close = aout_Close;
87 /*****************************************************************************
88 * aout_Open: opens a BPushGameSound
89 *****************************************************************************/
90 static int aout_Open( aout_thread_t *p_aout )
92 /* Allocate structure */
93 p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
94 if( p_aout->p_sys == NULL )
96 msg_Err( p_aout, "out of memory" );
100 /* Allocate gs_audio_format */
101 p_aout->p_sys->p_format = (gs_audio_format *) malloc( sizeof( gs_audio_format ) );
102 if( p_aout->p_sys->p_format == NULL )
104 free( p_aout->p_sys );
105 msg_Err( p_aout, "out of memory" );
109 /* Initialize some variables */
110 p_aout->p_sys->p_format->frame_rate = 44100.0;
111 p_aout->p_sys->p_format->channel_count = p_aout->i_channels;
112 p_aout->p_sys->p_format->format = gs_audio_format::B_GS_S16;
113 p_aout->p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
114 p_aout->p_sys->p_format->buffer_size = 4*8192;
115 p_aout->p_sys->i_buffer_pos = 0;
117 /* Allocate BPushGameSound */
118 p_aout->p_sys->p_sound = new BPushGameSound( 8192,
119 p_aout->p_sys->p_format,
121 if( p_aout->p_sys->p_sound == NULL )
123 free( p_aout->p_sys->p_format );
124 free( p_aout->p_sys );
125 msg_Err( p_aout, "cannot allocate BPushGameSound" );
129 if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
131 free( p_aout->p_sys->p_format );
132 free( p_aout->p_sys );
133 msg_Err( p_aout, "cannot initialize BPushGameSound" );
137 p_aout->p_sys->p_sound->StartPlaying( );
139 p_aout->p_sys->p_sound->LockForCyclic( &p_aout->p_sys->p_buffer,
140 (size_t *)&p_aout->p_sys->i_buffer_size );
145 /*****************************************************************************
146 * aout_SetFormat: sets the dsp output format
147 *****************************************************************************/
148 static int aout_SetFormat( aout_thread_t *p_aout )
153 /*****************************************************************************
154 * aout_GetBufInfo: buffer status query
155 *****************************************************************************/
156 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
158 /* Each value is 4 bytes long (stereo signed 16 bits) */
159 int i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
161 i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
164 i_hard_pos += p_aout->p_sys->i_buffer_size;
167 return( i_hard_pos );
170 /*****************************************************************************
171 * aout_Play: plays a sound samples buffer
172 *****************************************************************************
173 * This function writes a buffer of i_length bytes in the dsp
174 *****************************************************************************/
175 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
179 if( (i_newbuf_pos = p_aout->p_sys->i_buffer_pos + i_size)
180 > p_aout->p_sys->i_buffer_size )
182 memcpy( (void *)((int)p_aout->p_sys->p_buffer
183 + p_aout->p_sys->i_buffer_pos),
185 p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos );
187 memcpy( (void *)((int)p_aout->p_sys->p_buffer),
188 buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
189 i_size - ( p_aout->p_sys->i_buffer_size
190 - p_aout->p_sys->i_buffer_pos ) );
192 p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
197 memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
200 p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
204 /*****************************************************************************
205 * aout_Close: closes the dsp audio device
206 *****************************************************************************/
207 static void aout_Close( aout_thread_t *p_aout )
209 p_aout->p_sys->p_sound->UnlockCyclic();
210 p_aout->p_sys->p_sound->StopPlaying( );
211 delete p_aout->p_sys->p_sound;
212 free( p_aout->p_sys->p_format );
213 free( p_aout->p_sys );