Revert "APU: Big refactor." I'll put this in a branch.

This reverts commit aebfc85cdf.
This commit is contained in:
Brandon Wright 2019-02-05 17:20:23 -06:00
parent aebfc85cdf
commit 19f03c44de
6 changed files with 659 additions and 466 deletions

File diff suppressed because it is too large Load Diff

View File

@ -62,7 +62,7 @@ class HermiteResampler : public Resampler
clear (void)
{
ring_buffer::clear ();
r_frac = 0.0;
r_frac = 1.0;
r_left [0] = r_left [1] = r_left [2] = r_left [3] = 0;
r_right[0] = r_right[1] = r_right[2] = r_right[3] = 0;
}
@ -138,7 +138,7 @@ class HermiteResampler : public Resampler
return (ring_buffer::space_filled() + sizeof(short) - 1) / sizeof(short);
}
return (int) trunc (((size >> 2) - r_frac) / r_step) * 2;
return (int) floor (((size >> 2) - r_frac) / r_step) * 2;
}
};

View File

@ -85,17 +85,17 @@ bool S9xAlsaSoundDriver::open_device()
}
printf (" --> (%s, %s, %dhz, %d ms)...\n",
"16-bit",
"Stereo",
Settings.SixteenBitSound ? "16-bit" : "8-bit",
Settings.Stereo ? "Stereo" : "Mono",
Settings.SoundPlaybackRate,
gui_config->sound_buffer_size);
snd_pcm_hw_params_alloca (&hw_params);
snd_pcm_hw_params_any (pcm, hw_params);
snd_pcm_hw_params_set_format (pcm, hw_params, SND_PCM_FORMAT_S16);
snd_pcm_hw_params_set_format (pcm, hw_params, Settings.SixteenBitSound ? SND_PCM_FORMAT_S16 : SND_PCM_FORMAT_U8);
snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_rate_resample (pcm, hw_params, 0);
snd_pcm_hw_params_set_channels (pcm, hw_params, 2);
snd_pcm_hw_params_set_channels (pcm, hw_params, Settings.Stereo ? 2 : 1);
snd_pcm_hw_params_get_rate_min (hw_params, &min, NULL);
snd_pcm_hw_params_get_rate_max (hw_params, &max, NULL);
@ -190,14 +190,14 @@ S9xAlsaSoundDriver::samples_available ()
{
// Using rate control, we should always keep the emulator's sound buffers empty to
// maintain an accurate measurement.
if (frames < (S9xGetSampleCount () >> 1))
if (frames < (S9xGetSampleCount () >> (Settings.Stereo ? 1 : 0)))
{
S9xClearSamples ();
return;
}
}
frames = MIN (frames, S9xGetSampleCount () >> 1);
frames = MIN (frames, S9xGetSampleCount () >> (Settings.Stereo ? 1 : 0));
bytes = snd_pcm_frames_to_bytes (pcm, frames);
@ -212,7 +212,7 @@ S9xAlsaSoundDriver::samples_available ()
sound_buffer_size = bytes;
}
S9xMixSamples (sound_buffer, frames * 2);
S9xMixSamples (sound_buffer, frames << (Settings.Stereo ? 1 : 0));
frames_written = 0;

View File

