Files
ppsspp/Core/HLE/sceVaudio.cpp
T
Henrik RydgårdandClaude Opus 5 fd72f308af sceAudio: fix a crash when reserving a channel with none free
GetFreeChannel counted down with an unsigned loop variable, so i >= 0
was always true. With every channel already reserved it wrapped past
zero and kept indexing g_audioChans until it walked off the end -
sceAudioChReserve(-1, ...) segfaulted the emulator instead of returning
"no channels available". Reproduces on audio/sceaudio/reserve, which
crashed before printing anything.

Also gives sceVaudioChReserve the parameter checks it never had. It
took any sample count, channel count and frequency; the hardware allows
256, 1024 or 2048 samples, stereo only, and the same sample rates the
SRC channel accepts. Every value in the test now matches - what's left
there is only reschedule markers.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JvJR8oJNSCimCM9KXVLjfq
2026-09-04 18:12:31 -06:00

125 lines
5.1 KiB
C++

// Copyright (c) 2012- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "Common/Serialize/Serializer.h"
#include "Common/Serialize/SerializeFuncs.h"
#include "Core/Reporting.h"
#include "Core/HLE/HLE.h"
#include "Core/HLE/FunctionWrappers.h"
#include "Core/HLE/ErrorCodes.h"
#include "Core/HLE/sceVaudio.h"
#include "Core/HLE/sceAudio.h"
#include "Core/HLE/__sceAudio.h"
// Ultra hacky Vaudio implementation. Not sure what the point of this API is.
bool vaudioReserved = false;
void __VaudioInit() {
vaudioReserved = false;
}
void __VaudioDoState(PointerWrap &p) {
auto s = p.Section("sceVaudio", 1);
if (!s)
return;
Do(p, vaudioReserved);
}
static u32 sceVaudioChReserve(int sampleCount, int freq, int format) {
// The vaudio channel is pickier than the normal ones: only three sample counts, stereo only,
// and the same set of sample rates the SRC channel takes.
if (format != 2) {
ERROR_LOG(Log::sceAudio, "sceVaudioChReserve(%i, %i, %i) - unexpected format", sampleCount, freq, format);
return SCE_KERNEL_ERROR_INVALID_FORMAT;
}
if (sampleCount != 256 && sampleCount != 1024 && sampleCount != 2048) {
ERROR_LOG(Log::sceAudio, "sceVaudioChReserve(%i, %i, %i) - invalid sample count", sampleCount, freq, format);
return SCE_KERNEL_ERROR_INVALID_SIZE;
}
if (freq != 0 && !SRCFrequencyAllowed(freq)) {
ERROR_LOG(Log::sceAudio, "sceVaudioChReserve(%i, %i, %i) - invalid frequency", sampleCount, freq, format);
return SCE_ERROR_AUDIO_INVALID_FREQUENCY;
}
if (vaudioReserved) {
ERROR_LOG(Log::sceAudio, "sceVaudioChReserve(%i, %i, %i) - already reserved", sampleCount, freq, format);
return SCE_KERNEL_ERROR_BUSY;
}
// We still have to check the channel also, which gives a different error.
if (g_audioChans[PSP_AUDIO_CHANNEL_VAUDIO].reserved) {
ERROR_LOG(Log::sceAudio, "sceVaudioChReserve(%i, %i, %i) - channel already reserved", sampleCount, freq, format);
return SCE_ERROR_AUDIO_CHANNEL_ALREADY_RESERVED;
}
DEBUG_LOG(Log::sceAudio, "sceVaudioChReserve(%i, %i, %i)", sampleCount, freq, format);
g_audioChans[PSP_AUDIO_CHANNEL_VAUDIO].reserved = true;
g_audioChans[PSP_AUDIO_CHANNEL_VAUDIO].sampleCount = sampleCount;
g_audioChans[PSP_AUDIO_CHANNEL_VAUDIO].format = format == 2 ? PSP_AUDIO_FORMAT_STEREO : PSP_AUDIO_FORMAT_MONO;
g_audioChans[PSP_AUDIO_CHANNEL_VAUDIO].leftVolume = 0;
g_audioChans[PSP_AUDIO_CHANNEL_VAUDIO].rightVolume = 0;
vaudioReserved = true;
__AudioSetSRCFrequency(freq);
return 0;
}
static u32 sceVaudioChRelease() {
DEBUG_LOG(Log::sceAudio, "sceVaudioChRelease(...)");
if (!g_audioChans[PSP_AUDIO_CHANNEL_VAUDIO].reserved) {
return SCE_ERROR_AUDIO_CHANNEL_NOT_RESERVED;
} else {
g_audioChans[PSP_AUDIO_CHANNEL_VAUDIO].reset();
g_audioChans[PSP_AUDIO_CHANNEL_VAUDIO].reserved = false;
vaudioReserved = false;
return 0;
}
}
static u32 sceVaudioOutputBlocking(int vol, u32 buffer) {
DEBUG_LOG(Log::sceAudio, "sceVaudioOutputBlocking(%i, %08x)", vol, buffer);
g_audioChans[PSP_AUDIO_CHANNEL_VAUDIO].leftVolume = vol;
g_audioChans[PSP_AUDIO_CHANNEL_VAUDIO].rightVolume = vol;
// TODO: This may be wrong, not sure if's in a different format?
g_audioChans[PSP_AUDIO_CHANNEL_VAUDIO].sampleAddress = buffer;
return __AudioEnqueue(g_audioChans[PSP_AUDIO_CHANNEL_VAUDIO], PSP_AUDIO_CHANNEL_VAUDIO, true);
}
static u32 sceVaudioSetEffectType(int effectType, int vol) {
ERROR_LOG_REPORT(Log::sceAudio, "UNIMPL sceVaudioSetEffectType(%i, %i)", effectType, vol);
return 0;
}
// SensMe shows that this controls the automatic audio volume normalizer
static u32 sceVaudioSetAlcMode(int alcMode) {
ERROR_LOG_REPORT(Log::sceAudio, "UNIMPL sceVaudioSetAlcMode(%i)", alcMode);
return 0;
}
const HLEFunction sceVaudio[] = {
{0X8986295E, &WrapU_IU<sceVaudioOutputBlocking>, "sceVaudioOutputBlocking", 'x', "ix" },
{0X03B6807D, &WrapU_III<sceVaudioChReserve>, "sceVaudioChReserve", 'x', "iii"},
{0X67585DFD, &WrapU_V<sceVaudioChRelease>, "sceVaudioChRelease", 'x', "" },
{0X346FBE94, &WrapU_II<sceVaudioSetEffectType>, "sceVaudioSetEffectType", 'x', "ii" },
{0XCBD4AC51, &WrapU_I<sceVaudioSetAlcMode>, "sceVaudioSetAlcMode", 'x', "i" },
{0X504E4745, nullptr, "sceVaudio_504E4745", '?', "" },
{0X27ACC20B, nullptr, "sceVaudioChReserveBuffering", '?', "" },
{0XE8E78DC8, nullptr, "sceVaudio_E8E78DC8", '?', "" },
};
void Register_sceVaudio() {
RegisterHLEModule("sceVaudio",ARRAY_SIZE(sceVaudio), sceVaudio );
}