snes9x/gtk/src/gtk_sound.cpp

210 lines
3.9 KiB
C++
Raw Normal View History

/*****************************************************************************\
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 <errno.h>
#include <unistd.h>
#include "gtk_s9x.h"
#include "gtk_sound.h"
#include "gtk_sound_driver.h"
#include "snes9x.h"
#include "apu/apu.h"
2010-09-25 17:46:12 +02:00
#ifdef USE_PORTAUDIO
#include "gtk_sound_driver_portaudio.h"
#endif
#ifdef USE_OSS
#include "gtk_sound_driver_oss.h"
#endif
#include "gtk_sound_driver_sdl.h"
#ifdef USE_ALSA
#include "gtk_sound_driver_alsa.h"
#endif
#ifdef USE_PULSEAUDIO
#include "gtk_sound_driver_pulse.h"
#endif
2017-12-01 01:14:17 +01:00
static int playback_rates[8] =
2010-09-25 17:46:12 +02:00
{
0, 8000, 11025, 16000, 22050, 32000, 44100, 48000
};
static S9xSoundDriver *driver;
2010-09-25 17:46:12 +02:00
int S9xSoundBase2log(int num)
2010-09-25 17:46:12 +02:00
{
int power;
if (num < 1)
return 0;
for (power = 0; num > 1; power++)
{
num >>= 1;
}
return power;
}
int S9xSoundPowerof2(int num)
2010-09-25 17:46:12 +02:00
{
return (1 << num);
}
void S9xPortSoundInit()
2010-09-25 17:46:12 +02:00
{
int pao_driver = 0;
int sdl_driver = 0;
int oss_driver = 0;
int alsa_driver = 0;
int pulse_driver = 0;
int max_driver = 0;
driver = NULL;
#ifdef USE_PORTAUDIO
sdl_driver++;
oss_driver++;
alsa_driver++;
pulse_driver++;
max_driver++;
#endif
#ifdef USE_OSS
sdl_driver++;
alsa_driver++;
pulse_driver++;
max_driver++;
#endif
2018-12-02 02:58:13 +01:00
/* SDL */
2010-09-25 17:46:12 +02:00
alsa_driver++;
pulse_driver++;
max_driver++;
#ifdef USE_ALSA
max_driver++;
pulse_driver++;
#endif
#ifdef USE_PULSEAUDIO
max_driver++;
#endif
if (gui_config->sound_driver >= max_driver)
gui_config->sound_driver = 0;
#ifdef USE_PORTAUDIO
if (gui_config->sound_driver == pao_driver)
driver = new S9xPortAudioSoundDriver();
2010-09-25 17:46:12 +02:00
#endif
#ifdef USE_OSS
if (gui_config->sound_driver == oss_driver)
driver = new S9xOSSSoundDriver();
2010-09-25 17:46:12 +02:00
#endif
if (gui_config->sound_driver == sdl_driver)
driver = new S9xSDLSoundDriver();
2010-09-25 17:46:12 +02:00
#ifdef USE_ALSA
if (gui_config->sound_driver == alsa_driver)
driver = new S9xAlsaSoundDriver();
2010-09-25 17:46:12 +02:00
#endif
#ifdef USE_PULSEAUDIO
if (gui_config->sound_driver == pulse_driver)
driver = new S9xPulseSoundDriver();
2010-09-25 17:46:12 +02:00
#endif
if (driver != NULL)
{
driver->init();
2010-09-25 17:46:12 +02:00
if (gui_config->auto_input_rate)
{
Settings.SoundInputRate = top_level->get_auto_input_rate();
2018-06-07 23:16:22 +02:00
if (Settings.SoundInputRate == 0.0)
{
2019-04-12 19:10:51 +02:00
Settings.SoundInputRate = 31950;
2018-06-07 23:16:22 +02:00
gui_config->auto_input_rate = 0;
}
}
else
{
Settings.SoundInputRate = CLAMP(gui_config->sound_input_rate, 31700, 32300);
}
2010-09-25 17:46:12 +02:00
Settings.SoundPlaybackRate = playback_rates[gui_config->sound_playback_rate];
S9xInitSound(0);
2010-09-25 17:46:12 +02:00
S9xSetSoundMute(false);
2010-09-25 17:46:12 +02:00
}
else
{
S9xSetSoundMute(gui_config->mute_sound);
2010-09-25 17:46:12 +02:00
}
}
void S9xPortSoundReinit()
2010-09-25 17:46:12 +02:00
{
S9xPortSoundDeinit();
2010-09-25 17:46:12 +02:00
/* Ensure the sound device is released before trying to reopen it. */
sync();
2010-09-25 17:46:12 +02:00
S9xPortSoundInit();
2010-09-25 17:46:12 +02:00
}
void S9xPortSoundDeinit()
2010-09-25 17:46:12 +02:00
{
S9xSoundStop();
2010-09-25 17:46:12 +02:00
if (driver)
driver->terminate();
2010-09-25 17:46:12 +02:00
delete driver;
}
void S9xSoundStart()
2010-09-25 17:46:12 +02:00
{
if (driver)
driver->start();
2010-09-25 17:46:12 +02:00
}
void S9xSoundStop()
2010-09-25 17:46:12 +02:00
{
if (driver)
driver->stop();
2010-09-25 17:46:12 +02:00
}
bool8 S9xOpenSoundDevice()
2010-09-25 17:46:12 +02:00
{
if (gui_config->mute_sound)
return false;
2010-09-25 17:46:12 +02:00
gui_config->sound_buffer_size = CLAMP(gui_config->sound_buffer_size, 2, 256);
2010-09-25 17:46:12 +02:00
return driver->open_device();
2010-09-25 17:46:12 +02:00
}
/* This really shouldn't be in the port layer */
void S9xToggleSoundChannel(int c)
2010-09-25 17:46:12 +02:00
{
static int sound_switch = 255;
if (c == 8)
sound_switch = 255;
else
sound_switch ^= 1 << c;
S9xSetSoundControl(sound_switch);
2010-09-25 17:46:12 +02:00
}