aout_beos.cpp 7.21 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.26 2002/07/31 20:56: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
 *
 * 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>

40 41
#include <vlc/vlc.h>
#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 52 53
{
    BPushGameSound * p_sound;
    gs_audio_format * p_format;
    void * p_buffer;
Sam Hocevar's avatar
 
Sam Hocevar committed
54 55
    int i_buffer_size;
    int i_buffer_pos;
56
};
57 58

/*****************************************************************************
59 60
 * Local prototypes.
 *****************************************************************************/
61 62 63
static int     SetFormat   ( aout_thread_t * );
static int     GetBufInfo  ( aout_thread_t *, int );
static void    Play        ( aout_thread_t *, byte_t *, int );
64 65

/*****************************************************************************
66
 * OpenAudio: opens a BPushGameSound
67
 *****************************************************************************/
68 69 70
int E_(OpenAudio) ( vlc_object_t *p_this )
{       
    aout_thread_t * p_aout = (aout_thread_t *)p_this;
71

72 73 74 75
    /* Allocate structure */
    p_aout->p_sys = (aout_sys_t*) malloc( sizeof( aout_sys_t ) );
    if( p_aout->p_sys == NULL )
    {
76
        msg_Err( p_aout, "out of memory" );
77 78 79 80 81 82 83 84
        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 );
85
        msg_Err( p_aout, "out of memory" );
86 87 88 89 90 91 92 93
        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;
94
    p_aout->p_sys->p_format->buffer_size = 4*8192;
95
    p_aout->p_sys->i_buffer_pos = 0;
96

97 98 99 100
    p_aout->pf_setformat = SetFormat;
    p_aout->pf_getbufinfo = GetBufInfo;
    p_aout->pf_play = Play;

101 102 103 104 105 106 107 108
    /* 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 );
109
        msg_Err( p_aout, "cannot allocate BPushGameSound" );
110 111 112 113 114 115 116
        return( 1 );
    }

    if( p_aout->p_sys->p_sound->InitCheck() != B_OK )
    {
        free( p_aout->p_sys->p_format );
        free( p_aout->p_sys );
117
        msg_Err( p_aout, "cannot initialize BPushGameSound" );
118 119 120 121 122 123 124 125 126 127 128 129
        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 );
}

/*****************************************************************************
130
 * SetFormat: sets the dsp output format
131
 *****************************************************************************/
132
static int SetFormat( aout_thread_t *p_aout )
133 134 135 136 137
{
    return( 0 );
}

/*****************************************************************************
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) */
Sam Hocevar's avatar
 
Sam Hocevar committed
143
    int i_hard_pos = 4 * p_aout->p_sys->p_sound->CurrentPosition();
144

145 146
    i_hard_pos = p_aout->p_sys->i_buffer_pos - i_hard_pos;
    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 158
 *****************************************************************************
 * This function writes a buffer of i_length bytes in the dsp
 *****************************************************************************/
159
static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
160
{
Sam Hocevar's avatar
 
Sam Hocevar committed
161
    int i_newbuf_pos;
162 163 164 165 166 167 168 169 170 171

    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),
172
                buffer + p_aout->p_sys->i_buffer_size - p_aout->p_sys->i_buffer_pos,
173 174
                i_size - ( p_aout->p_sys->i_buffer_size
                             - 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 180 181 182
    }
    else
    {
        memcpy( (void *)((int)p_aout->p_sys->p_buffer + p_aout->p_sys->i_buffer_pos),
                buffer, i_size );
183

184 185 186 187 188
        p_aout->p_sys->i_buffer_pos = i_newbuf_pos;
    }
}

/*****************************************************************************
189
 * CloseAudio: closes the dsp audio device
190
 *****************************************************************************/
191 192 193 194
void E_(CloseAudio) ( vlc_object_t *p_this )
{       
    aout_thread_t * p_aout = (aout_thread_t *)p_this;

195 196 197 198 199 200 201
    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 );
}