1 /*****************************************************************************
2 * esd.c : EsounD module
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: esd.c,v 1.15 2002/06/01 12:31:59 sam Exp $
7 * Authors: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <errno.h> /* ENOMEM */
28 #include <fcntl.h> /* open(), O_WRONLY */
29 #include <string.h> /* strerror() */
30 #include <unistd.h> /* write(), close() */
31 #include <stdlib.h> /* calloc(), malloc(), free() */
38 /*****************************************************************************
39 * aout_sys_t: esd audio output method descriptor
40 *****************************************************************************
41 * This structure is part of the audio output thread descriptor.
42 * It describes some esd specific variables.
43 *****************************************************************************/
46 esd_format_t esd_format;
50 /*****************************************************************************
52 *****************************************************************************/
53 static void aout_getfunctions ( function_list_t * p_function_list );
54 static int aout_Open ( aout_thread_t * );
55 static int aout_SetFormat ( aout_thread_t * );
56 static int aout_GetBufInfo ( aout_thread_t *, int );
57 static void aout_Play ( aout_thread_t *, byte_t *, int );
58 static void aout_Close ( aout_thread_t * );
60 /*****************************************************************************
61 * Build configuration tree.
62 *****************************************************************************/
67 SET_DESCRIPTION( _("EsounD audio module") )
68 ADD_CAPABILITY( AOUT, 50 )
69 ADD_SHORTCUT( "esound" )
73 aout_getfunctions( &p_module->p_functions->aout );
76 MODULE_DEACTIVATE_START
77 MODULE_DEACTIVATE_STOP
79 /*****************************************************************************
80 * Functions exported as capabilities. They are declared as static so that
81 * we don't pollute the namespace too much.
82 *****************************************************************************/
83 static void aout_getfunctions( function_list_t * p_function_list )
85 p_function_list->functions.aout.pf_open = aout_Open;
86 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
87 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
88 p_function_list->functions.aout.pf_play = aout_Play;
89 p_function_list->functions.aout.pf_close = aout_Close;
92 /*****************************************************************************
93 * aout_Open: open an esd socket
94 *****************************************************************************/
95 static int aout_Open( aout_thread_t *p_aout )
97 /* mpg123 does it this way */
98 int i_bits = ESD_BITS16;
99 int i_mode = ESD_STREAM;
100 int i_func = ESD_PLAY;
102 /* Allocate structure */
103 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
104 if( p_aout->p_sys == NULL )
106 msg_Err( p_aout, "out of memory" );
110 /* Initialize some variables */
111 p_aout->i_rate = esd_audio_rate; /* We use actual esd rate value, not
117 p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
119 if( p_aout->i_channels == 1 )
121 p_aout->p_sys->esd_format |= ESD_MONO;
125 p_aout->p_sys->esd_format |= ESD_STEREO;
128 /* open a socket for playing a stream
129 * and try to open /dev/dsp if there's no EsounD */
130 if ( (p_aout->p_sys->i_fd
131 = esd_play_stream_fallback(p_aout->p_sys->esd_format,
132 p_aout->i_rate, NULL, "vlc")) < 0 )
134 msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %ld Hz)",
135 p_aout->p_sys->esd_format, p_aout->i_rate );
142 /*****************************************************************************
143 * aout_SetFormat: set the output format
144 *****************************************************************************/
145 static int aout_SetFormat( aout_thread_t *p_aout )
149 i_fd = esd_open_sound(NULL);
150 p_aout->i_latency = esd_get_latency(i_fd);
152 msg_Dbg( p_aout, "aout_esd_latency: %d", p_aout->i_latency );
157 /*****************************************************************************
158 * aout_GetBufInfo: buffer status query
159 *****************************************************************************/
160 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
162 /* arbitrary value that should be changed */
163 return( i_buffer_limit );
166 /*****************************************************************************
167 * aout_Play: play a sound samples buffer
168 *****************************************************************************
169 * This function writes a buffer of i_length bytes in the socket
170 *****************************************************************************/
171 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
175 if (p_aout->p_sys->esd_format & ESD_STEREO)
177 if (p_aout->p_sys->esd_format & ESD_BITS16)
179 i_amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->i_rate;
183 i_amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->i_rate;
188 if (p_aout->p_sys->esd_format & ESD_BITS16)
190 i_amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->i_rate;
194 i_amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->i_rate;
198 write( p_aout->p_sys->i_fd, buffer, i_size );
201 /*****************************************************************************
202 * aout_Close: close the Esound socket
203 *****************************************************************************/
204 static void aout_Close( aout_thread_t *p_aout )
206 close( p_aout->p_sys->i_fd );