]> git.sesse.net Git - vlc/blob - modules/audio_output/esd.c
15ef25b61c47a144b6ff4c18bcb85db5487188ba
[vlc] / modules / audio_output / esd.c
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 $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
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() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/aout.h>
35
36 #include <esd.h>
37
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  *****************************************************************************/
44 struct aout_sys_t
45 {
46     esd_format_t esd_format;
47     int          i_fd;
48 };
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static int  Open         ( vlc_object_t * );
54 static void Close        ( vlc_object_t * );
55
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 );
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 vlc_module_begin();
64     set_description( _("EsounD audio module") ); 
65     set_capability( "audio output", 50 );
66     set_callbacks( Open, Close );
67     add_shortcut( "esound" );
68 vlc_module_end();
69
70 /*****************************************************************************
71  * Open: open an esd socket
72  *****************************************************************************/
73 static int Open( vlc_object_t *p_this )
74 {
75     aout_thread_t *p_aout = (aout_thread_t *)p_this;
76
77     /* mpg123 does it this way */
78     int i_bits = ESD_BITS16;
79     int i_mode = ESD_STREAM;
80     int i_func = ESD_PLAY;
81
82     /* Allocate structure */
83     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
84     if( p_aout->p_sys == NULL )
85     {
86         msg_Err( p_aout, "out of memory" );
87         return( 1 );
88     }
89
90     /* Initialize some variables */
91     p_aout->i_rate = esd_audio_rate; /* We use actual esd rate value, not
92                                       * initial value */
93
94     i_bits = ESD_BITS16;
95     i_mode = ESD_STREAM;
96     i_func = ESD_PLAY;
97     p_aout->p_sys->esd_format = (i_bits | i_mode | i_func) & (~ESD_MASK_CHAN);
98
99     if( p_aout->i_channels == 1 )
100     {
101         p_aout->p_sys->esd_format |= ESD_MONO;
102     }
103     else
104     {
105         p_aout->p_sys->esd_format |= ESD_STEREO;
106     }
107
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 )
113     {
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 );
116         return( -1 );
117     }
118
119     p_aout->pf_setformat = SetFormat;
120     p_aout->pf_getbufinfo = GetBufInfo;
121     p_aout->pf_play = Play;
122
123     return( 0 );
124 }
125
126 /*****************************************************************************
127  * SetFormat: set the output format
128  *****************************************************************************/
129 static int SetFormat( aout_thread_t *p_aout )
130 {
131     int i_fd;
132
133     i_fd = esd_open_sound(NULL);
134     p_aout->i_latency = esd_get_latency(i_fd);
135    
136     msg_Dbg( p_aout, "aout_esd_latency: %d", p_aout->i_latency );
137
138     return( 0 );
139 }
140
141 /*****************************************************************************
142  * GetBufInfo: buffer status query
143  *****************************************************************************/
144 static int GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
145 {
146     /* arbitrary value that should be changed */
147     return( i_buffer_limit );
148 }
149
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 )
156 {
157     int i_amount;
158     
159     if (p_aout->p_sys->esd_format & ESD_STEREO)
160     {
161         if (p_aout->p_sys->esd_format & ESD_BITS16)
162         {
163             i_amount = (44100 * (ESD_BUF_SIZE + 64)) / p_aout->i_rate;
164         }
165         else
166         {
167             i_amount = (44100 * (ESD_BUF_SIZE + 128)) / p_aout->i_rate;
168         }
169     }
170     else
171     {
172         if (p_aout->p_sys->esd_format & ESD_BITS16)
173         {
174             i_amount = (2 * 44100 * (ESD_BUF_SIZE + 128)) / p_aout->i_rate;
175         }
176         else
177         {
178             i_amount = (2 * 44100 * (ESD_BUF_SIZE + 256)) / p_aout->i_rate;
179         }
180     }
181
182     write( p_aout->p_sys->i_fd, buffer, i_size );
183 }
184
185 /*****************************************************************************
186  * Close: close the Esound socket
187  *****************************************************************************/
188 static void Close( vlc_object_t *p_this )
189 {
190     aout_thread_t *p_aout = (aout_thread_t *)p_this;
191
192     close( p_aout->p_sys->i_fd );
193 }
194