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

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

View File

@ -4,7 +4,8 @@
For further information, consult the LICENSE file in the root directory.
\*****************************************************************************/
#include <math.h>
#include <cmath>
#include <vector>
#include "../snes9x.h"
#include "apu.h"
#include "../msu1.h"
@ -14,35 +15,30 @@
#include "bapu/snes/snes.hpp"
#define APU_DEFAULT_INPUT_RATE 31950 // ~ 59.94Hz
#define APU_MINIMUM_SAMPLE_COUNT 512
#define APU_MINIMUM_SAMPLE_BLOCK 128
#define APU_NUMERATOR_NTSC 15664
#define APU_DENOMINATOR_NTSC 328125
#define APU_NUMERATOR_PAL 34176
#define APU_DENOMINATOR_PAL 709379
static const int APU_DEFAULT_INPUT_RATE = 31950; // ~59.94Hz
static const int APU_SAMPLE_BLOCK = 48;
static const int APU_NUMERATOR_NTSC = 15664;
static const int APU_DENOMINATOR_NTSC = 328125;
static const int APU_NUMERATOR_PAL = 34176;
static const int APU_DENOMINATOR_PAL = 709379;
// Max number of sample frames we'll ever generate before call to port API
static const int MAX_SAMPLE_FRAMES = (32040 + 59) / 60;
namespace SNES
{
#include "bapu/dsp/blargg_endian.h"
CPU cpu;
}
namespace spc
{
static apu_callback sa_callback = NULL;
static void *extra_data = NULL;
static apu_callback callback = NULL;
static void *callback_data = NULL;
static bool8 sound_in_sync = TRUE;
static bool8 sound_enabled = FALSE;
static int buffer_size;
static int lag_master = 0;
static int lag = 0;
static uint8 *landing_buffer = NULL;
static uint8 *shrink_buffer = NULL;
static uint16 dsp_buffer[MAX_SAMPLE_FRAMES * 2];
static Resampler *resampler = NULL;
@ -61,112 +57,52 @@ namespace spc
namespace msu
{
static int buffer_size;
static uint8 *landing_buffer = NULL;
// Always 16-bit, Stereo; 1.5x dsp buffer to never overflow
static const int buffer_size = MAX_SAMPLE_FRAMES * 6;
static uint8 mixing_buffer[buffer_size];
static Resampler *resampler = NULL;
static int resample_buffer_size = -1;
static uint8 *resample_buffer = NULL;
static std::vector<uint16> resample_buffer;
}
static void EightBitize (uint8 *, int);
static void DeStereo (uint8 *, int);
static void ReverseStereo (uint8 *, int);
static void UpdatePlaybackRate(void);
static void SPCSnapshotCallback(void);
static inline int S9xAPUGetClock(int32);
static inline int S9xAPUGetClockRemainder(int32);
static void EightBitize (uint8 *buffer, int sample_count)
static void reset_dsp_output()
{
uint8 *buf8 = (uint8 *) buffer;
int16 *buf16 = (int16 *) buffer;
for (int i = 0; i < sample_count; i++)
buf8[i] = (uint8) ((buf16[i] / 256) + 128);
SNES::dsp.spc_dsp.set_output((SNES::SPC_DSP::sample_t *)spc::dsp_buffer,
MAX_SAMPLE_FRAMES * 2);
}
static void DeStereo (uint8 *buffer, int sample_count)
bool8 S9xMixSamples(uint8 *dest, int sample_count)
{
int16 *buf = (int16 *) buffer;
int32 s1, s2;
for (int i = 0; i < (sample_count >> 1); i++)
{
s1 = (int32) buf[2 * i];
s2 = (int32) buf[2 * i + 1];
buf[i] = (int16) ((s1 + s2) >> 1);
}
}
static void ReverseStereo (uint8 *src_buffer, int sample_count)
{
int16 *buffer = (int16 *) src_buffer;
for (int i = 0; i < sample_count; i += 2)
{
buffer[i + 1] ^= buffer[i];
buffer[i] ^= buffer[i + 1];
buffer[i + 1] ^= buffer[i];
}
}
bool8 S9xMixSamples (uint8 *buffer, int sample_count)
{
static int shrink_buffer_size = -1;
uint8 *dest;
if (!Settings.SixteenBitSound || !Settings.Stereo)
{
/* We still need both stereo samples for generating the mono sample */
if (!Settings.Stereo)
sample_count <<= 1;
/* We still have to generate 16-bit samples for bit-dropping, too */
if (shrink_buffer_size < (sample_count << 1))
{
delete[] spc::shrink_buffer;
spc::shrink_buffer = new uint8[sample_count << 1];
shrink_buffer_size = sample_count << 1;
}
dest = spc::shrink_buffer;
}
else
dest = buffer;
if (Settings.MSU1 && msu::resample_buffer_size < (sample_count << 1))
{
delete[] msu::resample_buffer;
msu::resample_buffer = new uint8[sample_count << 1];
msu::resample_buffer_size = sample_count << 1;
}
int16 *out = (int16 *)dest;
if (Settings.Mute)
{
memset(dest, 0, sample_count << 1);
memset(out, 0, sample_count << 1);
spc::resampler->clear();
if (Settings.MSU1)
msu::resampler->clear();
return (FALSE);
}
else
{
if (spc::resampler->avail() >= (sample_count + spc::lag))
if (spc::resampler->avail() >= sample_count)
{
spc::resampler->read((short *) dest, sample_count);
if (spc::lag == spc::lag_master)
spc::lag = 0;
spc::resampler->read((short *)out, sample_count);
if (Settings.MSU1)
{
if (msu::resampler->avail() >= sample_count)
{
msu::resampler->read((short *)msu::resample_buffer, sample_count);
if ((int)msu::resample_buffer.size() < sample_count)
msu::resample_buffer.resize(sample_count);
msu::resampler->read((short *)msu::resample_buffer.data(),
sample_count);
for (int i = 0; i < sample_count; ++i)
*((int16*)(dest+(i * 2))) += *((int16*)(msu::resample_buffer +(i * 2)));
out[i] += msu::resample_buffer[i];
}
else // should never occur
assert(0);
@ -174,58 +110,33 @@ bool8 S9xMixSamples (uint8 *buffer, int sample_count)
}
else
{
memset(buffer, (Settings.SixteenBitSound ? 0 : 128), (sample_count << (Settings.SixteenBitSound ? 1 : 0)) >> (Settings.Stereo ? 0 : 1));
if (spc::lag == 0)
spc::lag = spc::lag_master;
return (FALSE);
memset(out, 0, sample_count << 1);
return false;
}
}
if (Settings.ReverseStereo && Settings.Stereo)
ReverseStereo(dest, sample_count);
if (!Settings.Stereo || !Settings.SixteenBitSound)
{
if (!Settings.Stereo)
{
DeStereo(dest, sample_count);
sample_count >>= 1;
}
if (!Settings.SixteenBitSound)
EightBitize(dest, sample_count);
memcpy(buffer, dest, (sample_count << (Settings.SixteenBitSound ? 1 : 0)));
}
return (TRUE);
return true;
}
int S9xGetSampleCount(void)
{
return (spc::resampler->avail() >> (Settings.Stereo ? 0 : 1));
return spc::resampler->avail();
}
/* TODO: Attach */
void S9xFinalizeSamples(void)
{
bool drop_current_msu1_samples = true;
bool drop_msu1_samples = true;
if (!Settings.Mute)
{
drop_current_msu1_samples = false;
drop_msu1_samples = false;
if (!spc::resampler->push((short *)spc::landing_buffer, SNES::dsp.spc_dsp.sample_count()))
if (!spc::resampler->push((short *)spc::dsp_buffer,
SNES::dsp.spc_dsp.sample_count()))
{
/* We weren't able to process the entire buffer. Potential overrun. */
spc::sound_in_sync = FALSE;
if (Settings.SoundSync && !Settings.TurboMode)
return;
// since we drop the current dsp samples we also want to drop generated msu1 samples
drop_current_msu1_samples = true;
spc::resampler->clear();
msu::resampler->clear();
drop_msu1_samples = true;
}
}
@ -235,31 +146,31 @@ void S9xFinalizeSamples (void)
if (Settings.MSU1)
{
// generate the same number of msu1 samples as dsp samples were generated
S9xMSU1SetOutput((int16 *)msu::landing_buffer, msu::buffer_size);
S9xMSU1SetOutput((int16 *)msu::mixing_buffer, msu::buffer_size);
S9xMSU1Generate(SNES::dsp.spc_dsp.sample_count());
if (!drop_current_msu1_samples && !msu::resampler->push((short *)msu::landing_buffer, S9xMSU1Samples()))
if (drop_msu1_samples)
msu::resampler->clear();
else if (!msu::resampler->push((short *)msu::mixing_buffer, S9xMSU1Samples()))
{
// should not occur, msu buffer is larger and we drop msu samples if spc buffer overruns
assert(0);
}
}
if (!Settings.SoundSync || Settings.TurboMode || Settings.Mute)
spc::sound_in_sync = TRUE;
else
if (spc::resampler->space_empty() >= spc::resampler->space_filled())
else if (spc::resampler->space_empty() >= spc::resampler->space_filled())
spc::sound_in_sync = TRUE;
else
spc::sound_in_sync = FALSE;
SNES::dsp.spc_dsp.set_output((SNES::SPC_DSP::sample_t *) spc::landing_buffer, spc::buffer_size);
reset_dsp_output();
}
void S9xLandSamples(void)
{
if (spc::sa_callback != NULL)
spc::sa_callback(spc::extra_data);
if (spc::callback != NULL)
spc::callback(spc::callback_data);
else
S9xFinalizeSamples();
}
@ -269,7 +180,6 @@ void S9xClearSamples (void)
spc::resampler->clear();
if (Settings.MSU1)
msu::resampler->clear();
spc::lag = spc::lag_master;
}
bool8 S9xSyncSound(void)
@ -284,8 +194,8 @@ bool8 S9xSyncSound (void)
void S9xSetSamplesAvailableCallback(apu_callback callback, void *data)
{
spc::sa_callback = callback;
spc::extra_data = data;
spc::callback = callback;
spc::callback_data = data;
}
void S9xUpdateDynamicRate(int avail, int buffer_size)
@ -317,67 +227,26 @@ static void UpdatePlaybackRate (void)
}
}
bool8 S9xInitSound (int buffer_ms, int lag_ms)
bool8 S9xInitSound(int unused, int unused2)
{
// buffer_ms : buffer size given in millisecond
// lag_ms : allowable time-lag given in millisecond
int sample_count = buffer_ms * 32040 / 1000;
int lag_sample_count = lag_ms * 32040 / 1000;
spc::lag_master = lag_sample_count;
if (Settings.Stereo)
spc::lag_master <<= 1;
spc::lag = spc::lag_master;
if (sample_count < APU_MINIMUM_SAMPLE_COUNT)
sample_count = APU_MINIMUM_SAMPLE_COUNT;
spc::buffer_size = sample_count << 2;
msu::buffer_size = (int)((sample_count << 2) * 1.5); // Always 16-bit, Stereo; 1.5 to never overflow before dsp buffer
printf("Sound buffer size: %d (%d samples)\n", spc::buffer_size, sample_count);
if (spc::landing_buffer)
delete[] spc::landing_buffer;
spc::landing_buffer = new uint8[spc::buffer_size * 2];
if (!spc::landing_buffer)
return (FALSE);
if (msu::landing_buffer)
delete[] msu::landing_buffer;
msu::landing_buffer = new uint8[msu::buffer_size * 2];
if (!msu::landing_buffer)
return (FALSE);
/* The resampler and spc unit use samples (16-bit short) as
arguments. Use 2x in the resampler for buffer leveling with SoundSync */
// The resampler and spc unit use samples (16-bit short) as arguments.
if (!spc::resampler)
{
spc::resampler = new HermiteResampler(spc::buffer_size >> (Settings.SoundSync ? 0 : 1));
spc::resampler = new HermiteResampler(MAX_SAMPLE_FRAMES * 2);
if (!spc::resampler)
{
delete[] spc::landing_buffer;
return (FALSE);
}
}
else
spc::resampler->resize(spc::buffer_size >> (Settings.SoundSync ? 0 : 1));
if (!msu::resampler)
{
msu::resampler = new HermiteResampler(msu::buffer_size);
if (!msu::resampler)
{
delete[] msu::landing_buffer;
return (FALSE);
}
}
else
msu::resampler->resize(msu::buffer_size);
SNES::dsp.spc_dsp.set_output ((SNES::SPC_DSP::sample_t *) spc::landing_buffer, spc::buffer_size);
reset_dsp_output();
UpdatePlaybackRate();
@ -401,7 +270,6 @@ void S9xSetSoundMute (bool8 mute)
void S9xDumpSPCSnapshot(void)
{
SNES::dsp.spc_dsp.dump_spc_snapshot();
}
static void SPCSnapshotCallback(void)
@ -412,8 +280,6 @@ static void SPCSnapshotCallback (void)
bool8 S9xInitAPU(void)
{
spc::landing_buffer = NULL;
spc::shrink_buffer = NULL;
spc::resampler = NULL;
msu::resampler = NULL;
@ -428,36 +294,12 @@ void S9xDeinitAPU (void)
spc::resampler = NULL;
}
if (spc::landing_buffer)
{
delete[] spc::landing_buffer;
spc::landing_buffer = NULL;
}
if (spc::shrink_buffer)
{
delete[] spc::shrink_buffer;
spc::shrink_buffer = NULL;
}
if (msu::resampler)
{
delete msu::resampler;
msu::resampler = NULL;
}
if (msu::landing_buffer)
{
delete[] msu::landing_buffer;
msu::landing_buffer = NULL;
}
if (msu::resample_buffer)
{
delete[] msu::resample_buffer;
msu::resample_buffer = NULL;
}
S9xMSU1DeInit();
}
@ -505,7 +347,7 @@ void S9xAPUEndScanline (void)
S9xAPUExecute();
SNES::dsp.synchronize();
if (SNES::dsp.spc_dsp.sample_count() >= APU_MINIMUM_SAMPLE_BLOCK || !spc::sound_in_sync)
if (SNES::dsp.spc_dsp.sample_count() >= APU_SAMPLE_BLOCK || !spc::sound_in_sync)
S9xLandSamples();
}
@ -532,7 +374,7 @@ void S9xResetAPU (void)
SNES::cpu.frequency = Settings.PAL ? PAL_MASTER_CLOCK : NTSC_MASTER_CLOCK;
SNES::smp.power();
SNES::dsp.power();
SNES::dsp.spc_dsp.set_output ((SNES::SPC_DSP::sample_t *) spc::landing_buffer, spc::buffer_size >> 1);
reset_dsp_output();
SNES::dsp.spc_dsp.set_spc_snapshot_callback(SPCSnapshotCallback);
spc::resampler->clear();
@ -548,7 +390,7 @@ void S9xSoftResetAPU (void)
SNES::cpu.reset();
SNES::smp.reset();
SNES::dsp.reset();
SNES::dsp.spc_dsp.set_output ((SNES::SPC_DSP::sample_t *) spc::landing_buffer, spc::buffer_size >> 1);
reset_dsp_output();
spc::resampler->clear();

