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_category( CAT_AUDIO );
69 set_subcategory( SUBCAT_AUDIO_AOUT );
70 set_callbacks( Open, Close );
71 add_shortcut( "esound" );
74 /*****************************************************************************
75 * Open: open an esd socket
76 *****************************************************************************/
77 static int Open( vlc_object_t *p_this )
79 aout_instance_t *p_aout = (aout_instance_t *)p_this;
80 struct aout_sys_t * p_sys;
85 /* Allocate structure */
86 p_sys = malloc( sizeof( aout_sys_t ) );
89 msg_Err( p_aout, "out of memory" );
93 p_aout->output.p_sys = p_sys;
95 p_aout->output.pf_play = Play;
96 aout_VolumeSoftInit( p_aout );
98 /* Initialize some variables */
99 p_sys->esd_format = ESD_BITS16 | ESD_STREAM | ESD_PLAY;
100 p_sys->esd_format &= ~ESD_MASK_CHAN;
102 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
103 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
104 if ( i_nb_channels > 2 )
106 /* EsounD doesn't support more than two channels. */
108 p_aout->output.output.i_physical_channels =
109 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
112 switch( i_nb_channels )
115 p_sys->esd_format |= ESD_MONO;
118 p_sys->esd_format |= ESD_STEREO;
122 /* Force the rate, otherwise the sound is very noisy */
123 p_aout->output.output.i_rate = ESD_DEFAULT_RATE;
125 /* open a socket for playing a stream
126 * and try to open /dev/dsp if there's no EsounD */
127 p_sys->i_fd = esd_play_stream_fallback( p_sys->esd_format,
128 p_aout->output.output.i_rate, NULL, "vlc" );
129 if( p_sys->i_fd < 0 )
131 msg_Err( p_aout, "cannot open esound socket (format 0x%08x at %d Hz)",
132 p_sys->esd_format, p_aout->output.output.i_rate );
137 p_aout->output.i_nb_samples = ESD_BUF_SIZE * 2;
139 /* ESD latency is calculated for 44100 Hz. We don't have any way to get the
140 * number of buffered samples, so I assume ESD_BUF_SIZE/2 */
142 (mtime_t)( esd_get_latency( i_newfd = esd_open_sound(NULL) ) +
144 * p_aout->output.output.i_bytes_per_frame
145 * p_aout->output.output.i_rate
148 / p_aout->output.output.i_bytes_per_frame
149 / p_aout->output.output.i_rate;
155 /*****************************************************************************
156 * Play: nothing to do
157 *****************************************************************************/
158 static void Play( aout_instance_t *p_aout )
160 struct aout_sys_t * p_sys = p_aout->output.p_sys;
161 aout_buffer_t * p_buffer;
164 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
166 if ( p_buffer != NULL )
169 char *data = p_buffer->p_buffer;
171 for( pos = 0; pos + ESD_BUF_SIZE <= p_buffer->i_nb_bytes;
172 pos += ESD_BUF_SIZE )
174 i_tmp = write( p_sys->i_fd, data + pos, ESD_BUF_SIZE );
177 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
180 aout_BufferFree( p_buffer );
184 /*****************************************************************************
185 * Close: close the Esound socket
186 *****************************************************************************/
187 static void Close( vlc_object_t *p_this )
189 aout_instance_t *p_aout = (aout_instance_t *)p_this;
190 struct aout_sys_t * p_sys = p_aout->output.p_sys;
192 close( p_sys->i_fd );
197 /*****************************************************************************
198 * Play: play a sound samples buffer
199 *****************************************************************************
200 * This function writes a buffer of i_length bytes in the socket
201 *****************************************************************************/
202 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
206 int m1 = p_aout->output.p_sys->esd_format & ESD_STEREO ? 1 : 2;
207 int m2 = p_aout->output.p_sys->esd_format & ESD_BITS16 ? 64 : 128;
209 i_amount = (m1 * 44100 * (ESD_BUF_SIZE + m1 * m2)) / p_aout->i_rate;
211 write( p_aout->output.p_sys->i_fd, buffer, i_size );