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_oss.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>
|
2019-02-07 02:41:33 +01:00
|
|
|
#include <sys/soundcard.h>
|
2010-09-25 17:46:12 +02:00
|
|
|
#include <sys/time.h>
|
2023-06-07 22:34:10 +02:00
|
|
|
#include <unistd.h>
|
2023-06-07 23:06:55 +02:00
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
static inline int log2(int num)
|
|
|
|
{
|
|
|
|
int power;
|
|
|
|
|
|
|
|
if (num < 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (power = 0; num > 1; power++)
|
|
|
|
{
|
|
|
|
num >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return power;
|
|
|
|
}
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
S9xOSSSoundDriver::S9xOSSSoundDriver()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
filedes = -1;
|
|
|
|
}
|
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
void S9xOSSSoundDriver::init()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
void S9xOSSSoundDriver::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 (filedes >= 0)
|
|
|
|
{
|
2019-02-07 02:41:33 +01:00
|
|
|
close(filedes);
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
void S9xOSSSoundDriver::start()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
void S9xOSSSoundDriver::stop()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
bool S9xOSSSoundDriver::open_device(int playback_rate, int buffer_size_ms)
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
int temp;
|
2017-11-20 19:07:54 +01:00
|
|
|
audio_buf_info info;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
output_buffer_size_bytes = (buffer_size_ms * playback_rate) / 1000;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
output_buffer_size_bytes *= 4;
|
|
|
|
if (output_buffer_size_bytes < 256)
|
|
|
|
output_buffer_size_bytes = 256;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
printf("OSS sound driver initializing...\n");
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
printf("Device: /dev/dsp: ");
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
filedes = open("/dev/dsp", O_WRONLY | O_NONBLOCK);
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
|
|
if (filedes < 0)
|
2017-11-20 19:07:54 +01:00
|
|
|
{
|
2019-02-07 02:41:33 +01:00
|
|
|
printf("Failed.\n");
|
2018-11-08 22:12:47 +01:00
|
|
|
char dspstring[16] = "/dev/dspX\0";
|
|
|
|
|
|
|
|
for (int i = 1; i <= 9; i++)
|
|
|
|
{
|
|
|
|
dspstring[8] = '0' + i;
|
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
printf("Trying %s: ", dspstring);
|
2018-11-08 22:12:47 +01:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
filedes = open(dspstring, O_WRONLY | O_NONBLOCK);
|
2018-11-08 22:12:47 +01:00
|
|
|
|
|
|
|
if (filedes < 0)
|
|
|
|
{
|
|
|
|
if (i == 9)
|
|
|
|
goto fail;
|
2019-02-07 02:41:33 +01:00
|
|
|
printf("Failed.\n");
|
2018-11-08 22:12:47 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2017-11-20 19:07:54 +01: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
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
printf(" --> (Format: 16-bit)...");
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
temp = AFMT_S16_LE;
|
2019-02-07 02:41:33 +01:00
|
|
|
if (ioctl(filedes, SNDCTL_DSP_SETFMT, &temp) < 0)
|
2019-02-06 00:21:23 +01:00
|
|
|
goto close_fail;
|
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
|
|
|
|
2019-02-06 00:21:23 +01:00
|
|
|
temp = 2;
|
2019-02-07 02:41:33 +01:00
|
|
|
printf(" --> (Stereo)...");
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
if (ioctl(filedes, SNDCTL_DSP_CHANNELS, &temp) < 0)
|
2010-09-25 17:46:12 +02:00
|
|
|
goto close_fail;
|
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
printf("OK\n");
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
printf(" --> (Frequency: %d)...", playback_rate);
|
|
|
|
if (ioctl(filedes, SNDCTL_DSP_SPEED, &playback_rate) < 0)
|
2010-09-25 17:46:12 +02:00
|
|
|
goto close_fail;
|
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
printf("OK\n");
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
|
|
/* OSS requires a power-of-two buffer size, first 16 bits are the number
|
|
|
|
* of fragments to generate, second 16 are the respective power-of-two. */
|
2023-06-07 23:06:55 +02:00
|
|
|
temp = (4 << 16) | (log2(output_buffer_size_bytes / 4));
|
2017-11-20 19:07:54 +01:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
if (ioctl(filedes, SNDCTL_DSP_SETFRAGMENT, &temp) < 0)
|
2017-11-20 19:07:54 +01:00
|
|
|
goto close_fail;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
ioctl(filedes, SNDCTL_DSP_GETOSPACE, &info);
|
2017-11-20 19:07:54 +01:00
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
output_buffer_size_bytes = info.fragsize * info.fragstotal;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
printf(" --> (Buffer size: %d bytes, %dms latency)...",
|
2023-06-07 22:34:10 +02:00
|
|
|
output_buffer_size_bytes,
|
|
|
|
(output_buffer_size_bytes * 250) / playback_rate);
|
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
|
|
|
close(filedes);
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
|
|
fail:
|
2019-02-07 02:41:33 +01:00
|
|
|
printf("failed\n");
|
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-07 22:34:10 +02:00
|
|
|
int S9xOSSSoundDriver::space_free()
|
2010-09-25 17:46:12 +02:00
|
|
|
{
|
|
|
|
audio_buf_info info;
|
2019-02-07 02:41:33 +01:00
|
|
|
ioctl(filedes, SNDCTL_DSP_GETOSPACE, &info);
|
2023-06-07 22:34:10 +02:00
|
|
|
return info.bytes / 2;
|
|
|
|
}
|
2017-11-20 19:07:54 +01:00
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
std::pair<int, int> S9xOSSSoundDriver::buffer_level()
|
|
|
|
{
|
|
|
|
return { space_free(), output_buffer_size_bytes / 2};
|
|
|
|
}
|
2017-11-20 19:07:54 +01:00
|
|
|
|
2023-07-05 23:38:44 +02:00
|
|
|
bool S9xOSSSoundDriver::write_samples(int16_t *data, int samples)
|
2023-06-07 22:34:10 +02:00
|
|
|
{
|
|
|
|
audio_buf_info info;
|
|
|
|
int bytes_to_write;
|
|
|
|
int bytes_written;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
ioctl(filedes, SNDCTL_DSP_GETOSPACE, &info);
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
if (samples > info.bytes / 2)
|
|
|
|
samples = info.bytes / 2;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
2023-06-07 22:34:10 +02:00
|
|
|
if (samples == 0)
|
2023-07-05 23:38:44 +02:00
|
|
|
return false;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
|
|
bytes_written = 0;
|
2023-06-07 22:34:10 +02:00
|
|
|
bytes_to_write = samples * 2;
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
|
|
while (bytes_to_write > bytes_written)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
2019-02-07 02:41:33 +01:00
|
|
|
result = write(filedes,
|
2023-06-07 22:34:10 +02:00
|
|
|
((char *)data) + bytes_written,
|
2019-02-07 02:41:33 +01:00
|
|
|
bytes_to_write - bytes_written);
|
2010-09-25 17:46:12 +02:00
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bytes_written += result;
|
|
|
|
}
|
2023-07-05 23:38:44 +02:00
|
|
|
|
|
|
|
return true;
|
2010-09-25 17:46:12 +02:00
|
|
|
}
|