GTK: SDL sound driver fixed.

This commit is contained in:
Brandon Wright 2019-02-09 19:18:45 -06:00
parent 2ba6553c14
commit b54ded7081
3 changed files with 49 additions and 11 deletions

View File

@ -145,7 +145,7 @@ S9xPortSoundInit ()
Settings.SoundPlaybackRate = playback_rates[gui_config->sound_playback_rate];
if (gui_config->sound_driver == sdl_driver)
S9xInitSound(gui_config->sound_buffer_size, 0);
S9xInitSound(0, 0);
else
S9xInitSound(0, 0);

View File

@ -12,22 +12,47 @@ static void sdl_audio_callback(void *userdata, Uint8 *stream, int len)
((S9xSDLSoundDriver *)userdata)->mix((unsigned char *)stream, len);
}
static void samples_available(void *data)
static void c_samples_available(void *data)
{
SDL_LockAudio();
S9xFinalizeSamples();
SDL_UnlockAudio();
((S9xSDLSoundDriver *)data)->samples_available();
}
void S9xSDLSoundDriver::samples_available()
{
int snes_samples_available = S9xGetSampleCount();
S9xMixSamples((uint8 *)temp, snes_samples_available);
if (Settings.SoundSync && !Settings.TurboMode && !Settings.Mute)
{
mutex.lock();
int samples = buffer->space_empty();
mutex.unlock();
while (samples < snes_samples_available)
{
usleep(100);
mutex.lock();
samples = buffer->space_empty();
mutex.unlock();
}
}
mutex.lock();
buffer->push(temp, snes_samples_available);
mutex.unlock();
}
void S9xSDLSoundDriver::mix(unsigned char *output, int bytes)
{
SDL_LockAudio();
S9xMixSamples(output, bytes >> 1);
SDL_UnlockAudio();
mutex.lock();
if (buffer->avail() >= bytes >> 1)
buffer->read((int16_t *)output, bytes >> 1);
mutex.unlock();
}
S9xSDLSoundDriver::S9xSDLSoundDriver()
{
buffer = NULL;
audiospec = NULL;
}
@ -44,6 +69,8 @@ void S9xSDLSoundDriver::terminate()
if (audiospec)
{
SDL_CloseAudio();
if (buffer)
delete buffer;
free(audiospec);
audiospec = NULL;
}
@ -77,14 +104,14 @@ bool S9xSDLSoundDriver::open_device()
audiospec->freq = Settings.SoundPlaybackRate;
audiospec->channels = 2;
audiospec->format = AUDIO_S16SYS;
audiospec->samples = (gui_config->sound_buffer_size * audiospec->freq / 1000) >> 1;
audiospec->samples = (gui_config->sound_buffer_size * audiospec->freq / 1000) >> 2;
audiospec->callback = sdl_audio_callback;
audiospec->userdata = this;
printf("SDL sound driver initializing...\n");
printf(" --> (Frequency: %dhz, Latency: %dms)...",
audiospec->freq,
(audiospec->samples * 1000 / audiospec->freq) << 1);
(audiospec->samples * 1000 / audiospec->freq));
if (SDL_OpenAudio(audiospec, NULL) < 0)
{
@ -98,7 +125,10 @@ bool S9xSDLSoundDriver::open_device()
printf("OK\n");
S9xSetSamplesAvailableCallback(samples_available, NULL);
buffer = new Resampler(gui_config->sound_buffer_size * audiospec->freq / 500);
buffer->time_ratio(1.0);
S9xSetSamplesAvailableCallback(c_samples_available, this);
return true;
}

View File

@ -11,6 +11,10 @@
#include "gtk_sound.h"
#include "gtk_sound_driver.h"
#include "../../apu/resampler.h"
#include <mutex>
#include <cstdint>
class S9xSDLSoundDriver : public S9xSoundDriver
{
@ -22,9 +26,13 @@ class S9xSDLSoundDriver : public S9xSoundDriver
void start();
void stop();
void mix(unsigned char *output, int bytes);
void samples_available();
private:
SDL_AudioSpec *audiospec;
Resampler *buffer;
std::mutex mutex;
int16_t temp[512];
};
#endif /* __GTK_SOUND_DRIVER_SDL_H */