rpcsx/rpcs3/Emu/Audio/Pulse/PulseBackend.cpp

89 lines
2.2 KiB
C++
Raw Normal View History

2020-06-20 02:44:32 +02:00
#ifndef HAVE_PULSE
#error "PulseAudio support disabled but still being built."
#endif
2017-08-24 10:42:01 +02:00
#include "Emu/System.h"
#include "PulseBackend.h"
2017-08-24 10:42:01 +02:00
#include <pulse/simple.h>
#include <pulse/error.h>
PulseBackend::PulseBackend()
2020-06-20 02:44:32 +02:00
: AudioBackend()
2017-08-24 10:42:01 +02:00
{
}
PulseBackend::~PulseBackend()
2017-08-24 10:42:01 +02:00
{
this->Close();
}
void PulseBackend::Close()
2017-08-24 10:42:01 +02:00
{
2020-06-20 02:44:32 +02:00
if (this->connection)
{
2017-08-24 10:42:01 +02:00
pa_simple_free(this->connection);
this->connection = nullptr;
}
}
void PulseBackend::Open(u32 /* num_buffers */)
2017-08-24 10:42:01 +02:00
{
pa_sample_spec ss;
2020-06-20 02:44:32 +02:00
ss.format = (m_sample_size == 2) ? PA_SAMPLE_S16LE : PA_SAMPLE_FLOAT32LE;
ss.rate = m_sampling_rate;
2017-08-24 10:42:01 +02:00
pa_channel_map channel_map;
2020-06-20 02:44:32 +02:00
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)
{
2020-06-20 02:44:32 +02:00
channel_map.channels = 6;
channel_map.map[0] = PA_CHANNEL_POSITION_FRONT_LEFT;
channel_map.map[1] = PA_CHANNEL_POSITION_FRONT_RIGHT;
2020-06-20 02:44:32 +02:00
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
{
2020-06-20 02:44:32 +02:00
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;
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;
channel_map.map[6] = PA_CHANNEL_POSITION_SIDE_LEFT;
channel_map.map[7] = PA_CHANNEL_POSITION_SIDE_RIGHT;
}
ss.channels = channel_map.channels;
2017-08-24 10:42:01 +02:00
int err;
this->connection = pa_simple_new(NULL, "RPCS3", PA_STREAM_PLAYBACK, NULL, "Game", &ss, &channel_map, NULL, &err);
2020-06-20 02:44:32 +02:00
if (!this->connection)
{
2017-08-24 10:42:01 +02:00
fprintf(stderr, "PulseAudio: Failed to initialize audio: %s\n", pa_strerror(err));
}
}
bool PulseBackend::AddData(const void* src, u32 num_samples)
2017-08-24 10:42:01 +02:00
{
AUDIT(this->connection);
int err;
2020-06-20 02:44:32 +02:00
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;
2017-08-24 10:42:01 +02:00
}
2020-06-20 02:44:32 +02:00
return true;
2017-08-24 10:42:01 +02:00
}