1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
/* aosound.c: libao sound I/O
Copyright (c) 2004 Gergely Szasz, Philip Kendall
$Id: aosound.c 3514 2008-02-09 14:49:36Z zubzero $
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
Philip: philip-fuse@shadowmagic.org.uk
Gergely: szaszg@hu.inter.net
*/
#include <config.h>
#include <string.h>
#include <ao/ao.h>
#include "settings.h"
#include "sound.h"
#include "ui/ui.h"
static ao_device *dev_for_ao;
static int sixteenbit = 1;
static char *filename = NULL;
static const char *default_filename = "fuse-sound.ao";
static int first_init = 1;
static void
driver_error( void )
{
switch( errno ) {
case AO_ENODRIVER:
ui_error( UI_ERROR_ERROR,
"ao: no driver corresponds to driver_id." );
break;
case AO_ENOTLIVE:
ui_error( UI_ERROR_ERROR,
"ao: driver is not a live output device." );
break;
case AO_ENOTFILE:
ui_error( UI_ERROR_ERROR,
"ao: driver is not a file output driver." );
break;
case AO_EBADOPTION:
ui_error( UI_ERROR_ERROR,
"ao: a valid option key has an invalid value." );
break;
case AO_EOPENDEVICE:
ui_error( UI_ERROR_ERROR,
"ao: cannot open output device." );
break;
case AO_EOPENFILE:
ui_error( UI_ERROR_ERROR,
"ao: cannot open output file '%s'.", filename );
break;
case AO_EFILEEXISTS:
ui_error( UI_ERROR_ERROR, "ao: output file '%s' already exists.",
filename );
break;
case AO_EFAIL:
ui_error( UI_ERROR_ERROR, "ao: unspecified error." );
}
}
static int
parse_driver_options( const char *device, int *driver_id, ao_option **options )
{
char *mutable, *option, *key, *value;
/* Get a copy of the device string we can modify */
if( !device || *device == '\0' )
return 0;
mutable = strdup( device );
if( !mutable ) {
ui_error( UI_ERROR_ERROR, "out of memory at %s:%d", __FILE__, __LINE__ );
return 1;
}
/* First, find the device name */
option = strchr( mutable, ':' );
if( option ) *option++ = '\0';
if( *mutable ) /* ! \0 */
*driver_id = ao_driver_id( mutable );
/* Now parse any further options */
while( option ) {
key = option;
option = strchr( option, ',' );
if( option ) *option++ = '\0';
value = strchr( key, '=' );
if( value ) *value++ = '\0';
if( strcmp( key, "file" ) == 0 ) {
filename = strdup( value );
if( !filename ) {
ui_error( UI_ERROR_ERROR, "out of memory at %s:%d", __FILE__,
__LINE__ );
free( mutable );
return 1;
}
} else if( key && value) {
ao_append_option( options, key, value );
} else if ( first_init ) {
ui_error( UI_ERROR_ERROR, "ignoring badly formed libao option (%s%s)",
key ? key : "", value ? value : "" );
}
}
free( mutable );
return 0;
}
int
sound_lowlevel_init( const char *device, int *freqptr, int *stereoptr )
{
ao_option *options = NULL;
ao_info *driver_info = NULL;
int driver_id = -1;
static ao_sample_format format = { .bits = 0 };
/* To prevent recursive errors */
static int sound_lowlevel_init_in_progress = 0;
int error;
if( sound_lowlevel_init_in_progress ) return 0;
sound_lowlevel_init_in_progress = 1;
ao_initialize();
error = parse_driver_options( device, &driver_id, &options );
if( error ) {
settings_current.sound = 0;
sound_lowlevel_init_in_progress = 0;
return error;
}
if( driver_id == -1 )
driver_id = ao_default_driver_id();
if( driver_id == -1 ) {
ui_error( UI_ERROR_ERROR, "ao: driver '%s' unknown",
device );
settings_current.sound = 0;
sound_lowlevel_init_in_progress = 0;
return 1;
}
driver_info = ao_driver_info( driver_id );
if( driver_info->type == AO_TYPE_FILE &&
format.bits != 0 ) { /* OK. We not want to trunc the file :-) */
ui_error( UI_ERROR_WARNING, "ao: must truncate audio file '%s'",
filename );
}
format.channels = *stereoptr ? 2 : 1;
format.rate = *freqptr;
format.bits = settings_current.sound_force_8bit ? 8 : 16;
format.byte_format = AO_FMT_LITTLE;
sixteenbit = settings_current.sound_force_8bit ? 0 : 1;
if( driver_info->type == AO_TYPE_LIVE ) {
dev_for_ao = ao_open_live( driver_id, &format, options);
} else {
if( !filename ) filename = (char *)default_filename;
dev_for_ao = ao_open_file( driver_id, filename, 1, &format, options);
}
if( !dev_for_ao ) {
driver_error();
settings_current.sound = 0;
sound_lowlevel_init_in_progress = 0;
return 1;
}
ao_free_options( options );
sound_lowlevel_init_in_progress = 0;
first_init = 0;
return 0;
}
void
sound_lowlevel_end( void )
{
if( filename != default_filename ) free( filename );
ao_close(dev_for_ao);
ao_shutdown();
}
void
sound_lowlevel_frame( libspectrum_signed_word *data, int len )
{
static signed char buf8[4096];
void *data8 = data;
len <<= 1; /* now in bytes */
if( !sixteenbit ) {
libspectrum_signed_word *src;
signed char *dst;
int f;
src = data;
dst = buf8;
len >>= 1;
for( f = 0; f < len; f++)
*dst++ = ( int )( ( *src++ ) / 256 );
data8 = buf8;
}
ao_play( dev_for_ao, data8, len );
}
|