aout_beos.cpp 7.92 KB
Newer Older
1
/*****************************************************************************
2
 * aout_beos.cpp: BeOS audio output
3
 *****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
4
 * Copyright (C) 1999, 2000, 2001 VideoLAN
5
 * $Id: aout_beos.cpp,v 1.25 2002/07/20 18:01:42 sam Exp $
6
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
7 8
 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdio.h>
#include <stdlib.h>                                      /* malloc(), free() */
#include <kernel/OS.h>
#include <View.h>
#include <Application.h>
#include <Message.h>
#include <Locker.h>
#include <media/MediaDefs.h>
#include <game/PushGameSound.h>
#include <malloc.h>
#include <string.h>

extern "C"
{
42 43
#include <vlc/vlc.h>
#include <vlc/aout.h>
44 45 46
}

/*****************************************************************************
47
 * aout_sys_t: BeOS audio output method descriptor
48 49
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
50
 * It describes some BeOS specific variables.
51
 *****************************************************************************/
52
struct aout_sys_t
53 54 55 56
{
    BPushGameSound * p_sound;
    gs_audio_format * p_format;
    void * p_buffer;
Sam Hocevar's avatar
 
Sam Hocevar committed
57 58
    int i_buffer_size;
    int i_buffer_pos;
59
};
60 61 62 63 64

extern "C"
{

/*****************************************************************************
65 66 67 68
 * Local prototypes.
 *****************************************************************************/
static int     aout_Open        ( aout_thread_t *p_aout );
static int     aout_SetFormat   ( aout_thread_t *p_aout );
Sam Hocevar's avatar
 
Sam Hocevar committed
69
static int     aout_GetBufInfo  ( aout_thread_t *p_aout, int i_buffer_info );
70 71 72 73 74 75 76 77
static void    aout_Play        ( aout_thread_t *p_aout,
                                  byte_t *buffer, int i_size );
static void    aout_Close       ( aout_thread_t *p_aout );

/*****************************************************************************
 * Functions exported as capabilities. They are declared as static so that
 * we don't pollute the namespace too much.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
78
void _M( aout_getfunctions )( function_list_t * p_function_list )
79
{
Sam Hocevar's avatar
 
Sam Hocevar committed
80 81 82 83 84
    p_function_list->functions.aout.pf_open = aout_Open;
    p_function_list->functions.aout.pf_setformat = aout_SetFormat;
    p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
    p_function_list->functions.aout.pf_play = aout_Play;
    p_function_list->functions.aout.pf_close = aout_Close;
85 86 87 88
}

/*****************************************************************************
 * aout_Open: opens a BPushGameSound
89
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
90
static int aout_Open( aout_thread_t *p_aout )
91 92 93 94 95
{
    /* Allocate structure */
    p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
    if( p_aout->p_sys == NULL )
    {
96
        msg_Err( p_aout, "out of memory" );
97 98 99 100 101 102 103 104
        return( 1 );
    }

    /* Allocate gs_audio_format */
    p_aout->p_sys->p_format = (gs_audio_format *) malloc( sizeof( gs_audio_format ) );
    if( p_aout->p_sys->p_format == NULL )
    {
        free( p_aout->p_sys );
105
        msg_Err( p_aout, "out of memory" );
106 107 108 109 110 111 112 113
        return( 1 );
    }

    /* Initialize some variables */
    p_aout->p_sys->p_format->frame_rate = 44100.0;
    p_aout->p_sys->p_format->channel_count = p_aout->i_channels;
    p_aout->p_sys->p_format->format = gs_audio_format::B_GS_S16;
    p_aout->p_sys->p_format->byte_order = B_MEDIA_LITTLE_ENDIAN;
114
    p_aout->p_sys->p_format->buffer_size = 4*8192;
115
    p_aout->p_sys->i_buffer_pos = 0;
116 117 118 119 120 121 122 123 124

    /* Allocate BPushGameSound */
    p_aout->p_sys->p_sound = new BPushGameSound( 8192,
                                                 p_aout->p_sys->p_format,
                                                 2, NULL );
    if( p_aout->p_sys->p_sound == NULL )
    {
        free( p_aout->p_sys->p_format );
        free( p_aout->p_sys );
125
        msg_Err( p_aout, "cannot allocate BPushGameSound" );
126 127 128 129 130 131 132
        return( 1 );
    }

    if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
    {
        free( p_aout->p_sys->p_format );
        free( p_aout->p_sys );
133
        msg_Err( p_aout, "cannot initialize BPushGameSound" );
134 135 136 137 138 139 140 141 142 143 144 145
        return( 1 );
    }

    p_aout->p_sys->p_sound->StartPlaying( );

    p_aout->p_sys->p_sound->LockForCyclic( &p_aout->p_sys->p_buffer,
                            (size_t *)&p_aout->p_sys->i_buffer_size );

    return( 0 );
}

/*****************************************************************************
146
 * aout_SetFormat: sets the dsp output format
147
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
148
static int aout_SetFormat( aout_thread_t *p_aout )
149 150 151 152 153
{
    return( 0 );
}

/*****************************************************************************
154
 * aout_GetBufInfo: buffer status query
155
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
156
static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
157
{
158
    /* Each value is 4 bytes long (stereo signed 16 bits) */
Sam Hocevar's avatar
 
Sam Hocevar committed
159
    int i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
160

161 162
    i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
    if( i_hard_pos < 0 )
163
    {
164
         i_hard_pos += p_aout->p_sys->i_buffer_size;
165 166
    }

167
    return( i_hard_pos );
168 169 170
}

/*****************************************************************************
171
 * aout_Play: plays a sound samples buffer
172 173 174
 *****************************************************************************
 * This function writes a buffer of i_length bytes in the dsp
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
175
static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
176
{
Sam Hocevar's avatar
 
Sam Hocevar committed
177
    int i_newbuf_pos;
178 179 180 181 182 183 184 185 186 187

    if( (i_newbuf_pos = p_aout->p_sys->i_buffer_pos + i_size)
              > p_aout->p_sys->i_buffer_size )
    {
        memcpy( (void *)((int)p_aout->p_sys->p_buffer
                        + p_aout->p_sys->i_buffer_pos),
                buffer,
                p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos );

        memcpy( (void *)((int)p_aout->p_sys->p_buffer),
188
                buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
189 190
                i_size - ( p_aout->p_sys->i_buffer_size
                             - p_aout->p_sys->i_buffer_pos ) );
191
        
192
        p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
193

194 195 196 197 198
    }
    else
    {
        memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
                buffer, i_size );
199

200 201 202 203 204
        p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
    }
}

/*****************************************************************************
205
 * aout_Close: closes the dsp audio device
206
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
207
static void aout_Close( aout_thread_t *p_aout )
208 209 210 211 212 213 214 215 216 217
{
    p_aout->p_sys->p_sound->UnlockCyclic();
    p_aout->p_sys->p_sound->StopPlaying( );
    delete p_aout->p_sys->p_sound;
    free( p_aout->p_sys->p_format );
    free( p_aout->p_sys );
}

} /* extern "C" */