Skip to content

Commit

Permalink
Copy strm files, no matching
Browse files Browse the repository at this point in the history
  • Loading branch information
robojumper committed Nov 5, 2024
1 parent 54ec310 commit a6a9609
Show file tree
Hide file tree
Showing 11 changed files with 1,233 additions and 32 deletions.
2 changes: 1 addition & 1 deletion config/SOUE01/symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40960,7 +40960,7 @@ sInitialized__29@unnamed@snd_SoundSystem_cpp@ = .sbss:0x805766B8; // type:object
sMaxVoices__Q34nw4r3snd11SoundSystem = .sbss:0x805766BC; // type:object size:0x4 data:4byte
@GUARD@GetInstance__Q44nw4r3snd6detail11SoundThreadFv@instance = .sbss:0x805766C0; // type:object size:0x8 data:byte
lbl_805766C8 = .sbss:0x805766C8; // type:object size:0x8 data:byte
lbl_805766D0 = .sbss:0x805766D0; // type:object size:0x8 data:4byte
typeInfo__Q44nw4r3snd6detail9StrmSound = .sbss:0x805766D0; // type:object size:0x8 data:4byte
@GUARD@GetInstance__Q44nw4r3snd6detail11TaskManagerFv@instance = .sbss:0x805766D8; // type:object size:0x8 data:byte
@GUARD@GetInstance__Q44nw4r3snd6detail12VoiceManagerFv@instance = .sbss:0x805766E0; // type:object size:0x1 data:byte
lbl_805766E8 = .sbss:0x805766E8; // type:object size:0x8 data:4byte
Expand Down
2 changes: 1 addition & 1 deletion configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ def nw4rLib(lib_name, objects, extra_cflags=[]):
Object(Matching, "nw4r/snd/snd_SoundStartable.cpp"),
Object(Matching, "nw4r/snd/snd_SoundSystem.cpp"),
Object(Matching, "nw4r/snd/snd_SoundThread.cpp"),
Object(NonMatching, "nw4r/snd/snd_StrmChannel.cpp"),
Object(Matching, "nw4r/snd/snd_StrmChannel.cpp"),
Object(NonMatching, "nw4r/snd/snd_StrmFile.cpp"),
Object(NonMatching, "nw4r/snd/snd_StrmPlayer.cpp"),
Object(NonMatching, "nw4r/snd/snd_StrmSound.cpp"),
Expand Down
12 changes: 6 additions & 6 deletions include/nw4r/snd/snd_BasicSound.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,14 @@ class BasicSound {
int mOutputLineFlag; // at 0xC8
f32 mMainOutVolume; // at 0xCC
f32 mMainSend; // at 0xD0
f32 mFxSend[AUX_BUS_NUM]; // at 0xF4
f32 mRemoteOutVolume[WPAD_MAX_CONTROLLERS]; // at 0xD4
f32 mFxSend[AUX_BUS_NUM]; // at 0xD4
f32 mRemoteOutVolume[WPAD_MAX_CONTROLLERS]; // at 0xF0

public:
NW4R_UT_LIST_NODE_DECL_EX(Prio); // at 0xB8
NW4R_UT_LIST_NODE_DECL_EX(PlayerPlay); // at 0xC0
NW4R_UT_LIST_NODE_DECL_EX(PlayerPrio); // at 0xC8
NW4R_UT_LIST_NODE_DECL_EX(ExtPlay); // at 0xD0
NW4R_UT_LIST_NODE_DECL_EX(Prio); // at 0x100
NW4R_UT_LIST_NODE_DECL_EX(PlayerPlay); // at 0x108
NW4R_UT_LIST_NODE_DECL_EX(PlayerPrio); // at 0x110
NW4R_UT_LIST_NODE_DECL_EX(ExtPlay); // at 0x118
};

NW4R_UT_LIST_TYPEDEF_DECL_EX(BasicSound, Prio);
Expand Down
41 changes: 20 additions & 21 deletions include/nw4r/snd/snd_StrmFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "nw4r/types_nw4r.h"
#include "nw4r/ut.h"


