1 /*****************************************************************************
2 * aout_esd.c : Esound functions library
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
25 * - use the libesd function to get latency when it's not buggy anymore
29 /*****************************************************************************
31 *****************************************************************************/
34 #include <errno.h> /* ENOMEM */
35 #include <fcntl.h> /* open(), O_WRONLY */
36 #include <string.h> /* strerror() */
37 #include <unistd.h> /* write(), close() */
38 #include <stdio.h> /* "intf_msg.h" */
39 #include <stdlib.h> /* calloc(), malloc(), free() */
44 #include "common.h" /* boolean_t, byte_t */
49 #include "audio_output.h" /* aout_thread_t */
51 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
54 /*****************************************************************************
55 * aout_sys_t: esd audio output method descriptor
56 *****************************************************************************
57 * This structure is part of the audio output thread descriptor.
58 * It describes some esd specific variables.
59 *****************************************************************************/
60 typedef struct aout_sys_s
62 esd_format_t esd_format;
66 /*****************************************************************************
67 * aout_SysOpen: opens an esd socket
68 *****************************************************************************/
69 int aout_SysOpen( aout_thread_t *p_aout )
71 /* mpg123 does it this way */
72 int i_bits = ESD_BITS16;
73 int i_mode = ESD_STREAM;
74 int i_func = ESD_PLAY;
76 /* Allocate structure */
77 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
78 if( p_aout->p_sys == NULL )
80 intf_ErrMsg("error: %s\n", strerror(ENOMEM) );
84 /* Initialize some variables */
85 p_aout->i_format = AOUT_DEFAULT_FORMAT;
86 p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR, AOUT_STEREO_DEFAULT );
87 p_aout->l_rate = main_GetIntVariable( AOUT_RATE_VAR, AOUT_RATE_DEFAULT );
92 p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
94 if( p_aout->i_channels == 1 )
95 p_aout->p_sys->esd_format |= ESD_MONO;
97 p_aout->p_sys->esd_format |= ESD_STEREO;
99 /* open a socket for playing a stream
100 * and try to open /dev/dsp if there's no EsounD */
102 = esd_play_stream_fallback(p_aout->p_sys->esd_format,
103 p_aout->l_rate, NULL, "vlc")) < 0 )
105 intf_ErrMsg( "aout error: can't open esound socket"
106 " (format 0x%08x at %ld Hz)\n",
107 p_aout->p_sys->esd_format, p_aout->l_rate );
114 /*****************************************************************************
115 * aout_SysReset: resets the dsp
116 *****************************************************************************/
117 int aout_SysReset( aout_thread_t *p_aout )
122 /*****************************************************************************
123 * aout_SysSetFormat: sets the dsp output format
124 *****************************************************************************/
125 int aout_SysSetFormat( aout_thread_t *p_aout )
130 /*****************************************************************************
131 * aout_SysSetChannels: sets the dsp's stereo or mono mode
132 *****************************************************************************/
133 int aout_SysSetChannels( aout_thread_t *p_aout )
138 /*****************************************************************************
139 * aout_SysSetRate: sets the dsp's audio output rate
140 *****************************************************************************/
141 int aout_SysSetRate( aout_thread_t *p_aout )
146 /*****************************************************************************
147 * aout_SysGetBufInfo: buffer status query
148 *****************************************************************************/
149 long aout_SysGetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
151 /* arbitrary value that should be changed */
152 return( l_buffer_limit );
155 /*****************************************************************************
156 * aout_SysPlaySamples: plays a sound samples buffer
157 *****************************************************************************
158 * This function writes a buffer of i_length bytes in the dsp
159 *****************************************************************************/
160 void aout_SysPlaySamples( aout_thread_t *p_aout, byte_t *buffer, int i_size )
164 if (p_aout->p_sys->esd_format & ESD_STEREO)
166 if (p_aout->p_sys->esd_format & ESD_BITS16)
167 amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->l_rate;
169 amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
173 if (p_aout->p_sys->esd_format & ESD_BITS16)
174 amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->l_rate;
176 amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->l_rate;
179 intf_DbgMsg( "aout: latency is %i\n", amount );
181 write( p_aout->i_fd, buffer, i_size );
184 /*****************************************************************************
185 * aout_SysClose: closes the dsp audio device
186 *****************************************************************************/
187 void aout_SysClose( aout_thread_t *p_aout )
189 close( p_aout->i_fd );