]> git.sesse.net Git - vlc/blob - plugins/beos/aout_beos.cpp
* ALL: new module API. Makes a few things a lot simpler, and we gain
[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.26 2002/07/31 20:56: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 #include <vlc/vlc.h>
41 #include <vlc/aout.h>
42
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  *****************************************************************************/
49 struct aout_sys_t
50 {
51     BPushGameSound * p_sound;
52     gs_audio_format * p_format;
53     void * p_buffer;
54     int i_buffer_size;
55     int i_buffer_pos;
56 };
57
58 /*****************************************************************************
59  * Local prototypes.
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 );
64
65 /*****************************************************************************
66  * OpenAudio: opens a BPushGameSound
67  *****************************************************************************/
68 int E_(OpenAudio) ( vlc_object_t *p_this )
69 {       
70     aout_thread_t * p_aout = (aout_thread_t *)p_this;
71
72     /* Allocate structure */
73     p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
74     if( p_aout->p_sys == NULL )
75     {
76         msg_Err( p_aout, "out of memory" );
77         return( 1 );
78     }
79
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 )
83     {
84         free( p_aout->p_sys );
85         msg_Err( p_aout, "out of memory" );
86         return( 1 );
87     }
88
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;
96
97     p_aout->pf_setformat = SetFormat;
98     p_aout->pf_getbufinfo = GetBufInfo;
99     p_aout->pf_play = Play;
100
101     /* Allocate BPushGameSound */
102     p_aout->p_sys->p_sound = new BPushGameSound( 8192,
103                                                  p_aout->p_sys->p_format,
104                                                  2, NULL );
105     if( p_aout->p_sys->p_sound == NULL )
106     {
107         free( p_aout->p_sys->p_format );
108         free( p_aout->p_sys );
109         msg_Err( p_aout, "cannot allocate BPushGameSound" );
110         return( 1 );
111     }
112
113     if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
114     {
115         free( p_aout->p_sys->p_format );
116         free( p_aout->p_sys );
117         msg_Err( p_aout, "cannot initialize BPushGameSound" );
118         return( 1 );
119     }
120
121     p_aout->p_sys->p_sound->StartPlaying( );
122
123     p_aout->p_sys->p_sound->LockForCyclic( &p_aout->p_sys->p_buffer,
124                             (size_t *)&p_aout->p_sys->i_buffer_size );
125
126     return( 0 );
127 }
128
129 /*****************************************************************************
130  * SetFormat: sets the dsp output format
131  *****************************************************************************/
132 static int SetFormat( aout_thread_t *p_aout )
133 {
134     return( 0 );
135 }
136
137 /*****************************************************************************
138  * GetBufInfo: buffer status query
139  *****************************************************************************/
140 static int GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
141 {
142     /* Each value is 4 bytes long (stereo signed 16 bits) */
143     int i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
144
145     i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
146     if( i_hard_pos < 0 )
147     {
148          i_hard_pos += p_aout->p_sys->i_buffer_size;
149     }
150
151     return( i_hard_pos );
152 }
153
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 )
160 {
161     int i_newbuf_pos;
162
163     if( (i_newbuf_pos = p_aout->p_sys->i_buffer_pos + i_size)
164               > p_aout->p_sys->i_buffer_size )
165     {
166         memcpy( (void *)((int)p_aout->p_sys->p_buffer
167                         + p_aout->p_sys->i_buffer_pos),
168                 buffer,
169                 p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos );
170
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 ) );
175         
176         p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
177
178     }
179     else
180     {
181         memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
182                 buffer, i_size );
183
184         p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
185     }
186 }
187
188 /*****************************************************************************
189  * CloseAudio: closes the dsp audio device
190  *****************************************************************************/
191 void E_(CloseAudio) ( vlc_object_t *p_this )
192 {       
193     aout_thread_t * p_aout = (aout_thread_t *)p_this;
194
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 );
200 }
201