-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54ec310
commit a6a9609
Showing
11 changed files
with
1,233 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.