]> git.sesse.net Git - vlc/blob - plugins/beos/aout_beos.cpp
* Fixed a quite old bug in the audio output which made the sound stutter
[vlc] / plugins / beos / aout_beos.cpp
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 $
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
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.
14  * 
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.
19  *
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  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdio.h>
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <kernel/OS.h>
31 #include <View.h>
32 #include <Application.h>
33 #include <Message.h>
34 #include <Locker.h>
35 #include <media/MediaDefs.h>
36 #include <game/PushGameSound.h>
37 #include <malloc.h>
38 #include <string.h>
39
40 extern "C"
41 {
42 #include <videolan/vlc.h>
43
44 #include "audio_output.h"
45 }
46
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
54 {
55     BPushGameSound * p_sound;
56     gs_audio_format * p_format;
57     void * p_buffer;
58     int i_buffer_size;
59     int i_buffer_pos;
60
61 } aout_sys_t;
62
63 extern "C"
64 {
65
66 /*****************************************************************************
67  * Local prototypes.
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 );
75
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 )
81 {
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;
87 }
88
89 /*****************************************************************************
90  * aout_Open: opens a BPushGameSound
91  *****************************************************************************/
92 static int aout_Open( aout_thread_t *p_aout )
93 {
94     /* Allocate structure */
95     p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
96     if( p_aout->p_sys == NULL )
97     {
98         intf_ErrMsg("error: %s", strerror(ENOMEM) );
99         return( 1 );
100     }
101
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 )
105     {
106         free( p_aout->p_sys );
107         intf_ErrMsg("error: cannot allocate memory for gs_audio_format" );
108         return( 1 );
109     }
110
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;
118
119     /* Allocate BPushGameSound */
120     p_aout->p_sys->p_sound = new BPushGameSound( 8192,
121                                                  p_aout->p_sys->p_format,
122                                                  2, NULL );
123     if( p_aout->p_sys->p_sound == NULL )
124     {
125         free( p_aout->p_sys->p_format );
126         free( p_aout->p_sys );
127         intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
128         return( 1 );
129     }
130
131     if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
132     {
133         free( p_aout->p_sys->p_format );
134         free( p_aout->p_sys );
135         intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
136         return( 1 );
137     }
138
139     p_aout->p_sys->p_sound->StartPlaying( );
140
141     p_aout->p_sys->p_sound->LockForCyclic( &p_aout->p_sys->p_buffer,
142                             (size_t *)&p_aout->p_sys->i_buffer_size );
143
144     return( 0 );
145 }
146
147 /*****************************************************************************
148  * aout_SetFormat: sets the dsp output format
149  *****************************************************************************/
150 static int aout_SetFormat( aout_thread_t *p_aout )
151 {
152     return( 0 );
153 }
154
155 /*****************************************************************************
156  * aout_GetBufInfo: buffer status query
157  *****************************************************************************/
158 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
159 {
160     /* Each value is 4 bytes long (stereo signed 16 bits) */
161     int i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
162
163     i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
164     if( i_hard_pos < 0 )
165     {
166          i_hard_pos += p_aout->p_sys->i_buffer_size;
167     }
168
169     return( i_hard_pos );
170 }
171
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 )
178 {
179     int i_newbuf_pos;
180
181     if( (i_newbuf_pos = p_aout->p_sys->i_buffer_pos + i_size)
182               > p_aout->p_sys->i_buffer_size )
183     {
184         memcpy( (void *)((int)p_aout->p_sys->p_buffer
185                         + p_aout->p_sys->i_buffer_pos),
186                 buffer,
187                 p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos );
188
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 ) );
193         
194         p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
195
196     }
197     else
198     {
199         memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
200                 buffer, i_size );
201
202         p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
203     }
204 }
205
206 /*****************************************************************************
207  * aout_Close: closes the dsp audio device
208  *****************************************************************************/
209 static void aout_Close( aout_thread_t *p_aout )
210 {
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 );
216 }
217
218 } /* extern "C" */
219