2018-11-15 23:42:29 +00: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 15:46:12 +00:00
|
|
|
#include "gtk_s9x.h"
|
|
|
|
#include "gtk_sound_driver_oss.h"
|
|
|
|
|
|
|
|
#include <sys/soundcard.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
oss_samples_available (void *data)
|
|
|
|
{
|
|
|
|
((S9xOSSSoundDriver *) data)->samples_available ();
|
|
|
|
}
|
|
|
|
|
2018-11-02 20:52:26 +00:00
|
|
|
S9xOSSSoundDriver::S9xOSSSoundDriver ()
|
2010-09-25 15:46:12 +00:00
|
|
|
{
|
|
|
|
filedes = -1;
|
|
|
|
sound_buffer = NULL;
|
|
|
|
sound_buffer_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-11-02 20:52:26 +00:00
|
|
|
S9xOSSSoundDriver::init ()
|
2010-09-25 15:46:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-11-02 20:52:26 +00:00
|
|
|
S9xOSSSoundDriver::terminate ()
|
2010-09-25 15:46:12 +00:00
|
|
|
{
|
|
|
|
stop ();
|
|
|
|
|
|
|
|
S9xSetSamplesAvailableCallback (NULL, NULL);
|
|
|
|
|
|
|
|
if (filedes >= 0)
|
|
|
|
{
|
|
|
|
close (filedes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sound_buffer)
|
|
|
|
{
|
|
|
|
free (sound_buffer);
|
|
|
|
sound_buffer = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-11-02 20:52:26 +00:00
|
|
|
S9xOSSSoundDriver::start ()
|
2010-09-25 15:46:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-11-02 20:52:26 +00:00
|
|
|
S9xOSSSoundDriver::stop ()
|
2010-09-25 15:46:12 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:32:32 +00:00
|
|
|
bool S9xOSSSoundDriver::open_device()
|
2010-09-25 15:46:12 +00:00
|
|
|
{
|
|
|
|
int temp;
|
2017-11-20 18:07:54 +00:00
|
|
|
audio_buf_info info;
|
2010-09-25 15:46:12 +00:00
|
|
|
|
|
|
|
output_buffer_size = (gui_config->sound_buffer_size * Settings.SoundPlaybackRate) / 1000;
|
|
|
|
|
2019-02-05 23:21:23 +00:00
|
|
|
output_buffer_size *= 4;
|
2010-09-25 15:46:12 +00:00
|
|
|
if (output_buffer_size < 256)
|
|
|
|
output_buffer_size = 256;
|
|
|
|
|
|
|
|
printf ("OSS sound driver initializing...\n");
|
|
|
|
|
2018-11-08 21:12:47 +00:00
|
|
|
printf ("Device: /dev/dsp: ");
|
2010-09-25 15:46:12 +00:00
|
|
|
|
|
|
|
filedes = open ("/dev/dsp", O_WRONLY | O_NONBLOCK);
|
|
|
|
|
|
|
|
if (filedes < 0)
|
2017-11-20 18:07:54 +00:00
|
|
|
{
|
2018-11-08 21:12:47 +00:00
|
|
|
printf ("Failed.\n");
|
|
|
|
char dspstring[16] = "/dev/dspX\0";
|
|
|
|
|
|
|
|
for (int i = 1; i <= 9; i++)
|
|
|
|
{
|
|
|
|
dspstring[8] = '0' + i;
|
|
|
|
|
|
|
|
printf ("Trying %s: ", dspstring);
|
|
|
|
|
|
|
|
filedes = open (dspstring, O_WRONLY | O_NONBLOCK);
|
|
|
|
|
|
|
|
if (filedes < 0)
|
|
|
|
{
|
|
|
|
if (i == 9)
|
|
|
|
goto fail;
|
|
|
|
printf ("Failed.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2017-11-20 18:07:54 +00:00
|
|
|
}
|
2010-09-25 15:46:12 +00:00
|
|
|
|
|
|
|
printf ("OK\n");
|
|
|
|
|
|
|
|
|
2019-02-05 23:21:23 +00:00
|
|
|
printf (" --> (Format: 16-bit)...");
|
2010-09-25 15:46:12 +00:00
|
|
|
|
2019-02-05 23:21:23 +00:00
|
|
|
temp = AFMT_S16_LE;
|
|
|
|
if (ioctl (filedes, SNDCTL_DSP_SETFMT, &temp) < 0)
|
|
|
|
goto close_fail;
|
2010-09-25 15:46:12 +00:00
|
|
|
|
|
|
|
printf ("OK\n");
|
|
|
|
|
2019-02-05 23:21:23 +00:00
|
|
|
temp = 2;
|
|
|
|
printf (" --> (Stereo)...");
|
2010-09-25 15:46:12 +00:00
|
|
|
|
|
|
|
if (ioctl (filedes, SNDCTL_DSP_CHANNELS, &temp) < 0)
|
|
|
|
goto close_fail;
|
|
|
|
|
|
|
|
printf ("OK\n");
|
|
|
|
|
|
|
|
printf (" --> (Frequency: %d)...", Settings.SoundPlaybackRate);
|
|
|
|
if (ioctl (filedes, SNDCTL_DSP_SPEED, &Settings.SoundPlaybackRate) < 0)
|
|
|
|
goto close_fail;
|
|
|
|
|
|
|
|
printf ("OK\n");
|
|
|
|
|
|
|
|
/* 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. */
|
2017-11-27 18:39:58 +00:00
|
|
|
temp = (4 << 16) | (S9xSoundBase2log (output_buffer_size / 4));
|
2017-11-20 18:07:54 +00:00
|
|
|
|
|
|
|
if (ioctl (filedes, SNDCTL_DSP_SETFRAGMENT, &temp) < 0)
|
|
|
|
goto close_fail;
|
2010-09-25 15:46:12 +00:00
|
|
|
|
2017-11-20 18:07:54 +00:00
|
|
|
ioctl (filedes, SNDCTL_DSP_GETOSPACE, &info);
|
|
|
|
|
|
|
|
output_buffer_size = info.fragsize * info.fragstotal;
|
2010-09-25 15:46:12 +00:00
|
|
|
|
|
|
|
printf (" --> (Buffer size: %d bytes, %dms latency)...",
|
|
|
|
output_buffer_size,
|
2019-02-05 23:21:23 +00:00
|
|
|
(output_buffer_size * 250) / Settings.SoundPlaybackRate);
|
2010-09-25 15:46:12 +00:00
|
|
|
|
|
|
|
printf ("OK\n");
|
|
|
|
|
|
|
|
S9xSetSamplesAvailableCallback (oss_samples_available, this);
|
|
|
|
|
2018-12-28 22:32:32 +00:00
|
|
|
return true;
|
2010-09-25 15:46:12 +00:00
|
|
|
|
|
|
|
close_fail:
|
|
|
|
|
|
|
|
close (filedes);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
printf ("failed\n");
|
|
|
|
|
2018-12-28 22:32:32 +00:00
|
|
|
return false;
|
2010-09-25 15:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2018-11-02 20:52:26 +00:00
|
|
|
S9xOSSSoundDriver::samples_available ()
|
2010-09-25 15:46:12 +00:00
|
|
|
{
|
|
|
|
audio_buf_info info;
|
|
|
|
int samples_to_write;
|
|
|
|
int bytes_to_write;
|
|
|
|
int bytes_written;
|
|
|
|
|
2017-11-20 18:07:54 +00:00
|
|
|
ioctl (filedes, SNDCTL_DSP_GETOSPACE, &info);
|
|
|
|
|
2017-11-20 19:09:05 +00:00
|
|
|
if (Settings.DynamicRateControl)
|
2017-11-20 18:07:54 +00:00
|
|
|
{
|
|
|
|
S9xUpdateDynamicRate (info.bytes, output_buffer_size);
|
|
|
|
}
|
|
|
|
|
2010-09-25 15:46:12 +00:00
|
|
|
S9xFinalizeSamples ();
|
|
|
|
|
|
|
|
samples_to_write = S9xGetSampleCount ();
|
|
|
|
|
2017-11-20 18:07:54 +00:00
|
|
|
if (Settings.DynamicRateControl)
|
|
|
|
{
|
|
|
|
// Using rate control, we should always keep the emulator's sound buffers empty to
|
|
|
|
// maintain an accurate measurement.
|
2019-02-05 23:21:23 +00:00
|
|
|
if (samples_to_write > (info.bytes >> 1))
|
2017-11-20 18:07:54 +00:00
|
|
|
{
|
|
|
|
S9xClearSamples ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-09-25 15:46:12 +00:00
|
|
|
|
2019-02-05 23:21:23 +00:00
|
|
|
samples_to_write = MIN (info.bytes >> 1, samples_to_write) & ~1;
|
2010-09-25 15:46:12 +00:00
|
|
|
|
|
|
|
if (samples_to_write < 0)
|
|
|
|
return;
|
|
|
|
|
2019-02-05 23:21:23 +00:00
|
|
|
if (sound_buffer_size < samples_to_write * 2)
|
2010-09-25 15:46:12 +00:00
|
|
|
{
|
2019-02-05 23:21:23 +00:00
|
|
|
sound_buffer = (uint8 *) realloc (sound_buffer, samples_to_write * 2);
|
|
|
|
sound_buffer_size = samples_to_write * 2;
|
2010-09-25 15:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
S9xMixSamples (sound_buffer, samples_to_write);
|
|
|
|
|
|
|
|
bytes_written = 0;
|
2019-02-05 23:21:23 +00:00
|
|
|
bytes_to_write = samples_to_write * 2;
|
2010-09-25 15:46:12 +00:00
|
|
|
|
|
|
|
while (bytes_to_write > bytes_written)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
result = write (filedes,
|
|
|
|
((char *) sound_buffer) + bytes_written,
|
|
|
|
bytes_to_write - bytes_written);
|
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bytes_written += result;
|
|
|
|
}
|
|
|
|
}
|