aout_beos.cpp 8.27 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.21 2002/02/15 13:32:52 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 58 59 60 61 62 63 64 65 66
 *****************************************************************************/
typedef struct aout_sys_s
{
    BPushGameSound * p_sound;
    gs_audio_format * p_format;
    void * p_buffer;
    long i_buffer_size;
    long i_buffer_pos;

} aout_sys_t;

extern "C"
{

/*****************************************************************************
67 68 69 70 71 72 73 74 75 76 77 78 79
 * Local prototypes.
 *****************************************************************************/
static int     aout_Open        ( aout_thread_t *p_aout );
static int     aout_SetFormat   ( aout_thread_t *p_aout );
static long    aout_GetBufInfo  ( aout_thread_t *p_aout, long l_buffer_info );
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
        return( 1 );
    }

    /* Initialize some variables */
Sam Hocevar's avatar
 
Sam Hocevar committed
112 113 114 115 116
    p_aout->i_format = AOUT_FORMAT_DEFAULT;
    p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
                                                  AOUT_STEREO_DEFAULT );
    p_aout->l_rate = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );

117 118 119 120
    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;
121
    p_aout->p_sys->p_format->buffer_size = 4*8192;
122
    p_aout->p_sys->i_buffer_pos = 0;
123 124 125 126 127 128 129 130 131

    /* 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
132
        intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
133 134 135 136 137 138 139
        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
140
        intf_ErrMsg("error: cannot allocate memory for BPushGameSound" );
141 142 143 144 145 146 147 148 149 150 151 152
        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 );
}

/*****************************************************************************
153
 * aout_SetFormat: sets the dsp output format
154
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
155
static int aout_SetFormat( aout_thread_t *p_aout )
156 157 158 159 160
{
    return( 0 );
}

/*****************************************************************************
161
 * aout_GetBufInfo: buffer status query
162
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
163
static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
164
{
165
    /* Each value is 4 bytes long (stereo signed 16 bits) */
166 167
    long i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();

168 169
    i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
    if( i_hard_pos < 0 )
170
    {
171
         i_hard_pos += p_aout->p_sys->i_buffer_size;
172 173
    }

174
    return( i_hard_pos );
175 176 177
}

/*****************************************************************************
178
 * aout_Play: plays a sound samples buffer
179 180 181
 *****************************************************************************
 * This function writes a buffer of i_length bytes in the dsp
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
182
static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
183 184 185 186 187 188 189 190 191 192 193 194
{
    long i_newbuf_pos;

    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),
195
                buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
196 197
                i_size - ( p_aout->p_sys->i_buffer_size
                             - p_aout->p_sys->i_buffer_pos ) );
198
        
199
        p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
200

201 202 203 204 205
    }
    else
    {
        memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
                buffer, i_size );
206

207 208 209 210 211
        p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
    }
}

/*****************************************************************************
212
 * aout_Close: closes the dsp audio device
213
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
214
static void aout_Close( aout_thread_t *p_aout )
215 216 217 218 219 220 221 222 223 224
{
    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" */