]> git.sesse.net Git - vlc/blob - plugins/dsp/aout_dsp.c
. Attempt to port the BeOS audio plugin to the new module API. Will
[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 "plugins.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->p_probe = aout_Probe;
96     p_function_list->functions.aout.p_open = aout_Open;
97     p_function_list->functions.aout.p_setformat = aout_SetFormat;
98     p_function_list->functions.aout.p_getbufinfo = aout_GetBufInfo;
99     p_function_list->functions.aout.p_play = aout_Play;
100     p_function_list->functions.aout.p_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     return( 100 );
124 }
125
126 /*****************************************************************************
127  * aout_Open: opens the audio device (the digital sound processor)
128  *****************************************************************************
129  * This function opens the dsp as a usual non-blocking write-only file, and
130  * modifies the p_aout->i_fd with the file's descriptor.
131  *****************************************************************************/
132 static int aout_Open( aout_thread_t *p_aout )
133 {
134     /* Allocate structure */
135     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
136     if( p_aout->p_sys == NULL )
137     {
138         intf_ErrMsg("aout error: %s", strerror(ENOMEM) );
139         return( 1 );
140     }
141
142     /* Initialize some variables */
143     p_aout->psz_device = main_GetPszVariable( AOUT_DSP_VAR, AOUT_DSP_DEFAULT );
144     p_aout->i_format   = AOUT_FORMAT_DEFAULT;
145     p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
146                                                   AOUT_STEREO_DEFAULT );
147     p_aout->l_rate     = main_GetIntVariable( AOUT_RATE_VAR,
148                                               AOUT_RATE_DEFAULT );
149
150     /* Open the sound device */
151     if( (p_aout->i_fd = open( p_aout->psz_device, O_WRONLY|O_NONBLOCK )) < 0 )
152     {
153         intf_ErrMsg( "aout error: can't open audio device (%s)",
154                      p_aout->psz_device );
155         return( -1 );
156     }
157
158     return( 0 );
159 }
160
161 /*****************************************************************************
162  * aout_SetFormat: resets the dsp and sets its format
163  *****************************************************************************
164  * This functions resets the DSP device, tries to initialize the output
165  * format with the value contained in the dsp structure, and if this value
166  * could not be set, the default value returned by ioctl is set. It then
167  * does the same for the stereo mode, and for the output rate.
168  *****************************************************************************/
169 static int aout_SetFormat( aout_thread_t *p_aout )
170 {
171     int i_format;
172     long l_rate;
173     boolean_t b_stereo = p_aout->b_stereo;
174
175     /* Reset the DSP device */
176     if( ioctl( p_aout->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
177     {
178         intf_ErrMsg( "aout error: can't reset audio device (%s)",
179                      p_aout->psz_device );
180         return( -1 );
181     }
182
183     /* Set the output format */
184     i_format = p_aout->i_format;
185     if( ioctl( p_aout->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
186     {
187         intf_ErrMsg( "aout error: can't set audio output format (%i)",
188                      p_aout->i_format );
189         return( -1 );
190     }
191
192     if( i_format != p_aout->i_format )
193     {
194         intf_DbgMsg( "aout debug: audio output format not supported (%i)",
195                      p_aout->i_format );
196         p_aout->i_format = i_format;
197     }
198
199     /* Set the number of channels */
200     if( ioctl( p_aout->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
201     {
202         intf_ErrMsg( "aout error: can't set number of audio channels (%i)",
203                      p_aout->i_channels );
204         return( -1 );
205     }
206
207     if( b_stereo != p_aout->b_stereo )
208     {
209         intf_DbgMsg( "aout debug: number of audio channels not supported (%i)",
210                      p_aout->i_channels );
211         p_aout->b_stereo = b_stereo;
212         p_aout->i_channels = 1 + b_stereo;
213     }
214
215     /* Set the output rate */
216     l_rate = p_aout->l_rate;
217     if( ioctl( p_aout->i_fd, SNDCTL_DSP_SPEED, &l_rate ) < 0 )
218     {
219         intf_ErrMsg( "aout error: can't set audio output rate (%li)",
220                      p_aout->l_rate );
221         return( -1 );
222     }
223
224     if( l_rate != p_aout->l_rate )
225     {
226         intf_DbgMsg( "aout debug: audio output rate not supported (%li)",
227                      p_aout->l_rate );
228         p_aout->l_rate = l_rate;
229     }
230
231     return( 0 );
232 }
233
234 /*****************************************************************************
235  * aout_GetBufInfo: buffer status query
236  *****************************************************************************
237  * This function fills in the audio_buf_info structure :
238  * - int fragments : number of available fragments (partially usend ones not
239  *   counted)
240  * - int fragstotal : total number of fragments allocated
241  * - int fragsize : size of a fragment in bytes
242  * - int bytes : available space in bytes (includes partially used fragments)
243  * Note! 'bytes' could be more than fragments*fragsize
244  *****************************************************************************/
245 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
246 {
247     ioctl( p_aout->i_fd, SNDCTL_DSP_GETOSPACE, &p_aout->p_sys->audio_buf );
248
249     /* returns the allocated space in bytes */
250     return ( (p_aout->p_sys->audio_buf.fragstotal
251                  * p_aout->p_sys->audio_buf.fragsize)
252             - p_aout->p_sys->audio_buf.bytes );
253 }
254
255 /*****************************************************************************
256  * aout_Play: plays a sound samples buffer
257  *****************************************************************************
258  * This function writes a buffer of i_length bytes in the dsp
259  *****************************************************************************/
260 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
261 {
262     if( p_aout->b_active )
263     {
264         write( p_aout->i_fd, buffer, i_size );
265     }
266 }
267
268 /*****************************************************************************
269  * aout_Close: closes the dsp audio device
270  *****************************************************************************/
271 static void aout_Close( aout_thread_t *p_aout )
272 {
273     close( p_aout->i_fd );
274 }
275