aout_beos.cpp 8.07 KB
Newer Older
1 2 3 4 5 6 7 8 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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
/*****************************************************************************
 * aout_beos.cpp: beos interface
 *****************************************************************************
 * Copyright (C) 1999, 2000 VideoLAN
 *
 * Authors:
 * Samuel Hocevar <sam@via.ecp.fr>
 *
 * 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 "defs.h"

#include <stdio.h>
#include <stdlib.h>                                      /* malloc(), free() */
#include <sys/types.h>                        /* on BSD, uio.h needs types.h */
#include <sys/uio.h>                                            /* "input.h" */
#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"
{
#include "config.h"
#include "common.h"
#include "threads.h"
#include "mtime.h"
#include "plugins.h"

#include "audio_output.h"

#include "intf_msg.h"

#include "main.h"
}

/*****************************************************************************
 * aout_sys_t: esd audio output method descriptor
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
 * It describes some esd specific variables.
 *****************************************************************************/
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"
{

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
78
 * aout_BeOpen: opens a BPushGameSound
79
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
80
int aout_BeOpen( aout_thread_t *p_aout )
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
{
    /* Allocate structure */
    p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
    if( p_aout->p_sys == NULL )
    {
        intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
        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 );
        intf_ErrMsg("error: cannot allocate memory for gs_audio_format\n" );
        return( 1 );
    }

    /* Initialize some variables */
100
    p_aout->i_format = AOUT_FORMAT_DEFAULT;
101 102 103 104 105 106 107 108
    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 );

    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;
109
    p_aout->p_sys->p_format->buffer_size = 4*8192;
110
    p_aout->p_sys->i_buffer_pos = 0;
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

    /* 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 );
        intf_ErrMsg("error: cannot allocate memory for BPushGameSound\n" );
        return( 1 );
    }

    if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
    {
        free( p_aout->p_sys->p_format );
        free( p_aout->p_sys );
        intf_ErrMsg("error: cannot allocate memory for BPushGameSound\n" );
        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 );
}
/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
140
 * aout_BeReset: resets the dsp
141
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
142
int aout_BeReset( aout_thread_t *p_aout )
143 144 145 146 147
{
    return( 0 );
}

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

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
156
 * aout_BeSetChannels: sets the dsp's stereo or mono mode
157
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
158
int aout_BeSetChannels( aout_thread_t *p_aout )
159 160 161 162 163
{
    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
164
 * aout_BeSetRate: sets the dsp's audio output rate
165
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
166
int aout_BeSetRate( aout_thread_t *p_aout )
167 168 169 170 171
{
    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
172
 * aout_BeGetBufInfo: buffer status query
173
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
174
long aout_BeGetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
175
{
176
    /* Each value is 4 bytes long (stereo signed 16 bits) */
177 178
    long i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();

179 180
    i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
    if( i_hard_pos < 0 )
181
    {
182
         i_hard_pos += p_aout->p_sys->i_buffer_size;
183 184
    }

185
    return( i_hard_pos );
186 187 188
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
189
 * aout_BePlaySamples: plays a sound samples buffer
190 191 192
 *****************************************************************************
 * This function writes a buffer of i_length bytes in the dsp
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
193
void aout_BePlaySamples( aout_thread_t *p_aout, byte_t *buffer, int i_size )
194 195 196 197 198 199 200 201 202 203 204 205
{
    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),
206
                buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
207 208
                i_size - ( p_aout->p_sys->i_buffer_size
                             - p_aout->p_sys->i_buffer_pos ) );
209
        
210
        p_aout->p_sys->i_buffer_pos = i_newbuf_pos - p_aout->p_sys->i_buffer_size;
211

212 213 214 215 216
    }
    else
    {
        memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
                buffer, i_size );
217

218 219 220 221 222
        p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
    }
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
223
 * aout_BeClose: closes the dsp audio device
224
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
225
void aout_BeClose( aout_thread_t *p_aout )
226 227 228 229 230 231 232 233 234 235
{
    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" */