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 *****************************************************************************/
24 #define MODULE_NAME dsp
25 #include "modules_inner.h"
29 * - an aout_GetFormats() function
31 * - make this library portable (see mpg123)
35 /*****************************************************************************
37 *****************************************************************************/
40 #include <errno.h> /* ENOMEM */
41 #include <fcntl.h> /* open(), O_WRONLY */
42 #include <sys/ioctl.h> /* ioctl() */
43 #include <string.h> /* strerror() */
44 #include <unistd.h> /* write(), close() */
45 #include <stdio.h> /* "intf_msg.h" */
46 #include <stdlib.h> /* calloc(), malloc(), free() */
49 #include <machine/soundcard.h> /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT,
50 SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED, SNDCTL_DSP_GETOSPACE */
52 #include <sys/soundcard.h> /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT,
53 SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED, SNDCTL_DSP_GETOSPACE */
57 #include "common.h" /* boolean_t, byte_t */
62 #include "audio_output.h" /* aout_thread_t */
64 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
69 /*****************************************************************************
70 * aout_sys_t: dsp audio output method descriptor
71 *****************************************************************************
72 * This structure is part of the audio output thread descriptor.
73 * It describes the dsp specific properties of an audio device.
74 *****************************************************************************/
75 typedef struct aout_sys_s
77 audio_buf_info audio_buf;
81 /*****************************************************************************
83 *****************************************************************************/
84 static int aout_Probe ( probedata_t *p_data );
85 static int aout_Open ( aout_thread_t *p_aout );
86 static int aout_SetFormat ( aout_thread_t *p_aout );
87 static long aout_GetBufInfo ( aout_thread_t *p_aout, long l_buffer_info );
88 static void aout_Play ( aout_thread_t *p_aout,
89 byte_t *buffer, int i_size );
90 static void aout_Close ( aout_thread_t *p_aout );
92 /*****************************************************************************
93 * Functions exported as capabilities. They are declared as static so that
94 * we don't pollute the namespace too much.
95 *****************************************************************************/
96 void _M( aout_getfunctions )( function_list_t * p_function_list )
98 p_function_list->pf_probe = aout_Probe;
99 p_function_list->functions.aout.pf_open = aout_Open;
100 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
101 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
102 p_function_list->functions.aout.pf_play = aout_Play;
103 p_function_list->functions.aout.pf_close = aout_Close;
106 /*****************************************************************************
107 * aout_Probe: probe the audio device and return a score
108 *****************************************************************************
109 * This function tries to open the DSP and returns a score to the plugin
110 * manager so that it can choose the most appropriate one.
111 *****************************************************************************/
112 static int aout_Probe( probedata_t *p_data )
114 char * psz_device = main_GetPszVariable( AOUT_DSP_VAR, AOUT_DSP_DEFAULT );
115 int i_fd = open( psz_device, O_WRONLY|O_NONBLOCK );
117 /* If we were unable to open the device, there is no way we can use
118 * the plugin. Return a score of 0. */
124 /* Otherwise, there are good chances we can use this plugin, return 100. */
127 if( TestMethod( AOUT_METHOD_VAR, "dsp" ) )
135 /*****************************************************************************
136 * aout_Open: opens the audio device (the digital sound processor)
137 *****************************************************************************
138 * This function opens the dsp as a usual non-blocking write-only file, and
139 * modifies the p_aout->i_fd with the file's descriptor.
140 *****************************************************************************/
141 static int aout_Open( aout_thread_t *p_aout )
143 /* Allocate structure */
144 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
145 if( p_aout->p_sys == NULL )
147 intf_ErrMsg("aout error: %s", strerror(ENOMEM) );
151 /* Initialize some variables */
152 p_aout->psz_device = main_GetPszVariable( AOUT_DSP_VAR, AOUT_DSP_DEFAULT );
153 p_aout->i_format = AOUT_FORMAT_DEFAULT;
154 p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
155 AOUT_STEREO_DEFAULT );
156 p_aout->l_rate = main_GetIntVariable( AOUT_RATE_VAR,
159 /* Open the sound device */
160 if( (p_aout->i_fd = open( p_aout->psz_device, O_WRONLY|O_NONBLOCK )) < 0 )
162 intf_ErrMsg( "aout error: can't open audio device (%s)",
163 p_aout->psz_device );
170 /*****************************************************************************
171 * aout_SetFormat: resets the dsp and sets its format
172 *****************************************************************************
173 * This functions resets the DSP device, tries to initialize the output
174 * format with the value contained in the dsp structure, and if this value
175 * could not be set, the default value returned by ioctl is set. It then
176 * does the same for the stereo mode, and for the output rate.
177 *****************************************************************************/
178 static int aout_SetFormat( aout_thread_t *p_aout )
182 boolean_t b_stereo = p_aout->b_stereo;
184 /* Reset the DSP device */
185 if( ioctl( p_aout->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
187 intf_ErrMsg( "aout error: can't reset audio device (%s)",
188 p_aout->psz_device );
192 /* Set the output format */
193 i_format = p_aout->i_format;
194 if( ioctl( p_aout->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
196 intf_ErrMsg( "aout error: can't set audio output format (%i)",
201 if( i_format != p_aout->i_format )
203 intf_DbgMsg( "aout debug: audio output format not supported (%i)",
205 p_aout->i_format = i_format;
208 /* Set the number of channels */
209 if( ioctl( p_aout->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
211 intf_ErrMsg( "aout error: can't set number of audio channels (%i)",
212 p_aout->i_channels );
216 if( b_stereo != p_aout->b_stereo )
218 intf_DbgMsg( "aout debug: number of audio channels not supported (%i)",
219 p_aout->i_channels );
220 p_aout->b_stereo = b_stereo;
221 p_aout->i_channels = 1 + b_stereo;
224 /* Set the output rate */
225 l_rate = p_aout->l_rate;
226 if( ioctl( p_aout->i_fd, SNDCTL_DSP_SPEED, &l_rate ) < 0 )
228 intf_ErrMsg( "aout error: can't set audio output rate (%li)",
233 if( l_rate != p_aout->l_rate )
235 intf_DbgMsg( "aout debug: audio output rate not supported (%li)",
237 p_aout->l_rate = l_rate;
243 /*****************************************************************************
244 * aout_GetBufInfo: buffer status query
245 *****************************************************************************
246 * This function fills in the audio_buf_info structure :
247 * - int fragments : number of available fragments (partially usend ones not
249 * - int fragstotal : total number of fragments allocated
250 * - int fragsize : size of a fragment in bytes
251 * - int bytes : available space in bytes (includes partially used fragments)
252 * Note! 'bytes' could be more than fragments*fragsize
253 *****************************************************************************/
254 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
256 ioctl( p_aout->i_fd, SNDCTL_DSP_GETOSPACE, &p_aout->p_sys->audio_buf );
258 /* returns the allocated space in bytes */
259 return ( (p_aout->p_sys->audio_buf.fragstotal
260 * p_aout->p_sys->audio_buf.fragsize)
261 - p_aout->p_sys->audio_buf.bytes );
264 /*****************************************************************************
265 * aout_Play: plays a sound samples buffer
266 *****************************************************************************
267 * This function writes a buffer of i_length bytes in the dsp
268 *****************************************************************************/
269 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
271 if( p_aout->b_active )
273 write( p_aout->i_fd, buffer, i_size );
277 /*****************************************************************************
278 * aout_Close: closes the dsp audio device
279 *****************************************************************************/
280 static void aout_Close( aout_thread_t *p_aout )
282 close( p_aout->i_fd );