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 $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
25 #define MODULE_NAME dsp
26 #include "modules_inner.h"
30 * - an aout_GetFormats() function
32 * - make this library portable (see mpg123)
36 /*****************************************************************************
38 *****************************************************************************/
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() */
50 #include <machine/soundcard.h> /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT,
51 SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED, SNDCTL_DSP_GETOSPACE */
53 #include <sys/soundcard.h> /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT,
54 SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED, SNDCTL_DSP_GETOSPACE */
58 #include "common.h" /* boolean_t, byte_t */
63 #include "audio_output.h" /* aout_thread_t */
65 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
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
78 audio_buf_info audio_buf;
82 /*****************************************************************************
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 );
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 )
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;
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 )
115 char * psz_device = main_GetPszVariable( AOUT_DSP_VAR, AOUT_DSP_DEFAULT );
116 int i_fd = open( psz_device, O_WRONLY|O_NONBLOCK );
118 /* If we were unable to open the device, there is no way we can use
119 * the plugin. Return a score of 0. */
125 /* Otherwise, there are good chances we can use this plugin, return 100. */
128 if( TestMethod( AOUT_METHOD_VAR, "dsp" ) )
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 )
144 /* Allocate structure */
145 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
146 if( p_aout->p_sys == NULL )
148 intf_ErrMsg("aout error: %s", strerror(ENOMEM) );
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,
160 /* Open the sound device */
161 if( (p_aout->i_fd = open( p_aout->psz_device, O_WRONLY|O_NONBLOCK )) < 0 )
163 intf_ErrMsg( "aout error: can't open audio device (%s)",
164 p_aout->psz_device );
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 )
183 boolean_t b_stereo = p_aout->b_stereo;
185 /* Reset the DSP device */
186 if( ioctl( p_aout->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
188 intf_ErrMsg( "aout error: can't reset audio device (%s)",
189 p_aout->psz_device );
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 )
197 intf_ErrMsg( "aout error: can't set audio output format (%i)",
202 if( i_format != p_aout->i_format )
204 intf_DbgMsg( "aout debug: audio output format not supported (%i)",
206 p_aout->i_format = i_format;
209 /* Set the number of channels */
210 if( ioctl( p_aout->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
212 intf_ErrMsg( "aout error: can't set number of audio channels (%i)",
213 p_aout->i_channels );
217 if( b_stereo != p_aout->b_stereo )
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;
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 )
229 intf_ErrMsg( "aout error: can't set audio output rate (%li)",
234 if( l_rate != p_aout->l_rate )
236 intf_DbgMsg( "aout debug: audio output rate not supported (%li)",
238 p_aout->l_rate = l_rate;
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
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 )
257 ioctl( p_aout->i_fd, SNDCTL_DSP_GETOSPACE, &p_aout->p_sys->audio_buf );
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 );
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 )
272 if( p_aout->b_active )
274 write( p_aout->i_fd, buffer, i_size );
278 /*****************************************************************************
279 * aout_Close: closes the dsp audio device
280 *****************************************************************************/
281 static void aout_Close( aout_thread_t *p_aout )
283 close( p_aout->i_fd );