1 /*****************************************************************************
2 * esd.c : EsounD module
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
5 * $Id: esd.c,v 1.1 2002/08/07 21:36:55 massiot 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 int Open ( vlc_object_t * );
54 static void Close ( vlc_object_t * );
56 static int SetFormat ( aout_thread_t * );
57 static int GetBufInfo ( aout_thread_t *, int );
58 static void Play ( aout_thread_t *, byte_t *, int );
60 /*****************************************************************************
62 *****************************************************************************/
64 set_description( _("EsounD audio module") );
65 set_capability( "audio output", 50 );
66 set_callbacks( Open, Close );
67 add_shortcut( "esound" );
70 /*****************************************************************************
71 * Open: open an esd socket
72 *****************************************************************************/
73 static int Open( vlc_object_t *p_this )
75 aout_thread_t *p_aout = (aout_thread_t *)p_this;
77 /* mpg123 does it this way */
78 int i_bits = ESD_BITS16;
79 int i_mode = ESD_STREAM;
80 int i_func = ESD_PLAY;
82 /* Allocate structure */
83 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
84 if( p_aout->p_sys == NULL )
86 msg_Err( p_aout, "out of memory" );
90 /* Initialize some variables */
91 p_aout->i_rate = esd_audio_rate; /* We use actual esd rate value, not
97 p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
99 if( p_aout->i_channels == 1 )
101 p_aout->p_sys->esd_format |= ESD_MONO;
105 p_aout->p_sys->esd_format |= ESD_STEREO;
108 /* open a socket for playing a stream
109 * and try to open /dev/dsp if there's no EsounD */
110 if ( (p_aout->p_sys->i_fd
111 = esd_play_stream_fallback(p_aout->p_sys->esd_format,
112 p_aout->i_rate, NULL, "vlc")) < 0 )
114 msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %ld Hz)",
115 p_aout->p_sys->esd_format, p_aout->i_rate );
119 p_aout->pf_setformat = SetFormat;
120 p_aout->pf_getbufinfo = GetBufInfo;
121 p_aout->pf_play = Play;
126 /*****************************************************************************
127 * SetFormat: set the output format
128 *****************************************************************************/
129 static int SetFormat( aout_thread_t *p_aout )
133 i_fd = esd_open_sound(NULL);
134 p_aout->i_latency = esd_get_latency(i_fd);
136 msg_Dbg( p_aout, "aout_esd_latency: %d", p_aout->i_latency );
141 /*****************************************************************************
142 * GetBufInfo: buffer status query
143 *****************************************************************************/
144 static int GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
146 /* arbitrary value that should be changed */
147 return( i_buffer_limit );
150 /*****************************************************************************
151 * Play: play a sound samples buffer
152 *****************************************************************************
153 * This function writes a buffer of i_length bytes in the socket
154 *****************************************************************************/
155 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
159 if (p_aout->p_sys->esd_format & ESD_STEREO)
161 if (p_aout->p_sys->esd_format & ESD_BITS16)
163 i_amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->i_rate;
167 i_amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->i_rate;
172 if (p_aout->p_sys->esd_format & ESD_BITS16)
174 i_amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->i_rate;
178 i_amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->i_rate;
182 write( p_aout->p_sys->i_fd, buffer, i_size );
185 /*****************************************************************************
186 * Close: close the Esound socket
187 *****************************************************************************/
188 static void Close( vlc_object_t *p_this )
190 aout_thread_t *p_aout = (aout_thread_t *)p_this;
192 close( p_aout->p_sys->i_fd );