View File

@ -62,7 +62,7 @@ class HermiteResampler : public Resampler
clear (void)
{
ring_buffer::clear ();
r_frac = 1.0;
r_frac = 0.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) floor (((size >> 2) - r_frac) / r_step) * 2;
return (int) trunc (((size >> 2) - r_frac) / r_step) * 2;
}
};

View File

@ -85,17 +85,17 @@ bool S9xAlsaSoundDriver::open_device()
}
printf (" --> (%s, %s, %dhz, %d ms)...\n",
Settings.SixteenBitSound ? "16-bit" : "8-bit",
Settings.Stereo ? "Stereo" : "Mono",
"16-bit",
"Stereo",
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, Settings.SixteenBitSound ? SND_PCM_FORMAT_S16 : SND_PCM_FORMAT_U8);
snd_pcm_hw_params_set_format (pcm, hw_params, SND_PCM_FORMAT_S16);
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, Settings.Stereo ? 2 : 1);
snd_pcm_hw_params_set_channels (pcm, hw_params, 2);
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 () >> (Settings.Stereo ? 1 : 0)))
if (frames < (S9xGetSampleCount () >> 1))
{
S9xClearSamples ();
return;
}
}
frames = MIN (frames, S9xGetSampleCount () >> (Settings.Stereo ? 1 : 0));
frames = MIN (frames, S9xGetSampleCount () >> 1);
bytes = snd_pcm_frames_to_bytes (pcm, frames);
@ -212,7 +212,7 @@ S9xAlsaSoundDriver::samples_available ()
sound_buffer_size = bytes;
}
S9xMixSamples (sound_buffer, frames << (Settings.Stereo ? 1 : 0));
S9xMixSamples (sound_buffer, frames * 2);
frames_written = 0;

