1 /*****************************************************************************
2 * aout_beos.cpp: BeOS audio output
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001 VideoLAN
5 * $Id: aout_beos.cpp,v 1.23 2002/02/24 22:06:50 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>
42 #include <videolan/vlc.h>
44 #include "audio_output.h"
47 /*****************************************************************************
48 * aout_sys_t: BeOS audio output method descriptor
49 *****************************************************************************
50 * This structure is part of the audio output thread descriptor.
51 * It describes some BeOS specific variables.
52 *****************************************************************************/
53 typedef struct aout_sys_s
55 BPushGameSound * p_sound;
56 gs_audio_format * p_format;
66 /*****************************************************************************
68 *****************************************************************************/
69 static int aout_Open ( aout_thread_t *p_aout );
70 static int aout_SetFormat ( aout_thread_t *p_aout );
71 static int aout_GetBufInfo ( aout_thread_t *p_aout, int i_buffer_info );
72 static void aout_Play ( aout_thread_t *p_aout,
73 byte_t *buffer, int i_size );
74 static void aout_Close ( aout_thread_t *p_aout );
76 /*****************************************************************************
77 * Functions exported as capabilities. They are declared as static so that
78 * we don't pollute the namespace too much.
79 *****************************************************************************/
80 void _M( aout_getfunctions )( function_list_t * p_function_list )
82 p_function_list->functions.aout.pf_open = aout_Open;
83 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
84 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
85 p_function_list->functions.aout.pf_play = aout_Play;
86 p_function_list->functions.aout.pf_close = aout_Close;
89 /*****************************************************************************
90 * aout_Open: opens a BPushGameSound
91 *****************************************************************************/
92 static int aout_Open( aout_thread_t *p_aout )
94 /* Allocate structure */
95 p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
96 if( p_aout->p_sys == NULL )
98 intf_ErrMsg("error: %s", strerror(ENOMEM) );
102 /* Allocate gs_audio_format */
103 p_aout->p_sys->p_format = (gs_audio_format *) malloc( sizeof( gs_audio_format ) );
104 if( p_aout->p_sys->p_format == NULL )
106 free( p_aout->p_sys );
107 intf_ErrMsg("error: cannot allocate memory for gs_audio_format" );
111 /* Initialize some variables */
112 p_aout->p_sys->p_format->frame_rate = 44100.0;
113 p_aout->p_sys->p_format->channel_count = p_aout->i_channels;
114 p_aout->p_sys->p_format->format = gs_audio_format::B_GS_S16;
115 p_aout->p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
116 p_aout->p_sys->p_format->buffer_size = 4*8192;
117 p_aout->p_sys->i_buffer_pos = 0;
119 /* Allocate BPushGameSound */
120 p_aout->p_sys->p_sound = new BPushGameSound( 8192,
121 p_aout->p_sys->p_format,
123 if( p_aout->p_sys->p_sound == NULL )
125 free( p_aout->p_sys->p_format );
126 free( p_aout->p_sys );
127 intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
131 if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
133 free( p_aout->p_sys->p_format );
134 free( p_aout->p_sys );
135 intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
139 p_aout->p_sys->p_sound->StartPlaying( );
141 p_aout->p_sys->p_sound->LockForCyclic( &p_aout->p_sys->p_buffer,
142 (size_t *)&p_aout->p_sys->i_buffer_size );
147 /*****************************************************************************
148 * aout_SetFormat: sets the dsp output format
149 *****************************************************************************/
150 static int aout_SetFormat( aout_thread_t *p_aout )
155 /*****************************************************************************
156 * aout_GetBufInfo: buffer status query
157 *****************************************************************************/
158 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
160 /* Each value is 4 bytes long (stereo signed 16 bits) */
161 int i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
163 i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
166 i_hard_pos += p_aout->p_sys->i_buffer_size;
169 return( i_hard_pos );
172 /*****************************************************************************
173 * aout_Play: plays a sound samples buffer
174 *****************************************************************************
175 * This function writes a buffer of i_length bytes in the dsp
176 *****************************************************************************/
177 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
181 if( (i_newbuf_pos = p_aout->p_sys->i_buffer_pos + i_size)
182 > p_aout->p_sys->i_buffer_size )
184 memcpy( (void *)((int)p_aout->p_sys->p_buffer
185 + p_aout->p_sys->i_buffer_pos),
187 p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos );
189 memcpy( (void *)((int)p_aout->p_sys->p_buffer),
190 buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
191 i_size - ( p_aout->p_sys->i_buffer_size
192 - p_aout->p_sys->i_buffer_pos ) );
194 p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
199 memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
202 p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
206 /*****************************************************************************
207 * aout_Close: closes the dsp audio device
208 *****************************************************************************/
209 static void aout_Close( aout_thread_t *p_aout )
211 p_aout->p_sys->p_sound->UnlockCyclic();
212 p_aout->p_sys->p_sound->StopPlaying( );
213 delete p_aout->p_sys->p_sound;
214 free( p_aout->p_sys->p_format );
215 free( p_aout->p_sys );