1 /*****************************************************************************
2 * aout.cpp: BeOS audio output
3 *****************************************************************************
4 * Copyright (C) 1999, 2000, 2001 VideoLAN
5 * $Id: AudioOutput.cpp,v 1.1 2002/08/04 17:23:43 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>
43 /*****************************************************************************
44 * aout_sys_t: BeOS audio output method descriptor
45 *****************************************************************************
46 * This structure is part of the audio output thread descriptor.
47 * It describes some BeOS specific variables.
48 *****************************************************************************/
51 BPushGameSound * p_sound;
52 gs_audio_format * p_format;
58 /*****************************************************************************
60 *****************************************************************************/
61 static int SetFormat ( aout_thread_t * );
62 static int GetBufInfo ( aout_thread_t *, int );
63 static void Play ( aout_thread_t *, byte_t *, int );
65 /*****************************************************************************
66 * OpenAudio: opens a BPushGameSound
67 *****************************************************************************/
68 int E_(OpenAudio) ( vlc_object_t *p_this )
70 aout_thread_t * p_aout = (aout_thread_t *)p_this;
72 /* Allocate structure */
73 p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
74 if( p_aout->p_sys == NULL )
76 msg_Err( p_aout, "out of memory" );
80 /* Allocate gs_audio_format */
81 p_aout->p_sys->p_format = (gs_audio_format *) malloc( sizeof( gs_audio_format ) );
82 if( p_aout->p_sys->p_format == NULL )
84 free( p_aout->p_sys );
85 msg_Err( p_aout, "out of memory" );
89 /* Initialize some variables */
90 p_aout->p_sys->p_format->frame_rate = 44100.0;
91 p_aout->p_sys->p_format->channel_count = p_aout->i_channels;
92 p_aout->p_sys->p_format->format = gs_audio_format::B_GS_S16;
93 p_aout->p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
94 p_aout->p_sys->p_format->buffer_size = 4*8192;
95 p_aout->p_sys->i_buffer_pos = 0;
97 p_aout->pf_setformat = SetFormat;
98 p_aout->pf_getbufinfo = GetBufInfo;
99 p_aout->pf_play = Play;
101 /* Allocate BPushGameSound */
102 p_aout->p_sys->p_sound = new BPushGameSound( 8192,
103 p_aout->p_sys->p_format,
105 if( p_aout->p_sys->p_sound == NULL )
107 free( p_aout->p_sys->p_format );
108 free( p_aout->p_sys );
109 msg_Err( p_aout, "cannot allocate BPushGameSound" );
113 if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
115 free( p_aout->p_sys->p_format );
116 free( p_aout->p_sys );
117 msg_Err( p_aout, "cannot initialize BPushGameSound" );
121 p_aout->p_sys->p_sound->StartPlaying( );
123 p_aout->p_sys->p_sound->LockForCyclic( &p_aout->p_sys->p_buffer,
124 (size_t *)&p_aout->p_sys->i_buffer_size );
129 /*****************************************************************************
130 * SetFormat: sets the dsp output format
131 *****************************************************************************/
132 static int SetFormat( aout_thread_t *p_aout )
137 /*****************************************************************************
138 * GetBufInfo: buffer status query
139 *****************************************************************************/
140 static int GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
142 /* Each value is 4 bytes long (stereo signed 16 bits) */
143 int i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
145 i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
148 i_hard_pos += p_aout->p_sys->i_buffer_size;
151 return( i_hard_pos );
154 /*****************************************************************************
155 * Play: plays a sound samples buffer
156 *****************************************************************************
157 * This function writes a buffer of i_length bytes in the dsp
158 *****************************************************************************/
159 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
163 if( (i_newbuf_pos = p_aout->p_sys->i_buffer_pos + i_size)
164 > p_aout->p_sys->i_buffer_size )
166 memcpy( (void *)((int)p_aout->p_sys->p_buffer
167 + p_aout->p_sys->i_buffer_pos),
169 p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos );
171 memcpy( (void *)((int)p_aout->p_sys->p_buffer),
172 buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
173 i_size - ( p_aout->p_sys->i_buffer_size
174 - p_aout->p_sys->i_buffer_pos ) );
176 p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
181 memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
184 p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
188 /*****************************************************************************
189 * CloseAudio: closes the dsp audio device
190 *****************************************************************************/
191 void E_(CloseAudio) ( vlc_object_t *p_this )
193 aout_thread_t * p_aout = (aout_thread_t *)p_this;
195 p_aout->p_sys->p_sound->UnlockCyclic();
196 p_aout->p_sys->p_sound->StopPlaying( );
197 delete p_aout->p_sys->p_sound;
198 free( p_aout->p_sys->p_format );
199 free( p_aout->p_sys );