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