Skip to content

Commit

Permalink
Merge branch 'master' into soon-tm
Browse files Browse the repository at this point in the history
  • Loading branch information
elad335 committed Jul 9, 2020
2 parents 64b93ae + 84470c3 commit 6089a8f
Show file tree
Hide file tree
Showing 73 changed files with 3,684 additions and 2,888 deletions.
15 changes: 9 additions & 6 deletions rpcs3/Emu/Audio/AL/OpenALBackend.cpp
Expand Up @@ -12,8 +12,7 @@ LOG_CHANNEL(OpenAL);
#define checkForAlcError(sit) do { ALCenum g_last_alc_error = alcGetError(m_device); if(g_last_alc_error != ALC_NO_ERROR) { OpenAL.error("%s: OpenALC error 0x%04x", sit, g_last_alc_error); return; }} while(0)

OpenALBackend::OpenALBackend()
: m_sampling_rate(get_sampling_rate())
, m_sample_size(get_sample_size())
: AudioBackend()
{
ALCdevice* m_device = alcOpenDevice(nullptr);
checkForAlcError("alcOpenDevice");
Expand All @@ -24,13 +23,17 @@ OpenALBackend::OpenALBackend()
alcMakeContextCurrent(m_context);
checkForAlcError("alcMakeContextCurrent");

if (get_channels() == 2)
switch (m_channels)
{
case 2:
m_format = (m_sample_size == 2) ? AL_FORMAT_STEREO16 : AL_FORMAT_STEREO_FLOAT32;
}
else
{
break;
case 6:
m_format = (m_sample_size == 2) ? AL_FORMAT_51CHN16 : AL_FORMAT_51CHN32;
break;
default:
m_format = (m_sample_size == 2) ? AL_FORMAT_71CHN16 : AL_FORMAT_71CHN32;
break;
}
}

Expand Down
3 changes: 0 additions & 3 deletions rpcs3/Emu/Audio/AL/OpenALBackend.h
Expand Up @@ -15,9 +15,6 @@ class OpenALBackend : public AudioBackend
u32 m_next_buffer = 0;
u32 m_num_unqueued = 0;

const u32 m_sampling_rate;
const u32 m_sample_size;

void unqueue_processed();

public:
Expand Down
13 changes: 7 additions & 6 deletions rpcs3/Emu/Audio/ALSA/ALSABackend.cpp
@@ -1,4 +1,4 @@
#ifndef HAVE_ALSA
#ifndef HAVE_ALSA
#error "ALSA support disabled but still being built."
#endif

Expand Down Expand Up @@ -26,6 +26,7 @@ static bool check(int err, const char* reason)
}

ALSABackend::ALSABackend()
: AudioBackend()
{
}

Expand All @@ -51,14 +52,14 @@ void ALSABackend::Open(u32 num_buffers)
if (!check(snd_pcm_hw_params_set_access(tls_handle, tls_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED), "snd_pcm_hw_params_set_access"))
return;

if (!check(snd_pcm_hw_params_set_format(tls_handle, tls_hw_params, g_cfg.audio.convert_to_u16 ? SND_PCM_FORMAT_S16_LE : SND_PCM_FORMAT_FLOAT_LE), "snd_pcm_hw_params_set_format"))
if (!check(snd_pcm_hw_params_set_format(tls_handle, tls_hw_params, m_convert_to_u16 ? SND_PCM_FORMAT_S16_LE : SND_PCM_FORMAT_FLOAT_LE), "snd_pcm_hw_params_set_format"))
return;

uint rate = get_sampling_rate();
uint rate = m_sampling_rate;
if (!check(snd_pcm_hw_params_set_rate_near(tls_handle, tls_hw_params, &rate, nullptr), "snd_pcm_hw_params_set_rate_near"))
return;

if (!check(snd_pcm_hw_params_set_channels(tls_handle, tls_hw_params, get_channels()), "snd_pcm_hw_params_set_channels"))
if (!check(snd_pcm_hw_params_set_channels(tls_handle, tls_hw_params, m_channels), "snd_pcm_hw_params_set_channels"))
return;

//uint period = 5333;
Expand Down Expand Up @@ -92,7 +93,7 @@ void ALSABackend::Open(u32 num_buffers)
if (!check(snd_pcm_sw_params_current(tls_handle, tls_sw_params), "snd_pcm_sw_params_current"))
return;

period_frames *= g_cfg.audio.startt;
period_frames *= m_start_threshold;

if (!check(snd_pcm_sw_params_set_start_threshold(tls_handle, tls_sw_params, period_frames + 1), "snd_pcm_sw_params_set_start_threshold"))
return;
Expand Down Expand Up @@ -132,7 +133,7 @@ void ALSABackend::Close()

