ctr_Repair/branches/0thNUP_No_NUP/ConsoleDataMigration/common/wave.cpp
N2614 e3eb8c9f31 NUPを実行しない0th NUP版のためのブランチ
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-05-23%20-%20ctr.7z%20+%20svn_v1.068.zip/ctr/svn/ctr_Repair@305 385bec56-5757-e545-9c3a-d8741f4650f1
2011-06-02 06:58:24 +00:00

151 lines
4.9 KiB
C++

/*---------------------------------------------------------------------------*
Project: Horizon
File: wave.c
Copyright (C)2009 Nintendo Co., Ltd. All rights reserved.
These coded instructions, statements, and computer programs contain
proprietary information of Nintendo of America Inc. and/or Nintendo
Company Ltd., and are protected by Federal copyright law. They may
not be disclosed to third parties or copied or duplicated in any form,
in whole or in part, without the prior written consent of Nintendo.
$Rev$
*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* includes */
#include "wave.h"
/*---------------------------------------------------------------------------*/
/* functions */
/*---------------------------------------------------------------------------*
Name: LoadWave
Description: Wave データを読み込む.
Arguments: filepath : ファイルパス
fmt : Wave データの fmt チャンクを格納
data : Wave データの data チャンクを格納
Returns: None.
*---------------------------------------------------------------------------*/
void LoadWaveCore(nn::fs::FileReader& reader, WaveFmt * fmt, WaveData * data, bool isSizeOnly)
{
reader.SetPosition(0);
WaveRiff riff; // RIFF ヘッダ
WaveChunk chunk; // CHUNK
u32 readbyte = 0; // ファイル読み込みの判定に使用
u32 fsize = reader.GetSize();
// RIFF ヘッダ読み込み
reader.Read((u8*)&riff, sizeof(WaveRiff));
// サイズを積算
readbyte += sizeof(WaveRiff);
// "RIFF" との一致を確認
if (riff.tag != MAKE_WAVE_TAG_VALUE('R','I','F','F'))
{
NN_PANIC("Not RIFF format.\n");
}
// "WAVE" との一致を確認
if ( riff.type != MAKE_WAVE_TAG_VALUE('W','A','V','E') )
{
NN_PANIC("Not Wave format.\n");
}
// ファイルサイズより大きくなるまで読み込む
while(fsize > readbyte)
{
// CHUNK の読み込み
reader.Read((u8*)&chunk, sizeof(WaveChunk));
readbyte += sizeof(WaveChunk);
// "fmt "
if (chunk.tag == MAKE_WAVE_TAG_VALUE('f','m','t',' '))
{
reader.Read((u8*)fmt, sizeof(WaveFmt));
readbyte += sizeof(WaveFmt);
// 拡張領域があれば読み捨て
if (chunk.size > sizeof(WaveFmt))
{
u16 ext_size;
reader.Read((u8*)&ext_size, sizeof(ext_size));
readbyte += sizeof(ext_size);
readbyte += chunk.size;
if (readbyte == fsize) break;
reader.Seek(ext_size, nn::fs::POSITION_BASE_CURRENT);
}
}
// "data"
else if (chunk.tag == MAKE_WAVE_TAG_VALUE('d','a','t','a'))
{
data->size = chunk.size;
if (isSizeOnly)
{
return;
}
else
{
reader.Read((u8*)data->buf, data->size);
// 8-bit wav ファイルの場合は unsigned -> signed の変換が必要
if (fmt->quantum_bits == 8)
{
s8* p = (s8*)data->buf;
for (int i = 0; i < data->size; i++)
{
p[i] -= 128;
}
}
}
readbyte += chunk.size;
}
// それ以外
else
{
readbyte += chunk.size;
if (readbyte == fsize) break;
// 読み飛ばす
reader.Seek(chunk.size, nn::fs::POSITION_BASE_CURRENT);
}
}
}
#ifndef NN_BUILD_RELEASE // Release ビルドでは HostIO は使えません。
void WriteWaveHeader(nn::hio::CTR::HostFile& hostFile, WaveFmt* fmt, s32 size)
{
WaveRiff riff;
riff.tag = MAKE_WAVE_TAG_VALUE('R','I','F','F');
riff.size =
+ sizeof(WaveRiff) // riff header
+ sizeof(WaveChunk) + sizeof(WaveFmt) // wave fmt header
+ sizeof(WaveChunk) + size // wave data
- 8;
riff.type = MAKE_WAVE_TAG_VALUE('W','A','V','E');
hostFile.Write(&riff, sizeof(WaveRiff));
WaveChunk chunk;
chunk.tag = MAKE_WAVE_TAG_VALUE('f','m','t',' ');
chunk.size = sizeof(WaveFmt);
hostFile.Write(&chunk, sizeof(WaveChunk));
hostFile.Write(fmt, sizeof(WaveFmt));
chunk.tag = MAKE_WAVE_TAG_VALUE('d','a','t','a');
chunk.size = size;
hostFile.Write(&chunk, sizeof(WaveChunk));
}
#endif // NN_BUILD_RELEASE
/*---------------------------------------------------------------------------*/
/* end of file */
/*---------------------------------------------------------------------------*/