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