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 $
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() */
69 #include "modules_export.h"
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
79 audio_buf_info audio_buf;
83 /*****************************************************************************
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 );
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 )
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;
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 )
116 char * psz_device = main_GetPszVariable( AOUT_DSP_VAR, AOUT_DSP_DEFAULT );
117 int i_fd = open( psz_device, O_WRONLY|O_NONBLOCK );
119 /* If we were unable to open the device, there is no way we can use
120 * the plugin. Return a score of 0. */
126 /* Otherwise, there are good chances we can use this plugin, return 100. */
129 if( TestMethod( AOUT_METHOD_VAR, "dsp" ) )
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 )
145 /* Allocate structure */
146 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
147 if( p_aout->p_sys == NULL )
149 intf_ErrMsg("aout error: %s", strerror(ENOMEM) );
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,
161 /* Open the sound device */
162 if( (p_aout->i_fd = open( p_aout->psz_device, O_WRONLY|O_NONBLOCK )) < 0 )
164 intf_ErrMsg( "aout error: can't open audio device (%s)",
165 p_aout->psz_device );
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 )
184 boolean_t b_stereo = p_aout->b_stereo;
186 /* Reset the DSP device */
187 if( ioctl( p_aout->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
189 intf_ErrMsg( "aout error: can't reset audio device (%s)",
190 p_aout->psz_device );
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 )
198 intf_ErrMsg( "aout error: can't set audio output format (%i)",
203 if( i_format != p_aout->i_format )
205 intf_DbgMsg( "aout debug: audio output format not supported (%i)",
207 p_aout->i_format = i_format;
210 /* Set the number of channels */
211 if( ioctl( p_aout->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
213 intf_ErrMsg( "aout error: can't set number of audio channels (%i)",
214 p_aout->i_channels );
218 if( b_stereo != p_aout->b_stereo )
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;
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 )
230 intf_ErrMsg( "aout error: can't set audio output rate (%li)",
235 if( l_rate != p_aout->l_rate )
237 intf_DbgMsg( "aout debug: audio output rate not supported (%li)",
239 p_aout->l_rate = l_rate;
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
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 )
258 ioctl( p_aout->i_fd, SNDCTL_DSP_GETOSPACE, &p_aout->p_sys->audio_buf );
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 );
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 )
273 if( p_aout->b_active )
275 write( p_aout->i_fd, buffer, i_size );
279 /*****************************************************************************
280 * aout_Close: closes the dsp audio device
281 *****************************************************************************/
282 static void aout_Close( aout_thread_t *p_aout )
284 close( p_aout->i_fd );