bool ALSABackend::AddData(const void* src, u32 num_samples)
{
u32 num_frames = num_samples / get_channels();
u32 num_frames = num_samples / m_channels;

int res = snd_pcm_writei(tls_handle, src, num_frames);

Expand Down
62 changes: 50 additions & 12 deletions rpcs3/Emu/Audio/AudioBackend.cpp
Expand Up @@ -2,29 +2,67 @@
#include "AudioBackend.h"
#include "Emu/system_config.h"

/*
* Helper methods
*/
u32 AudioBackend::get_sampling_rate()
AudioBackend::AudioBackend()
{
m_convert_to_u16 = static_cast<bool>(g_cfg.audio.convert_to_u16);
m_sample_size = m_convert_to_u16 ? sizeof(u16) : sizeof(float);
m_start_threshold = g_cfg.audio.start_threshold;

const u32 sampling_period_multiplier_u32 = g_cfg.audio.sampling_period_multiplier;

if (sampling_period_multiplier_u32 == 100)
return DEFAULT_AUDIO_SAMPLING_RATE;
{
m_sampling_rate = DEFAULT_AUDIO_SAMPLING_RATE;
}
else
{
const f32 sampling_period_multiplier = sampling_period_multiplier_u32 / 100.0f;
const f32 sampling_rate_multiplier = 1.0f / sampling_period_multiplier;
m_sampling_rate = static_cast<u32>(f32{ DEFAULT_AUDIO_SAMPLING_RATE } *sampling_rate_multiplier);
}

const audio_downmix downmix = g_cfg.audio.audio_channel_downmix.get();

switch (downmix)
{
case audio_downmix::no_downmix:
m_channels = 8;
break;
case audio_downmix::downmix_to_stereo:
m_channels = 2;
break;
case audio_downmix::downmix_to_5_1:
m_channels = 6;
break;
case audio_downmix::use_application_settings:
m_channels = 2; // TODO
break;
default:
fmt::throw_exception("Unknown audio channel mode %s (%d)", downmix, static_cast<int>(downmix));
}
}

/*
* Helper methods
*/
u32 AudioBackend::get_sampling_rate() const
{
return m_sampling_rate;
}

const f32 sampling_period_multiplier = sampling_period_multiplier_u32 / 100.0f;
const f32 sampling_rate_multiplier = 1.0f / sampling_period_multiplier;
return static_cast<u32>(f32{DEFAULT_AUDIO_SAMPLING_RATE} * sampling_rate_multiplier);
u32 AudioBackend::get_sample_size() const
{
return m_sample_size;
}

u32 AudioBackend::get_sample_size()
u32 AudioBackend::get_channels() const
{
return g_cfg.audio.convert_to_u16 ? sizeof(u16) : sizeof(float);
return m_channels;
}

u32 AudioBackend::get_channels()
bool AudioBackend::get_convert_to_u16() const
{
return g_cfg.audio.downmix_to_2ch ? 2 : 8;
return m_convert_to_u16;
}

bool AudioBackend::has_capability(u32 cap) const
Expand Down
17 changes: 14 additions & 3 deletions rpcs3/Emu/Audio/AudioBackend.h
Expand Up @@ -20,6 +20,8 @@ class AudioBackend
SET_FREQUENCY_RATIO = 0x8, // Implements SetFrequencyRatio
};

AudioBackend();

virtual ~AudioBackend() = default;

/*
Expand Down Expand Up @@ -94,13 +96,22 @@ class AudioBackend
/*
* Helper methods
*/
static u32 get_sampling_rate();
u32 get_sampling_rate() const;

static u32 get_sample_size();
u32 get_sample_size() const;

static u32 get_channels();
u32 get_channels() const;

bool get_convert_to_u16() const;

bool has_capability(u32 cap) const;

void dump_capabilities(std::string& out) const;

protected:
bool m_convert_to_u16 = false;
u32 m_sample_size = sizeof(float);
u32 m_sampling_rate = DEFAULT_AUDIO_SAMPLING_RATE;
u32 m_channels = 0;
u32 m_start_threshold = 1;
};
23 changes: 10 additions & 13 deletions rpcs3/Emu/Audio/FAudio/FAudioBackend.cpp
@@ -1,4 +1,4 @@
#ifndef HAVE_FAUDIO
#ifndef HAVE_FAUDIO
#error "FAudio support disabled but still being built."
#endif

Expand All @@ -10,6 +10,7 @@
LOG_CHANNEL(FAudio_, "FAudio");

FAudioBackend::FAudioBackend()
: AudioBackend()
{
u32 res;

Expand All @@ -20,7 +21,7 @@ FAudioBackend::FAudioBackend()
return;
}