View File

@ -66,10 +66,7 @@ bool S9xOSSSoundDriver::open_device()
output_buffer_size = (gui_config->sound_buffer_size * Settings.SoundPlaybackRate) / 1000;
if (Settings.Stereo)
output_buffer_size *= 2;
if (Settings.SixteenBitSound)
output_buffer_size *= 2;
output_buffer_size *= 4;
if (output_buffer_size < 256)
output_buffer_size = 256;
@ -106,35 +103,16 @@ bool S9xOSSSoundDriver::open_device()
printf ("OK\n");
if (Settings.SixteenBitSound)
{
printf (" --> (Format: 16-bit)...");
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");
if (Settings.Stereo)
{
temp = 2;
printf (" --> (Stereo)...");
}
else
{
temp = 1;
printf (" --> (Mono)...");
}
if (ioctl (filedes, SNDCTL_DSP_CHANNELS, &temp) < 0)
goto close_fail;
@ -160,9 +138,7 @@ bool S9xOSSSoundDriver::open_device()
printf (" --> (Buffer size: %d bytes, %dms latency)...",
output_buffer_size,
(((output_buffer_size * 1000) >> (Settings.Stereo ? 1 : 0))
>> (Settings.SixteenBitSound ? 1 : 0))
/ (Settings.SoundPlaybackRate));
(output_buffer_size * 250) / Settings.SoundPlaybackRate);
printf ("OK\n");
@ -203,29 +179,28 @@ 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 >> (Settings.SixteenBitSound ? 1 : 0)))
if (samples_to_write > (info.bytes >> 1))
{
S9xClearSamples ();
return;
}
}
samples_to_write = MIN (info.bytes >> (Settings.SixteenBitSound ? 1 : 0),
samples_to_write);
samples_to_write = MIN (info.bytes >> 1, samples_to_write) & ~1;
if (samples_to_write < 0)
return;
if (sound_buffer_size < samples_to_write << (Settings.SixteenBitSound ? 1 : 0))
if (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);
sound_buffer = (uint8 *) realloc (sound_buffer, samples_to_write * 2);
sound_buffer_size = samples_to_write * 2;
}
S9xMixSamples (sound_buffer, samples_to_write);
bytes_written = 0;
bytes_to_write = samples_to_write << (Settings.SixteenBitSound ? 1 : 0);
bytes_to_write = samples_to_write * 2;
while (bytes_to_write > bytes_written)
{

View File

@ -10,7 +10,7 @@
static inline int
frames_to_bytes (int frames)
{
return (frames * (Settings.SixteenBitSound ? 2 : 1) * (Settings.Stereo ? 2 : 1));
return frames * 4;
}
static void
@ -106,8 +106,8 @@ bool S9xPortAudioSoundDriver::open_device()
audio_stream = NULL;
}
param.channelCount = Settings.Stereo ? 2 : 1;
param.sampleFormat = Settings.SixteenBitSound ? paInt16 : paUInt8;
param.channelCount = 2;
param.sampleFormat = paInt16;
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 () >> (Settings.Stereo ? 1 : 0)))
if (frames < (S9xGetSampleCount () >> 1))
{
S9xClearSamples ();
return;
}
}
frames = MIN (frames, S9xGetSampleCount () >> (Settings.Stereo ? 1 : 0));
frames = MIN (frames, S9xGetSampleCount () >> 1);
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 << (Settings.Stereo ? 1 : 0));
S9xMixSamples (sound_buffer, frames << 1);
Pa_WriteStream (audio_stream, sound_buffer, frames);
}

