snes9x/gtk/src/gtk_sound_driver_sdl.cpp

109 lines
2.6 KiB
C++
Raw Normal View History

/*****************************************************************************\
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
This file is licensed under the Snes9x License.
For further information, consult the LICENSE file in the root directory.
\*****************************************************************************/
2010-09-25 17:46:12 +02:00
#include "gtk_sound_driver_sdl.h"
#include "gtk_s9x.h"
#include "apu/apu.h"
#include "snes9x.h"
2010-09-25 17:46:12 +02:00
2019-02-10 02:18:45 +01:00
void S9xSDLSoundDriver::samples_available()
{
int snes_samples_available = S9xGetSampleCount();
S9xMixSamples((uint8 *)temp, snes_samples_available);
if (Settings.SoundSync && !Settings.TurboMode && !Settings.Mute)
{
mutex.lock();
2019-05-14 22:34:25 +02:00
int samples = buffer.space_empty();
2019-02-10 02:18:45 +01:00
mutex.unlock();
while (samples < snes_samples_available)
{
usleep(100);
mutex.lock();
2019-05-14 22:34:25 +02:00
samples = buffer.space_empty();
2019-02-10 02:18:45 +01:00
mutex.unlock();
}
}
mutex.lock();
2019-05-14 22:34:25 +02:00
buffer.push(temp, snes_samples_available);
2019-02-10 02:18:45 +01:00
mutex.unlock();
2010-09-25 17:46:12 +02:00
}
void S9xSDLSoundDriver::mix(unsigned char *output, int bytes)
2010-09-25 17:46:12 +02:00
{
2019-02-10 02:18:45 +01:00
mutex.lock();
2019-05-14 22:34:25 +02:00
if (buffer.avail() >= bytes >> 1)
buffer.read((int16_t *)output, bytes >> 1);
2019-02-10 02:18:45 +01:00
mutex.unlock();
2010-09-25 17:46:12 +02:00
}
S9xSDLSoundDriver::S9xSDLSoundDriver()
2010-09-25 17:46:12 +02:00
{
}
void S9xSDLSoundDriver::init()
2010-09-25 17:46:12 +02:00
{
SDL_InitSubSystem(SDL_INIT_AUDIO);
stop();
2010-09-25 17:46:12 +02:00
}
void S9xSDLSoundDriver::terminate()
2010-09-25 17:46:12 +02:00
{
stop();
2019-05-14 22:34:25 +02:00
SDL_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
2010-09-25 17:46:12 +02:00
}
void S9xSDLSoundDriver::start()
2010-09-25 17:46:12 +02:00
{
if (!gui_config->mute_sound)
{
2019-05-14 22:34:25 +02:00
SDL_PauseAudio(0);
2010-09-25 17:46:12 +02:00
}
}
void S9xSDLSoundDriver::stop()
2010-09-25 17:46:12 +02:00
{
2019-05-14 22:34:25 +02:00
SDL_PauseAudio(1);
2010-09-25 17:46:12 +02:00
}
bool S9xSDLSoundDriver::open_device()
2010-09-25 17:46:12 +02:00
{
2019-05-14 22:34:25 +02:00
audiospec = {};
audiospec.freq = Settings.SoundPlaybackRate;
audiospec.channels = 2;
audiospec.format = AUDIO_S16SYS;
audiospec.samples = (gui_config->sound_buffer_size * audiospec.freq / 1000) >> 2;
audiospec.callback = [](void *userdata, uint8_t *stream, int len) {
((S9xSDLSoundDriver *)userdata)->mix((unsigned char *)stream, len);
};
2019-05-14 22:34:25 +02:00
audiospec.userdata = this;
2010-09-25 17:46:12 +02:00
printf("SDL sound driver initializing...\n");
printf(" --> (Frequency: %dhz, Latency: %dms)...",
2019-05-14 22:34:25 +02:00
audiospec.freq,
(audiospec.samples * 1000 / audiospec.freq));
2010-09-25 17:46:12 +02:00
2019-05-14 22:34:25 +02:00
if (SDL_OpenAudio(&audiospec, NULL) < 0)
2010-09-25 17:46:12 +02:00
{
printf("Failed\n");
return false;
2010-09-25 17:46:12 +02:00
}
printf("OK\n");
2010-09-25 17:46:12 +02:00
2019-05-14 22:34:25 +02:00
buffer.resize(gui_config->sound_buffer_size * audiospec.freq / 500);
2019-02-10 02:18:45 +01:00
S9xSetSamplesAvailableCallback([](void *userdata) {
((decltype(this)) userdata)->samples_available();;
}, this);
2010-09-25 17:46:12 +02:00
return true;
2010-09-25 17:46:12 +02:00
}