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.
|
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
|
2023-06-07 23:06:55 +02:00
|
|
|
|
#include "s9x_sound_driver_pulse.hpp"
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
|
#include <cstring>
|
2023-06-07 23:06:55 +02:00
|
|
|
|
#include <cstdio>
|
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>
|
2023-10-30 13:00:00 +01:00
|
|
|
|
#include "fmt/format.h"
|
|
|
|
|
#include "messages.h"
|
|
|
|
|
#include "snes9x.h"
|
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
|
|
|
|
{
|
2022-03-12 18:19:39 +01:00
|
|
|
|
init();
|
2010-09-25 17:46:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
|
void S9xPulseSoundDriver::init()
|
2010-09-25 17:46:12 +02:00
|
|
|
|
{
|
2022-03-12 18:19:39 +01:00
|
|
|
|
mainloop = {};
|
|
|
|
|
context = {};
|
|
|
|
|
stream = {};
|
|
|
|
|
buffer_size = {};
|
2010-09-25 17:46:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
|
void S9xPulseSoundDriver::deinit()
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
|
bool S9xPulseSoundDriver::open_device(int playback_rate, int buffer_size_ms)
|
2010-09-25 17:46:12 +02:00
|
|
|
|
{
|
2022-03-12 18:19:39 +01:00
|
|
|
|
init();
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
2022-03-12 18:19:39 +01:00
|
|
|
|
pa_sample_spec ss;
|
2019-02-06 00:21:23 +01:00
|
|
|
|
ss.channels = 2;
|
|
|
|
|
ss.format = PA_SAMPLE_S16NE;
|
2023-06-07 22:34:10 +02:00
|
|
|
|
ss.rate = playback_rate;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
2022-03-12 18:19:39 +01:00
|
|
|
|
pa_buffer_attr buffer_attr;
|
2023-07-03 22:35:11 +02:00
|
|
|
|
buffer_attr.tlength = 2 * pa_usec_to_bytes(buffer_size_ms * 1000, &ss);
|
2019-02-06 00:21:23 +01:00
|
|
|
|
buffer_attr.maxlength = buffer_attr.tlength * 2;
|
2023-04-02 19:46:49 +02:00
|
|
|
|
buffer_attr.minreq = pa_usec_to_bytes(3000, &ss);
|
2023-07-03 22:35:11 +02:00
|
|
|
|
buffer_attr.prebuf = buffer_attr.tlength / 2;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
2023-10-30 13:00:00 +01:00
|
|
|
|
S9xMessage(S9X_INFO, S9X_NO_INFO, "Initializing PulseAudio sound driver…");
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
2023-10-30 13:00:00 +01:00
|
|
|
|
S9xMessage(S9X_INFO, S9X_NO_INFO,
|
|
|
|
|
fmt::format(" --> ({0:Ld} Hz, 16‑bit stereo, {1:Ld} ms)…",
|
|
|
|
|
playback_rate,
|
|
|
|
|
buffer_size_ms).c_str());
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
2022-03-12 18:19:39 +01:00
|
|
|
|
int err = PA_ERR_UNKNOWN;
|
2019-02-06 00:21:23 +01:00
|
|
|
|
mainloop = pa_threaded_mainloop_new();
|
|
|
|
|
context = pa_context_new(pa_threaded_mainloop_get_api(mainloop), "Snes9x");
|
|
|
|
|
pa_context_set_state_callback(context, context_state_cb, this);
|
2022-03-12 18:19:39 +01:00
|
|
|
|
pa_context_connect(context, nullptr, PA_CONTEXT_NOFLAGS, nullptr);
|
2017-11-23 01:14:49 +01:00
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
|
lock();
|
2022-03-12 18:19:39 +01:00
|
|
|
|
pa_threaded_mainloop_start(mainloop);
|
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)
|
2022-03-12 18:19:39 +01:00
|
|
|
|
return false;
|
2017-11-23 01:14:49 +01:00
|
|
|
|
|
2022-03-12 18:19:39 +01:00
|
|
|
|
stream = pa_stream_new(context, "Game", &ss, nullptr);
|
2017-11-23 01:14:49 +01:00
|
|
|
|
|
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,
|
2022-03-12 18:19:39 +01:00
|
|
|
|
nullptr,
|
2019-02-06 00:21:23 +01:00
|
|
|
|
&buffer_attr,
|
2023-04-02 19:46:49 +02:00
|
|
|
|
PA_STREAM_EARLY_REQUESTS,
|
2022-03-12 18:19:39 +01:00
|
|
|
|
nullptr,
|
|
|
|
|
nullptr) < 0)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
2022-03-12 18:19:39 +01:00
|
|
|
|
return false;
|
2017-11-23 01:14:49 +01:00
|
|
|
|
|
2022-03-12 18:19:39 +01:00
|
|
|
|
auto actual_buffer_attr = pa_stream_get_buffer_attr(stream);
|
|
|
|
|
unlock();
|
2017-11-23 01:14:49 +01:00
|
|
|
|
|
|
|
|
|
buffer_size = actual_buffer_attr->tlength;
|
|
|
|
|
|
2023-10-30 13:00:00 +01:00
|
|
|
|
S9xMessage(S9X_INFO, S9X_NO_INFO, "OK");
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
2018-12-28 23:32:32 +01:00
|
|
|
|
return true;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
|
int S9xPulseSoundDriver::space_free()
|
2010-09-25 17:46:12 +02:00
|
|
|
|
{
|
2019-02-06 00:21:23 +01:00
|
|
|
|
lock();
|
2022-03-12 18:19:39 +01:00
|
|
|
|
size_t bytes = pa_stream_writable_size(stream);
|
2019-02-06 00:21:23 +01:00
|
|
|
|
unlock();
|
2023-06-07 22:34:10 +02:00
|
|
|
|
return bytes / 2;
|
|
|
|
|
}
|
2017-11-23 01:14:49 +01:00
|
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
|
std::pair<int, int> S9xPulseSoundDriver::buffer_level()
|
|
|
|
|
{
|
|
|
|
|
lock();
|
|
|
|
|
size_t bytes = pa_stream_writable_size(stream);
|
|
|
|
|
unlock();
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
|
return { bytes, buffer_size };
|
|
|
|
|
}
|
2022-03-12 18:19:39 +01:00
|
|
|
|
|
2023-06-30 00:44:13 +02:00
|
|
|
|
bool S9xPulseSoundDriver::write_samples(int16_t *data, int samples)
|
2023-06-07 22:34:10 +02:00
|
|
|
|
{
|
2023-06-30 00:44:13 +02:00
|
|
|
|
bool retval = true;
|
|
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
|
lock();
|
|
|
|
|
size_t bytes = pa_stream_writable_size(stream);
|
|
|
|
|
unlock();
|
2019-02-10 01:48:17 +01:00
|
|
|
|
|
2023-07-03 22:35:11 +02:00
|
|
|
|
if (draining)
|
|
|
|
|
{
|
2023-10-11 02:44:06 +02:00
|
|
|
|
if (bytes > (size_t)buffer_size / 2)
|
2023-07-03 22:35:11 +02:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
draining = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 02:44:06 +02:00
|
|
|
|
if ((size_t)samples * 2 > bytes)
|
2023-07-03 22:35:11 +02:00
|
|
|
|
{
|
|
|
|
|
draining = true;
|
2023-06-30 00:44:13 +02:00
|
|
|
|
return false;
|
2023-07-03 22:35:11 +02:00
|
|
|
|
}
|
2023-06-30 00:44:13 +02:00
|
|
|
|
|
2023-10-11 02:44:06 +02:00
|
|
|
|
if ((size_t)samples * 2 < bytes)
|
2023-06-30 00:44:13 +02:00
|
|
|
|
bytes = samples * 2;
|
2017-11-23 01:14:49 +01:00
|
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
|
if (bytes == 0)
|
2023-06-30 00:44:13 +02:00
|
|
|
|
return false;
|
2017-11-25 02:35:49 +01:00
|
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
|
lock();
|
2023-06-07 22:34:10 +02:00
|
|
|
|
void *output_buffer;
|
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
|
|
|
|
{
|
2022-03-12 18:19:39 +01:00
|
|
|
|
pa_stream_flush(stream, nullptr, nullptr);
|
2019-02-06 00:21:23 +01:00
|
|
|
|
unlock();
|
2023-06-30 00:44:13 +02:00
|
|
|
|
return false;
|
2017-11-25 02:35:49 +01:00
|
|
|
|
}
|
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();
|
2023-06-30 00:44:13 +02:00
|
|
|
|
return false;
|
2017-11-25 02:35:49 +01:00
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
|
std::memcpy(output_buffer, data, bytes);
|
2022-03-12 18:19:39 +01:00
|
|
|
|
pa_stream_write(stream, output_buffer, bytes, nullptr, 0, PA_SEEK_RELATIVE);
|
2019-02-06 00:21:23 +01:00
|
|
|
|
unlock();
|
2023-06-30 00:44:13 +02:00
|
|
|
|
|
|
|
|
|
return retval;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
}
|