aout_beos.cpp 8.01 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
Sam Hocevar's avatar
 
Sam Hocevar committed
5
 * $Id: aout_beos.cpp,v 1.23 2002/02/24 22:06:50 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"
{
Sam Hocevar's avatar
 
Sam Hocevar committed
42
#include <videolan/vlc.h>
43 44 45 46 47

#include "audio_output.h"
}

/*****************************************************************************
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 54 55 56 57
 *****************************************************************************/
typedef struct aout_sys_s
{
    BPushGameSound * p_sound;
    gs_audio_format * p_format;
    void * p_buffer;
Sam Hocevar's avatar
 
Sam Hocevar committed
58 59
    int i_buffer_size;
    int i_buffer_pos;
60 61 62 63 64 65 66

} aout_sys_t;

extern "C"
{

/*****************************************************************************
67 68 69 70
 * 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
71
static int     aout_GetBufInfo  ( aout_thread_t *p_aout, int i_buffer_info );
72 73 74 75 76 77 78 79
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
80
void _M( aout_getfunctions )( function_list_t * p_function_list )
81
{
Sam Hocevar's avatar
 
Sam Hocevar committed
82 83 84 85 86
    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;
87 88 89 90
}

/*****************************************************************************
 * aout_Open: opens a BPushGameSound
91
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
92
static int aout_Open( aout_thread_t *p_aout )
93 94 95 96 97
{
    /* Allocate structure */
    p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
    if( p_aout->p_sys == NULL )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
98
        intf_ErrMsg("error: %s", strerror(ENOMEM) );
99 100 101 102 103 104 105 106
        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 );
Sam Hocevar's avatar
 
Sam Hocevar committed
107
        intf_ErrMsg("error: cannot allocate memory for gs_audio_format" );
108 109 110 111 112 113 114 115
        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;
116
    p_aout->p_sys->p_format->buffer_size = 4*8192;
117
    p_aout->p_sys->i_buffer_pos = 0;
118 119 120 121 122 123 124 125 126

    /* 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 );
Sam Hocevar's avatar
 
Sam Hocevar committed
127
        intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
128 129 130 131 132 133 134
        return( 1 );
    }

    if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
    {
        free( p_aout->p_sys->p_format );
        free( p_aout->p_sys );
Sam Hocevar's avatar
 
Sam Hocevar committed
135
        intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
136 137 138 139 140 141 142 143 144 145 146 147
        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 );
}

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

/*****************************************************************************
156
 * aout_GetBufInfo: buffer status query
157
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
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) */
Sam Hocevar's avatar
 
Sam Hocevar committed
161
    int i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
162

163 164
    i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
    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 176
 *****************************************************************************
 * This function writes a buffer of i_length bytes in the dsp
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
177
static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
178
{
Sam Hocevar's avatar
 
Sam Hocevar committed
179
    int i_newbuf_pos;
180 181 182 183 184 185 186 187 188 189

    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),
190
                buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
191 192
                i_size - ( p_aout->p_sys->i_buffer_size
                             - 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 198 199 200
    }
    else
    {
        memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
                buffer, i_size );
201

202 203 204 205 206
        p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
    }
}

/*****************************************************************************
207
 * aout_Close: closes the dsp audio device
208
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
209
static void aout_Close( aout_thread_t *p_aout )
210 211 212 213 214 215 216 217 218 219
{
    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" */