]> git.sesse.net Git - vlc/blob - plugins/dsp/aout_dsp.c
* Ported Glide and MGA plugins to the new module API. MGA never worked,
[vlc] / plugins / dsp / aout_dsp.c
1 /*****************************************************************************
2  * aout_dsp.c : dsp functions library
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors: Michel Kaempf <maxx@via.ecp.fr>
7  *          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 #define MODULE_NAME dsp
25 #include "modules_inner.h"
26
27 /* TODO:
28  *
29  * - an aout_GetFormats() function
30  * - dsp inline/static
31  * - make this library portable (see mpg123)
32  *
33  */
34
35 /*****************************************************************************
36  * Preamble
37  *****************************************************************************/
38 #include "defs.h"
39
40 #include <errno.h>                                                 /* ENOMEM */
41 #include <fcntl.h>                                       /* open(), O_WRONLY */
42 #include <sys/ioctl.h>                                            /* ioctl() */
43 #include <string.h>                                            /* strerror() */
44 #include <unistd.h>                                      /* write(), close() */
45 #include <stdio.h>                                           /* "intf_msg.h" */
46 #include <stdlib.h>                            /* calloc(), malloc(), free() */
47
48 #ifdef SYS_BSD
49 #include <machine/soundcard.h>       /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT,
50                    SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED, SNDCTL_DSP_GETOSPACE */
51 #else
52 #include <sys/soundcard.h>           /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT,
53                    SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED, SNDCTL_DSP_GETOSPACE */
54 #endif
55
56 #include "config.h"
57 #include "common.h"                                     /* boolean_t, byte_t */
58 #include "threads.h"
59 #include "mtime.h"
60 #include "tests.h"
61
62 #include "audio_output.h"                                   /* aout_thread_t */
63
64 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
65 #include "main.h"
66
67 #include "modules.h"
68
69 /*****************************************************************************
70  * aout_sys_t: dsp audio output method descriptor
71  *****************************************************************************
72  * This structure is part of the audio output thread descriptor.
73  * It describes the dsp specific properties of an audio device.
74  *****************************************************************************/
75 typedef struct aout_sys_s
76 {
77     audio_buf_info        audio_buf;
78
79 } aout_sys_t;
80
81 /*****************************************************************************
82  * Local prototypes.
83  *****************************************************************************/
84 static int     aout_Probe       ( probedata_t *p_data );
85 static int     aout_Open        ( aout_thread_t *p_aout );
86 static int     aout_SetFormat   ( aout_thread_t *p_aout );
87 static long    aout_GetBufInfo  ( aout_thread_t *p_aout, long l_buffer_info );
88 static void    aout_Play        ( aout_thread_t *p_aout,
89                                   byte_t *buffer, int i_size );
90 static void    aout_Close       ( aout_thread_t *p_aout );
91
92 /*****************************************************************************
93  * Functions exported as capabilities. They are declared as static so that
94  * we don't pollute the namespace too much.
95  *****************************************************************************/
96 void _M( aout_getfunctions )( function_list_t * p_function_list )
97 {
98     p_function_list->pf_probe = aout_Probe;
99     p_function_list->functions.aout.pf_open = aout_Open;
100     p_function_list->functions.aout.pf_setformat = aout_SetFormat;
101     p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
102     p_function_list->functions.aout.pf_play = aout_Play;
103     p_function_list->functions.aout.pf_close = aout_Close;
104 }
105
106 /*****************************************************************************
107  * aout_Probe: probe the audio device and return a score
108  *****************************************************************************
109  * This function tries to open the DSP and returns a score to the plugin
110  * manager so that it can choose the most appropriate one.
111  *****************************************************************************/
112 static int aout_Probe( probedata_t *p_data )
113 {
114     char * psz_device = main_GetPszVariable( AOUT_DSP_VAR, AOUT_DSP_DEFAULT );
115     int i_fd = open( psz_device, O_WRONLY|O_NONBLOCK );
116
117     /* If we were unable to open the device, there is no way we can use
118      * the plugin. Return a score of 0. */
119     if( i_fd < 0 )
120     {
121         return( 0 );
122     }
123
124     /* Otherwise, there are good chances we can use this plugin, return 100. */
125     close( i_fd );
126
127     if( TestMethod( AOUT_METHOD_VAR, "dsp" ) )
128     {
129         return( 999 );
130     }
131
132     return( 100 );
133 }
134
135 /*****************************************************************************
136  * aout_Open: opens the audio device (the digital sound processor)
137  *****************************************************************************
138  * This function opens the dsp as a usual non-blocking write-only file, and
139  * modifies the p_aout->i_fd with the file's descriptor.
140  *****************************************************************************/
141 static int aout_Open( aout_thread_t *p_aout )
142 {
143     /* Allocate structure */
144     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
145     if( p_aout->p_sys == NULL )
146     {
147         intf_ErrMsg("aout error: %s", strerror(ENOMEM) );
148         return( 1 );
149     }
150
151     /* Initialize some variables */
152     p_aout->psz_device = main_GetPszVariable( AOUT_DSP_VAR, AOUT_DSP_DEFAULT );
153     p_aout->i_format   = AOUT_FORMAT_DEFAULT;
154     p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
155                                                   AOUT_STEREO_DEFAULT );
156     p_aout->l_rate     = main_GetIntVariable( AOUT_RATE_VAR,
157                                               AOUT_RATE_DEFAULT );
158
159     /* Open the sound device */
160     if( (p_aout->i_fd = open( p_aout->psz_device, O_WRONLY|O_NONBLOCK )) < 0 )
161     {
162         intf_ErrMsg( "aout error: can't open audio device (%s)",
163                      p_aout->psz_device );
164         return( -1 );
165     }
166
167     return( 0 );
168 }
169
170 /*****************************************************************************
171  * aout_SetFormat: resets the dsp and sets its format
172  *****************************************************************************
173  * This functions resets the DSP device, tries to initialize the output
174  * format with the value contained in the dsp structure, and if this value
175  * could not be set, the default value returned by ioctl is set. It then
176  * does the same for the stereo mode, and for the output rate.
177  *****************************************************************************/
178 static int aout_SetFormat( aout_thread_t *p_aout )
179 {
180     int i_format;
181     long l_rate;
182     boolean_t b_stereo = p_aout->b_stereo;
183
184     /* Reset the DSP device */
185     if( ioctl( p_aout->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
186     {
187         intf_ErrMsg( "aout error: can't reset audio device (%s)",
188                      p_aout->psz_device );
189         return( -1 );
190     }
191
192     /* Set the output format */
193     i_format = p_aout->i_format;
194     if( ioctl( p_aout->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
195     {
196         intf_ErrMsg( "aout error: can't set audio output format (%i)",
197                      p_aout->i_format );
198         return( -1 );
199     }
200
201     if( i_format != p_aout->i_format )
202     {
203         intf_DbgMsg( "aout debug: audio output format not supported (%i)",
204                      p_aout->i_format );
205         p_aout->i_format = i_format;
206     }
207
208     /* Set the number of channels */
209     if( ioctl( p_aout->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
210     {
211         intf_ErrMsg( "aout error: can't set number of audio channels (%i)",
212                      p_aout->i_channels );
213         return( -1 );
214     }
215
216     if( b_stereo != p_aout->b_stereo )
217     {
218         intf_DbgMsg( "aout debug: number of audio channels not supported (%i)",
219                      p_aout->i_channels );
220         p_aout->b_stereo = b_stereo;
221         p_aout->i_channels = 1 + b_stereo;
222     }
223
224     /* Set the output rate */
225     l_rate = p_aout->l_rate;
226     if( ioctl( p_aout->i_fd, SNDCTL_DSP_SPEED, &l_rate ) < 0 )
227     {
228         intf_ErrMsg( "aout error: can't set audio output rate (%li)",
229                      p_aout->l_rate );
230         return( -1 );
231     }
232
233     if( l_rate != p_aout->l_rate )
234     {
235         intf_DbgMsg( "aout debug: audio output rate not supported (%li)",
236                      p_aout->l_rate );
237         p_aout->l_rate = l_rate;
238     }
239
240     return( 0 );
241 }
242
243 /*****************************************************************************
244  * aout_GetBufInfo: buffer status query
245  *****************************************************************************
246  * This function fills in the audio_buf_info structure :
247  * - int fragments : number of available fragments (partially usend ones not
248  *   counted)
249  * - int fragstotal : total number of fragments allocated
250  * - int fragsize : size of a fragment in bytes
251  * - int bytes : available space in bytes (includes partially used fragments)
252  * Note! 'bytes' could be more than fragments*fragsize
253  *****************************************************************************/
254 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
255 {
256     ioctl( p_aout->i_fd, SNDCTL_DSP_GETOSPACE, &p_aout->p_sys->audio_buf );
257
258     /* returns the allocated space in bytes */
259     return ( (p_aout->p_sys->audio_buf.fragstotal
260                  * p_aout->p_sys->audio_buf.fragsize)
261             - p_aout->p_sys->audio_buf.bytes );
262 }
263
264 /*****************************************************************************
265  * aout_Play: plays a sound samples buffer
266  *****************************************************************************
267  * This function writes a buffer of i_length bytes in the dsp
268  *****************************************************************************/
269 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
270 {
271     if( p_aout->b_active )
272     {
273         write( p_aout->i_fd, buffer, i_size );
274     }
275 }
276
277 /*****************************************************************************
278  * aout_Close: closes the dsp audio device
279  *****************************************************************************/
280 static void aout_Close( aout_thread_t *p_aout )
281 {
282     close( p_aout->i_fd );
283 }
284