libgui/source/sounds/SoundDecoder.cpp

218 lines
6.0 KiB
C++
Raw Permalink Normal View History

2017-10-29 10:28:14 +01:00
/****************************************************************************
* Copyright (C) 2009-2013 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <malloc.h>
#include <string.h>
#include <unistd.h>
2018-06-21 20:44:58 +02:00
#include <coreinit/time.h>
#include <coreinit/thread.h>
#include <coreinit/cache.h>
2019-08-14 23:24:55 +02:00
#include <gui/sounds/SoundDecoder.hpp>
#include "fs/CFile.hpp"
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
static const uint32_t FixedPointShift = 15;
static const uint32_t FixedPointScale = 1 << FixedPointShift;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
SoundDecoder::SoundDecoder() {
file_fd = NULL;
Init();
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
SoundDecoder::SoundDecoder(const std::string &filepath) {
2018-06-21 20:44:58 +02:00
file_fd = new CFile(filepath, CFile::ReadOnly);
Init();
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
SoundDecoder::SoundDecoder(const uint8_t *buffer, int32_t size) {
2018-06-21 20:44:58 +02:00
file_fd = new CFile(buffer, size);
Init();
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
SoundDecoder::~SoundDecoder() {
ExitRequested = true;
2020-08-13 12:38:07 +02:00
while (Decoding)
2018-06-21 20:44:58 +02:00
OSSleepTicks(OSMicrosecondsToTicks(1000));
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
//! lock unlock once to make sure it's really not decoding
Lock();
Unlock();
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
delete file_fd;
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
file_fd = NULL;
2017-10-29 10:28:14 +01:00
2020-08-13 12:58:19 +02:00
if (ResampleBuffer) {
2018-06-21 20:44:58 +02:00
free(ResampleBuffer);
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
}
2019-08-14 23:24:55 +02:00
int32_t SoundDecoder::Seek(int32_t pos) {
2020-08-13 12:38:07 +02:00
CurPos = pos;
return file_fd->seek(CurPos, SEEK_SET);
2019-08-14 23:24:55 +02:00
}
2018-06-21 20:44:58 +02:00
void SoundDecoder::Init() {
SoundType = SOUND_RAW;
SoundBlocks = 8;
SoundBlockSize = 0x4000;
ResampleTo48kHz = false;
CurPos = 0;
whichLoad = 0;
Loop = false;
EndOfFile = false;
Decoding = false;
ExitRequested = false;
SoundBuffer.SetBufferBlockSize(SoundBlockSize);
SoundBuffer.Resize(SoundBlocks);
ResampleBuffer = NULL;
ResampleRatio = 0;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
int32_t SoundDecoder::Rewind() {
CurPos = 0;
EndOfFile = false;
file_fd->rewind();
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
return 0;
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
int32_t SoundDecoder::Read(uint8_t *buffer, int32_t buffer_size, int32_t pos) {
2018-06-21 20:44:58 +02:00
int32_t ret = file_fd->read(buffer, buffer_size);
CurPos += ret;
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
return ret;
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void SoundDecoder::EnableUpsample(void) {
2020-08-13 12:38:07 +02:00
if ((ResampleBuffer == NULL)
&& IsStereo() && Is16Bit()
&& SampleRate != 32000
&& SampleRate != 48000) {
ResampleBuffer = (uint8_t *) memalign(32, SoundBlockSize);
ResampleRatio = (FixedPointScale * SampleRate) / 48000;
SoundBlockSize = (SoundBlockSize * ResampleRatio) / FixedPointScale;
2018-06-21 20:44:58 +02:00
SoundBlockSize &= ~0x03;
// set new sample rate
SampleRate = 48000;
}
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void SoundDecoder::Upsample(int16_t *src, int16_t *dst, uint32_t nr_src_samples, uint32_t nr_dst_samples) {
int32_t timer = 0;
2020-08-13 12:38:07 +02:00
for (uint32_t i = 0, n = 0; i < nr_dst_samples; i += 2) {
if ((n + 3) < nr_src_samples) {
2018-06-21 20:44:58 +02:00
// simple fixed point linear interpolation
2020-08-13 12:38:07 +02:00
dst[i] = src[n] + (((src[n + 2] - src[n]) * timer) >> FixedPointShift);
dst[i + 1] = src[n + 1] + (((src[n + 3] - src[n + 1]) * timer) >> FixedPointShift);
2018-06-21 20:44:58 +02:00
} else {
2020-08-13 12:38:07 +02:00
dst[i] = src[n];
dst[i + 1] = src[n + 1];
2018-06-21 20:44:58 +02:00
}
timer += ResampleRatio;
2020-08-13 12:38:07 +02:00
if (timer >= (int32_t) FixedPointScale) {
2018-06-21 20:44:58 +02:00
n += 2;
timer -= FixedPointScale;
}
}
2017-10-29 10:28:14 +01:00
}
2018-06-21 20:44:58 +02:00
void SoundDecoder::Decode() {
2020-08-13 12:58:19 +02:00
if (!file_fd || ExitRequested || EndOfFile) {
2018-06-21 20:44:58 +02:00
return;
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
// check if we are not at the pre-last buffer (last buffer is playing)
uint16_t whichPlaying = SoundBuffer.Which();
2020-08-13 12:38:07 +02:00
if (((whichPlaying == 0) && (whichLoad == SoundBuffer.Size() - 2))
|| ((whichPlaying == 1) && (whichLoad == SoundBuffer.Size() - 1))
|| (whichLoad == (whichPlaying - 2))) {
2018-06-21 20:44:58 +02:00
return;
}
Decoding = true;
2020-08-13 12:38:07 +02:00
int32_t done = 0;
uint8_t *write_buf = SoundBuffer.GetBuffer(whichLoad);
if (!write_buf) {
2018-06-21 20:44:58 +02:00
ExitRequested = true;
Decoding = false;
return;
}
2020-08-13 12:58:19 +02:00
if (ResampleTo48kHz && !ResampleBuffer) {
2018-06-21 20:44:58 +02:00
EnableUpsample();
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
while (done < SoundBlockSize) {
int32_t ret = Read(&write_buf[done], SoundBlockSize - done, Tell());
2018-06-21 20:44:58 +02:00
2020-08-13 12:38:07 +02:00
if (ret <= 0) {
if (Loop) {
2018-06-21 20:44:58 +02:00
Rewind();
continue;
} else {
EndOfFile = true;
break;
}
}
done += ret;
}
2020-08-13 12:38:07 +02:00
if (done > 0) {
2018-06-21 20:44:58 +02:00
// check if we need to resample
2020-08-13 12:38:07 +02:00
if (ResampleBuffer && ResampleRatio) {
2018-06-21 20:44:58 +02:00
memcpy(ResampleBuffer, write_buf, done);
int32_t src_samples = done >> 1;
2020-08-13 12:38:07 +02:00
int32_t dest_samples = (src_samples * FixedPointScale) / ResampleRatio;
2018-06-21 20:44:58 +02:00
dest_samples &= ~0x01;
2020-08-13 12:38:07 +02:00
Upsample((int16_t *) ResampleBuffer, (int16_t *) write_buf, src_samples, dest_samples);
2018-06-21 20:44:58 +02:00
done = dest_samples << 1;
}
//! TODO: remove this later and add STEREO support with two voices, for now we convert to MONO
2020-08-13 12:38:07 +02:00
if (IsStereo()) {
int16_t *monoBuf = (int16_t *) write_buf;
2018-06-21 20:44:58 +02:00
done = done >> 1;
2020-08-13 12:58:19 +02:00
for (int32_t i = 0; i < done; i++) {
2017-10-29 10:28:14 +01:00
monoBuf[i] = monoBuf[i << 1];
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
}
2017-10-29 10:28:14 +01:00
DCFlushRange(write_buf, done);
2018-06-21 20:44:58 +02:00
SoundBuffer.SetBufferSize(whichLoad, done);
SoundBuffer.SetBufferReady(whichLoad, true);
2020-08-13 12:58:19 +02:00
if (++whichLoad >= SoundBuffer.Size()) {
2018-06-21 20:44:58 +02:00
whichLoad = 0;
2020-08-13 12:58:19 +02:00
}
2018-06-21 20:44:58 +02:00
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
// check if next in queue needs to be filled as well and do so
2020-08-13 12:58:19 +02:00
if (!SoundBuffer.IsBufferReady(whichLoad)) {
2018-06-21 20:44:58 +02:00
Decode();
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
Decoding = false;
2017-10-29 10:28:14 +01:00
}