@ -66,7 +66,10 @@ bool S9xOSSSoundDriver::open_device()
output_buffer_size = (gui_config->sound_buffer_size * Settings.SoundPlaybackRate) / 1000;
output_buffer_size *= 4;
if (Settings.Stereo)
output_buffer_size *= 2;
if (Settings.SixteenBitSound)
output_buffer_size *= 2;
if (output_buffer_size < 256)
output_buffer_size = 256;
@ -103,16 +106,35 @@ bool S9xOSSSoundDriver::open_device()
printf ("OK\n");
printf (" --> (Format: 16-bit)...");
if (Settings.SixteenBitSound)
{
printf (" --> (Format: 16-bit)...");
temp = AFMT_S16_LE;
if (ioctl (filedes, SNDCTL_DSP_SETFMT, &temp) < 0)
goto close_fail;
temp = AFMT_S16_LE;
if (ioctl (filedes, SNDCTL_DSP_SETFMT, &temp) < 0)
goto close_fail;
}
else
{
printf (" --> (Format: 8-bit)...");
temp = AFMT_U8;
if (ioctl (filedes, SNDCTL_DSP_SETFMT, &temp) < 0)
goto close_fail;
}
printf ("OK\n");
temp = 2;
printf (" --> (Stereo)...");
if (Settings.Stereo)
{
temp = 2;
printf (" --> (Stereo)...");
}
else
{
temp = 1;
printf (" --> (Mono)...");
}
if (ioctl (filedes, SNDCTL_DSP_CHANNELS, &temp) < 0)
goto close_fail;
@ -138,7 +160,9 @@ bool S9xOSSSoundDriver::open_device()
printf (" --> (Buffer size: %d bytes, %dms latency)...",
output_buffer_size,
(output_buffer_size * 250) / Settings.SoundPlaybackRate);
(((output_buffer_size * 1000) >> (Settings.Stereo ? 1 : 0))
>> (Settings.SixteenBitSound ? 1 : 0))
/ (Settings.SoundPlaybackRate));
printf ("OK\n");
@ -179,28 +203,29 @@ S9xOSSSoundDriver::samples_available ()
{
// Using rate control, we should always keep the emulator's sound buffers empty to
// maintain an accurate measurement.
if (samples_to_write > (info.bytes >> 1))
if (samples_to_write > (info.bytes >> (Settings.SixteenBitSound ? 1 : 0)))
{
S9xClearSamples ();
return;
}
}
samples_to_write = MIN (info.bytes >> 1, samples_to_write) & ~1;
samples_to_write = MIN (info.bytes >> (Settings.SixteenBitSound ? 1 : 0),
samples_to_write);
if (samples_to_write < 0)
return;
if (sound_buffer_size < samples_to_write * 2)
if (sound_buffer_size < samples_to_write << (Settings.SixteenBitSound ? 1 : 0))
{
sound_buffer = (uint8 *) realloc (sound_buffer, samples_to_write * 2);
sound_buffer_size = samples_to_write * 2;
sound_buffer = (uint8 *) realloc (sound_buffer, samples_to_write << (Settings.SixteenBitSound ? 1 : 0));
sound_buffer_size = samples_to_write << (Settings.SixteenBitSound ? 1 : 0);
}
S9xMixSamples (sound_buffer, samples_to_write);
bytes_written = 0;
bytes_to_write = samples_to_write * 2;
bytes_to_write = samples_to_write << (Settings.SixteenBitSound ? 1 : 0);
while (bytes_to_write > bytes_written)
{

View File

@ -10,7 +10,7 @@
static inline int
frames_to_bytes (int frames)
{
return frames * 4;
return (frames * (Settings.SixteenBitSound ? 2 : 1) * (Settings.Stereo ? 2 : 1));
}
static void
@ -106,8 +106,8 @@ bool S9xPortAudioSoundDriver::open_device()
audio_stream = NULL;
}
param.channelCount = 2;
param.sampleFormat = paInt16;
param.channelCount = Settings.Stereo ? 2 : 1;
param.sampleFormat = Settings.SixteenBitSound ? paInt16 : paUInt8;
param.hostApiSpecificStreamInfo = NULL;
printf ("PortAudio sound driver initializing...\n");
@ -200,14 +200,14 @@ S9xPortAudioSoundDriver::samples_available ()
{
// Using rate control, we should always keep the emulator's sound buffers empty to
// maintain an accurate measurement.
if (frames < (S9xGetSampleCount () >> 1))
if (frames < (S9xGetSampleCount () >> (Settings.Stereo ? 1 : 0)))
{
S9xClearSamples ();
return;
}
}
frames = MIN (frames, S9xGetSampleCount () >> 1);
frames = MIN (frames, S9xGetSampleCount () >> (Settings.Stereo ? 1 : 0));
bytes = frames_to_bytes (frames);
if (sound_buffer_size < bytes || sound_buffer == NULL)
@ -216,7 +216,7 @@ S9xPortAudioSoundDriver::samples_available ()
sound_buffer_size = bytes;
}
S9xMixSamples (sound_buffer, frames << 1);
S9xMixSamples (sound_buffer, frames << (Settings.Stereo ? 1 : 0));
Pa_WriteStream (audio_stream, sound_buffer, frames);
}

