libgui/source/sounds/WavDecoder.cpp

149 lines
4.3 KiB
C++
Raw Normal View History

2017-10-29 10:28:14 +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>
2019-08-14 23:24:55 +02:00
#include <gui/sounds/WavDecoder.hpp>
#include "fs/CFile.hpp"
#include "utils/utils.h"
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
WavDecoder::WavDecoder(const char *filepath)
: SoundDecoder(filepath) {
2018-06-21 20:44:58 +02:00
SoundType = SOUND_WAV;
SampleRate = 48000;
Format = CHANNELS_STEREO | FORMAT_PCM_16_BIT;
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (!file_fd) {
2018-06-21 20:44:58 +02:00
return;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
OpenFile();
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
WavDecoder::WavDecoder(const uint8_t *snd, int32_t len)
: SoundDecoder(snd, len) {
2018-06-21 20:44:58 +02:00
SoundType = SOUND_WAV;
SampleRate = 48000;
Format = CHANNELS_STEREO | FORMAT_PCM_16_BIT;
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (!file_fd) {
2018-06-21 20:44:58 +02:00
return;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
OpenFile();
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
WavDecoder::~WavDecoder() {
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void WavDecoder::OpenFile() {
SWaveHdr Header;
SWaveFmtChunk FmtChunk;
memset(&Header, 0, sizeof(SWaveHdr));
memset(&FmtChunk, 0, sizeof(SWaveFmtChunk));
file_fd->read((uint8_t *) &Header, sizeof(SWaveHdr));
file_fd->read((uint8_t *) &FmtChunk, sizeof(SWaveFmtChunk));
if (Header.magicRIFF != 0x52494646) { // 'RIFF'
CloseFile();
return;
2020-08-13 12:38:07 +02:00
} else if (Header.magicWAVE != 0x57415645) { // 'WAVE'
2018-06-21 20:44:58 +02:00
CloseFile();
return;
2020-08-13 12:38:07 +02:00
} else if (FmtChunk.magicFMT != 0x666d7420) { // 'fmt '
2018-06-21 20:44:58 +02:00
CloseFile();
return;
}
2020-08-13 12:38:07 +02:00
DataOffset = sizeof(SWaveHdr) + le32(FmtChunk.size) + 8;
2018-06-21 20:44:58 +02:00
file_fd->seek(DataOffset, SEEK_SET);
SWaveChunk DataChunk;
file_fd->read((uint8_t *) &DataChunk, sizeof(SWaveChunk));
2020-08-13 12:38:07 +02:00
while (DataChunk.magicDATA != 0x64617461) { // 'data'
DataOffset += 8 + le32(DataChunk.size);
2018-06-21 20:44:58 +02:00
file_fd->seek(DataOffset, SEEK_SET);
int32_t ret = file_fd->read((uint8_t *) &DataChunk, sizeof(SWaveChunk));
2020-08-13 12:38:07 +02:00
if (ret <= 0) {
2018-06-21 20:44:58 +02:00
CloseFile();
return;
}
}
DataOffset += 8;
DataSize = le32(DataChunk.size);
Is16Bit = (le16(FmtChunk.bps) == 16);
SampleRate = le32(FmtChunk.freq);
2020-08-13 12:58:19 +02:00
if (le16(FmtChunk.channels) == 1 && le16(FmtChunk.bps) == 8 && le16(FmtChunk.alignment) <= 1) {
2018-06-21 20:44:58 +02:00
Format = CHANNELS_MONO | FORMAT_PCM_8_BIT;
2020-08-13 12:58:19 +02:00
} else if (le16(FmtChunk.channels) == 1 && le16(FmtChunk.bps) == 16 && le16(FmtChunk.alignment) <= 2) {
2018-06-21 20:44:58 +02:00
Format = CHANNELS_MONO | FORMAT_PCM_16_BIT;
2020-08-13 12:58:19 +02:00
} else if (le16(FmtChunk.channels) == 2 && le16(FmtChunk.bps) == 8 && le16(FmtChunk.alignment) <= 2) {
2018-06-21 20:44:58 +02:00
Format = CHANNELS_STEREO | FORMAT_PCM_8_BIT;
2020-08-13 12:58:19 +02:00
} else if (le16(FmtChunk.channels) == 2 && le16(FmtChunk.bps) == 16 && le16(FmtChunk.alignment) <= 4) {
2018-06-21 20:44:58 +02:00
Format = CHANNELS_STEREO | FORMAT_PCM_16_BIT;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void WavDecoder::CloseFile() {
2020-08-13 12:58:19 +02:00
if (file_fd) {
2018-06-21 20:44:58 +02:00
delete file_fd;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
file_fd = NULL;
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
int32_t WavDecoder::Read(uint8_t *buffer, int32_t buffer_size, int32_t pos) {
2020-08-13 12:58:19 +02:00
if (!file_fd) {
2018-06-21 20:44:58 +02:00
return -1;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (CurPos >= (int32_t) DataSize) {
2018-06-21 20:44:58 +02:00
return 0;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
file_fd->seek(DataOffset + CurPos, SEEK_SET);
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (buffer_size > (int32_t) DataSize - CurPos) {
2020-08-13 12:38:07 +02:00
buffer_size = DataSize - CurPos;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
int32_t read = file_fd->read(buffer, buffer_size);
2020-08-13 12:38:07 +02:00
if (read > 0) {
2018-06-21 20:44:58 +02:00
if (Is16Bit) {
read &= ~0x0001;
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
for (uint32_t i = 0; i < (uint32_t) (read / sizeof(uint16_t)); ++i) {
2018-06-21 20:44:58 +02:00
((uint16_t *) buffer)[i] = le16(((uint16_t *) buffer)[i]);
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
}
CurPos += read;
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
return read;
2017-10-29 10:28:14 +01:00
}