View File

@ -11,8 +11,7 @@
#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();
}
@ -25,13 +24,11 @@ S9xPulseSoundDriver::S9xPulseSoundDriver ()
buffer_size = 0;
}
void
S9xPulseSoundDriver::init ()
void S9xPulseSoundDriver::init()
{
}
void
S9xPulseSoundDriver::terminate ()
void S9xPulseSoundDriver::terminate()
{
S9xSetSamplesAvailableCallback(NULL, NULL);
@ -56,36 +53,30 @@ S9xPulseSoundDriver::terminate ()
}
}
void
S9xPulseSoundDriver::start ()
void S9xPulseSoundDriver::start()
{
}
void
S9xPulseSoundDriver::stop ()
void S9xPulseSoundDriver::stop()
{
}
void
S9xPulseSoundDriver::lock ()
void S9xPulseSoundDriver::lock()
{
pa_threaded_mainloop_lock(mainloop);
}
void
S9xPulseSoundDriver::unlock ()
void S9xPulseSoundDriver::unlock()
{
pa_threaded_mainloop_unlock(mainloop);
}
void
S9xPulseSoundDriver::wait ()
void S9xPulseSoundDriver::wait()
{
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;
int state;
@ -100,8 +91,7 @@ context_state_cb (pa_context *c, void *userdata)
}
}
static void
stream_state_callback (pa_stream *p, void *userdata)
static void stream_state_callback(pa_stream *p, void *userdata)
{
S9xPulseSoundDriver *driver = (S9xPulseSoundDriver *)userdata;
int state;
@ -123,13 +113,13 @@ bool S9xPulseSoundDriver::open_device()
pa_buffer_attr buffer_attr;
const pa_buffer_attr *actual_buffer_attr;
ss.channels = Settings.Stereo ? 2 : 1;
ss.format = Settings.SixteenBitSound ? PA_SAMPLE_S16NE : PA_SAMPLE_U8;
ss.channels = 2;
ss.format = PA_SAMPLE_S16NE;
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 = -1;
buffer_attr.maxlength = buffer_attr.tlength * 2;
buffer_attr.minreq = -1;
buffer_attr.prebuf = -1;
@ -137,8 +127,8 @@ bool S9xPulseSoundDriver::open_device()
printf(" --> (%dhz, %s %s, %dms)...",
Settings.SoundPlaybackRate,
Settings.SixteenBitSound ? "16-bit" : "8-bit",
Settings.Stereo ? "Stereo" : "Mono",
"16-bit",
"Stereo",
gui_config->sound_buffer_size);
fflush(stdout);
@ -217,8 +207,7 @@ error0:
return false;
}
void
S9xPulseSoundDriver::samples_available ()
void S9xPulseSoundDriver::samples_available()
{
size_t bytes;
int samples;
@ -243,14 +232,14 @@ S9xPulseSoundDriver::samples_available ()
if (Settings.DynamicRateControl)
{
if ((int) bytes < (samples << (Settings.SixteenBitSound ? 1 : 0)))
if ((int)bytes < (samples * 2))
{
S9xClearSamples();
return;
}
}
bytes = MIN ((int) bytes, (samples << (Settings.SixteenBitSound ? 1 : 0)));
bytes = MIN((int)bytes, samples * 2) & ~1;
if (!bytes)
return;
@ -263,13 +252,14 @@ S9xPulseSoundDriver::samples_available ()
unlock();
return;
}
if (bytes <= 0 || !output_buffer)
{
unlock();
return;
}
S9xMixSamples ((uint8 *) output_buffer, bytes >> (Settings.SixteenBitSound ? 1 : 0));
S9xMixSamples((uint8 *)output_buffer, bytes >> 1);
pa_stream_write(stream, output_buffer, bytes, NULL, 0, PA_SEEK_RELATIVE);
unlock();