View File

@ -11,12 +11,13 @@
#include <sys/time.h>
#include <fcntl.h>
static void pulse_samples_available(void *data)
static void
pulse_samples_available (void *data)
{
((S9xPulseSoundDriver *)data)->samples_available();
((S9xPulseSoundDriver *) data)->samples_available ();
}
S9xPulseSoundDriver::S9xPulseSoundDriver()
S9xPulseSoundDriver::S9xPulseSoundDriver ()
{
mainloop = NULL;
context = NULL;
@ -24,85 +25,94 @@ S9xPulseSoundDriver::S9xPulseSoundDriver()
buffer_size = 0;
}
void S9xPulseSoundDriver::init()
void
S9xPulseSoundDriver::init ()
{
}
void S9xPulseSoundDriver::terminate()
void
S9xPulseSoundDriver::terminate ()
{
S9xSetSamplesAvailableCallback(NULL, NULL);
S9xSetSamplesAvailableCallback (NULL, NULL);
if (mainloop)
pa_threaded_mainloop_stop(mainloop);
pa_threaded_mainloop_stop (mainloop);
if (stream)
{
pa_stream_disconnect(stream);
pa_stream_unref(stream);
pa_stream_disconnect (stream);
pa_stream_unref (stream);
}
if (context)
{
pa_context_disconnect(context);
pa_context_unref(context);
pa_context_disconnect (context);
pa_context_unref (context);
}
if (mainloop)
{
pa_threaded_mainloop_free(mainloop);
pa_threaded_mainloop_free (mainloop);
}
}
void S9xPulseSoundDriver::start()
void
S9xPulseSoundDriver::start ()
{
}
void S9xPulseSoundDriver::stop()
void
S9xPulseSoundDriver::stop ()
{
}
void S9xPulseSoundDriver::lock()
void
S9xPulseSoundDriver::lock ()
{
pa_threaded_mainloop_lock(mainloop);
pa_threaded_mainloop_lock (mainloop);
}
void S9xPulseSoundDriver::unlock()
void
S9xPulseSoundDriver::unlock ()
{
pa_threaded_mainloop_unlock(mainloop);
pa_threaded_mainloop_unlock (mainloop);
}
void S9xPulseSoundDriver::wait()
void
S9xPulseSoundDriver::wait ()
{
pa_threaded_mainloop_wait(mainloop);
pa_threaded_mainloop_wait (mainloop);
}
static void context_state_cb(pa_context *c, void *userdata)
static void
context_state_cb (pa_context *c, void *userdata)
{
S9xPulseSoundDriver *driver = (S9xPulseSoundDriver *)userdata;
S9xPulseSoundDriver *driver = (S9xPulseSoundDriver *) userdata;
int state;
state = pa_context_get_state(c);
state = pa_context_get_state (c);
if (state == PA_CONTEXT_READY ||
state == PA_CONTEXT_FAILED ||
if (state == PA_CONTEXT_READY ||
state == PA_CONTEXT_FAILED ||
state == PA_CONTEXT_TERMINATED)
{
pa_threaded_mainloop_signal(driver->mainloop, 0);
pa_threaded_mainloop_signal (driver->mainloop, 0);
}
}
static void stream_state_callback(pa_stream *p, void *userdata)
static void
stream_state_callback (pa_stream *p, void *userdata)
{
S9xPulseSoundDriver *driver = (S9xPulseSoundDriver *)userdata;
S9xPulseSoundDriver *driver = (S9xPulseSoundDriver *) userdata;
int state;
state = pa_stream_get_state(p);
state = pa_stream_get_state (p);
if (state == PA_STREAM_READY ||
state == PA_STREAM_FAILED ||
if (state == PA_STREAM_READY ||
state == PA_STREAM_FAILED ||
state == PA_STREAM_TERMINATED)
{
pa_threaded_mainloop_signal(driver->mainloop, 0);
pa_threaded_mainloop_signal (driver->mainloop, 0);
}
}
@ -113,154 +123,154 @@ bool S9xPulseSoundDriver::open_device()
pa_buffer_attr buffer_attr;
const pa_buffer_attr *actual_buffer_attr;
ss.channels = 2;
ss.format = PA_SAMPLE_S16NE;
ss.rate = Settings.SoundPlaybackRate;
ss.channels = Settings.Stereo ? 2 : 1;
ss.format = Settings.SixteenBitSound ? PA_SAMPLE_S16NE : PA_SAMPLE_U8;
ss.rate = Settings.SoundPlaybackRate;
buffer_attr.fragsize = -1;
buffer_attr.tlength = pa_usec_to_bytes(gui_config->sound_buffer_size * 1000, &ss);
buffer_attr.maxlength = buffer_attr.tlength * 2;
buffer_attr.minreq = -1;
buffer_attr.prebuf = -1;
buffer_attr.fragsize = -1;
buffer_attr.tlength = pa_usec_to_bytes (gui_config->sound_buffer_size * 1000, &ss);
buffer_attr.maxlength = -1;
buffer_attr.minreq = -1;
buffer_attr.prebuf = -1;
printf("PulseAudio sound driver initializing...\n");
printf ("PulseAudio sound driver initializing...\n");
printf(" --> (%dhz, %s %s, %dms)...",
Settings.SoundPlaybackRate,
"16-bit",
"Stereo",
gui_config->sound_buffer_size);
fflush(stdout);
printf (" --> (%dhz, %s %s, %dms)...",
Settings.SoundPlaybackRate,
Settings.SixteenBitSound ? "16-bit" : "8-bit",
Settings.Stereo ? "Stereo" : "Mono",
gui_config->sound_buffer_size);
fflush (stdout);
mainloop = pa_threaded_mainloop_new();
mainloop = pa_threaded_mainloop_new ();
if (!mainloop)
{
fprintf(stderr, "Failed to create mainloop.\n");
fprintf (stderr, "Failed to create mainloop.\n");
goto error0;
}
context = pa_context_new(pa_threaded_mainloop_get_api(mainloop), "Snes9x");
context = pa_context_new (pa_threaded_mainloop_get_api (mainloop), "Snes9x");
if (!context)
goto error1;
pa_context_set_state_callback(context, context_state_cb, this);
if ((err = pa_context_connect(context, NULL, PA_CONTEXT_NOFLAGS, NULL)) != 0)
pa_context_set_state_callback (context, context_state_cb, this);
if ((err = pa_context_connect (context, NULL, PA_CONTEXT_NOFLAGS, NULL)) != 0)
goto error2;
lock();
lock ();
if ((err = pa_threaded_mainloop_start(mainloop)) != 0)
if ((err = pa_threaded_mainloop_start (mainloop)) != 0)
goto error2;
wait();
wait ();
if ((err = pa_context_get_state(context)) != PA_CONTEXT_READY)
if ((err = pa_context_get_state (context)) != PA_CONTEXT_READY)
{
printf("Coundn't create context: State: %d\n", err);
printf ("Coundn't create context: State: %d\n", err);
goto error2;
}
stream = pa_stream_new(context, "Game", &ss, NULL);
stream = pa_stream_new (context, "Game", &ss, NULL);
if (!stream)
goto error2;
pa_stream_set_state_callback(stream, stream_state_callback, this);
pa_stream_set_state_callback (stream, stream_state_callback, this);
if (pa_stream_connect_playback(stream,
NULL,
&buffer_attr,
PA_STREAM_ADJUST_LATENCY,
NULL,
NULL) < 0)
if (pa_stream_connect_playback (stream,
NULL,
&buffer_attr,
PA_STREAM_ADJUST_LATENCY,
NULL,
NULL) < 0)
goto error3;
wait();
wait ();
if (pa_stream_get_state(stream) != PA_STREAM_READY)
if (pa_stream_get_state (stream) != PA_STREAM_READY)
{
goto error3;
}
actual_buffer_attr = pa_stream_get_buffer_attr(stream);
actual_buffer_attr = pa_stream_get_buffer_attr (stream);
buffer_size = actual_buffer_attr->tlength;
printf("OK\n");
printf ("OK\n");
S9xSetSamplesAvailableCallback(pulse_samples_available, this);
S9xSetSamplesAvailableCallback (pulse_samples_available, this);
unlock();
unlock ();
return true;
error3:
pa_stream_disconnect(stream);
pa_stream_unref(stream);
pa_stream_disconnect (stream);
pa_stream_unref (stream);
error2:
pa_context_disconnect(context);
pa_context_unref(context);
unlock();
pa_context_disconnect (context);
pa_context_unref (context);
unlock ();
error1:
pa_threaded_mainloop_free(mainloop);
pa_threaded_mainloop_free (mainloop);
error0:
printf("Failed: %s\n", pa_strerror(err));
printf ("Failed: %s\n", pa_strerror (err));
return false;
}
void S9xPulseSoundDriver::samples_available()
void
S9xPulseSoundDriver::samples_available ()
{
size_t bytes;
int samples;
const pa_buffer_attr *buffer_attr;
void *output_buffer = NULL;
lock();
bytes = pa_stream_writable_size(stream);
buffer_attr = pa_stream_get_buffer_attr(stream);
unlock();
lock ();
bytes = pa_stream_writable_size (stream);
buffer_attr = pa_stream_get_buffer_attr (stream);
unlock ();
buffer_size = buffer_attr->tlength;
if (Settings.DynamicRateControl)
{
S9xUpdateDynamicRate(bytes, buffer_size);
S9xUpdateDynamicRate (bytes, buffer_size);
}
S9xFinalizeSamples();
S9xFinalizeSamples ();
samples = S9xGetSampleCount();
samples = S9xGetSampleCount ();
if (Settings.DynamicRateControl)
{
if ((int)bytes < (samples * 2))
if ((int) bytes < (samples << (Settings.SixteenBitSound ? 1 : 0)))
{
S9xClearSamples();
S9xClearSamples ();
return;
}
}
bytes = MIN((int)bytes, samples * 2) & ~1;
bytes = MIN ((int) bytes, (samples << (Settings.SixteenBitSound ? 1 : 0)));
if (!bytes)
return;
lock();
lock ();
if (pa_stream_begin_write(stream, &output_buffer, &bytes) != 0)
if (pa_stream_begin_write (stream, &output_buffer, &bytes) != 0)
{
pa_stream_flush(stream, NULL, NULL);
unlock();
pa_stream_flush (stream, NULL, NULL);
unlock ();
return;
}
if (bytes <= 0 || !output_buffer)
{
unlock();
unlock ();
return;
}
S9xMixSamples((uint8 *)output_buffer, bytes >> 1);
pa_stream_write(stream, output_buffer, bytes, NULL, 0, PA_SEEK_RELATIVE);
S9xMixSamples ((uint8 *) output_buffer, bytes >> (Settings.SixteenBitSound ? 1 : 0));
pa_stream_write (stream, output_buffer, bytes, NULL, 0, PA_SEEK_RELATIVE);
unlock();
unlock ();
}