]> git.sesse.net Git - vlc/blob - plugins/beos/aout_beos.cpp
BSD port, including :
[vlc] / plugins / beos / aout_beos.cpp
1 /*****************************************************************************
2  * aout_beos.cpp: beos interface
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  * $Id: aout_beos.cpp,v 1.7 2001/01/05 18:46:43 massiot Exp $
6  *
7  * Authors:
8  * Samuel Hocevar <sam@via.ecp.fr>
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 "defs.h"
29
30 #include <stdio.h>
31 #include <stdlib.h>                                      /* malloc(), free() */
32 #include <kernel/OS.h>
33 #include <View.h>
34 #include <Application.h>
35 #include <Message.h>
36 #include <Locker.h>
37 #include <media/MediaDefs.h>
38 #include <game/PushGameSound.h>
39 #include <malloc.h>
40 #include <string.h>
41
42 extern "C"
43 {
44 #include "config.h"
45 #include "common.h"
46 #include "threads.h"
47 #include "mtime.h"
48 #include "plugins.h"
49
50 #include "audio_output.h"
51
52 #include "intf_msg.h"
53
54 #include "main.h"
55 }
56
57 /*****************************************************************************
58  * aout_sys_t: esd audio output method descriptor
59  *****************************************************************************
60  * This structure is part of the audio output thread descriptor.
61  * It describes some esd specific variables.
62  *****************************************************************************/
63 typedef struct aout_sys_s
64 {
65     BPushGameSound * p_sound;
66     gs_audio_format * p_format;
67     void * p_buffer;
68     long i_buffer_size;
69     long i_buffer_pos;
70
71 } aout_sys_t;
72
73 extern "C"
74 {
75
76 /*****************************************************************************
77  * aout_BeOpen: opens a BPushGameSound
78  *****************************************************************************/
79 int aout_BeOpen( aout_thread_t *p_aout )
80 {
81     /* Allocate structure */
82     p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
83     if( p_aout->p_sys == NULL )
84     {
85         intf_ErrMsg("error: %s", strerror(ENOMEM) );
86         return( 1 );
87     }
88
89     /* Allocate gs_audio_format */
90     p_aout->p_sys->p_format = (gs_audio_format *) malloc( sizeof( gs_audio_format ) );
91     if( p_aout->p_sys->p_format == NULL )
92     {
93         free( p_aout->p_sys );
94         intf_ErrMsg("error: cannot allocate memory for gs_audio_format" );
95         return( 1 );
96     }
97
98     /* Initialize some variables */
99     p_aout->i_format = AOUT_FORMAT_DEFAULT;
100     p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
101                                                   AOUT_STEREO_DEFAULT );
102     p_aout->l_rate = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
103
104     p_aout->p_sys->p_format->frame_rate = 44100.0;
105     p_aout->p_sys->p_format->channel_count = p_aout->i_channels;
106     p_aout->p_sys->p_format->format = gs_audio_format::B_GS_S16;
107     p_aout->p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
108     p_aout->p_sys->p_format->buffer_size = 4*8192;
109     p_aout->p_sys->i_buffer_pos = 0;
110
111     /* Allocate BPushGameSound */
112     p_aout->p_sys->p_sound = new BPushGameSound( 8192,
113                                                  p_aout->p_sys->p_format,
114                                                  2, NULL );
115     if( p_aout->p_sys->p_sound == NULL )
116     {
117         free( p_aout->p_sys->p_format );
118         free( p_aout->p_sys );
119         intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
120         return( 1 );
121     }
122
123     if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
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     p_aout->p_sys->p_sound->StartPlaying( );
132
133     p_aout->p_sys->p_sound->LockForCyclic( &p_aout->p_sys->p_buffer,
134                             (size_t *)&p_aout->p_sys->i_buffer_size );
135
136     return( 0 );
137 }
138 /*****************************************************************************
139  * aout_BeReset: resets the dsp
140  *****************************************************************************/
141 int aout_BeReset( aout_thread_t *p_aout )
142 {
143     return( 0 );
144 }
145
146 /*****************************************************************************
147  * aout_BeSetFormat: sets the dsp output format
148  *****************************************************************************/
149 int aout_BeSetFormat( aout_thread_t *p_aout )
150 {
151     return( 0 );
152 }
153
154 /*****************************************************************************
155  * aout_BeSetChannels: sets the dsp's stereo or mono mode
156  *****************************************************************************/
157 int aout_BeSetChannels( aout_thread_t *p_aout )
158 {
159     return( 0 );
160 }
161
162 /*****************************************************************************
163  * aout_BeSetRate: sets the dsp's audio output rate
164  *****************************************************************************/
165 int aout_BeSetRate( aout_thread_t *p_aout )
166 {
167     return( 0 );
168 }
169
170 /*****************************************************************************
171  * aout_BeGetBufInfo: buffer status query
172  *****************************************************************************/
173 long aout_BeGetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
174 {
175     /* Each value is 4 bytes long (stereo signed 16 bits) */
176     long i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
177
178     i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
179     if( i_hard_pos < 0 )
180     {
181          i_hard_pos += p_aout->p_sys->i_buffer_size;
182     }
183
184     return( i_hard_pos );
185 }
186
187 /*****************************************************************************
188  * aout_BePlaySamples: plays a sound samples buffer
189  *****************************************************************************
190  * This function writes a buffer of i_length bytes in the dsp
191  *****************************************************************************/
192 void aout_BePlaySamples( aout_thread_t *p_aout, byte_t *buffer, int i_size )
193 {
194     long i_newbuf_pos;
195
196     if( (i_newbuf_pos = p_aout->p_sys->i_buffer_pos + i_size)
197               > p_aout->p_sys->i_buffer_size )
198     {
199         memcpy( (void *)((int)p_aout->p_sys->p_buffer
200                         + p_aout->p_sys->i_buffer_pos),
201                 buffer,
202                 p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos );
203
204         memcpy( (void *)((int)p_aout->p_sys->p_buffer),
205                 buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
206                 i_size - ( p_aout->p_sys->i_buffer_size
207                              - p_aout->p_sys->i_buffer_pos ) );
208         
209         p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
210
211     }
212     else
213     {
214         memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
215                 buffer, i_size );
216
217         p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
218     }
219 }
220
221 /*****************************************************************************
222  * aout_BeClose: closes the dsp audio device
223  *****************************************************************************/
224 void aout_BeClose( aout_thread_t *p_aout )
225 {
226     p_aout->p_sys->p_sound->UnlockCyclic();
227     p_aout->p_sys->p_sound->StopPlaying( );
228     delete p_aout->p_sys->p_sound;
229     free( p_aout->p_sys->p_format );
230     free( p_aout->p_sys );
231 }
232
233 } /* extern "C" */
234