res = FAudio_CreateMasteringVoice(m_instance, &m_master_voice, g_cfg.audio.downmix_to_2ch ? 2 : 8, 48000, 0, 0, nullptr);
res = FAudio_CreateMasteringVoice(m_instance, &m_master_voice, m_channels, 48000, 0, 0, nullptr);
if (res)
{
FAudio_.fatal("FAudio_CreateMasteringVoice() failed(0x%08x)", res);
Expand Down Expand Up @@ -102,17 +103,13 @@ void FAudioBackend::Close()

void FAudioBackend::Open(u32 /* num_buffers */)
{
const u32 sample_size = AudioBackend::get_sample_size();
const u32 channels = AudioBackend::get_channels();
const u32 sampling_rate = AudioBackend::get_sampling_rate();

FAudioWaveFormatEx waveformatex;
waveformatex.wFormatTag = g_cfg.audio.convert_to_u16 ? FAUDIO_FORMAT_PCM : FAUDIO_FORMAT_IEEE_FLOAT;
waveformatex.nChannels = channels;
waveformatex.nSamplesPerSec = sampling_rate;
waveformatex.nAvgBytesPerSec = static_cast<u32>(sampling_rate * channels * sample_size);
waveformatex.nBlockAlign = channels * sample_size;
waveformatex.wBitsPerSample = sample_size * 8;
waveformatex.wFormatTag = m_convert_to_u16 ? FAUDIO_FORMAT_PCM : FAUDIO_FORMAT_IEEE_FLOAT;
waveformatex.nChannels = m_channels;
waveformatex.nSamplesPerSec = m_sampling_rate;
waveformatex.nAvgBytesPerSec = static_cast<u32>(m_sampling_rate * m_channels * m_sample_size);
waveformatex.nBlockAlign = m_channels * m_sample_size;
waveformatex.wBitsPerSample = m_sample_size * 8;
waveformatex.cbSize = 0;

u32 res = FAudio_CreateSourceVoice(m_instance, &m_source_voice, &waveformatex, 0, FAUDIO_DEFAULT_FREQ_RATIO, nullptr, nullptr, nullptr);
Expand Down Expand Up @@ -140,7 +137,7 @@ bool FAudioBackend::AddData(const void* src, u32 num_samples)
}

FAudioBuffer buffer;
buffer.AudioBytes = num_samples * AudioBackend::get_sample_size();
buffer.AudioBytes = num_samples * m_sample_size;
buffer.Flags = 0;
buffer.LoopBegin = FAUDIO_NO_LOOP_REGION;
buffer.LoopCount = 0;
Expand Down
34 changes: 24 additions & 10 deletions rpcs3/Emu/Audio/Pulse/PulseBackend.cpp
@@ -1,4 +1,4 @@
#ifndef HAVE_PULSE
#ifndef HAVE_PULSE
#error "PulseAudio support disabled but still being built."
#endif

Expand All @@ -9,6 +9,7 @@
#include <pulse/error.h>

PulseBackend::PulseBackend()
: AudioBackend()
{
}

Expand All @@ -19,7 +20,8 @@ PulseBackend::~PulseBackend()

void PulseBackend::Close()
{
if(this->connection) {
if (this->connection)
{
pa_simple_free(this->connection);
this->connection = nullptr;
}
Expand All @@ -28,20 +30,30 @@ void PulseBackend::Close()
void PulseBackend::Open(u32 /* num_buffers */)
{
pa_sample_spec ss;
ss.format = (get_sample_size() == 2) ? PA_SAMPLE_S16LE : PA_SAMPLE_FLOAT32LE;
ss.rate = get_sampling_rate();
ss.format = (m_sample_size == 2) ? PA_SAMPLE_S16LE : PA_SAMPLE_FLOAT32LE;
ss.rate = m_sampling_rate;

pa_channel_map channel_map;

if (get_channels() == 2)
if (m_channels == 2)
{
channel_map.channels = 2;
channel_map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
channel_map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
}
else if (m_channels == 6)
{
channel_map.channels = 2;
channel_map.channels = 6;
channel_map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
channel_map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
channel_map.map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
channel_map.map[3] = PA_CHANNEL_POSITION_LFE;
channel_map.map[4] = PA_CHANNEL_POSITION_REAR_LEFT;
channel_map.map[5] = PA_CHANNEL_POSITION_REAR_RIGHT;
}
else
{
channel_map.channels = 8;
channel_map.channels = 8;
channel_map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
channel_map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
channel_map.map[2] = PA_CHANNEL_POSITION_FRONT_CENTER;
Expand All @@ -55,7 +67,8 @@ void PulseBackend::Open(u32 /* num_buffers */)

int err;
this->connection = pa_simple_new(NULL, "RPCS3", PA_STREAM_PLAYBACK, NULL, "Game", &ss, &channel_map, NULL, &err);
if(!this->connection) {
if (!this->connection)
{
fprintf(stderr, "PulseAudio: Failed to initialize audio: %s\n", pa_strerror(err));
}
}
Expand All @@ -65,10 +78,11 @@ bool PulseBackend::AddData(const void* src, u32 num_samples)
AUDIT(this->connection);

int err;
if(pa_simple_write(this->connection, src, num_samples * get_sample_size(), &err) < 0) {
if (pa_simple_write(this->connection, src, num_samples * m_sample_size, &err) < 0)
{
fprintf(stderr, "PulseAudio: Failed to write audio stream: %s\n", pa_strerror(err));
return false;
}

return true;
}

0 comments on commit 6089a8f

Please sign in to comment.