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