1 /*****************************************************************************
2 * esd.c : EsounD module
3 *****************************************************************************
4 * Copyright (C) 2000, 2001 VideoLAN
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() */
35 #include "aout_internal.h"
37 #include <sys/socket.h>
41 /*****************************************************************************
42 * aout_sys_t: esd audio output method descriptor
43 *****************************************************************************
44 * This structure is part of the audio output thread descriptor.
45 * It describes some esd specific variables.
46 *****************************************************************************/
49 esd_format_t esd_format;
52 mtime_t latency; /* unused, but we might do something with it */
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close ( vlc_object_t * );
60 static void Play ( aout_instance_t * );
62 /*****************************************************************************
64 *****************************************************************************/
66 set_description( _("EsounD audio output") );
67 set_capability( "audio output", 50 );
68 set_callbacks( Open, Close );
69 add_shortcut( "esound" );
72 /*****************************************************************************
73 * Open: open an esd socket
74 *****************************************************************************/
75 static int Open( vlc_object_t *p_this )
77 aout_instance_t *p_aout = (aout_instance_t *)p_this;
78 struct aout_sys_t * p_sys;
83 /* Allocate structure */
84 p_sys = malloc( sizeof( aout_sys_t ) );
87 msg_Err( p_aout, "out of memory" );
91 p_aout->output.p_sys = p_sys;
93 p_aout->output.pf_play = Play;
94 aout_VolumeSoftInit( p_aout );
96 /* Initialize some variables */
97 p_sys->esd_format = ESD_BITS16 | ESD_STREAM | ESD_PLAY;
98 p_sys->esd_format &= ~ESD_MASK_CHAN;
100 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
101 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
102 if ( i_nb_channels > 2 )
104 /* EsounD doesn't support more than two channels. */
106 p_aout->output.output.i_physical_channels =
107 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
110 switch( i_nb_channels )
113 p_sys->esd_format |= ESD_MONO;
116 p_sys->esd_format |= ESD_STEREO;
120 /* Force the rate, otherwise the sound is very noisy */
121 p_aout->output.output.i_rate = ESD_DEFAULT_RATE;
123 /* open a socket for playing a stream
124 * and try to open /dev/dsp if there's no EsounD */
125 p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
126 p_aout->output.output.i_rate, NULL, "vlc" );
127 if( p_sys->i_fd < 0 )
129 msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %d Hz)",
130 p_sys->esd_format, p_aout->output.output.i_rate );
135 p_aout->output.i_nb_samples = ESD_BUF_SIZE * 2;
137 /* ESD latency is calculated for 44100 Hz. We don't have any way to get the
138 * number of buffered samples, so I assume ESD_BUF_SIZE/2 */
140 (mtime_t)( esd_get_latency( i_newfd = esd_open_sound(NULL) ) +
142 * p_aout->output.output.i_bytes_per_frame
143 * p_aout->output.output.i_rate
146 / p_aout->output.output.i_bytes_per_frame
147 / p_aout->output.output.i_rate;
153 /*****************************************************************************
154 * Play: nothing to do
155 *****************************************************************************/
156 static void Play( aout_instance_t *p_aout )
158 struct aout_sys_t * p_sys = p_aout->output.p_sys;
159 aout_buffer_t * p_buffer;
162 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
164 if ( p_buffer != NULL )
167 char *data = p_buffer->p_buffer;
169 for( pos = 0; pos + ESD_BUF_SIZE <= p_buffer->i_nb_bytes;
170 pos += ESD_BUF_SIZE )
172 i_tmp = write( p_sys->i_fd, data + pos, ESD_BUF_SIZE );
175 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
178 aout_BufferFree( p_buffer );
182 /*****************************************************************************
183 * Close: close the Esound socket
184 *****************************************************************************/
185 static void Close( vlc_object_t *p_this )
187 aout_instance_t *p_aout = (aout_instance_t *)p_this;
188 struct aout_sys_t * p_sys = p_aout->output.p_sys;
190 close( p_sys->i_fd );
195 /*****************************************************************************
196 * Play: play a sound samples buffer
197 *****************************************************************************
198 * This function writes a buffer of i_length bytes in the socket
199 *****************************************************************************/
200 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
204 int m1 = p_aout->output.p_sys->esd_format & ESD_STEREO ? 1 : 2;
205 int m2 = p_aout->output.p_sys->esd_format & ESD_BITS16 ? 64 : 128;
207 i_amount = (m1 * 44100 * (ESD_BUF_SIZE + m1 * m2)) / p_aout->i_rate;
209 write( p_aout->output.p_sys->i_fd, buffer, i_size );