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