1 /*****************************************************************************
2 * aout_dsp.c : dsp functions library
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
6 * Authors: Michel Kaempf <maxx@via.ecp.fr>
7 * Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
26 * - an aout_GetFormats() function
28 * - make this library portable (see mpg123)
32 /*****************************************************************************
34 *****************************************************************************/
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() */
46 #include <machine/soundcard.h> /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT,
47 SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED, SNDCTL_DSP_GETOSPACE */
49 #include <sys/soundcard.h> /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT,
50 SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED, SNDCTL_DSP_GETOSPACE */
54 #include "common.h" /* boolean_t, byte_t */
59 #include "audio_output.h" /* aout_thread_t */
61 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
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
74 audio_buf_info audio_buf;
78 /*****************************************************************************
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 );
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 )
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;
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 )
111 char * psz_device = main_GetPszVariable( AOUT_DSP_VAR, AOUT_DSP_DEFAULT );
112 int i_fd = open( psz_device, O_WRONLY|O_NONBLOCK );
114 /* If we were unable to open the device, there is no way we can use
115 * the plugin. Return a score of 0. */
121 /* Otherwise, there are good chances we can use this plugin, return 100. */
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 )
134 /* Allocate structure */
135 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
136 if( p_aout->p_sys == NULL )
138 intf_ErrMsg("aout error: %s", strerror(ENOMEM) );
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,
150 /* Open the sound device */
151 if( (p_aout->i_fd = open( p_aout->psz_device, O_WRONLY|O_NONBLOCK )) < 0 )
153 intf_ErrMsg( "aout error: can't open audio device (%s)",
154 p_aout->psz_device );
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 )
173 boolean_t b_stereo = p_aout->b_stereo;
175 /* Reset the DSP device */
176 if( ioctl( p_aout->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
178 intf_ErrMsg( "aout error: can't reset audio device (%s)",
179 p_aout->psz_device );
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 )
187 intf_ErrMsg( "aout error: can't set audio output format (%i)",
192 if( i_format != p_aout->i_format )
194 intf_DbgMsg( "aout debug: audio output format not supported (%i)",
196 p_aout->i_format = i_format;
199 /* Set the number of channels */
200 if( ioctl( p_aout->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
202 intf_ErrMsg( "aout error: can't set number of audio channels (%i)",
203 p_aout->i_channels );
207 if( b_stereo != p_aout->b_stereo )
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;
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 )
219 intf_ErrMsg( "aout error: can't set audio output rate (%li)",
224 if( l_rate != p_aout->l_rate )
226 intf_DbgMsg( "aout debug: audio output rate not supported (%li)",
228 p_aout->l_rate = l_rate;
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
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 )
247 ioctl( p_aout->i_fd, SNDCTL_DSP_GETOSPACE, &p_aout->p_sys->audio_buf );
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 );
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 )
262 if( p_aout->b_active )
264 write( p_aout->i_fd, buffer, i_size );
268 /*****************************************************************************
269 * aout_Close: closes the dsp audio device
270 *****************************************************************************/
271 static void aout_Close( aout_thread_t *p_aout )
273 close( p_aout->i_fd );