From bcf89754c71421fdae956fcac7903ea278188795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 16 Mar 2025 11:50:57 +0100 Subject: [PATCH 1/4] First step: Split out the track analysis. --- Core/HLE/AtracCtx.cpp | 12 +++--- Core/HLE/AtracCtx.h | 8 ++-- Core/HLE/AtracCtx2.cpp | 8 ++-- Core/HLE/AtracCtx2.h | 4 +- Core/HLE/sceAtrac.cpp | 92 +++++++++++++++++++++++++++++++++++++----- 5 files changed, 98 insertions(+), 26 deletions(-) diff --git a/Core/HLE/AtracCtx.cpp b/Core/HLE/AtracCtx.cpp index d9252c0aef..9521711b4a 100644 --- a/Core/HLE/AtracCtx.cpp +++ b/Core/HLE/AtracCtx.cpp @@ -241,8 +241,7 @@ void Track::DebugLog() { DEBUG_LOG(Log::ME, "loopStartSample: %d loopEndSample: %d", loopStartSample, loopEndSample); } -int Atrac::Analyze(u32 addr, u32 size) { - track_ = {}; +int Atrac::Analyze(const Track &track, u32 addr, u32 size) { first_ = {}; first_.addr = addr; first_.size = size; @@ -265,10 +264,10 @@ int Atrac::Analyze(u32 addr, u32 size) { return SCE_ERROR_ATRAC_UNKNOWN_FORMAT; } - int retval = AnalyzeAtracTrack(addr, size, &track_); + track_ = track; first_._filesize_dontuse = track_.fileSize; track_.DebugLog(); - return retval; + return 0; } int AnalyzeAtracTrack(u32 addr, u32 size, Track *track) { @@ -537,14 +536,15 @@ int AnalyzeAA3Track(u32 addr, u32 size, u32 fileSize, Track *track) { return 0; } -int Atrac::AnalyzeAA3(u32 addr, u32 size, u32 fileSize) { +int Atrac::AnalyzeAA3(const Track &track, u32 addr, u32 size, u32 fileSize) { first_.addr = addr; first_.size = size; first_._filesize_dontuse = fileSize; AnalyzeReset(); - return AnalyzeAA3Track(addr, size, fileSize, &track_); + track_ = track; + return 0; } int Atrac::GetSoundSample(int *endSample, int *loopStartSample, int *loopEndSample) const { diff --git a/Core/HLE/AtracCtx.h b/Core/HLE/AtracCtx.h index e32ee428e0..edff2c89d4 100644 --- a/Core/HLE/AtracCtx.h +++ b/Core/HLE/AtracCtx.h @@ -223,8 +223,8 @@ public: virtual int BytesPerFrame() const = 0; virtual int SamplesPerFrame() const = 0; - virtual int Analyze(u32 addr, u32 size) = 0; - virtual int AnalyzeAA3(u32 addr, u32 size, u32 filesize) = 0; + virtual int Analyze(const Track &track, u32 addr, u32 size) = 0; + virtual int AnalyzeAA3(const Track &track, u32 addr, u32 size, u32 filesize) = 0; void UpdateContextFromPSPMem(); virtual void WriteContextToPSPMem() = 0; @@ -275,8 +275,8 @@ public: void DoState(PointerWrap &p) override; void WriteContextToPSPMem() override; - int Analyze(u32 addr, u32 size) override; - int AnalyzeAA3(u32 addr, u32 size, u32 filesize) override; + int Analyze(const Track &track, u32 addr, u32 size) override; + int AnalyzeAA3(const Track &track, u32 addr, u32 size, u32 filesize) override; int GetNextDecodePosition(int *pos) const override; int RemainingFrames() const override; diff --git a/Core/HLE/AtracCtx2.cpp b/Core/HLE/AtracCtx2.cpp index acb3f23b4e..72c8862140 100644 --- a/Core/HLE/AtracCtx2.cpp +++ b/Core/HLE/AtracCtx2.cpp @@ -82,12 +82,12 @@ void Atrac2::WriteContextToPSPMem() { NotifyMemInfo(MemBlockFlags::WRITE, context_.ptr, sizeof(SceAtracContext), "AtracContext"); } -int Atrac2::Analyze(u32 addr, u32 size) { - return AnalyzeAtracTrack(addr, size, &track_); +int Atrac2::Analyze(const Track &track, u32 addr, u32 size) { + return 0; } -int Atrac2::AnalyzeAA3(u32 addr, u32 size, u32 filesize) { - return AnalyzeAA3Track(addr, size, filesize, &track_); +int Atrac2::AnalyzeAA3(const Track &track, u32 addr, u32 size, u32 filesize) { + return 0; } int Atrac2::RemainingFrames() const { diff --git a/Core/HLE/AtracCtx2.h b/Core/HLE/AtracCtx2.h index adc408b54b..55c3c26eb5 100644 --- a/Core/HLE/AtracCtx2.h +++ b/Core/HLE/AtracCtx2.h @@ -11,8 +11,8 @@ public: void DoState(PointerWrap &p) override; void WriteContextToPSPMem() override; - int Analyze(u32 addr, u32 size) override; - int AnalyzeAA3(u32 addr, u32 size, u32 filesize) override; + int Analyze(const Track &track, u32 addr, u32 size) override; + int AnalyzeAA3(const Track &track, u32 addr, u32 size, u32 filesize) override; int GetNextDecodePosition(int *pos) const { return 0; } int RemainingFrames() const override; diff --git a/Core/HLE/sceAtrac.cpp b/Core/HLE/sceAtrac.cpp index 18570c00c1..468367fea2 100644 --- a/Core/HLE/sceAtrac.cpp +++ b/Core/HLE/sceAtrac.cpp @@ -571,12 +571,24 @@ static u32 sceAtracSetHalfwayBuffer(int atracID, u32 buffer, u32 readSize, u32 b return hleLogError(Log::ME, SCE_ERROR_ATRAC_INCORRECT_READ_SIZE, "read size too large"); } - int ret = atrac->Analyze(buffer, readSize); + Track track; + int ret = AnalyzeAtracTrack(buffer, readSize, &track); if (ret < 0) { return hleLogError(Log::ME, ret); } + ret = atrac->Analyze(track, buffer, readSize); + if (ret < 0) { + return hleLogError(Log::ME, ret); + } + + if (atrac->CodecType() != atracContextTypes[atracID]) { + // TODO: Should this not change the buffer size? + return hleReportError(Log::ME, SCE_ERROR_ATRAC_WRONG_CODECTYPE, "atracID uses different codec type than data"); + } + ret = atrac->SetData(buffer, readSize, bufferSize, 2); + // not sure the real delay time return hleDelayResult(hleLogDebugOrError(Log::ME, ret), "atrac set data", 100); } @@ -596,7 +608,13 @@ static u32 sceAtracSetData(int atracID, u32 buffer, u32 bufferSize) { return hleLogError(Log::ME, SCE_ERROR_ATRAC_BAD_ATRACID, "bad atrac ID"); } - int ret = atrac->Analyze(buffer, bufferSize); + Track track; + int ret = AnalyzeAtracTrack(buffer, bufferSize, &track); + if (ret < 0) { + return hleLogError(Log::ME, ret); + } + + ret = atrac->Analyze(track, buffer, bufferSize); if (ret < 0) { return hleLogError(Log::ME, ret); } @@ -618,8 +636,14 @@ static int sceAtracSetDataAndGetID(u32 buffer, int bufferSize) { bufferSize = 0x10000000; } + Track track; + int ret = AnalyzeAtracTrack(buffer, bufferSize, &track); + if (ret < 0) { + return hleLogError(Log::ME, ret); + } + AtracBase *atrac = allocAtrac(); - int ret = atrac->Analyze(buffer, bufferSize); + ret = atrac->Analyze(track, buffer, bufferSize); if (ret < 0) { delete atrac; return hleLogError(Log::ME, ret); @@ -638,8 +662,15 @@ static int sceAtracSetHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 buffer if (readSize > bufferSize) { return hleLogError(Log::ME, SCE_ERROR_ATRAC_INCORRECT_READ_SIZE, "read size too large"); } + + Track track; + int ret = AnalyzeAtracTrack(buffer, bufferSize, &track); + if (ret < 0) { + return hleLogError(Log::ME, ret); + } + AtracBase *atrac = allocAtrac(); - int ret = atrac->Analyze(buffer, readSize); + ret = atrac->Analyze(track, buffer, readSize); if (ret < 0) { delete atrac; return hleLogError(Log::ME, ret); @@ -752,7 +783,13 @@ static int sceAtracSetMOutHalfwayBuffer(int atracID, u32 buffer, u32 readSize, u return hleLogError(Log::ME, SCE_ERROR_ATRAC_INCORRECT_READ_SIZE, "read size too large"); } - int ret = atrac->Analyze(buffer, readSize); + Track track; + int ret = AnalyzeAtracTrack(buffer, bufferSize, &track); + if (ret < 0) { + return hleLogError(Log::ME, ret); + } + + ret = atrac->Analyze(track, buffer, readSize); if (ret < 0) { return hleLogError(Log::ME, ret); } @@ -767,6 +804,8 @@ static int sceAtracSetMOutHalfwayBuffer(int atracID, u32 buffer, u32 readSize, u } // Note: This doesn't seem to be part of any available libatrac3plus library. +// So we should probably remove it? +// HalfwayBuffer can fully replace it though, of course (just set readSize == bufferSize). static u32 sceAtracSetMOutData(int atracID, u32 buffer, u32 bufferSize) { AtracBase *atrac = getAtrac(atracID); // Don't use AtracValidate* here. @@ -774,10 +813,17 @@ static u32 sceAtracSetMOutData(int atracID, u32 buffer, u32 bufferSize) { return hleLogError(Log::ME, SCE_ERROR_ATRAC_BAD_ATRACID, "bad atrac ID"); } - int ret = atrac->Analyze(buffer, bufferSize); + Track track; + int ret = AnalyzeAtracTrack(buffer, bufferSize, &track); if (ret < 0) { return hleLogError(Log::ME, ret); } + + ret = atrac->Analyze(track, buffer, bufferSize); + if (ret < 0) { + return hleLogError(Log::ME, ret); + } + if (atrac->Channels() != 1) { // It seems it still sets the data. atrac->SetData(buffer, bufferSize, bufferSize, 2); @@ -789,9 +835,16 @@ static u32 sceAtracSetMOutData(int atracID, u32 buffer, u32 bufferSize) { } // Note: This doesn't seem to be part of any available libatrac3plus library. +// See note in above function. static int sceAtracSetMOutDataAndGetID(u32 buffer, u32 bufferSize) { + Track track; + int ret = AnalyzeAtracTrack(buffer, bufferSize, &track); + if (ret < 0) { + return hleLogError(Log::ME, ret); + } + AtracBase *atrac = allocAtrac(); - int ret = atrac->Analyze(buffer, bufferSize); + ret = atrac->Analyze(track, buffer, bufferSize); if (ret < 0) { delete atrac; return hleLogError(Log::ME, ret); @@ -814,8 +867,15 @@ static int sceAtracSetMOutHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 bu if (readSize > bufferSize) { return hleLogError(Log::ME, SCE_ERROR_ATRAC_INCORRECT_READ_SIZE, "read size too large"); } + + Track track; + int ret = AnalyzeAtracTrack(buffer, bufferSize, &track); + if (ret < 0) { + return hleLogError(Log::ME, ret); + } + AtracBase *atrac = allocAtrac(); - int ret = atrac->Analyze(buffer, readSize); + ret = atrac->Analyze(track, buffer, readSize); if (ret < 0) { delete atrac; return hleLogError(Log::ME, ret); @@ -835,8 +895,14 @@ static int sceAtracSetMOutHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 bu } static int sceAtracSetAA3DataAndGetID(u32 buffer, u32 bufferSize, u32 fileSize, u32 metadataSizeAddr) { + Track track; + int ret = AnalyzeAtracTrack(buffer, bufferSize, &track); + if (ret < 0) { + return hleLogError(Log::ME, ret); + } + AtracBase *atrac = allocAtrac(); - int ret = atrac->AnalyzeAA3(buffer, bufferSize, fileSize); + ret = atrac->AnalyzeAA3(track, buffer, bufferSize, fileSize); if (ret < 0) { delete atrac; return hleLogError(Log::ME, ret); @@ -957,8 +1023,14 @@ static int sceAtracSetAA3HalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 buf return hleLogError(Log::ME, SCE_ERROR_ATRAC_INCORRECT_READ_SIZE, "read size too large"); } + Track track; + int ret = AnalyzeAtracTrack(buffer, bufferSize, &track); + if (ret < 0) { + return hleLogError(Log::ME, ret); + } + AtracBase *atrac = allocAtrac(); - int ret = atrac->AnalyzeAA3(buffer, readSize, fileSize); + ret = atrac->AnalyzeAA3(track, buffer, readSize, fileSize); if (ret < 0) { delete atrac; return hleLogError(Log::ME, ret); From 8ab41e387ed4260344073b3b0558a6091c733936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 16 Mar 2025 12:21:16 +0100 Subject: [PATCH 2/4] Reorganize to make the next step easier --- Core/HLE/AtracCtx.cpp | 1 + Core/HLE/AtracCtx.h | 15 +++++- Core/HLE/AtracCtx2.cpp | 36 --------------- Core/HLE/AtracCtx2.h | 3 ++ Core/HLE/sceAtrac.cpp | 102 +++++++++++++++++++++++++---------------- 5 files changed, 79 insertions(+), 78 deletions(-) diff --git a/Core/HLE/AtracCtx.cpp b/Core/HLE/AtracCtx.cpp index 9521711b4a..d13434638c 100644 --- a/Core/HLE/AtracCtx.cpp +++ b/Core/HLE/AtracCtx.cpp @@ -756,6 +756,7 @@ int Atrac::SetData(u32 buffer, u32 readSize, u32 bufferSize, int outputChannels) } CreateDecoder(); INFO_LOG(Log::ME, "Atrac::SetData (buffer=%08x, readSize=%d, bufferSize=%d): %s %s (%d channels) audio", buffer, readSize, bufferSize, codecName, channelName, track_.channels); + return 0; } diff --git a/Core/HLE/AtracCtx.h b/Core/HLE/AtracCtx.h index edff2c89d4..9f04164588 100644 --- a/Core/HLE/AtracCtx.h +++ b/Core/HLE/AtracCtx.h @@ -194,8 +194,8 @@ public: outputChannels_ = channels; } - // Refactor? - int atracID_ = -1; + virtual void SetID(int id) = 0; + virtual int GetID() const = 0; PSPPointer context_{}; @@ -280,6 +280,14 @@ public: int GetNextDecodePosition(int *pos) const override; int RemainingFrames() const override; + + void SetID(int id) override { + atracID_ = id; + } + int GetID() const override { + return atracID_; + } + u32 SecondBufferSize() const override { return second_.size; } @@ -361,4 +369,7 @@ private: u32 bufferPos_ = 0; u32 bufferValidBytes_ = 0; u32 bufferHeaderSize_ = 0; + + // Refactor? + int atracID_ = -1; }; diff --git a/Core/HLE/AtracCtx2.cpp b/Core/HLE/AtracCtx2.cpp index 72c8862140..98520913cf 100644 --- a/Core/HLE/AtracCtx2.cpp +++ b/Core/HLE/AtracCtx2.cpp @@ -44,42 +44,6 @@ void Atrac2::DoState(PointerWrap &p) { } void Atrac2::WriteContextToPSPMem() { - if (!context_.IsValid()) { - return; - } - // context points into PSP memory. - SceAtracContext *context = context_; - context->info.buffer = 0; // bufferAddr_; // first_.addr; - context->info.bufferByte = 0; // bufferMaxSize_; - context->info.secondBuffer = 0; // TODO - context->info.secondBufferByte = 0; // TODO - context->info.codec = track_.codecType; - context->info.loopNum = loopNum_; - context->info.loopStart = track_.loopStartSample > 0 ? track_.loopStartSample : 0; - context->info.loopEnd = track_.loopEndSample > 0 ? track_.loopEndSample : 0; - - // Note that we read in the state when loading the atrac object, so it's safe - // to update it back here all the time. Some games, like Sol Trigger, change it. - // TODO: Should we just keep this in PSP ram then, or something? - context->info.state = bufferState_; - if (track_.firstSampleOffset != 0) { - context->info.samplesPerChan = track_.FirstSampleOffsetFull(); - } else { - context->info.samplesPerChan = (track_.codecType == PSP_MODE_AT_3_PLUS ? ATRAC3PLUS_MAX_SAMPLES : ATRAC3_MAX_SAMPLES); - } - context->info.sampleSize = track_.bytesPerFrame; - context->info.numChan = track_.channels; - context->info.dataOff = track_.dataByteOffset; - context->info.endSample = track_.endSample + track_.FirstSampleOffsetFull(); - context->info.dataEnd = track_.fileSize; - context->info.curOff = 0; // first_.fileoffset; - context->info.decodePos = track_.DecodePosBySample(currentSample_); - context->info.streamDataByte = 0; // first_.size - track_.dataOff; - - u8 *buf = (u8 *)context; - *(u32_le *)(buf + 0xfc) = atracID_; - - NotifyMemInfo(MemBlockFlags::WRITE, context_.ptr, sizeof(SceAtracContext), "AtracContext"); } int Atrac2::Analyze(const Track &track, u32 addr, u32 size) { diff --git a/Core/HLE/AtracCtx2.h b/Core/HLE/AtracCtx2.h index 55c3c26eb5..1d754f04c2 100644 --- a/Core/HLE/AtracCtx2.h +++ b/Core/HLE/AtracCtx2.h @@ -14,6 +14,9 @@ public: int Analyze(const Track &track, u32 addr, u32 size) override; int AnalyzeAA3(const Track &track, u32 addr, u32 size, u32 filesize) override; + void SetID(int id) override {} + int GetID() const override { return 0; } + int GetNextDecodePosition(int *pos) const { return 0; } int RemainingFrames() const override; int LoopStatus() const override { return 0; } diff --git a/Core/HLE/sceAtrac.cpp b/Core/HLE/sceAtrac.cpp index 468367fea2..b0686892bd 100644 --- a/Core/HLE/sceAtrac.cpp +++ b/Core/HLE/sceAtrac.cpp @@ -180,11 +180,11 @@ static AtracBase *getAtrac(int atracID) { return atrac; } -static int createAtrac(AtracBase *atrac) { +static int RegisterAtrac(AtracBase *atrac) { for (int i = 0; i < (int)ARRAY_SIZE(atracContexts); ++i) { if (atracContextTypes[i] == atrac->CodecType() && atracContexts[i] == 0) { atracContexts[i] = atrac; - atrac->atracID_ = i; + atrac->SetID(i); return i; } } @@ -210,7 +210,7 @@ static u32 sceAtracGetAtracID(int codecType) { } AtracBase *atrac = allocAtrac(codecType); - int atracID = createAtrac(atrac); + int atracID = RegisterAtrac(atrac); if (atracID < 0) { delete atrac; return hleLogError(Log::ME, atracID, "no free ID"); @@ -576,17 +576,15 @@ static u32 sceAtracSetHalfwayBuffer(int atracID, u32 buffer, u32 readSize, u32 b if (ret < 0) { return hleLogError(Log::ME, ret); } + if (track.codecType != atracContextTypes[atracID]) { + // TODO: Should this not change the buffer size? + return hleLogError(Log::ME, SCE_ERROR_ATRAC_WRONG_CODECTYPE, "atracID uses different codec type than data"); + } ret = atrac->Analyze(track, buffer, readSize); if (ret < 0) { return hleLogError(Log::ME, ret); } - - if (atrac->CodecType() != atracContextTypes[atracID]) { - // TODO: Should this not change the buffer size? - return hleReportError(Log::ME, SCE_ERROR_ATRAC_WRONG_CODECTYPE, "atracID uses different codec type than data"); - } - ret = atrac->SetData(buffer, readSize, bufferSize, 2); // not sure the real delay time @@ -613,17 +611,15 @@ static u32 sceAtracSetData(int atracID, u32 buffer, u32 bufferSize) { if (ret < 0) { return hleLogError(Log::ME, ret); } + if (track.codecType != atracContextTypes[atracID]) { + // TODO: Should this not change the buffer size? + return hleReportError(Log::ME, SCE_ERROR_ATRAC_WRONG_CODECTYPE, "atracID uses different codec type than data"); + } ret = atrac->Analyze(track, buffer, bufferSize); if (ret < 0) { return hleLogError(Log::ME, ret); } - - if (atrac->CodecType() != atracContextTypes[atracID]) { - // TODO: Should this not change the buffer size? - return hleReportError(Log::ME, SCE_ERROR_ATRAC_WRONG_CODECTYPE, "atracID uses different codec type than data"); - } - ret = atrac->SetData(buffer, bufferSize, bufferSize, 2); return hleDelayResult(hleLogDebugOrError(Log::ME, ret), "atrac set data", 100); } @@ -648,14 +644,19 @@ static int sceAtracSetDataAndGetID(u32 buffer, int bufferSize) { delete atrac; return hleLogError(Log::ME, ret); } - int atracID = createAtrac(atrac); + ret = atrac->SetData(buffer, bufferSize, bufferSize, 2); + if (ret < 0) { + delete atrac; + return hleLogError(Log::ME, ret); + } + + int atracID = RegisterAtrac(atrac); if (atracID < 0) { delete atrac; return hleLogError(Log::ME, atracID, "no free ID"); } - ret = atrac->SetData(buffer, bufferSize, bufferSize, 2); - return hleDelayResult(hleLogDebugOrError(Log::ME, ret == 0 ? atracID : ret), "atrac set data", 100); + return hleDelayResult(hleLogDebug(Log::ME, atracID), "atrac set data", 100); } static int sceAtracSetHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 bufferSize) { @@ -675,13 +676,19 @@ static int sceAtracSetHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 buffer delete atrac; return hleLogError(Log::ME, ret); } - int atracID = createAtrac(atrac); + ret = atrac->SetData(buffer, readSize, bufferSize, 2); + if (ret < 0) { + delete atrac; + return hleLogError(Log::ME, ret); + } + + int atracID = RegisterAtrac(atrac); if (atracID < 0) { delete atrac; return hleLogError(Log::ME, atracID, "no free ID"); } - ret = atrac->SetData(buffer, readSize, bufferSize, 2); - return hleDelayResult(hleLogDebugOrError(Log::ME, ret == 0 ? atracID : ret), "atrac set data", 100); + + return hleDelayResult(hleLogDebug(Log::ME, atracID), "atrac set data", 100); } static u32 sceAtracStartEntry() { @@ -823,7 +830,6 @@ static u32 sceAtracSetMOutData(int atracID, u32 buffer, u32 bufferSize) { if (ret < 0) { return hleLogError(Log::ME, ret); } - if (atrac->Channels() != 1) { // It seems it still sets the data. atrac->SetData(buffer, bufferSize, bufferSize, 2); @@ -842,6 +848,9 @@ static int sceAtracSetMOutDataAndGetID(u32 buffer, u32 bufferSize) { if (ret < 0) { return hleLogError(Log::ME, ret); } + if (track.channels != 1) { + return hleReportError(Log::ME, SCE_ERROR_ATRAC_NOT_MONO, "not mono data"); + } AtracBase *atrac = allocAtrac(); ret = atrac->Analyze(track, buffer, bufferSize); @@ -849,18 +858,18 @@ static int sceAtracSetMOutDataAndGetID(u32 buffer, u32 bufferSize) { delete atrac; return hleLogError(Log::ME, ret); } - if (atrac->Channels() != 1) { + ret = atrac->SetData(buffer, bufferSize, bufferSize, 1); + if (ret < 0) { delete atrac; - return hleReportError(Log::ME, SCE_ERROR_ATRAC_NOT_MONO, "not mono data"); + return hleLogError(Log::ME, ret); } - int atracID = createAtrac(atrac); + + int atracID = RegisterAtrac(atrac); if (atracID < 0) { delete atrac; return hleLogError(Log::ME, atracID, "no free ID"); } - - ret = atrac->SetData(buffer, bufferSize, bufferSize, 1); - return hleDelayResult(hleLogDebugOrError(Log::ME, ret == 0 ? atracID : ret), "atrac set data", 100); + return hleDelayResult(hleLogDebugOrError(Log::ME, atracID), "atrac set data", 100); } static int sceAtracSetMOutHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 bufferSize) { @@ -873,6 +882,9 @@ static int sceAtracSetMOutHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 bu if (ret < 0) { return hleLogError(Log::ME, ret); } + if (track.channels != 1) { + return hleReportError(Log::ME, SCE_ERROR_ATRAC_NOT_MONO, "not mono data"); + } AtracBase *atrac = allocAtrac(); ret = atrac->Analyze(track, buffer, readSize); @@ -880,18 +892,19 @@ static int sceAtracSetMOutHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 bu delete atrac; return hleLogError(Log::ME, ret); } - if (atrac->Channels() != 1) { + ret = atrac->SetData(buffer, readSize, bufferSize, 1); + if (ret < 0) { delete atrac; - return hleReportError(Log::ME, SCE_ERROR_ATRAC_NOT_MONO, "not mono data"); + return hleLogError(Log::ME, ret); } - int atracID = createAtrac(atrac); + + int atracID = RegisterAtrac(atrac); if (atracID < 0) { delete atrac; return hleLogError(Log::ME, atracID, "no free ID"); } - ret = atrac->SetData(buffer, readSize, bufferSize, 1); - return hleDelayResult(hleLogDebugOrError(Log::ME, ret == 0 ? atracID : ret), "atrac set data", 100); + return hleDelayResult(hleLogDebug(Log::ME, atracID), "atrac set data", 100); } static int sceAtracSetAA3DataAndGetID(u32 buffer, u32 bufferSize, u32 fileSize, u32 metadataSizeAddr) { @@ -907,14 +920,19 @@ static int sceAtracSetAA3DataAndGetID(u32 buffer, u32 bufferSize, u32 fileSize, delete atrac; return hleLogError(Log::ME, ret); } - int atracID = createAtrac(atrac); + ret = atrac->SetData(buffer, bufferSize, bufferSize, 2); + if (ret < 0) { + delete atrac; + return hleLogError(Log::ME, ret); + } + + int atracID = RegisterAtrac(atrac); if (atracID < 0) { delete atrac; return hleLogError(Log::ME, atracID, "no free ID"); } - ret = atrac->SetData(buffer, bufferSize, bufferSize, 2); - return hleDelayResult(hleLogDebugOrError(Log::ME, ret == 0 ? atracID : ret), "atrac set data", 100); + return hleDelayResult(hleLogDebug(Log::ME, atracID), "atrac set aa3 data", 100); } static u32 _sceAtracGetContextAddress(int atracID) { @@ -1035,14 +1053,18 @@ static int sceAtracSetAA3HalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 buf delete atrac; return hleLogError(Log::ME, ret); } - int atracID = createAtrac(atrac); + ret = atrac->SetData(buffer, readSize, bufferSize, 2); + if (ret < 0) { + delete atrac; + return hleLogError(Log::ME, ret); + } + + int atracID = RegisterAtrac(atrac); if (atracID < 0) { delete atrac; return hleLogError(Log::ME, atracID, "no free ID"); } - - ret = atrac->SetData(buffer, readSize, bufferSize, 2); - return hleDelayResult(hleLogDebugOrError(Log::ME, ret == 0 ? atracID : ret), "atrac set data", 100); + return hleDelayResult(hleLogDebug(Log::ME, atracID), "atrac set data", 100); } // External interface used by sceSas' AT3 integration. From fe0edc0bdac719fa7caac2260d2083ec9e7aabbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 16 Mar 2025 12:36:53 +0100 Subject: [PATCH 3/4] One more cleanup before merging Analyze and SetData... --- Core/HLE/AtracCtx.cpp | 4 ++++ Core/HLE/sceAtrac.cpp | 21 ++++----------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/Core/HLE/AtracCtx.cpp b/Core/HLE/AtracCtx.cpp index d13434638c..e59afb9b06 100644 --- a/Core/HLE/AtracCtx.cpp +++ b/Core/HLE/AtracCtx.cpp @@ -757,6 +757,10 @@ int Atrac::SetData(u32 buffer, u32 readSize, u32 bufferSize, int outputChannels) CreateDecoder(); INFO_LOG(Log::ME, "Atrac::SetData (buffer=%08x, readSize=%d, bufferSize=%d): %s %s (%d channels) audio", buffer, readSize, bufferSize, codecName, channelName, track_.channels); + if (track_.channels == 2 && outputChannels == 1) { + // We still do all the tasks, we just return this error. + return SCE_ERROR_ATRAC_NOT_MONO; + } return 0; } diff --git a/Core/HLE/sceAtrac.cpp b/Core/HLE/sceAtrac.cpp index b0686892bd..11da51f2c1 100644 --- a/Core/HLE/sceAtrac.cpp +++ b/Core/HLE/sceAtrac.cpp @@ -702,7 +702,6 @@ static u32 sceAtracSetLoopNum(int atracID, int loopNum) { if (err != 0) { return hleLogError(Log::ME, err); } - int ret = atrac->SetLoopNum(loopNum); if (ret == SCE_ERROR_ATRAC_NO_LOOP_INFORMATION && loopNum == -1) { // Not really an issue @@ -800,14 +799,8 @@ static int sceAtracSetMOutHalfwayBuffer(int atracID, u32 buffer, u32 readSize, u if (ret < 0) { return hleLogError(Log::ME, ret); } - if (atrac->Channels() != 1) { - // It seems it still sets the data. - atrac->SetData(buffer, readSize, bufferSize, 2); - return hleReportError(Log::ME, SCE_ERROR_ATRAC_NOT_MONO, "not mono data"); - } else { - ret = atrac->SetData(buffer, readSize, bufferSize, 1); - return hleDelayResult(hleLogDebugOrError(Log::ME, ret), "atrac set data mono", 100); - } + ret = atrac->SetData(buffer, readSize, bufferSize, 1); + return hleDelayResult(hleLogDebugOrError(Log::ME, ret), "atrac set data mono", 100); } // Note: This doesn't seem to be part of any available libatrac3plus library. @@ -830,14 +823,8 @@ static u32 sceAtracSetMOutData(int atracID, u32 buffer, u32 bufferSize) { if (ret < 0) { return hleLogError(Log::ME, ret); } - if (atrac->Channels() != 1) { - // It seems it still sets the data. - atrac->SetData(buffer, bufferSize, bufferSize, 2); - return hleReportError(Log::ME, SCE_ERROR_ATRAC_NOT_MONO, "not mono data"); - } else { - ret = atrac->SetData(buffer, bufferSize, bufferSize, 1); - return hleDelayResult(hleLogDebugOrError(Log::ME, ret), "atrac set data mono", 100); - } + ret = atrac->SetData(buffer, bufferSize, bufferSize, 1); + return hleDelayResult(hleLogDebugOrError(Log::ME, ret), "atrac set data mono", 100); } // Note: This doesn't seem to be part of any available libatrac3plus library. From 3979b6af5488e025152b436595389e2ee8987e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 16 Mar 2025 12:59:32 +0100 Subject: [PATCH 4/4] Combine the two Analyze functions --- Core/HLE/AtracCtx.cpp | 19 +++++-------------- Core/HLE/AtracCtx.h | 7 +++---- Core/HLE/AtracCtx2.cpp | 6 +----- Core/HLE/AtracCtx2.h | 3 +-- Core/HLE/sceAtrac.cpp | 20 ++++++++++---------- 5 files changed, 20 insertions(+), 35 deletions(-) diff --git a/Core/HLE/AtracCtx.cpp b/Core/HLE/AtracCtx.cpp index e59afb9b06..e0bc7923e6 100644 --- a/Core/HLE/AtracCtx.cpp +++ b/Core/HLE/AtracCtx.cpp @@ -241,7 +241,7 @@ void Track::DebugLog() { DEBUG_LOG(Log::ME, "loopStartSample: %d loopEndSample: %d", loopStartSample, loopEndSample); } -int Atrac::Analyze(const Track &track, u32 addr, u32 size) { +int Atrac::Analyze(const Track &track, u32 addr, u32 size, u32 fileSize) { first_ = {}; first_.addr = addr; first_.size = size; @@ -258,14 +258,16 @@ int Atrac::Analyze(const Track &track, u32 addr, u32 size) { return SCE_KERNEL_ERROR_ILLEGAL_ADDRESS; } + bool aa3 = fileSize != 0; + // TODO: Validate stuff. - if (Memory::ReadUnchecked_U32(addr) != RIFF_CHUNK_MAGIC) { + if (!aa3 && Memory::ReadUnchecked_U32(addr) != RIFF_CHUNK_MAGIC) { ERROR_LOG(Log::ME, "Couldn't find RIFF header"); return SCE_ERROR_ATRAC_UNKNOWN_FORMAT; } track_ = track; - first_._filesize_dontuse = track_.fileSize; + first_._filesize_dontuse = aa3 ? fileSize : track_.fileSize; track_.DebugLog(); return 0; } @@ -536,17 +538,6 @@ int AnalyzeAA3Track(u32 addr, u32 size, u32 fileSize, Track *track) { return 0; } -int Atrac::AnalyzeAA3(const Track &track, u32 addr, u32 size, u32 fileSize) { - first_.addr = addr; - first_.size = size; - first_._filesize_dontuse = fileSize; - - AnalyzeReset(); - - track_ = track; - return 0; -} - int Atrac::GetSoundSample(int *endSample, int *loopStartSample, int *loopEndSample) const { *endSample = track_.endSample; *loopStartSample = track_.loopStartSample == -1 ? -1 : track_.loopStartSample - track_.FirstSampleOffsetFull(); diff --git a/Core/HLE/AtracCtx.h b/Core/HLE/AtracCtx.h index 9f04164588..652a2ffa7c 100644 --- a/Core/HLE/AtracCtx.h +++ b/Core/HLE/AtracCtx.h @@ -223,8 +223,8 @@ public: virtual int BytesPerFrame() const = 0; virtual int SamplesPerFrame() const = 0; - virtual int Analyze(const Track &track, u32 addr, u32 size) = 0; - virtual int AnalyzeAA3(const Track &track, u32 addr, u32 size, u32 filesize) = 0; + // Set filesize to 0 for autodetect, unless you are using aa3. + virtual int Analyze(const Track &track, u32 addr, u32 size, u32 filesize) = 0; void UpdateContextFromPSPMem(); virtual void WriteContextToPSPMem() = 0; @@ -275,8 +275,7 @@ public: void DoState(PointerWrap &p) override; void WriteContextToPSPMem() override; - int Analyze(const Track &track, u32 addr, u32 size) override; - int AnalyzeAA3(const Track &track, u32 addr, u32 size, u32 filesize) override; + int Analyze(const Track &track, u32 addr, u32 size, u32 filesize) override; int GetNextDecodePosition(int *pos) const override; int RemainingFrames() const override; diff --git a/Core/HLE/AtracCtx2.cpp b/Core/HLE/AtracCtx2.cpp index 98520913cf..24e1223ed5 100644 --- a/Core/HLE/AtracCtx2.cpp +++ b/Core/HLE/AtracCtx2.cpp @@ -46,11 +46,7 @@ void Atrac2::DoState(PointerWrap &p) { void Atrac2::WriteContextToPSPMem() { } -int Atrac2::Analyze(const Track &track, u32 addr, u32 size) { - return 0; -} - -int Atrac2::AnalyzeAA3(const Track &track, u32 addr, u32 size, u32 filesize) { +int Atrac2::Analyze(const Track &track, u32 addr, u32 size, u32 filesize) { return 0; } diff --git a/Core/HLE/AtracCtx2.h b/Core/HLE/AtracCtx2.h index 1d754f04c2..41b2ec85f5 100644 --- a/Core/HLE/AtracCtx2.h +++ b/Core/HLE/AtracCtx2.h @@ -11,8 +11,7 @@ public: void DoState(PointerWrap &p) override; void WriteContextToPSPMem() override; - int Analyze(const Track &track, u32 addr, u32 size) override; - int AnalyzeAA3(const Track &track, u32 addr, u32 size, u32 filesize) override; + int Analyze(const Track &track, u32 addr, u32 size, u32 filesize) override; void SetID(int id) override {} int GetID() const override { return 0; } diff --git a/Core/HLE/sceAtrac.cpp b/Core/HLE/sceAtrac.cpp index 11da51f2c1..644a068be0 100644 --- a/Core/HLE/sceAtrac.cpp +++ b/Core/HLE/sceAtrac.cpp @@ -581,7 +581,7 @@ static u32 sceAtracSetHalfwayBuffer(int atracID, u32 buffer, u32 readSize, u32 b return hleLogError(Log::ME, SCE_ERROR_ATRAC_WRONG_CODECTYPE, "atracID uses different codec type than data"); } - ret = atrac->Analyze(track, buffer, readSize); + ret = atrac->Analyze(track, buffer, readSize, 0); if (ret < 0) { return hleLogError(Log::ME, ret); } @@ -616,7 +616,7 @@ static u32 sceAtracSetData(int atracID, u32 buffer, u32 bufferSize) { return hleReportError(Log::ME, SCE_ERROR_ATRAC_WRONG_CODECTYPE, "atracID uses different codec type than data"); } - ret = atrac->Analyze(track, buffer, bufferSize); + ret = atrac->Analyze(track, buffer, bufferSize, 0); if (ret < 0) { return hleLogError(Log::ME, ret); } @@ -639,7 +639,7 @@ static int sceAtracSetDataAndGetID(u32 buffer, int bufferSize) { } AtracBase *atrac = allocAtrac(); - ret = atrac->Analyze(track, buffer, bufferSize); + ret = atrac->Analyze(track, buffer, bufferSize, 0); if (ret < 0) { delete atrac; return hleLogError(Log::ME, ret); @@ -671,7 +671,7 @@ static int sceAtracSetHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 buffer } AtracBase *atrac = allocAtrac(); - ret = atrac->Analyze(track, buffer, readSize); + ret = atrac->Analyze(track, buffer, readSize, 0); if (ret < 0) { delete atrac; return hleLogError(Log::ME, ret); @@ -795,7 +795,7 @@ static int sceAtracSetMOutHalfwayBuffer(int atracID, u32 buffer, u32 readSize, u return hleLogError(Log::ME, ret); } - ret = atrac->Analyze(track, buffer, readSize); + ret = atrac->Analyze(track, buffer, readSize, 0); if (ret < 0) { return hleLogError(Log::ME, ret); } @@ -819,7 +819,7 @@ static u32 sceAtracSetMOutData(int atracID, u32 buffer, u32 bufferSize) { return hleLogError(Log::ME, ret); } - ret = atrac->Analyze(track, buffer, bufferSize); + ret = atrac->Analyze(track, buffer, bufferSize, 0); if (ret < 0) { return hleLogError(Log::ME, ret); } @@ -840,7 +840,7 @@ static int sceAtracSetMOutDataAndGetID(u32 buffer, u32 bufferSize) { } AtracBase *atrac = allocAtrac(); - ret = atrac->Analyze(track, buffer, bufferSize); + ret = atrac->Analyze(track, buffer, bufferSize, 0); if (ret < 0) { delete atrac; return hleLogError(Log::ME, ret); @@ -874,7 +874,7 @@ static int sceAtracSetMOutHalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 bu } AtracBase *atrac = allocAtrac(); - ret = atrac->Analyze(track, buffer, readSize); + ret = atrac->Analyze(track, buffer, readSize, 0); if (ret < 0) { delete atrac; return hleLogError(Log::ME, ret); @@ -902,7 +902,7 @@ static int sceAtracSetAA3DataAndGetID(u32 buffer, u32 bufferSize, u32 fileSize, } AtracBase *atrac = allocAtrac(); - ret = atrac->AnalyzeAA3(track, buffer, bufferSize, fileSize); + ret = atrac->Analyze(track, buffer, bufferSize, fileSize); if (ret < 0) { delete atrac; return hleLogError(Log::ME, ret); @@ -1035,7 +1035,7 @@ static int sceAtracSetAA3HalfwayBufferAndGetID(u32 buffer, u32 readSize, u32 buf } AtracBase *atrac = allocAtrac(); - ret = atrac->AnalyzeAA3(track, buffer, readSize, fileSize); + ret = atrac->Analyze(track, buffer, readSize, fileSize); if (ret < 0) { delete atrac; return hleLogError(Log::ME, ret);