aout_dsp.c 9.33 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
/*****************************************************************************
 * aout_dsp.c : dsp functions library
 *****************************************************************************
 * Copyright (C) 1999, 2000 VideoLAN
 *
 * Authors:
 *
 * 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.
 *****************************************************************************/

/* TODO:
 *
Sam Hocevar's avatar
Sam Hocevar committed
25
 * - an aout_DspGetFormats() function
26 27
 * - dsp inline/static
 * - make this library portable (see mpg123)
Sam Hocevar's avatar
Sam Hocevar committed
28
 * - macroify aout_DspPlaySamples &/| aout_DspGetBufInfo ?
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
 *
 */

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include "defs.h"

#include <errno.h>                                                 /* ENOMEM */
#include <fcntl.h>                                       /* open(), O_WRONLY */
#include <sys/ioctl.h>                                            /* ioctl() */
#include <string.h>                                            /* strerror() */
#include <unistd.h>                                      /* write(), close() */
#include <stdio.h>                                           /* "intf_msg.h" */
#include <stdlib.h>                            /* calloc(), malloc(), free() */

#ifdef SYS_BSD
#include <machine/soundcard.h>       /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT,
                   SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED, SNDCTL_DSP_GETOSPACE */
#else
#include <sys/soundcard.h>           /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT,
                   SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED, SNDCTL_DSP_GETOSPACE */
#endif

#include "config.h"
#include "common.h"                                     /* boolean_t, byte_t */
#include "threads.h"
#include "mtime.h"
#include "plugins.h"

#include "audio_output.h"                                   /* aout_thread_t */

#include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
#include "main.h"

/*****************************************************************************
 * vout_dsp_t: dsp audio output method descriptor
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
 * It describes the dsp specific properties of an audio device.
 *****************************************************************************/
typedef struct aout_sys_s
{
    audio_buf_info        audio_buf;

} aout_sys_t;

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
77
 * aout_DspOpen: opens the audio device (the digital sound processor)
78 79 80 81
 *****************************************************************************
 * - This function opens the dsp as an usual non-blocking write-only file, and
 *   modifies the p_aout->p_sys->i_fd with the file's descriptor.
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
82
int aout_DspOpen( aout_thread_t *p_aout )
83 84 85 86 87 88 89 90 91 92
{
    /* Allocate structure */
    p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
    if( p_aout->p_sys == NULL )
    {
        intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
        return( 1 );
    }

    /* Initialize some variables */
93
    p_aout->i_format = AOUT_FORMAT_DEFAULT;
94 95 96 97 98
    p_aout->psz_device = main_GetPszVariable( AOUT_DSP_VAR, AOUT_DSP_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 );

    /* Open the sound device */
99
    if ( (p_aout->i_fd = open( p_aout->psz_device, O_WRONLY|O_NONBLOCK )) < 0 )
100 101 102 103 104 105 106 107 108
    {
        intf_ErrMsg( "aout error: can't open audio device (%s)\n", p_aout->psz_device );
        return( -1 );
    }

    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
109
 * aout_DspReset: resets the dsp
110
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
111
int aout_DspReset( aout_thread_t *p_aout )
112 113 114 115 116 117 118 119 120 121 122
{
    if ( ioctl( p_aout->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
    {
        intf_ErrMsg( "aout error: can't reset audio device (%s)\n", p_aout->psz_device );
    return( -1 );
    }

    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
123
 * aout_DspSetFormat: sets the dsp output format
124 125 126 127 128
 *****************************************************************************
 * This functions tries to initialize the dsp output format with the value
 * contained in the dsp structure, and if this value could not be set, the
 * default value returned by ioctl is set.
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
129
int aout_DspSetFormat( aout_thread_t *p_aout )
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
{
    int i_format;

    i_format = p_aout->i_format;
    if ( ioctl( p_aout->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
    {
        intf_ErrMsg( "aout error: can't set audio output format (%i)\n", p_aout->i_format );
        return( -1 );
    }

    if ( i_format != p_aout->i_format )
    {
        intf_DbgMsg( "aout debug: audio output format not supported (%i)\n", p_aout->i_format );
        p_aout->i_format = i_format;
    }

    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
150
 * aout_DspSetChannels: sets the dsp's stereo or mono mode
151 152 153
 *****************************************************************************
 * This function acts just like the previous one...
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
154
int aout_DspSetChannels( aout_thread_t *p_aout )
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
{
    boolean_t b_stereo = p_aout->b_stereo;

    if ( ioctl( p_aout->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
    {
        intf_ErrMsg( "aout error: can't set number of audio channels (%i)\n", p_aout->i_channels );
        return( -1 );
    }

    if ( b_stereo != p_aout->b_stereo )
    {
        intf_DbgMsg( "aout debug: number of audio channels not supported (%i)\n", p_aout->i_channels );
        p_aout->b_stereo = b_stereo;
        p_aout->i_channels = 1 + b_stereo;
    }

    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
175
 * aout_DspSetRate: sets the dsp's audio output rate
176 177 178 179 180
 *****************************************************************************
 * This function tries to initialize the dsp with the rate contained in the
 * dsp structure, but if the dsp doesn't support this value, the function uses
 * the value returned by ioctl...
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
181
int aout_DspSetRate( aout_thread_t *p_aout )
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
{
    long l_rate;

    l_rate = p_aout->l_rate;
    if ( ioctl( p_aout->i_fd, SNDCTL_DSP_SPEED, &l_rate ) < 0 )
    {
        intf_ErrMsg( "aout error: can't set audio output rate (%li)\n", p_aout->l_rate );
        return( -1 );
    }

    if ( l_rate != p_aout->l_rate )
    {
        intf_DbgMsg( "aout debug: audio output rate not supported (%li)\n", p_aout->l_rate );
        p_aout->l_rate = l_rate;
    }

    return( 0 );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
202
 * aout_DspGetBufInfo: buffer status query
203 204 205 206 207 208 209 210 211
 *****************************************************************************
 * This function fills in the audio_buf_info structure :
 * - int fragments : number of available fragments (partially usend ones not
 *   counted)
 * - int fragstotal : total number of fragments allocated
 * - int fragsize : size of a fragment in bytes
 * - int bytes : available space in bytes (includes partially used fragments)
 * Note! 'bytes' could be more than fragments*fragsize
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
212
long aout_DspGetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
213 214 215 216 217 218 219 220 221 222
{
    ioctl( p_aout->i_fd, SNDCTL_DSP_GETOSPACE, &p_aout->p_sys->audio_buf );

    /* returns the allocated space in bytes */
    return ( (p_aout->p_sys->audio_buf.fragstotal
                 * p_aout->p_sys->audio_buf.fragsize)
            - p_aout->p_sys->audio_buf.bytes );
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
223
 * aout_DspPlaySamples: plays a sound samples buffer
224 225 226
 *****************************************************************************
 * This function writes a buffer of i_length bytes in the dsp
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
227
void aout_DspPlaySamples( aout_thread_t *p_aout, byte_t *buffer, int i_size )
228 229 230 231 232 233 234 235
{
    if( p_aout->b_active )
    {
        write( p_aout->i_fd, buffer, i_size );
    }
}

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
236
 * aout_DspClose: closes the dsp audio device
237
 *****************************************************************************/
Sam Hocevar's avatar
Sam Hocevar committed
238
void aout_DspClose( aout_thread_t *p_aout )
239 240 241 242
{
    close( p_aout->i_fd );
}