feat(audioout): implement 12-channel AudioOut2 ports

sceAudioOut2PortCreate threw for 12-channel ports, which PPSA26344
creates as its two 7.1.4 float beds (data format 0xc00).

Each accepted layout (1, 2, 6, 8 or 12 channels, float or int16) now
folds into the stereo mix through a coefficient table: centre, side,
back and top front channels at -3 dB, top back channels at -6 dB into
the front, LFE dropped. Zero coefficients are skipped, so NaN or Inf on
a dropped channel does not reach the mix. Ports of 3, 4, 5 or 7
channels, which were mixed as L/R, now throw, as do other counts and
formats with any of bits 12-31 set. The assumed format fields,
channel order and coefficients are listed in TechnicalDebt.

The new audio_out2_port_layouts test checks each layout's fold, float
and int16, and the formats that throw.
This commit is contained in:
Celegans12
2026-10-05 11:58:22 +02:00
parent cb33f858c0
commit 799aaf5c2f
5 changed files with 300 additions and 17 deletions
@@ -62,4 +62,14 @@ if(BUILD_TESTING)
set_tests_properties(audio_out2_latency PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceAudioOut>")
endif()
add_test_executable(audio_out2_port_layouts_tests tests/AudioOut2PortLayouts.cpp)
target_include_directories(audio_out2_port_layouts_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(audio_out2_port_layouts_tests PRIVATE libSceAudioOut libc)
configure_windows_unwind(audio_out2_port_layouts_tests)
add_test(NAME audio_out2_port_layouts COMMAND audio_out2_port_layouts_tests)
if(WIN32)
set_tests_properties(audio_out2_port_layouts PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libSceAudioOut>")
endif()
endif()
@@ -36,9 +36,10 @@ static constexpr std::uint32_t AUDIO_OUT2_SAMPLE_RATE = 48000;
// The grain a push carries, in samples per channel: a context's num_grains (Demon's Souls: 256, 5.33 ms).
static constexpr std::uint32_t AUDIO_OUT2_DEFAULT_GRAIN = 256;
static constexpr std::uint32_t AUDIO_OUT2_OUTPUT_CHANNELS = 2;
static constexpr std::uint32_t AUDIO_OUT2_PORT_CHANNELS_MAX = 8;
static constexpr std::uint32_t AUDIO_OUT2_PORT_CHANNELS_MAX = 12;
struct AudioOut2Context;
struct AudioOut2StereoFold;
struct AudioOut2Port {
bool used = false;
@@ -49,8 +50,9 @@ struct AudioOut2Port {
std::uint32_t flags = 0;
std::uint32_t channels = 0;
bool int16 = false;
const AudioOut2StereoFold* fold = nullptr;
const void* data = nullptr;
float volume[AUDIO_OUT2_PORT_CHANNELS_MAX] = {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
float volume[AUDIO_OUT2_PORT_CHANNELS_MAX] = {1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
std::uint64_t dataSets = 0;
std::uint64_t attributeTraces = 0;
};
@@ -2,6 +2,7 @@
#include <cstdint>
#include <cstring>
#include <cstdio>
#include <iterator>
#include <mutex>
#include <stdexcept>
#include <string>
@@ -26,6 +27,7 @@ static constexpr std::uint32_t ATTRIBUTE_VOLUME = 1;
static constexpr std::uint32_t FORMAT_CHANNELS_SHIFT = 8;
static constexpr std::uint32_t FORMAT_CHANNELS_MASK = 0xFu;
static constexpr std::uint32_t FORMAT_TYPE_MASK = 0x7Fu;
static constexpr std::uint32_t FORMAT_FIELDS_MASK = 0xFFFu;
static std::mutex g_portsLock;
// Grows on demand: the title opens its bed ports plus max_object_ports object ports at once.
@@ -37,9 +39,28 @@ static AudioOut2Port* FromHandle(AudioOut2PortHandle handle) {
return &g_ports[index];
}
// 8-channel order FL FR C LFE RL RR SL SR (a swapped rear/side pair order sums the same): the rear
// and side pairs fold into the front at -3 dB, the centre into both sides, and the LFE is dropped.
static constexpr float DOWNMIX_GAIN = 0.7071f;
static constexpr float FOLD_GAIN = 0.7071f;
static constexpr float FOLD_GAIN_TWICE = FOLD_GAIN * FOLD_GAIN;
struct AudioOut2StereoFold {
std::uint32_t channels;
float left[AUDIO_OUT2_PORT_CHANNELS_MAX];
float right[AUDIO_OUT2_PORT_CHANNELS_MAX];
};
static constexpr AudioOut2StereoFold STEREO_FOLDS[] = {
{1, {1.0f}, {1.0f}},
{2, {1.0f, 0.0f}, {0.0f, 1.0f}},
{6, {1.0f, 0.0f, FOLD_GAIN, 0.0f, FOLD_GAIN, 0.0f}, {0.0f, 1.0f, FOLD_GAIN, 0.0f, 0.0f, FOLD_GAIN}},
{8, {1.0f, 0.0f, FOLD_GAIN, 0.0f, FOLD_GAIN, 0.0f, FOLD_GAIN, 0.0f}, {0.0f, 1.0f, FOLD_GAIN, 0.0f, 0.0f, FOLD_GAIN, 0.0f, FOLD_GAIN}},
{12, {1.0f, 0.0f, FOLD_GAIN, 0.0f, FOLD_GAIN, 0.0f, FOLD_GAIN, 0.0f, FOLD_GAIN, 0.0f, FOLD_GAIN_TWICE, 0.0f},
{0.0f, 1.0f, FOLD_GAIN, 0.0f, 0.0f, FOLD_GAIN, 0.0f, FOLD_GAIN, 0.0f, FOLD_GAIN, 0.0f, FOLD_GAIN_TWICE}},
};
static const AudioOut2StereoFold* StereoFoldFor(std::uint32_t channels) {
const auto found = std::find_if(std::begin(STEREO_FOLDS), std::end(STEREO_FOLDS), [channels](const AudioOut2StereoFold& fold) { return fold.channels == channels; });
return found == std::end(STEREO_FOLDS) ? nullptr : found;
}
static void ReadFrame(const AudioOut2Port& port, std::uint32_t frame, float* in) {
const auto first = static_cast<std::size_t>(frame) * port.channels;
@@ -57,22 +78,16 @@ static void AccumulatePadPort(const AudioOut2Port& port, AudioOut2Route route, f
}
static void AccumulatePort(const AudioOut2Port& port, float* out, std::uint32_t frames) {
const auto ch = port.channels;
const float* volume = port.volume;
const auto& fold = *port.fold;
float in[AUDIO_OUT2_PORT_CHANNELS_MAX];
for (std::uint32_t frame = 0; frame < frames; frame++) {
ReadFrame(port, frame, in);
float left = 0.0f;
float right = 0.0f;
if (ch == 1) {
left = right = in[0] * volume[0];
} else if (ch < 8) {
left = in[0] * volume[0];
right = in[1] * volume[1];
} else {
const float centre = in[2] * volume[2] * DOWNMIX_GAIN;
left = in[0] * volume[0] + centre + (in[4] * volume[4] + in[6] * volume[6]) * DOWNMIX_GAIN;
right = in[1] * volume[1] + centre + (in[5] * volume[5] + in[7] * volume[7]) * DOWNMIX_GAIN;
for (std::uint32_t c = 0; c < port.channels; c++) {
const float sample = in[c] * port.volume[c];
if (fold.left[c] != 0.0f) left += sample * fold.left[c];
if (fold.right[c] != 0.0f) right += sample * fold.right[c];
}
out[frame * AUDIO_OUT2_OUTPUT_CHANNELS] += left;
out[frame * AUDIO_OUT2_OUTPUT_CHANNELS + 1] += right;
@@ -144,7 +159,8 @@ int APS5_VABI sceAudioOut2PortCreate(AudioOut2ContextHandle ctx, const AudioOut2
entry.flags = params->flags;
entry.channels = (params->data_format >> FORMAT_CHANNELS_SHIFT) & FORMAT_CHANNELS_MASK;
const auto sampleType = params->data_format & FORMAT_TYPE_MASK;
if (entry.channels == 0 || entry.channels > AUDIO_OUT2_PORT_CHANNELS_MAX || sampleType > 1) {
entry.fold = StereoFoldFor(entry.channels);
if (entry.fold == nullptr || sampleType > 1 || (params->data_format & ~FORMAT_FIELDS_MASK) != 0) {
entry = AudioOut2Port{};
throw std::runtime_error("sceAudioOut2PortCreate: data format 0x" + [&] { char text[16]; std::snprintf(text, sizeof(text), "%x", params->data_format); return std::string(text); }() + " is not implemented");
}
@@ -0,0 +1,252 @@
#include "SceTypes.hpp"
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iterator>
#include <limits>
#include <stdexcept>
#include <string>
#include <vector>
extern "C" {
int APS5_VABI sceAudioOut2ContextResetParam(AudioOut2ContextParam*);
int APS5_VABI sceAudioOut2ContextCreate(const AudioOut2ContextParam*, void*, std::size_t, AudioOut2ContextHandle*);
int APS5_VABI sceAudioOut2ContextDestroy(AudioOut2ContextHandle);
int APS5_VABI sceAudioOut2ContextPush(AudioOut2ContextHandle, std::uint32_t);
int APS5_VABI sceAudioOut2PortCreate(AudioOut2ContextHandle, const AudioOut2PortParam*, AudioOut2PortHandle*);
int APS5_VABI sceAudioOut2PortDestroy(AudioOut2PortHandle);
int APS5_VABI sceAudioOut2PortSetAttributes(AudioOut2PortHandle, const AudioOut2Attribute*, std::uint32_t);
}
static void Require(bool value) { if (!value) std::abort(); }
static void SetEnvironment(const char* name, const std::string& value) {
#ifdef _WIN32
_putenv_s(name, value.c_str());
#else
setenv(name, value.c_str(), 1);
#endif
}
template<typename TFunction>
static bool ThrowsRuntimeError(TFunction function) {
try {
function();
} catch (const std::runtime_error&) {
return true;
}
return false;
}
namespace {
constexpr std::uint32_t grain = 256;
constexpr std::uint32_t frequency = 48000;
constexpr std::uint16_t portTypeMain = 0;
constexpr std::uint32_t attributeData = 0;
constexpr std::uint32_t attributeVolume = 1;
constexpr std::uint32_t formatFloat = 0;
constexpr std::uint32_t formatS16 = 1;
constexpr std::uint32_t silentGrains = 32;
constexpr float masterGain = 0.5f;
constexpr float fold = 0.70710678f;
constexpr float tolerance = 1e-5f;
struct Layout {
std::uint32_t channels;
std::vector<float> left;
std::vector<float> right;
};
const Layout mono{1, {1.0f}, {1.0f}};
const Layout stereo{2, {1.0f, 0.0f}, {0.0f, 1.0f}};
const Layout surround51{6, {1.0f, 0.0f, fold, 0.0f, fold, 0.0f}, {0.0f, 1.0f, fold, 0.0f, 0.0f, fold}};
const Layout surround71{8, {1.0f, 0.0f, fold, 0.0f, fold, 0.0f, fold, 0.0f}, {0.0f, 1.0f, fold, 0.0f, 0.0f, fold, 0.0f, fold}};
const Layout surround714{12, {1.0f, 0.0f, fold, 0.0f, fold, 0.0f, fold, 0.0f, fold, 0.0f, fold * fold, 0.0f},
{0.0f, 1.0f, fold, 0.0f, 0.0f, fold, 0.0f, fold, 0.0f, fold, 0.0f, fold * fold}};
std::uint32_t Format(std::uint32_t channels, std::uint32_t type) {
return channels << 8 | type;
}
AudioOut2ContextHandle CreateContext() {
AudioOut2ContextParam params{};
Require(sceAudioOut2ContextResetParam(&params) == 0);
params.num_grains = grain;
params.queue_depth = 1;
AudioOut2ContextHandle context = 0;
Require(sceAudioOut2ContextCreate(&params, nullptr, 0, &context) == 0);
Require(context != 0);
return context;
}
int CreatePort(AudioOut2ContextHandle context, std::uint32_t format, AudioOut2PortHandle* port) {
AudioOut2PortParam params{};
params.port_type = portTypeMain;
params.data_format = format;
params.sampling_freq = frequency;
return sceAudioOut2PortCreate(context, &params, port);
}
void SetData(AudioOut2PortHandle port, const void* data) {
const AudioOut2Attribute attribute{attributeData, 0, &data, sizeof(data)};
Require(sceAudioOut2PortSetAttributes(port, &attribute, 1) == 0);
}
void SetVolume(AudioOut2PortHandle port, const std::vector<float>& volume) {
const AudioOut2Attribute attribute{attributeVolume, 0, volume.data(), volume.size() * sizeof(float)};
Require(sceAudioOut2PortSetAttributes(port, &attribute, 1) == 0);
}
std::vector<float> Play(std::uint32_t format, const void* data, const std::vector<float>& volume) {
const auto path = std::filesystem::temp_directory_path() / "anyps5_audio_out2_port_layouts.raw";
std::filesystem::remove(path);
SetEnvironment("SDL_DISKAUDIOFILE", path.string());
const auto context = CreateContext();
AudioOut2PortHandle port = 0;
Require(CreatePort(context, format, &port) == 0);
SetVolume(port, volume);
SetData(port, data);
Require(sceAudioOut2ContextPush(context, 1) == 0);
SetData(port, nullptr);
for (std::uint32_t push = 0; push < silentGrains; push++) Require(sceAudioOut2ContextPush(context, 1) == 0);
Require(sceAudioOut2PortDestroy(port) == 0);
Require(sceAudioOut2ContextDestroy(context) == 0);
std::ifstream file(path, std::ios::binary);
const std::vector<char> bytes{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
file.close();
std::filesystem::remove(path);
Require(bytes.size() % sizeof(float) == 0);
const auto* samples = reinterpret_cast<const float*>(bytes.data());
std::size_t first = 0;
std::size_t last = bytes.size() / sizeof(float);
while (first < last && samples[first] == 0.0f) first++;
while (last > first && samples[last - 1] == 0.0f) last--;
return {samples + first, samples + last};
}
float Sample(std::uint32_t channel, std::uint32_t frame) {
const float sign = frame % 2 == 0 ? 1.0f : -1.0f;
return sign * 0.01f * static_cast<float>(channel + 1) * static_cast<float>(1 + frame % 5);
}
std::vector<float> FloatGrain(std::uint32_t channels) {
std::vector<float> data(static_cast<std::size_t>(grain) * channels);
for (std::uint32_t frame = 0; frame < grain; frame++) {
for (std::uint32_t channel = 0; channel < channels; channel++) data[frame * channels + channel] = Sample(channel, frame);
}
return data;
}
std::vector<std::int16_t> S16Grain(std::uint32_t channels) {
std::vector<std::int16_t> data(static_cast<std::size_t>(grain) * channels);
for (std::uint32_t frame = 0; frame < grain; frame++) {
for (std::uint32_t channel = 0; channel < channels; channel++) data[frame * channels + channel] = static_cast<std::int16_t>(std::lround(Sample(channel, frame) * 32768.0f));
}
return data;
}
std::vector<float> Volume(std::uint32_t channels) {
std::vector<float> volume(channels);
for (std::uint32_t channel = 0; channel < channels; channel++) volume[channel] = 1.0f - 0.05f * static_cast<float>(channel);
return volume;
}
template<typename TSample>
void RequireFold(const std::vector<float>& played, const Layout& layout, const std::vector<TSample>& data, const std::vector<float>& volume, float scale) {
Require(played.size() == static_cast<std::size_t>(grain) * 2);
for (std::uint32_t frame = 0; frame < grain; frame++) {
float left = 0.0f;
float right = 0.0f;
for (std::uint32_t channel = 0; channel < layout.channels; channel++) {
const float sample = static_cast<float>(data[frame * layout.channels + channel]) * scale * volume[channel];
left += sample * layout.left[channel];
right += sample * layout.right[channel];
}
Require(std::fabs(played[frame * 2] - left * masterGain) <= tolerance);
Require(std::fabs(played[frame * 2 + 1] - right * masterGain) <= tolerance);
}
}
void TestFloatLayout(const Layout& layout) {
const auto data = FloatGrain(layout.channels);
const auto volume = Volume(layout.channels);
RequireFold(Play(Format(layout.channels, formatFloat), data.data(), volume), layout, data, volume, 1.0f);
}
void TestS16Layout(const Layout& layout) {
const auto data = S16Grain(layout.channels);
const auto volume = Volume(layout.channels);
RequireFold(Play(Format(layout.channels, formatS16), data.data(), volume), layout, data, volume, 1.0f / 32768.0f);
}
void TestHeightChannels() {
std::vector<float> data(static_cast<std::size_t>(grain) * surround714.channels, 0.0f);
for (std::uint32_t frame = 0; frame < grain; frame++) {
data[frame * 12 + 8] = 0.5f;
data[frame * 12 + 9] = -0.25f;
data[frame * 12 + 10] = 0.5f;
data[frame * 12 + 11] = 1.0f;
}
const std::vector<float> volume{1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.5f};
const auto played = Play(Format(12, formatFloat), data.data(), volume);
Require(played.size() == static_cast<std::size_t>(grain) * 2);
for (std::uint32_t frame = 0; frame < grain; frame++) {
Require(std::fabs(played[frame * 2] - (0.5f * fold + 0.5f * fold * fold) * masterGain) <= tolerance);
Require(std::fabs(played[frame * 2 + 1] - (-0.25f * fold + 0.5f * fold * fold) * masterGain) <= tolerance);
}
}
void TestDroppedLfe() {
auto data = FloatGrain(surround714.channels);
auto expected = data;
for (std::uint32_t frame = 0; frame < grain; frame++) {
data[frame * surround714.channels + 3] = std::numeric_limits<float>::infinity();
expected[frame * surround714.channels + 3] = 0.0f;
}
const auto volume = Volume(surround714.channels);
RequireFold(Play(Format(surround714.channels, formatFloat), data.data(), volume), surround714, expected, volume, 1.0f);
}
void TestRejectedLayouts() {
const auto context = CreateContext();
for (const std::uint32_t channels : {0u, 3u, 4u, 5u, 7u, 9u, 10u, 11u, 13u, 15u}) {
AudioOut2PortHandle port = 0;
Require(ThrowsRuntimeError([&] { CreatePort(context, Format(channels, formatFloat), &port); }));
Require(ThrowsRuntimeError([&] { CreatePort(context, Format(channels, formatS16), &port); }));
}
AudioOut2PortHandle port = 0;
Require(ThrowsRuntimeError([&] { CreatePort(context, Format(12, 2), &port); }));
Require(ThrowsRuntimeError([&] { CreatePort(context, Format(12, formatFloat) | 0x1000, &port); }));
Require(ThrowsRuntimeError([&] { CreatePort(context, Format(12, formatFloat) | 0x10000, &port); }));
Require(CreatePort(context, Format(12, formatFloat), &port) == 0);
Require(port != 0);
Require(sceAudioOut2PortDestroy(port) == 0);
Require(sceAudioOut2ContextDestroy(context) == 0);
}
}
int main() {
SetEnvironment("SDL_AUDIODRIVER", "disk");
for (const Layout* layout : {&mono, &stereo, &surround51, &surround71, &surround714}) {
TestFloatLayout(*layout);
TestS16Layout(*layout);
}
{
const auto data = FloatGrain(8);
const auto volume = Volume(8);
RequireFold(Play(Format(8, formatFloat) | 0x80, data.data(), volume), surround71, data, volume, 1.0f);
}
TestHeightChannels();
TestDroppedLfe();
TestRejectedLayouts();
return 0;
}
+3
View File
@@ -76,6 +76,9 @@ Throughout the project, every function at every stage either **does exactly what
- [sceAudioOutSetMixLevelPadSpk](../../core/libs/prx/libSceAudioOut/src/AudioOut.cpp) (libSceAudioOut) - effect of a negative mix level unknown; only 0 to 32768 (0 dB) is accepted
- [sceAudioOutSetMixLevelPadSpk](../../core/libs/prx/libSceAudioOut/src/AudioOut.cpp) (libSceAudioOut) - no source applies the level (shadPS4 only stores it); it is assumed to be a linear gain on the port's own output, and the -9 dB default (11626) is applied to every pad speaker port, including those of titles that never call the function
- [sceAudioOutGetLastOutputTime](../../core/libs/prx/libSceAudioOut/src/AudioOut.cpp) (libSceAudioOut) - signature and behaviour taken from the [shadPS4](https://github.com/shadps4-emu/shadPS4) and [fpPS4](https://github.com/red-prig/fpPS4) reimplementations of the PS4 library, not confirmed on a PS5 title: the time is the process time at which `sceAudioOutOutput` or `sceAudioOutOutputs` accepted the port's last block, not the time the block reached the device
- [sceAudioOut2PortCreate](../../core/libs/prx/libSceAudioOut/src/AudioOut2Port.cpp) (libSceAudioOut) - `data_format` is read as the channel count (bits 8-11: only 1, 2, 6, 8 or 12) and sample type (bits 0-6: 0 float, 1 int16); bit 7 and the port `flags` are not decoded, and bits 12-31 throw
- [sceAudioOut2PortCreate](../../core/libs/prx/libSceAudioOut/src/AudioOut2Port.cpp) (libSceAudioOut) - 12 channels are assumed to be L R C LFE Ls Rs Lb Rb Ltf Rtf Ltb Rtb as in KytyPS5's 12-to-8 fold, and 6 and 8 channels to be its first 6 and 8; no title or SDK header confirms the order
- [sceAudioOut2PortCreate](../../core/libs/prx/libSceAudioOut/src/AudioOut2Port.cpp) (libSceAudioOut) - the stereo fold is assumed, not the console's downmix: C at -3 dB into both sides, Ls/Rs, Lb/Rb and Ltf/Rtf at -3 dB and Ltb/Rtb at -6 dB into their own side, LFE dropped
- [scePsmlMfsrGetContextBufferRequirement1100, scePsmlMfsrCreateContext1100, scePsmlMfsrGetDispatchMfsrPacket1100](../../core/libs/prx/libScePsml_debug/Export.cpp) (libScePsml_debug) - argument layouts unknown; only 0x8A810001 (not initialized) is returned
- [BnMAMrsfVWo](../../core/libs/prx/libc/src/HeapDiagnostics.cpp) (libc) - unknown name, signature
- [scePngEncEncode](../../core/libs/prx/libScePngEnc/Export.cpp) (libScePngEnc) - whether rows may stay unfiltered when `filter_type` names a subset of the filters is unknown; as in shadPS4, they may only for 0 and for the all-filters mask