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_alsa.hpp"
|
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-07 02:41:33 +01:00
|
|
|
S9xAlsaSoundDriver::S9xAlsaSoundDriver()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2022-03-12 18:19:39 +01:00
|
|
|
pcm = {};
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
void S9xAlsaSoundDriver::init()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
void S9xAlsaSoundDriver::deinit()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
2019-02-07 02:41:33 +01:00
|
|
|
stop();
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
|
|
if (pcm)
|
|
|
|
{
|
2019-02-07 02:41:33 +01:00
|
|
|
snd_pcm_drain(pcm);
|
|
|
|
snd_pcm_close(pcm);
|
2022-03-12 18:19:39 +01:00
|
|
|
pcm = nullptr;
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
void S9xAlsaSoundDriver::start()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
void S9xAlsaSoundDriver::stop()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
bool S9xAlsaSoundDriver::open_device(int playback_rate, int buffer_size_ms)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
int err;
|
2017-11-20 19:07:54 +01:00
|
|
|
unsigned int periods = 8;
|
2023-06-07 22:34:10 +02:00
|
|
|
unsigned int buffer_size = buffer_size_ms * 1000;
|
2010-09-25 17:46:12 +02:00
|
|
|
snd_pcm_sw_params_t *sw_params;
|
2017-11-20 19:07:54 +01:00
|
|
|
snd_pcm_hw_params_t *hw_params;
|
2010-09-25 17:46:12 +02:00
|
|
|
snd_pcm_uframes_t alsa_buffer_size, alsa_period_size;
|
2018-06-17 01:13:21 +02:00
|
|
|
unsigned int min = 0;
|
|
|
|
unsigned int max = 0;
|
2023-06-07 22:34:10 +02:00
|
|
|
unsigned int playback_rate_param = 0;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
printf("ALSA sound driver initializing...\n");
|
|
|
|
printf(" --> (Device: default)...\n");
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2022-03-12 18:19:39 +01:00
|
|
|
err = snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
|
2016-09-27 22:25:37 +02:00
|
|
|
|
|
|
|
if (err < 0)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
printf(" --> (16-bit Stereo, %dhz, %d ms)...\n",
|
2023-06-07 22:34:10 +02:00
|
|
|
playback_rate,
|
|
|
|
buffer_size_ms);
|
2019-02-07 02:41:33 +01:00
|
|
|
|
|
|
|
snd_pcm_hw_params_alloca(&hw_params);
|
|
|
|
snd_pcm_hw_params_any(pcm, hw_params);
|
|
|
|
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, 2);
|
|
|
|
|
2022-03-12 18:19:39 +01:00
|
|
|
snd_pcm_hw_params_get_rate_min(hw_params, &min, nullptr);
|
|
|
|
snd_pcm_hw_params_get_rate_max(hw_params, &max, nullptr);
|
2019-02-07 02:41:33 +01:00
|
|
|
printf(" --> Available rates: %d to %d\n", min, max);
|
2023-06-07 22:34:10 +02:00
|
|
|
if (playback_rate > (int)max || playback_rate < (int)min)
|
2018-06-17 01:13:21 +02:00
|
|
|
{
|
2023-06-07 22:34:10 +02:00
|
|
|
printf(" Rate %d not available. Using %d instead.\n", playback_rate, max);
|
|
|
|
playback_rate = max;
|
2018-06-17 01:13:21 +02:00
|
|
|
}
|
2023-06-07 22:34:10 +02:00
|
|
|
|
|
|
|
playback_rate_param = playback_rate;
|
|
|
|
snd_pcm_hw_params_set_rate_near(pcm, hw_params, &playback_rate_param, nullptr);
|
2018-06-17 01:13:21 +02:00
|
|
|
|
2022-03-12 18:19:39 +01:00
|
|
|
snd_pcm_hw_params_get_buffer_time_min(hw_params, &min, nullptr);
|
|
|
|
snd_pcm_hw_params_get_buffer_time_max(hw_params, &max, nullptr);
|
2019-02-07 02:41:33 +01:00
|
|
|
printf(" --> Available buffer sizes: %dms to %dms\n", min / 1000, max / 1000);
|
2018-06-17 01:13:21 +02:00
|
|
|
if (buffer_size < min && buffer_size > max)
|
|
|
|
{
|
2019-02-07 02:41:33 +01:00
|
|
|
printf(" Buffer size %dms not available. Using %d instead.\n", buffer_size / 1000, (min + max) / 2000);
|
2018-06-17 01:13:21 +02:00
|
|
|
buffer_size = (min + max) / 2;
|
|
|
|
}
|
2022-03-12 18:19:39 +01:00
|
|
|
snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_size, nullptr);
|
2018-06-17 01:13:21 +02:00
|
|
|
|
2022-03-12 18:19:39 +01:00
|
|
|
snd_pcm_hw_params_get_periods_min(hw_params, &min, nullptr);
|
|
|
|
snd_pcm_hw_params_get_periods_max(hw_params, &max, nullptr);
|
2019-02-07 02:41:33 +01:00
|
|
|
printf(" --> Period ranges: %d to %d blocks\n", min, max);
|
2018-06-17 01:13:21 +02:00
|
|
|
if (periods > max)
|
|
|
|
{
|
|
|
|
periods = max;
|
|
|
|
}
|
2022-03-12 18:19:39 +01:00
|
|
|
snd_pcm_hw_params_set_periods_near(pcm, hw_params, &periods, nullptr);
|
2017-11-20 19:07:54 +01:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
if ((err = snd_pcm_hw_params(pcm, hw_params)) < 0)
|
2018-06-17 01:13:21 +02:00
|
|
|
{
|
2019-02-07 02:41:33 +01:00
|
|
|
printf(" Hardware parameter set failed.\n");
|
2010-09-25 17:46:12 +02:00
|
|
|
goto close_fail;
|
2018-06-17 01:13:21 +02:00
|
|
|
}
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
snd_pcm_sw_params_alloca(&sw_params);
|
|
|
|
snd_pcm_sw_params_current(pcm, sw_params);
|
|
|
|
snd_pcm_get_params(pcm, &alsa_buffer_size, &alsa_period_size);
|
2010-09-25 17:46:12 +02:00
|
|
|
/* Don't start until we're [nearly] full */
|
2019-02-07 02:41:33 +01:00
|
|
|
snd_pcm_sw_params_set_start_threshold(pcm, sw_params, (alsa_buffer_size / 2));
|
|
|
|
err = snd_pcm_sw_params(pcm, sw_params);
|
2017-11-20 19:07:54 +01:00
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
output_buffer_size_bytes = snd_pcm_frames_to_bytes(pcm, alsa_buffer_size);
|
2017-11-20 19:07:54 +01:00
|
|
|
|
2010-09-25 17:46:12 +02:00
|
|
|
if (err < 0)
|
2018-06-17 01:13:21 +02:00
|
|
|
{
|
2019-02-07 02:41:33 +01:00
|
|
|
printf(" Software parameter set failed.\n");
|
2010-09-25 17:46:12 +02:00
|
|
|
goto close_fail;
|
2018-06-17 01:13:21 +02:00
|
|
|
}
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
printf("OK\n");
|
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
|
|
|
|
|
|
|
close_fail:
|
2019-02-07 02:41:33 +01:00
|
|
|
snd_pcm_drain(pcm);
|
|
|
|
snd_pcm_close(pcm);
|
2022-03-12 18:19:39 +01:00
|
|
|
pcm = nullptr;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
|
|
fail:
|
2019-02-07 02:41:33 +01:00
|
|
|
printf("Failed: %s\n", snd_strerror(err));
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2018-12-28 23:32:32 +01:00
|
|
|
return false;
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
|
2023-06-29 00:58:00 +02:00
|
|
|
bool S9xAlsaSoundDriver::write_samples(int16_t *data, int samples)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
snd_pcm_sframes_t frames_written, frames;
|
2022-03-12 18:19:39 +01:00
|
|
|
size_t bytes;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
frames = snd_pcm_avail(pcm);
|
2017-11-20 19:07:54 +01:00
|
|
|
|
2010-09-25 17:46:12 +02:00
|
|
|
if (frames < 0)
|
|
|
|
{
|
2019-02-07 02:41:33 +01:00
|
|
|
frames = snd_pcm_recover(pcm, frames, 1);
|
2023-06-29 00:58:00 +02:00
|
|
|
return false;
|
2017-11-20 19:07:54 +01:00
|
|
|
}
|
|
|
|
|
2023-06-29 00:58:00 +02:00
|
|
|
bool result = true;
|
2023-06-07 22:34:10 +02:00
|
|
|
snd_pcm_nonblock(pcm, 0);
|
|
|
|
if (frames > samples / 2)
|
2023-06-29 00:58:00 +02:00
|
|
|
{
|
2023-06-07 22:34:10 +02:00
|
|
|
frames = samples / 2;
|
2023-06-29 00:58:00 +02:00
|
|
|
result = false;
|
|
|
|
}
|
2017-11-20 19:07:54 +01:00
|
|
|
|
2019-02-10 01:48:17 +01:00
|
|
|
bytes = snd_pcm_frames_to_bytes(pcm, frames);
|
2010-09-25 17:46:12 +02:00
|
|
|
if (bytes <= 0)
|
2023-06-29 00:58:00 +02:00
|
|
|
return false;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
|
|
frames_written = 0;
|
|
|
|
while (frames_written < frames)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
result = snd_pcm_writei(pcm,
|
2023-06-07 22:34:10 +02:00
|
|
|
&data[snd_pcm_frames_to_bytes(pcm, frames_written) / 2],
|
2019-02-07 02:41:33 +01:00
|
|
|
frames - frames_written);
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
{
|
2019-02-07 02:41:33 +01:00
|
|
|
result = snd_pcm_recover(pcm, result, 1);
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
frames_written += result;
|
|
|
|
}
|
|
|
|
}
|
2023-06-29 00:58:00 +02:00
|
|
|
|
|
|
|
return result;
|
2023-06-07 22:34:10 +02:00
|
|
|
}
|
2023-04-02 19:46:49 +02:00
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
int S9xAlsaSoundDriver::space_free()
|
|
|
|
{
|
|
|
|
return snd_pcm_avail(pcm) * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<int, int> S9xAlsaSoundDriver::buffer_level()
|
|
|
|
{
|
|
|
|
return { snd_pcm_avail(pcm), output_buffer_size_bytes / 4 };
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|