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
|
/* sdlsound.c: SDL sound I/O
Copyright (c) 2002-2004 Alexander Yurchenko, Russell Marks, Philip Kendall,
Fredrick Meunier
$Id: sdlsound.c 4031 2009-06-08 00:33:53Z fredm $
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.
*/
#include <config.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <SDL.h>
#include "settings.h"
#include "sfifo.h"
#include "sound.h"
#include "ui/ui.h"
static void sdlwrite( void *userdata, Uint8 *stream, int len );
sfifo_t sound_fifo;
/* Number of Spectrum frames audio latency to use */
#define NUM_FRAMES 2
/* Records sound writer status information */
static int audio_output_started;
int
sound_lowlevel_init( const char *device, int *freqptr, int *stereoptr )
{
SDL_AudioSpec requested, received;
int error;
float hz;
int sound_framesiz;
#ifndef __MORPHOS__
/* I'd rather just use setenv, but Windows doesn't have it */
if( device ) {
const char *environment = "SDL_AUDIODRIVER=";
char *command = malloc( strlen( environment ) + strlen( device ) + 1 );
strcpy( command, environment );
strcat( command, device );
error = putenv( command );
free( command );
if( error ) {
settings_current.sound = 0;
ui_error( UI_ERROR_ERROR, "Couldn't set SDL_AUDIODRIVER: %s",
strerror ( error ) );
return 1;
}
}
#endif /* #ifndef __MORPHOS__ */
SDL_InitSubSystem( SDL_INIT_AUDIO );
memset( &requested, 0, sizeof( SDL_AudioSpec ) );
requested.freq = *freqptr;
requested.channels = *stereoptr ? 2 : 1;
requested.format = AUDIO_S16SYS;
requested.callback = sdlwrite;
/* Adjust relative processor speed to deal with adjusting sound generation
frequency against emulation speed (more flexible than adjusting generated
sample rate) */
hz = (float)sound_get_effective_processor_speed() /
machine_current->timings.tstates_per_frame;
/* Amount of audio data we will accumulate before yielding back to the OS.
Not much point having more than 100Hz playback, we probably get
downgraded by the OS as being a hog too (unlimited Hz limits playback
speed to about 2000% on my Mac, 100Hz allows up to 5000% for me) */
if( hz > 100.0 ) hz = 100.0;
sound_framesiz = *freqptr / hz;
requested.samples = sound_framesiz;
if ( SDL_OpenAudio( &requested, &received ) < 0 ) {
settings_current.sound = 0;
ui_error( UI_ERROR_ERROR, "Couldn't open sound device: %s",
SDL_GetError() );
return 1;
}
*freqptr = received.freq;
if( received.format != AUDIO_S16SYS ) {
/* close audio and then just let SDL convert to this wacky format at a
supported sample rate */
SDL_CloseAudio();
requested.freq = *freqptr;
sound_framesiz = *freqptr / hz;
requested.samples = sound_framesiz;
if( SDL_OpenAudio( &requested, NULL ) < 0 ) {
settings_current.sound = 0;
ui_error( UI_ERROR_ERROR, "Couldn't open sound device: %s",
SDL_GetError() );
return 1;
}
} else {
*stereoptr = received.channels == 1 ? 0 : 1;
}
sound_framesiz = *freqptr / hz;
sound_framesiz <<= 1;
if( ( error = sfifo_init( &sound_fifo, NUM_FRAMES
* received.channels
* sound_framesiz + 1 ) ) ) {
ui_error( UI_ERROR_ERROR, "Problem initialising sound fifo: %s",
strerror ( error ) );
return 1;
}
/* wait to run sound until we have some sound to play */
audio_output_started = 0;
return 0;
}
void
sound_lowlevel_end( void )
{
SDL_PauseAudio( 1 );
SDL_LockAudio();
SDL_CloseAudio();
SDL_QuitSubSystem( SDL_INIT_AUDIO );
sfifo_flush( &sound_fifo );
sfifo_close( &sound_fifo );
}
/* Copy data to fifo */
void
sound_lowlevel_frame( libspectrum_signed_word *data, int len )
{
int i = 0;
/* Convert to bytes */
libspectrum_signed_byte* bytes = (libspectrum_signed_byte*)data;
len <<= 1;
while( len ) {
if( ( i = sfifo_write( &sound_fifo, bytes, len ) ) < 0 ) {
break;
} else if (!i) {
SDL_Delay(10);
}
bytes += i;
len -= i;
}
if( i < 0 ) {
ui_error( UI_ERROR_ERROR, "Couldn't write sound fifo: %s",
strerror( i ) );
}
if( !audio_output_started ) {
SDL_PauseAudio( 0 );
audio_output_started = 1;
}
}
#ifndef MIN
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
/* Write len samples from fifo into stream */
void
sdlwrite( void *userdata, Uint8 *stream, int len )
{
int f;
/* Try to only read an even number of bytes so as not to fragment a sample */
len = MIN( len, sfifo_used( &sound_fifo ) );
len &= sound_stereo ? 0xfffc : 0xfffe;
/* Read input_size bytes from fifo into sound stream */
while( ( f = sfifo_read( &sound_fifo, stream, len ) ) > 0 ) {
stream += f;
len -= f;
}
/* If we ran out of sound, do nothing else as SDL has prefilled
the output buffer with silence :( */
}
|