2012-01-21 21:57:41 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2010
|
|
|
|
* by Dimok
|
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any
|
|
|
|
* damages arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any
|
|
|
|
* purpose, including commercial applications, and to alter it and
|
|
|
|
* redistribute it freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you
|
|
|
|
* must not claim that you wrote the original software. If you use
|
|
|
|
* this software in a product, an acknowledgment in the product
|
|
|
|
* documentation would be appreciated but is not required.
|
|
|
|
*
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and
|
|
|
|
* must not be misrepresented as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* for WiiXplorer 2010
|
|
|
|
***************************************************************************/
|
|
|
|
#include <string.h>
|
|
|
|
#include "WavDecoder.hpp"
|
|
|
|
|
|
|
|
WavDecoder::WavDecoder(const char * filepath)
|
2012-07-26 00:12:17 +02:00
|
|
|
: SoundDecoder(filepath)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-07-26 00:12:17 +02:00
|
|
|
SoundType = SOUND_WAV;
|
|
|
|
SampleRate = 48000;
|
|
|
|
Format = VOICE_STEREO_16BIT;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
if(!file_fd)
|
|
|
|
return;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
OpenFile();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WavDecoder::WavDecoder(const u8 * snd, int len)
|
2012-07-26 00:12:17 +02:00
|
|
|
: SoundDecoder(snd, len)
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
2012-07-26 00:12:17 +02:00
|
|
|
SoundType = SOUND_WAV;
|
|
|
|
SampleRate = 48000;
|
|
|
|
Format = VOICE_STEREO_16BIT;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
if(!file_fd)
|
|
|
|
return;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
OpenFile();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WavDecoder::~WavDecoder()
|
|
|
|
{
|
2012-07-26 00:12:17 +02:00
|
|
|
CloseFile();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WavDecoder::OpenFile()
|
|
|
|
{
|
2012-08-11 17:43:19 +02:00
|
|
|
DataOffset = 0;
|
2012-07-26 00:12:17 +02:00
|
|
|
SWaveHdr Header;
|
|
|
|
SWaveFmtChunk FmtChunk;
|
|
|
|
memset(&Header, 0, sizeof(SWaveHdr));
|
|
|
|
memset(&FmtChunk, 0, sizeof(SWaveFmtChunk));
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
file_fd->read((u8 *) &Header, sizeof(SWaveHdr));
|
|
|
|
file_fd->read((u8 *) &FmtChunk, sizeof(SWaveFmtChunk));
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
if(Header.magicRIFF != 'RIFF')
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
CloseFile();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(Header.magicWAVE != 'WAVE')
|
|
|
|
{
|
|
|
|
CloseFile();
|
|
|
|
return;
|
|
|
|
}
|
2012-08-11 17:43:19 +02:00
|
|
|
if(FmtChunk.magicFMT == 'bext') //Stupid metadata
|
|
|
|
{
|
|
|
|
DataOffset += le32(FmtChunk.size) + 8;
|
|
|
|
file_fd->seek(sizeof(SWaveHdr) + le32(FmtChunk.size) + 8, SEEK_SET);
|
|
|
|
file_fd->read((u8 *)&FmtChunk, sizeof(SWaveFmtChunk));
|
|
|
|
}
|
|
|
|
if(FmtChunk.magicFMT != 'fmt ')
|
2012-01-21 21:57:41 +01:00
|
|
|
{
|
|
|
|
CloseFile();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-11 17:43:19 +02:00
|
|
|
DataOffset += sizeof(SWaveHdr) + le32(FmtChunk.size) + 8;
|
2012-07-26 00:12:17 +02:00
|
|
|
file_fd->seek(DataOffset, SEEK_SET);
|
|
|
|
SWaveChunk DataChunk;
|
|
|
|
file_fd->read((u8 *) &DataChunk, sizeof(SWaveChunk));
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-05-03 01:08:11 +02:00
|
|
|
while(DataChunk.magicDATA != 'data')
|
|
|
|
{
|
|
|
|
DataOffset += 8+le32(DataChunk.size);
|
|
|
|
file_fd->seek(DataOffset, SEEK_SET);
|
|
|
|
int ret = file_fd->read((u8 *) &DataChunk, sizeof(SWaveChunk));
|
|
|
|
if(ret <= 0)
|
|
|
|
{
|
|
|
|
CloseFile();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
DataOffset += 8;
|
|
|
|
DataSize = le32(DataChunk.size);
|
|
|
|
Is16Bit = (le16(FmtChunk.bps) == 16);
|
|
|
|
SampleRate = le32(FmtChunk.freq);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
if(le16(FmtChunk.channels) == 1 && le16(FmtChunk.bps) == 8 && le16(FmtChunk.alignment) <= 1)
|
2012-01-21 21:57:41 +01:00
|
|
|
Format = VOICE_MONO_8BIT;
|
2012-07-26 00:12:17 +02:00
|
|
|
else if(le16(FmtChunk.channels) == 1 && le16(FmtChunk.bps) == 16 && le16(FmtChunk.alignment) <= 2)
|
2012-01-21 21:57:41 +01:00
|
|
|
Format = VOICE_MONO_16BIT;
|
2012-07-26 00:12:17 +02:00
|
|
|
else if(le16(FmtChunk.channels) == 2 && le16(FmtChunk.bps) == 8 && le16(FmtChunk.alignment) <= 2)
|
2012-01-21 21:57:41 +01:00
|
|
|
Format = VOICE_STEREO_8BIT;
|
2012-07-26 00:12:17 +02:00
|
|
|
else if(le16(FmtChunk.channels) == 2 && le16(FmtChunk.bps) == 16 && le16(FmtChunk.alignment) <= 4)
|
2012-01-21 21:57:41 +01:00
|
|
|
Format = VOICE_STEREO_16BIT;
|
|
|
|
|
2012-08-11 21:47:13 +02:00
|
|
|
SWaveChunk LoopChunk;
|
|
|
|
SWaveSmplChunk SmplChunk;
|
|
|
|
SmplChunk.Start = 0;
|
|
|
|
file_fd->seek(DataOffset + DataSize, SEEK_SET);
|
|
|
|
while(file_fd->read((u8 *)&LoopChunk, sizeof(SWaveChunk)) == sizeof(SWaveChunk))
|
|
|
|
{
|
|
|
|
if(LoopChunk.magicDATA == 'smpl')
|
|
|
|
{
|
|
|
|
file_fd->seek(-8, SEEK_CUR);
|
|
|
|
file_fd->read((u8*)&SmplChunk, sizeof(SWaveSmplChunk));
|
2012-08-16 19:18:15 +02:00
|
|
|
SmplChunk.Start = ((le32(SmplChunk.Start) * le16(FmtChunk.channels) * le16(FmtChunk.bps) / 8) + 8191) & ~8191;
|
2012-08-11 21:47:13 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
file_fd->seek(le32(LoopChunk.size), SEEK_CUR);
|
|
|
|
}
|
|
|
|
SetLoopStart(SmplChunk.Start);
|
2012-07-26 00:12:17 +02:00
|
|
|
Decode();
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void WavDecoder::CloseFile()
|
|
|
|
{
|
2012-07-26 00:12:17 +02:00
|
|
|
if(file_fd)
|
|
|
|
delete file_fd;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
file_fd = NULL;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int WavDecoder::Read(u8 * buffer, int buffer_size, int)
|
|
|
|
{
|
2012-07-26 00:12:17 +02:00
|
|
|
if(!file_fd)
|
|
|
|
return -1;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
if(CurPos >= (int) DataSize)
|
|
|
|
return 0;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
file_fd->seek(DataOffset+CurPos, SEEK_SET);
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
if(buffer_size > (int) DataSize-CurPos)
|
|
|
|
buffer_size = DataSize-CurPos;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
int read = file_fd->read(buffer, buffer_size);
|
|
|
|
if(read > 0)
|
|
|
|
{
|
|
|
|
if(Is16Bit)
|
|
|
|
{
|
|
|
|
read &= ~0x0001;
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
for(u32 i = 0; i < (u32) (read / sizeof (u16)); ++i)
|
|
|
|
((u16 *)buffer)[i] = le16(((u16 *)buffer)[i]);
|
|
|
|
}
|
|
|
|
CurPos += read;
|
|
|
|
}
|
2012-01-21 21:57:41 +01:00
|
|
|
|
2012-07-26 00:12:17 +02:00
|
|
|
return read;
|
2012-01-21 21:57:41 +01:00
|
|
|
}
|