namespace nw4r {
namespace snd {
namespace detail {
Expand Down Expand Up @@ -70,30 +69,30 @@ struct HeadBlock {

} // namespace StrmFile

struct StrmInfo {
u8 format; // at 0x0
u8 loopFlag; // at 0x1
u8 numChannels; // at 0x2
int sampleRate; // at 0x4
u16 blockHeaderOffset; // at 0x8
u32 loopStart; // at 0xC
u32 loopEnd; // at 0x10
u32 dataOffset; // at 0x14
u32 numBlocks; // at 0x18
u32 blockSize; // at 0x1C
u32 blockSamples; // at 0x20
u32 lastBlockSize; // at 0x24
u32 lastBlockSamples; // at 0x28
u32 lastBlockPaddedSize; // at 0x2C
u32 adpcmDataInterval; // at 0x30
u32 adpcmDataSize; // at 0x34
};

class StrmFileReader {
public:
static const u32 SIGNATURE = 'RSTM';
static const int VERSION = NW4R_VERSION(1, 0);

struct StrmInfo {
u8 format; // at 0x0
u8 loopFlag; // at 0x1
u8 numChannels; // at 0x2
int sampleRate; // at 0x4
u16 blockHeaderOffset; // at 0x8
u32 loopStart; // at 0xC
u32 loopEnd; // at 0x10
u32 dataOffset; // at 0x14
u32 numBlocks; // at 0x18
u32 blockSize; // at 0x1C
u32 blockSamples; // at 0x20
u32 lastBlockSize; // at 0x24
u32 lastBlockSamples; // at 0x28
u32 lastBlockPaddedSize; // at 0x2C
u32 adpcmDataInterval; // at 0x30
u32 adpcmDataSize; // at 0x34
};

public:
StrmFileReader();

Expand Down Expand Up @@ -127,7 +126,7 @@ class StrmFileLoader {
bool LoadFileHeader(void *pStrmBin, u32 size);
bool ReadAdpcBlockData(u16 *pYN1, u16 *pYN2, int block, int channels);

bool ReadStrmInfo(StrmInfo *pStrmInfo) const {
bool ReadStrmInfo(StrmFileReader::StrmInfo *pStrmInfo) const {
if (!mReader.IsAvailable()) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions include/nw4r/snd/snd_StrmPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class StrmPlayer : public BasicPlayer, public SoundThread::PlayerCallback {
// TODO: Why 8 if the player only supports 2???
static const int STRM_CHANNEL_MAX = 8;

StrmInfo strmInfo; // at 0x0
StrmFileReader::StrmInfo strmInfo; // at 0x0
AdpcmInfo adpcmInfo[STRM_CHANNEL_MAX]; // at 0x38
u16 loopYn1[STRM_CHANNEL_MAX]; // at 0x1B8
u16 loopYn2[STRM_CHANNEL_MAX]; // at 0x1D8
Expand Down Expand Up @@ -151,7 +151,7 @@ class StrmPlayer : public BasicPlayer, public SoundThread::PlayerCallback {
void SetAdpcmLoopContext(int channels, u16 *pPredScale);

private:
StrmInfo mStrmInfo; // at 0x80
StrmFileReader::StrmInfo mStrmInfo; // at 0x80

bool mSetupFlag; // at 0xB8
bool mActiveFlag; // at 0xB9
Expand Down
1 change: 1 addition & 0 deletions include/nw4r/snd/snd_StrmSoundHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace snd {

class StrmSoundHandle : private ut::NonCopyable {
public:
StrmSoundHandle(SoundHandle *pHandle);
void DetachSound();

bool IsAttachedSound() {
Expand Down
86 changes: 86 additions & 0 deletions src/nw4r/snd/snd_StrmChannel.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,87 @@
#include "nw4r/snd/snd_StrmChannel.h"

#include "nw4r/ut.h"
#include "string.h"

namespace nw4r {
namespace snd {
namespace detail {

void StrmBufferPool::Setup(void *pBase, u32 size, int count) {
if (count == 0) {
return;
}

ut::AutoInterruptLock lock;

mBuffer = pBase;
mBufferSize = size;

mBlockSize = size / count;
mBlockCount = count;

mAllocCount = 0;
memset(&mAllocFlags, 0, sizeof(mAllocFlags));
}

void StrmBufferPool::Shutdown() {
ut::AutoInterruptLock lock;

mBuffer = NULL;
mBufferSize = 0;

mBlockSize = 0;
mBlockCount = 0;
}

void *StrmBufferPool::Alloc() {
ut::AutoInterruptLock lock;

if (mAllocCount >= mBlockCount) {
return NULL;
}

int usableFlags = ut::RoundUp(mBlockCount, BITS_PER_BYTE) / BITS_PER_BYTE;

for (int i = 0; i < usableFlags; i++) {
u8 flag = static_cast<u8>(mAllocFlags[i]);

// All blocks allocated in this flag set
if (flag == 0xFF) {
continue;
}

u8 mask = 1 << 0;

for (int j = 0; j < 8; j++, mask <<= 1) {
// Block represented by this bit is in use
if (flag & mask) {
continue;
}

mAllocFlags[i] |= mask;
mAllocCount++;

return ut::AddOffsetToPtr(mBuffer, mBlockSize * (j + i * BITS_PER_BYTE));
}
}

return NULL;
}

void StrmBufferPool::Free(void *pBuffer) {
ut::AutoInterruptLock lock;

s32 offset = ut::GetOffsetFromPtr(mBuffer, pBuffer);
u32 block = offset / mBlockSize;

u32 byte = block / BITS_PER_BYTE;
u32 bit = block % BITS_PER_BYTE;

mAllocFlags[byte] &= ~(1 << bit);
mAllocCount--;
}

} // namespace detail
} // namespace snd
} // namespace nw4r
149 changes: 148 additions & 1 deletion src/nw4r/snd/snd_StrmFile.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,148 @@
// #include "nw4r/snd/snd_StrmFile.h"
#include "nw4r/snd/snd_StrmFile.h"

#include "nw4r/snd/snd_StrmPlayer.h"
#include "nw4r/snd/snd_WaveFile.h"

namespace nw4r {
namespace snd {
namespace detail {

bool StrmFileReader::IsValidFileHeader(const void *pStrmBin) {
const ut::BinaryFileHeader *pFileHeader = static_cast<const ut::BinaryFileHeader *>(pStrmBin);

if (pFileHeader->magic != SIGNATURE) {
return false;
}

if (pFileHeader->version < NW4R_VERSION(1, 0)) {
return false;
}

if (pFileHeader->version > VERSION) {
return false;
}

return true;
}

StrmFileReader::StrmFileReader() : mHeader(NULL), mHeadBlock(NULL) {}

void StrmFileReader::Setup(const void *pStrmBin) {
if (!IsValidFileHeader(pStrmBin)) {
return;
}

mHeader = static_cast<const StrmFile::Header *>(pStrmBin);

mHeadBlock = static_cast<const StrmFile::HeadBlock *>(ut::AddOffsetToPtr(mHeader, mHeader->headBlockOffset));

(void)Util::GetDataRefAddress0(mHeadBlock->refDataHeader,
&mHeadBlock->refDataHeader); // debug leftover
}

bool StrmFileReader::ReadStrmInfo(StrmInfo *pStrmInfo) const {
const StrmFile::StrmDataInfo *pStrmData =
Util::GetDataRefAddress0(mHeadBlock->refDataHeader, &mHeadBlock->refDataHeader);

pStrmInfo->format = pStrmData->format;
pStrmInfo->loopFlag = pStrmData->loopFlag;
pStrmInfo->numChannels = pStrmData->numChannels;
pStrmInfo->sampleRate = (pStrmData->sampleRate24 << 16) + pStrmData->sampleRate;
pStrmInfo->blockHeaderOffset = pStrmData->blockHeaderOffset;
pStrmInfo->loopStart = pStrmData->loopStart;
pStrmInfo->loopEnd = pStrmData->loopEnd;
pStrmInfo->dataOffset = pStrmData->dataOffset;
pStrmInfo->numBlocks = pStrmData->numBlocks;
pStrmInfo->blockSize = pStrmData->blockSize;
pStrmInfo->blockSamples = pStrmData->blockSamples;
pStrmInfo->lastBlockSize = pStrmData->lastBlockSize;
pStrmInfo->lastBlockSamples = pStrmData->lastBlockSamples;
pStrmInfo->lastBlockPaddedSize = pStrmData->lastBlockPaddedSize;
pStrmInfo->adpcmDataInterval = pStrmData->adpcmDataInterval;
pStrmInfo->adpcmDataSize = pStrmData->adpcmDataSize;

return true;
}

bool StrmFileReader::ReadAdpcmInfo(AdpcmInfo *pAdpcmInfo, int channels) const {
const StrmFile::StrmDataInfo *pStrmData =
Util::GetDataRefAddress0(mHeadBlock->refDataHeader, &mHeadBlock->refDataHeader);

if (pStrmData->format != WaveFile::FORMAT_ADPCM) {
return false;
}

const StrmFile::ChannelTable *pChannelTable =
Util::GetDataRefAddress0(mHeadBlock->refChannelTable, &mHeadBlock->refDataHeader);

if (channels >= pChannelTable->channelCount) {
return false;
}

const StrmFile::ChannelInfo *pChannelInfo =
Util::GetDataRefAddress0(pChannelTable->refChannelHeader[channels], &mHeadBlock->refDataHeader);

const AdpcmInfo *pSrcInfo = Util::GetDataRefAddress0(pChannelInfo->refAdpcmInfo, &mHeadBlock->refDataHeader);

*pAdpcmInfo = *pSrcInfo;
return true;
}

bool StrmFileLoader::LoadFileHeader(void *pStrmBin, u32 size) {
u8 headerArea[HEADER_ALIGNED_SIZE + 32];
u32 bytesRead;

mStream.Seek(0, ut::FileStream::SEEKORG_BEG);
bytesRead = mStream.Read(ut::RoundUp(headerArea, 32), HEADER_ALIGNED_SIZE);
if (bytesRead != HEADER_ALIGNED_SIZE) {
return false;
}

StrmFile::Header *pHeader = static_cast<StrmFile::Header *>(ut::RoundUp(headerArea, 32));

StrmFileReader reader;
if (!reader.IsValidFileHeader(pHeader)) {
return false;
}

if (pHeader->adpcBlockOffset > size) {
return false;
}

u32 loadSize = pHeader->headBlockOffset + pHeader->headBlockSize;

mStream.Seek(0, ut::FileStream::SEEKORG_BEG);
bytesRead = mStream.Read(pStrmBin, loadSize);
if (bytesRead != loadSize) {
return false;
}

mReader.Setup(pStrmBin);
return true;
}

bool StrmFileLoader::ReadAdpcBlockData(u16 *pYN1, u16 *pYN2, int block, int channels) {
if (!mReader.IsAvailable()) {
return false;
}

s32 offset = mReader.GetAdpcBlockOffset() + block * channels * (2 * sizeof(u16)) + sizeof(ut::BinaryBlockHeader);

mStream.Seek(offset, ut::FileStream::SEEKORG_BEG);

u16 buffer[StrmPlayer::StrmHeader::STRM_CHANNEL_MAX * 2] ALIGN_DECL(32);
if (sizeof(buffer) != mStream.Read(buffer, sizeof(buffer))) {
return false;
}

for (int i = 0; i < channels; i++) {
pYN1[i] = buffer[i * 2];
pYN2[i] = buffer[i * 2 + 1];
}

return true;
}

} // namespace detail
} // namespace snd
} // namespace nw4r
Loading

0 comments on commit a6a9609

Please sign in to comment.