2018-11-16 00:42:29 +01:00
|
|
|
/*****************************************************************************\
|
|
|
|
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_pulse.h"
|
2019-02-07 02:41:33 +01:00
|
|
|
#include "gtk_s9x.h"
|
2020-07-05 00:53:38 +02:00
|
|
|
#include "snes9x.h"
|
|
|
|
#include "apu/apu.h"
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
#include <fcntl.h>
|
2010-09-25 17:46:12 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
static void pulse_samples_available(void *data)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
((S9xPulseSoundDriver *)data)->samples_available();
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
S9xPulseSoundDriver::S9xPulseSoundDriver()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2017-11-23 01:14:49 +01:00
|
|
|
mainloop = NULL;
|
|
|
|
context = NULL;
|
|
|
|
stream = NULL;
|
2010-09-25 17:46:12 +02:00
|
|
|
buffer_size = 0;
|
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
void S9xPulseSoundDriver::init()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
void S9xPulseSoundDriver::terminate()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
S9xSetSamplesAvailableCallback(NULL, NULL);
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2017-12-07 18:36:50 +01:00
|
|
|
if (mainloop)
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_threaded_mainloop_stop(mainloop);
|
2017-12-07 18:36:50 +01:00
|
|
|
|
2017-11-23 01:14:49 +01:00
|
|
|
if (stream)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_stream_disconnect(stream);
|
|
|
|
pa_stream_unref(stream);
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2017-11-23 01:14:49 +01:00
|
|
|
if (context)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_context_disconnect(context);
|
|
|
|
pa_context_unref(context);
|
2017-11-23 01:14:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mainloop)
|
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_threaded_mainloop_free(mainloop);
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
void S9xPulseSoundDriver::start()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
void S9xPulseSoundDriver::stop()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
void S9xPulseSoundDriver::lock()
|
2017-11-23 01:14:49 +01:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_threaded_mainloop_lock(mainloop);
|
2017-11-23 01:14:49 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
void S9xPulseSoundDriver::unlock()
|
2017-11-23 01:14:49 +01:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_threaded_mainloop_unlock(mainloop);
|
2017-11-23 01:14:49 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
void S9xPulseSoundDriver::wait()
|
2017-11-23 01:14:49 +01:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_threaded_mainloop_wait(mainloop);
|
2017-11-23 01:14:49 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
static void context_state_cb(pa_context *c, void *userdata)
|
2017-11-23 01:14:49 +01:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
S9xPulseSoundDriver *driver = (S9xPulseSoundDriver *)userdata;
|
2017-11-23 01:14:49 +01:00
|
|
|
int state;
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
state = pa_context_get_state(c);
|
2017-11-23 01:14:49 +01:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
if (state == PA_CONTEXT_READY ||
|
|
|
|
state == PA_CONTEXT_FAILED ||
|
2017-11-23 01:14:49 +01:00
|
|
|
state == PA_CONTEXT_TERMINATED)
|
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_threaded_mainloop_signal(driver->mainloop, 0);
|
2017-11-23 01:14:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
static void stream_state_callback(pa_stream *p, void *userdata)
|
2017-11-23 01:14:49 +01:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
S9xPulseSoundDriver *driver = (S9xPulseSoundDriver *)userdata;
|
2017-11-23 01:14:49 +01:00
|
|
|
int state;
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
state = pa_stream_get_state(p);
|
2017-11-23 01:14:49 +01:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
if (state == PA_STREAM_READY ||
|
|
|
|
state == PA_STREAM_FAILED ||
|
2017-11-23 01:14:49 +01:00
|
|
|
state == PA_STREAM_TERMINATED)
|
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_threaded_mainloop_signal(driver->mainloop, 0);
|
2017-11-23 01:14:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-28 23:32:32 +01:00
|
|
|
bool S9xPulseSoundDriver::open_device()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2017-11-23 01:14:49 +01:00
|
|
|
int err = PA_ERR_UNKNOWN;
|
2010-09-25 17:46:12 +02:00
|
|
|
pa_sample_spec ss;
|
|
|
|
pa_buffer_attr buffer_attr;
|
2017-11-23 01:14:49 +01:00
|
|
|
const pa_buffer_attr *actual_buffer_attr;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
ss.channels = 2;
|
|
|
|
ss.format = PA_SAMPLE_S16NE;
|
|
|
|
ss.rate = Settings.SoundPlaybackRate;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
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;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
printf("PulseAudio sound driver initializing...\n");
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
printf(" --> (%dhz, 16-bit Stereo, %dms)...",
|
2019-02-06 00:21:23 +01:00
|
|
|
Settings.SoundPlaybackRate,
|
|
|
|
gui_config->sound_buffer_size);
|
|
|
|
fflush(stdout);
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
mainloop = pa_threaded_mainloop_new();
|
2017-11-23 01:14:49 +01:00
|
|
|
if (!mainloop)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
fprintf(stderr, "Failed to create mainloop.\n");
|
2017-11-23 01:14:49 +01:00
|
|
|
goto error0;
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
context = pa_context_new(pa_threaded_mainloop_get_api(mainloop), "Snes9x");
|
2017-11-23 01:14:49 +01:00
|
|
|
if (!context)
|
|
|
|
goto error1;
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_context_set_state_callback(context, context_state_cb, this);
|
|
|
|
if ((err = pa_context_connect(context, NULL, PA_CONTEXT_NOFLAGS, NULL)) != 0)
|
2017-11-23 01:14:49 +01:00
|
|
|
goto error2;
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
lock();
|
2017-11-23 01:14:49 +01:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
if ((err = pa_threaded_mainloop_start(mainloop)) != 0)
|
2017-11-23 01:14:49 +01:00
|
|
|
goto error2;
|
2019-02-06 00:21:23 +01:00
|
|
|
wait();
|
2017-11-23 01:14:49 +01:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
if ((err = pa_context_get_state(context)) != PA_CONTEXT_READY)
|
2017-11-23 01:14:49 +01:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
printf("Coundn't create context: State: %d\n", err);
|
2017-11-23 01:14:49 +01:00
|
|
|
goto error2;
|
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
stream = pa_stream_new(context, "Game", &ss, NULL);
|
2017-11-23 01:14:49 +01:00
|
|
|
|
|
|
|
if (!stream)
|
|
|
|
goto error2;
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_stream_set_state_callback(stream, stream_state_callback, this);
|
2017-11-23 01:14:49 +01:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
if (pa_stream_connect_playback(stream,
|
|
|
|
NULL,
|
|
|
|
&buffer_attr,
|
|
|
|
PA_STREAM_ADJUST_LATENCY,
|
|
|
|
NULL,
|
|
|
|
NULL) < 0)
|
2017-11-23 01:14:49 +01:00
|
|
|
goto error3;
|
2019-02-06 00:21:23 +01:00
|
|
|
wait();
|
2017-11-23 01:14:49 +01:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
if (pa_stream_get_state(stream) != PA_STREAM_READY)
|
2017-11-23 01:14:49 +01:00
|
|
|
{
|
|
|
|
goto error3;
|
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
actual_buffer_attr = pa_stream_get_buffer_attr(stream);
|
2017-11-23 01:14:49 +01:00
|
|
|
|
|
|
|
buffer_size = actual_buffer_attr->tlength;
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
printf("OK\n");
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
S9xSetSamplesAvailableCallback(pulse_samples_available, this);
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
unlock();
|
2017-11-23 01:14:49 +01:00
|
|
|
|
2018-12-28 23:32:32 +01:00
|
|
|
return true;
|
2017-11-23 01:14:49 +01:00
|
|
|
|
|
|
|
error3:
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_stream_disconnect(stream);
|
|
|
|
pa_stream_unref(stream);
|
2017-11-23 01:14:49 +01:00
|
|
|
error2:
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_context_disconnect(context);
|
|
|
|
pa_context_unref(context);
|
|
|
|
unlock();
|
2017-11-23 01:14:49 +01:00
|
|
|
error1:
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_threaded_mainloop_free(mainloop);
|
2017-11-23 01:14:49 +01:00
|
|
|
error0:
|
2019-02-06 00:21:23 +01:00
|
|
|
printf("Failed: %s\n", pa_strerror(err));
|
2017-11-23 01:14:49 +01:00
|
|
|
|
2018-12-28 23:32:32 +01:00
|
|
|
return false;
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
void S9xPulseSoundDriver::samples_available()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2017-11-23 01:14:49 +01:00
|
|
|
size_t bytes;
|
|
|
|
int samples;
|
|
|
|
const pa_buffer_attr *buffer_attr;
|
|
|
|
void *output_buffer = NULL;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
lock();
|
|
|
|
bytes = pa_stream_writable_size(stream);
|
|
|
|
buffer_attr = pa_stream_get_buffer_attr(stream);
|
|
|
|
unlock();
|
2017-11-23 01:14:49 +01:00
|
|
|
|
|
|
|
buffer_size = buffer_attr->tlength;
|
|
|
|
|
|
|
|
if (Settings.DynamicRateControl)
|
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
S9xUpdateDynamicRate(bytes, buffer_size);
|
2017-11-23 01:14:49 +01:00
|
|
|
}
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
samples = S9xGetSampleCount();
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-10 01:48:17 +01:00
|
|
|
if (Settings.DynamicRateControl && !Settings.SoundSync)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
if ((int)bytes < (samples * 2))
|
2017-11-23 01:14:49 +01:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
S9xClearSamples();
|
2017-11-23 01:14:49 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2019-02-10 01:48:17 +01:00
|
|
|
if (Settings.SoundSync && !Settings.TurboMode && !Settings.Mute)
|
|
|
|
{
|
2021-12-20 19:15:27 +01:00
|
|
|
if ((int)bytes < samples * 2)
|
2019-02-10 01:48:17 +01:00
|
|
|
{
|
2019-02-12 19:00:03 +01:00
|
|
|
int usec_to_sleep = ((samples >> 1) - (bytes >> 2)) * 10000 /
|
|
|
|
(Settings.SoundPlaybackRate / 100);
|
|
|
|
usleep(usec_to_sleep > 0 ? usec_to_sleep : 0);
|
2019-02-10 01:48:17 +01:00
|
|
|
lock();
|
|
|
|
bytes = pa_stream_writable_size(stream);
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
bytes = MIN((int)bytes, samples * 2) & ~1;
|
2017-11-23 01:14:49 +01:00
|
|
|
|
2017-11-25 02:35:49 +01:00
|
|
|
if (!bytes)
|
|
|
|
return;
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
lock();
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
if (pa_stream_begin_write(stream, &output_buffer, &bytes) != 0)
|
2017-11-25 02:35:49 +01:00
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
pa_stream_flush(stream, NULL, NULL);
|
|
|
|
unlock();
|
2017-11-25 02:35:49 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-02-06 00:21:23 +01:00
|
|
|
|
2017-11-25 02:35:49 +01:00
|
|
|
if (bytes <= 0 || !output_buffer)
|
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
unlock();
|
2017-11-25 02:35:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
S9xMixSamples((uint8 *)output_buffer, bytes >> 1);
|
|
|
|
pa_stream_write(stream, output_buffer, bytes, NULL, 0, PA_SEEK_RELATIVE);
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
unlock();
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|