2014-06-22 15:02:04 +00:00
|
|
|
#include <string>
|
2017-02-27 20:57:46 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
2024-04-09 15:29:57 +00:00
|
|
|
#include "ext/minimp3/minimp3_ex.h"
|
|
|
|
|
2020-10-03 22:25:21 +00:00
|
|
|
#include "Common/File/VFS/VFS.h"
|
2020-10-04 18:48:47 +00:00
|
|
|
#include "Common/UI/Root.h"
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2023-07-16 10:15:44 +00:00
|
|
|
#include "Common/Data/Text/I18n.h"
|
2020-08-15 18:53:08 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2020-10-03 22:25:21 +00:00
|
|
|
#include "Common/Data/Format/RIFF.h"
|
2020-08-15 10:25:39 +00:00
|
|
|
#include "Common/Log.h"
|
2023-03-24 16:19:57 +00:00
|
|
|
#include "Common/System/System.h"
|
2023-07-16 10:15:44 +00:00
|
|
|
#include "Common/System/OSD.h"
|
2020-08-10 07:12:51 +00:00
|
|
|
#include "Common/Serialize/SerializeFuncs.h"
|
2020-08-15 18:53:08 +00:00
|
|
|
#include "Common/TimeUtil.h"
|
2020-10-05 18:58:33 +00:00
|
|
|
#include "Common/Data/Collections/FixedSizeQueue.h"
|
2014-06-22 15:02:04 +00:00
|
|
|
#include "Core/HW/SimpleAudioDec.h"
|
2015-01-29 13:58:03 +00:00
|
|
|
#include "Core/HLE/__sceAudio.h"
|
2023-03-24 11:00:36 +00:00
|
|
|
#include "Core/System.h"
|
2015-02-23 08:07:34 +00:00
|
|
|
#include "Core/Config.h"
|
2023-07-16 10:15:44 +00:00
|
|
|
#include "UI/GameInfoCache.h"
|
2020-08-02 17:44:48 +00:00
|
|
|
#include "UI/BackgroundAudio.h"
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2020-08-02 19:55:46 +00:00
|
|
|
struct WavData {
|
2020-08-03 09:29:54 +00:00
|
|
|
int num_channels = -1;
|
|
|
|
int sample_rate = -1;
|
|
|
|
int numFrames = -1;
|
|
|
|
int samplesPerSec = -1;
|
|
|
|
int avgBytesPerSec = -1;
|
|
|
|
int raw_offset_loop_start = 0;
|
|
|
|
int raw_offset_loop_end = 0;
|
|
|
|
int loop_start_offset = 0;
|
|
|
|
int loop_end_offset = 0;
|
2020-08-02 19:55:46 +00:00
|
|
|
int codec = 0;
|
2020-08-03 09:29:54 +00:00
|
|
|
int raw_bytes_per_frame = 0;
|
|
|
|
uint8_t *raw_data = nullptr;
|
|
|
|
int raw_data_size = 0;
|
2024-10-10 09:55:07 +00:00
|
|
|
u8 at3_extradata[16]{};
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2024-04-09 15:29:57 +00:00
|
|
|
bool Read(RIFFReader &riff);
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2020-08-02 19:55:46 +00:00
|
|
|
~WavData() {
|
2020-08-03 09:29:54 +00:00
|
|
|
free(raw_data);
|
|
|
|
raw_data = nullptr;
|
2020-08-02 19:55:46 +00:00
|
|
|
}
|
2023-09-11 20:55:59 +00:00
|
|
|
|
2024-10-10 09:55:07 +00:00
|
|
|
[[nodiscard]]
|
2023-09-11 20:55:59 +00:00
|
|
|
bool IsSimpleWAV() const {
|
|
|
|
bool isBad = raw_bytes_per_frame > sizeof(int16_t) * num_channels;
|
|
|
|
return !isBad && num_channels > 0 && sample_rate >= 8000 && codec == 0;
|
|
|
|
}
|
2020-08-02 19:55:46 +00:00
|
|
|
};
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2024-04-09 15:29:57 +00:00
|
|
|
bool WavData::Read(RIFFReader &file_) {
|
2020-08-02 19:55:46 +00:00
|
|
|
// If we have no loop start info, we'll just loop the entire audio.
|
2020-08-03 09:29:54 +00:00
|
|
|
raw_offset_loop_start = 0;
|
|
|
|
raw_offset_loop_end = 0;
|
2020-08-02 19:55:46 +00:00
|
|
|
|
|
|
|
if (file_.Descend('RIFF')) {
|
|
|
|
file_.ReadInt(); //get past 'WAVE'
|
|
|
|
if (file_.Descend('fmt ')) { //enter the format chunk
|
|
|
|
int temp = file_.ReadInt();
|
|
|
|
int format = temp & 0xFFFF;
|
|
|
|
switch (format) {
|
|
|
|
case 0xFFFE:
|
|
|
|
codec = PSP_CODEC_AT3PLUS;
|
|
|
|
break;
|
|
|
|
case 0x270:
|
|
|
|
codec = PSP_CODEC_AT3;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// Raw wave data, no codec
|
|
|
|
codec = 0;
|
|
|
|
break;
|
|
|
|
default:
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::sceAudio, "Unexpected wave format %04x", format);
|
2024-04-09 15:29:57 +00:00
|
|
|
return false;
|
2020-08-02 19:55:46 +00:00
|
|
|
}
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2020-08-02 19:55:46 +00:00
|
|
|
num_channels = temp >> 16;
|
2014-06-22 16:55:14 +00:00
|
|
|
|
2020-08-02 19:55:46 +00:00
|
|
|
samplesPerSec = file_.ReadInt();
|
2020-08-03 09:29:54 +00:00
|
|
|
/*avgBytesPerSec =*/ file_.ReadInt();
|
2016-08-05 17:48:18 +00:00
|
|
|
|
2020-08-02 19:55:46 +00:00
|
|
|
temp = file_.ReadInt();
|
2020-08-03 09:29:54 +00:00
|
|
|
raw_bytes_per_frame = temp & 0xFFFF;
|
2020-08-02 19:55:46 +00:00
|
|
|
|
|
|
|
if (codec == PSP_CODEC_AT3) {
|
|
|
|
// The first two bytes are actually not a useful part of the extradata.
|
|
|
|
// We already read 16 bytes, so make sure there's enough left.
|
|
|
|
if (file_.GetCurrentChunkSize() >= 32) {
|
|
|
|
file_.ReadData(at3_extradata, 16);
|
|
|
|
} else {
|
|
|
|
memset(at3_extradata, 0, sizeof(at3_extradata));
|
2014-06-22 16:55:14 +00:00
|
|
|
}
|
2014-06-22 15:02:04 +00:00
|
|
|
}
|
2020-08-02 19:55:46 +00:00
|
|
|
file_.Ascend();
|
2024-07-14 12:42:59 +00:00
|
|
|
// INFO_LOG(Log::AUDIO, "got fmt data: %i", samplesPerSec);
|
2020-08-02 19:55:46 +00:00
|
|
|
} else {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::Audio, "Error - no format chunk in wav");
|
2020-08-02 19:55:46 +00:00
|
|
|
file_.Ascend();
|
2024-04-09 15:29:57 +00:00
|
|
|
return false;
|
2020-08-02 19:55:46 +00:00
|
|
|
}
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2020-08-02 19:55:46 +00:00
|
|
|
if (file_.Descend('smpl')) {
|
|
|
|
std::vector<u8> smplData;
|
|
|
|
smplData.resize(file_.GetCurrentChunkSize());
|
|
|
|
file_.ReadData(&smplData[0], (int)smplData.size());
|
|
|
|
|
|
|
|
int numLoops = *(int *)&smplData[28];
|
|
|
|
struct AtracLoopInfo {
|
|
|
|
int cuePointID;
|
|
|
|
int type;
|
|
|
|
int startSample;
|
|
|
|
int endSample;
|
|
|
|
int fraction;
|
|
|
|
int playCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (numLoops > 0 && smplData.size() >= 36 + sizeof(AtracLoopInfo) * numLoops) {
|
|
|
|
AtracLoopInfo *loops = (AtracLoopInfo *)&smplData[36];
|
|
|
|
int samplesPerFrame = codec == PSP_CODEC_AT3PLUS ? 2048 : 1024;
|
|
|
|
|
|
|
|
for (int i = 0; i < numLoops; ++i) {
|
|
|
|
// Only seen forward loops, so let's ignore others.
|
|
|
|
if (loops[i].type != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// We ignore loop interpolation (fraction) and play count for now.
|
2020-08-03 09:29:54 +00:00
|
|
|
raw_offset_loop_start = (loops[i].startSample / samplesPerFrame) * raw_bytes_per_frame;
|
|
|
|
loop_start_offset = loops[i].startSample % samplesPerFrame;
|
|
|
|
raw_offset_loop_end = (loops[i].endSample / samplesPerFrame) * raw_bytes_per_frame;
|
|
|
|
loop_end_offset = loops[i].endSample % samplesPerFrame;
|
2020-08-02 19:55:46 +00:00
|
|
|
|
|
|
|
if (loops[i].playCount == 0) {
|
|
|
|
// This was an infinite loop, so ignore the rest.
|
|
|
|
// In practice, there's usually only one and it's usually infinite.
|
|
|
|
break;
|
2019-08-17 20:31:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-02 19:55:46 +00:00
|
|
|
file_.Ascend();
|
|
|
|
}
|
|
|
|
|
|
|
|
// enter the data chunk
|
|
|
|
if (file_.Descend('data')) {
|
|
|
|
int numBytes = file_.GetCurrentChunkSize();
|
2020-08-03 09:29:54 +00:00
|
|
|
numFrames = numBytes / raw_bytes_per_frame; // numFrames
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2024-01-26 18:42:25 +00:00
|
|
|
// It seems the atrac3 codec likes to read a little bit outside.
|
2024-01-28 15:23:27 +00:00
|
|
|
const int padding = 32; // 32 is the value FFMPEG uses.
|
2024-01-26 18:42:25 +00:00
|
|
|
raw_data = (uint8_t *)malloc(numBytes + padding);
|
2020-08-03 09:29:54 +00:00
|
|
|
raw_data_size = numBytes;
|
2023-09-26 22:07:35 +00:00
|
|
|
|
2020-08-02 19:55:46 +00:00
|
|
|
if (num_channels == 1 || num_channels == 2) {
|
2020-08-03 09:29:54 +00:00
|
|
|
file_.ReadData(raw_data, numBytes);
|
2014-06-22 15:02:04 +00:00
|
|
|
} else {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::Audio, "Error - bad blockalign or channels");
|
2020-08-03 09:29:54 +00:00
|
|
|
free(raw_data);
|
|
|
|
raw_data = nullptr;
|
2024-04-09 15:29:57 +00:00
|
|
|
return false;
|
2014-06-22 15:02:04 +00:00
|
|
|
}
|
2017-12-04 16:33:42 +00:00
|
|
|
file_.Ascend();
|
2014-06-22 15:02:04 +00:00
|
|
|
} else {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::Audio, "Error - no data chunk in wav");
|
2020-08-02 19:55:46 +00:00
|
|
|
file_.Ascend();
|
2024-04-09 15:29:57 +00:00
|
|
|
return false;
|
2014-06-22 15:02:04 +00:00
|
|
|
}
|
2020-08-02 19:55:46 +00:00
|
|
|
file_.Ascend();
|
|
|
|
} else {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::Audio, "Could not descend into RIFF file.");
|
2024-04-09 15:29:57 +00:00
|
|
|
return false;
|
2014-06-22 15:02:04 +00:00
|
|
|
}
|
2020-08-02 19:55:46 +00:00
|
|
|
sample_rate = samplesPerSec;
|
2024-04-09 15:29:57 +00:00
|
|
|
return true;
|
2020-08-02 19:55:46 +00:00
|
|
|
}
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2020-08-02 19:55:46 +00:00
|
|
|
// Really simple looping in-memory AT3 player that also takes care of reading the file format.
|
|
|
|
// Turns out that AT3 files used for this are modified WAVE files so fairly easy to parse.
|
|
|
|
class AT3PlusReader {
|
|
|
|
public:
|
|
|
|
explicit AT3PlusReader(const std::string &data)
|
|
|
|
: file_((const uint8_t *)&data[0], (int32_t)data.size()) {
|
|
|
|
// Normally 8k but let's be safe.
|
|
|
|
buffer_ = new short[32 * 1024];
|
|
|
|
|
|
|
|
skip_next_samples_ = 0;
|
|
|
|
|
|
|
|
wave_.Read(file_);
|
|
|
|
|
2024-04-11 11:02:36 +00:00
|
|
|
uint8_t *extraData = nullptr;
|
|
|
|
size_t extraDataSize = 0;
|
|
|
|
size_t blockSize = 0;
|
2020-08-02 19:55:46 +00:00
|
|
|
if (wave_.codec == PSP_CODEC_AT3) {
|
2024-04-11 11:02:36 +00:00
|
|
|
extraData = &wave_.at3_extradata[2];
|
|
|
|
extraDataSize = 14;
|
|
|
|
blockSize = wave_.raw_bytes_per_frame;
|
|
|
|
} else if (wave_.codec == PSP_CODEC_AT3PLUS) {
|
|
|
|
blockSize = wave_.raw_bytes_per_frame;
|
2020-08-02 19:55:46 +00:00
|
|
|
}
|
2024-04-11 11:02:36 +00:00
|
|
|
decoder_ = CreateAudioDecoder((PSPAudioType)wave_.codec, wave_.sample_rate, wave_.num_channels, blockSize, extraData, extraDataSize);
|
2024-07-14 12:42:59 +00:00
|
|
|
INFO_LOG(Log::Audio, "read ATRAC, frames: %d, rate %d", wave_.numFrames, wave_.sample_rate);
|
2014-06-22 15:02:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 19:55:46 +00:00
|
|
|
~AT3PlusReader() {
|
2014-06-22 15:02:04 +00:00
|
|
|
delete[] buffer_;
|
2019-08-17 20:31:52 +00:00
|
|
|
buffer_ = nullptr;
|
2014-06-22 15:02:04 +00:00
|
|
|
delete decoder_;
|
2019-08-17 20:31:52 +00:00
|
|
|
decoder_ = nullptr;
|
2014-06-22 15:02:04 +00:00
|
|
|
}
|
|
|
|
|
2024-10-10 09:55:07 +00:00
|
|
|
bool IsOK() const { return wave_.raw_data != nullptr; }
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2015-01-29 13:58:03 +00:00
|
|
|
bool Read(int *buffer, int len) {
|
2020-08-03 09:29:54 +00:00
|
|
|
if (!wave_.raw_data)
|
2014-06-22 15:57:12 +00:00
|
|
|
return false;
|
2015-02-28 21:24:35 +00:00
|
|
|
|
2014-07-16 14:49:48 +00:00
|
|
|
while (bgQueue.size() < (size_t)(len * 2)) {
|
2024-04-16 13:31:11 +00:00
|
|
|
int outSamples = 0;
|
2024-04-11 14:46:55 +00:00
|
|
|
int inbytesConsumed = 0;
|
2024-04-16 13:31:11 +00:00
|
|
|
bool result = decoder_->Decode(wave_.raw_data + raw_offset_, wave_.raw_bytes_per_frame, &inbytesConsumed, 2, (int16_t *)buffer_, &outSamples);
|
|
|
|
if (!result || !outSamples)
|
2014-06-22 15:57:12 +00:00
|
|
|
return false;
|
2024-04-16 13:31:11 +00:00
|
|
|
int outBytes = outSamples * 2 * sizeof(int16_t);
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2020-08-03 09:29:54 +00:00
|
|
|
if (wave_.raw_offset_loop_end != 0 && raw_offset_ == wave_.raw_offset_loop_end) {
|
2019-08-17 20:31:52 +00:00
|
|
|
// Only take the remaining bytes, but convert to stereo s16.
|
2020-08-03 09:29:54 +00:00
|
|
|
outBytes = std::min(outBytes, wave_.loop_end_offset * 4);
|
2019-08-17 20:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int start = skip_next_samples_;
|
|
|
|
skip_next_samples_ = 0;
|
|
|
|
|
|
|
|
for (int i = start; i < outBytes / 2; i++) {
|
2014-06-22 15:02:04 +00:00
|
|
|
bgQueue.push(buffer_[i]);
|
|
|
|
}
|
|
|
|
|
2020-08-03 09:29:54 +00:00
|
|
|
if (wave_.raw_offset_loop_end != 0 && raw_offset_ == wave_.raw_offset_loop_end) {
|
2019-08-17 20:31:52 +00:00
|
|
|
// Time to loop. Account for the addition below.
|
2020-08-03 09:29:54 +00:00
|
|
|
raw_offset_ = wave_.raw_offset_loop_start - wave_.raw_bytes_per_frame;
|
2019-08-17 20:31:52 +00:00
|
|
|
// This time we're counting each stereo sample.
|
2020-08-03 09:29:54 +00:00
|
|
|
skip_next_samples_ = wave_.loop_start_offset * 2;
|
2019-08-17 20:31:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle loops when there's no loop info.
|
2020-08-03 09:29:54 +00:00
|
|
|
raw_offset_ += wave_.raw_bytes_per_frame;
|
|
|
|
if (raw_offset_ >= wave_.raw_data_size) {
|
2014-06-22 15:02:04 +00:00
|
|
|
raw_offset_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < len * 2; i++) {
|
|
|
|
buffer[i] = bgQueue.pop_front();
|
|
|
|
}
|
2014-06-22 15:57:12 +00:00
|
|
|
return true;
|
2014-06-22 15:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-12-04 16:27:47 +00:00
|
|
|
RIFFReader file_;
|
2020-08-02 19:55:46 +00:00
|
|
|
|
|
|
|
WavData wave_;
|
|
|
|
|
2019-08-17 20:31:52 +00:00
|
|
|
int raw_offset_ = 0;
|
|
|
|
int skip_next_samples_ = 0;
|
2014-06-22 15:02:04 +00:00
|
|
|
FixedSizeQueue<s16, 128 * 1024> bgQueue;
|
2019-08-17 20:31:52 +00:00
|
|
|
short *buffer_ = nullptr;
|
2024-04-10 10:19:48 +00:00
|
|
|
AudioDecoder *decoder_ = nullptr;
|
2014-06-22 15:02:04 +00:00
|
|
|
};
|
|
|
|
|
2020-08-02 17:44:48 +00:00
|
|
|
BackgroundAudio g_BackgroundAudio;
|
|
|
|
|
2020-08-02 17:56:57 +00:00
|
|
|
BackgroundAudio::BackgroundAudio() {
|
|
|
|
buffer = new int[BUFSIZE]();
|
2021-02-16 02:04:11 +00:00
|
|
|
sndLoadPending_.store(false);
|
2020-08-02 17:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BackgroundAudio::~BackgroundAudio() {
|
2023-09-15 09:23:54 +00:00
|
|
|
delete at3Reader_;
|
2020-08-02 17:56:57 +00:00
|
|
|
delete[] buffer;
|
|
|
|
}
|
|
|
|
|
2020-08-02 17:44:48 +00:00
|
|
|
void BackgroundAudio::Clear(bool hard) {
|
2017-01-28 09:38:38 +00:00
|
|
|
if (!hard) {
|
2020-08-03 09:29:54 +00:00
|
|
|
fadingOut_ = true;
|
|
|
|
volume_ = 1.0f;
|
2017-01-28 09:38:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-08-03 09:29:54 +00:00
|
|
|
if (at3Reader_) {
|
|
|
|
delete at3Reader_;
|
|
|
|
at3Reader_ = nullptr;
|
2015-02-28 20:42:32 +00:00
|
|
|
}
|
2020-08-03 09:29:54 +00:00
|
|
|
playbackOffset_ = 0;
|
2021-02-16 02:04:11 +00:00
|
|
|
sndLoadPending_ = false;
|
2015-02-28 20:42:32 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 23:31:38 +00:00
|
|
|
void BackgroundAudio::SetGame(const Path &path) {
|
2020-08-03 09:29:54 +00:00
|
|
|
if (path == bgGamePath_) {
|
2014-06-22 15:02:04 +00:00
|
|
|
// Do nothing
|
|
|
|
return;
|
|
|
|
}
|
2014-06-22 15:26:15 +00:00
|
|
|
|
2021-02-16 02:04:11 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
if (path.empty()) {
|
2020-08-02 17:44:48 +00:00
|
|
|
Clear(false);
|
2021-02-16 02:04:11 +00:00
|
|
|
sndLoadPending_ = false;
|
2020-08-03 09:29:54 +00:00
|
|
|
fadingOut_ = true;
|
2017-01-28 09:38:38 +00:00
|
|
|
} else {
|
2020-08-02 17:44:48 +00:00
|
|
|
Clear(true);
|
2020-08-03 09:29:54 +00:00
|
|
|
gameLastChanged_ = time_now_d();
|
2021-02-16 02:04:11 +00:00
|
|
|
sndLoadPending_ = true;
|
2020-08-03 09:29:54 +00:00
|
|
|
fadingOut_ = false;
|
2017-01-28 09:38:38 +00:00
|
|
|
}
|
2020-08-03 09:29:54 +00:00
|
|
|
volume_ = 1.0f;
|
|
|
|
bgGamePath_ = path;
|
2014-06-22 15:02:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 11:00:36 +00:00
|
|
|
bool BackgroundAudio::Play() {
|
2020-08-03 09:29:54 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2015-02-28 20:42:32 +00:00
|
|
|
|
|
|
|
// Immediately stop the sound if it is turned off while playing.
|
|
|
|
if (!g_Config.bEnableSound) {
|
2020-08-02 17:44:48 +00:00
|
|
|
Clear(true);
|
2023-03-24 16:19:57 +00:00
|
|
|
System_AudioClear();
|
2023-03-24 11:00:36 +00:00
|
|
|
return true;
|
2015-02-28 20:42:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-02 19:55:46 +00:00
|
|
|
double now = time_now_d();
|
2021-10-09 15:24:54 +00:00
|
|
|
int sz = 44100 / 60;
|
2021-10-10 22:04:59 +00:00
|
|
|
if (lastPlaybackTime_ > 0.0 && lastPlaybackTime_ <= now) {
|
2021-10-09 15:24:54 +00:00
|
|
|
sz = (int)((now - lastPlaybackTime_) * 44100);
|
|
|
|
}
|
2020-08-02 21:02:30 +00:00
|
|
|
sz = std::min(BUFSIZE / 2, sz);
|
2020-08-03 09:29:54 +00:00
|
|
|
if (at3Reader_) {
|
2021-02-09 23:20:13 +00:00
|
|
|
if (at3Reader_->Read(buffer, sz)) {
|
|
|
|
if (fadingOut_) {
|
2024-10-10 08:24:40 +00:00
|
|
|
float vol = volume_;
|
2021-02-09 23:20:13 +00:00
|
|
|
for (int i = 0; i < sz*2; i += 2) {
|
2024-10-10 08:24:40 +00:00
|
|
|
buffer[i] = (int)((float)buffer[i] * vol);
|
|
|
|
buffer[i + 1] = (int)((float)buffer[i + 1] * vol);
|
|
|
|
vol += delta_;
|
2017-01-28 09:38:38 +00:00
|
|
|
}
|
2024-10-10 08:24:40 +00:00
|
|
|
volume_ = vol;
|
2017-01-28 09:38:38 +00:00
|
|
|
}
|
2015-01-29 13:58:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-08-02 21:02:30 +00:00
|
|
|
for (int i = 0; i < sz * 2; i += 2) {
|
|
|
|
buffer[i] = 0;
|
|
|
|
buffer[i + 1] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 16:19:57 +00:00
|
|
|
System_AudioPushSamples(buffer, sz);
|
2020-08-02 21:02:30 +00:00
|
|
|
|
2020-08-03 09:29:54 +00:00
|
|
|
if (at3Reader_ && fadingOut_ && volume_ <= 0.0f) {
|
2020-08-02 21:02:30 +00:00
|
|
|
Clear(true);
|
2020-08-03 09:29:54 +00:00
|
|
|
fadingOut_ = false;
|
|
|
|
gameLastChanged_ = 0;
|
2020-08-02 21:02:30 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 09:29:54 +00:00
|
|
|
lastPlaybackTime_ = now;
|
2020-08-02 21:02:30 +00:00
|
|
|
|
2023-03-24 11:00:36 +00:00
|
|
|
return true;
|
2014-07-16 14:49:48 +00:00
|
|
|
}
|
2019-09-27 21:53:31 +00:00
|
|
|
|
2020-08-02 17:44:48 +00:00
|
|
|
void BackgroundAudio::Update() {
|
2019-09-27 21:53:31 +00:00
|
|
|
// If there's a game, and some time has passed since the selected game
|
|
|
|
// last changed... (to prevent crazy amount of reads when skipping through a list)
|
2021-02-16 02:04:11 +00:00
|
|
|
if (sndLoadPending_ && (time_now_d() - gameLastChanged_ > 0.5)) {
|
2020-08-03 09:29:54 +00:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2021-02-16 02:04:11 +00:00
|
|
|
// Already loaded somehow? Or no game info cache?
|
|
|
|
if (at3Reader_ || !g_gameInfoCache)
|
|
|
|
return;
|
2019-09-27 21:53:31 +00:00
|
|
|
|
2021-02-16 02:04:11 +00:00
|
|
|
// Grab some audio from the current game and play it.
|
2024-01-28 15:23:27 +00:00
|
|
|
std::shared_ptr<GameInfo> gameInfo = g_gameInfoCache->GetInfo(nullptr, bgGamePath_, GameInfoFlags::SND);
|
|
|
|
if (!gameInfo->Ready(GameInfoFlags::SND)) {
|
2021-02-16 02:04:11 +00:00
|
|
|
// Should try again shortly..
|
|
|
|
return;
|
|
|
|
}
|
2019-09-27 21:53:31 +00:00
|
|
|
|
2021-02-16 02:04:11 +00:00
|
|
|
const std::string &data = gameInfo->sndFileData;
|
|
|
|
if (!data.empty()) {
|
|
|
|
at3Reader_ = new AT3PlusReader(data);
|
|
|
|
lastPlaybackTime_ = 0.0;
|
2019-09-27 21:53:31 +00:00
|
|
|
}
|
2021-02-16 02:04:11 +00:00
|
|
|
sndLoadPending_ = false;
|
2019-09-27 21:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-12 13:37:16 +00:00
|
|
|
|
2023-07-16 13:04:21 +00:00
|
|
|
inline int16_t ConvertU8ToI16(uint8_t value) {
|
|
|
|
int ivalue = value - 128;
|
|
|
|
return ivalue * 255;
|
|
|
|
}
|
|
|
|
|
2023-07-16 10:15:44 +00:00
|
|
|
Sample *Sample::Load(const std::string &path) {
|
2024-04-09 15:29:57 +00:00
|
|
|
size_t data_size = 0;
|
|
|
|
uint8_t *data = g_VFS.ReadFile(path.c_str(), &data_size);
|
|
|
|
if (!data || data_size > 100000000) {
|
2024-07-14 12:42:59 +00:00
|
|
|
WARN_LOG(Log::Audio, "Failed to load sample '%s'", path.c_str());
|
2023-07-16 10:15:44 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-04-09 15:29:57 +00:00
|
|
|
const char *mp3_magic = "ID3\03";
|
|
|
|
const char *wav_magic = "RIFF";
|
|
|
|
if (!memcmp(data, wav_magic, 4)) {
|
|
|
|
RIFFReader reader(data, (int)data_size);
|
|
|
|
WavData wave;
|
|
|
|
if (!wave.Read(reader)) {
|
|
|
|
delete[] data;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// A wav file.
|
|
|
|
delete[] data;
|
2023-07-16 10:15:44 +00:00
|
|
|
|
2024-04-09 15:29:57 +00:00
|
|
|
if (!wave.IsSimpleWAV()) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::Audio, "Wave format not supported for mixer playback. Must be 8-bit or 16-bit raw mono or stereo. '%s'", path.c_str());
|
2024-04-09 15:29:57 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2023-07-16 10:15:44 +00:00
|
|
|
|
2024-04-09 15:29:57 +00:00
|
|
|
int16_t *samples = new int16_t[wave.num_channels * wave.numFrames];
|
|
|
|
if (wave.raw_bytes_per_frame == wave.num_channels * 2) {
|
|
|
|
// 16-bit
|
|
|
|
memcpy(samples, wave.raw_data, wave.numFrames * wave.raw_bytes_per_frame);
|
|
|
|
} else if (wave.raw_bytes_per_frame == wave.num_channels) {
|
|
|
|
// 8-bit. Convert.
|
|
|
|
for (int i = 0; i < wave.num_channels * wave.numFrames; i++) {
|
|
|
|
samples[i] = ConvertU8ToI16(wave.raw_data[i]);
|
|
|
|
}
|
|
|
|
}
|
2023-07-16 10:15:44 +00:00
|
|
|
|
2024-04-09 15:29:57 +00:00
|
|
|
// Protect against bad metadata.
|
|
|
|
int actualFrames = std::min(wave.numFrames, wave.raw_data_size / wave.raw_bytes_per_frame);
|
2023-07-16 10:15:44 +00:00
|
|
|
|
2024-04-09 15:29:57 +00:00
|
|
|
return new Sample(samples, wave.num_channels, actualFrames, wave.sample_rate);
|
2023-07-16 13:04:21 +00:00
|
|
|
}
|
2023-09-26 22:07:35 +00:00
|
|
|
|
2024-04-09 15:29:57 +00:00
|
|
|
// Something else.
|
|
|
|
// Let's see if minimp3 can read it.
|
|
|
|
mp3dec_t mp3d;
|
|
|
|
mp3dec_init(&mp3d);
|
|
|
|
mp3dec_file_info_t mp3_info;
|
2024-07-17 12:37:15 +00:00
|
|
|
int retval = mp3dec_load_buf(&mp3d, data, data_size, &mp3_info, nullptr, nullptr);
|
|
|
|
|
|
|
|
if (retval < 0 || mp3_info.samples == 0) {
|
|
|
|
ERROR_LOG(Log::Audio, "Couldn't load MP3 for sound effect from %s", path.c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-04-09 15:29:57 +00:00
|
|
|
// mp3_info contains the decoded data.
|
|
|
|
int16_t *sample_data = new int16_t[mp3_info.samples];
|
|
|
|
memcpy(sample_data, mp3_info.buffer, mp3_info.samples * sizeof(int16_t));
|
2024-07-17 12:37:15 +00:00
|
|
|
|
|
|
|
Sample *sample = new Sample(sample_data, mp3_info.channels, (int)mp3_info.samples / mp3_info.channels, mp3_info.hz);
|
2024-04-09 15:29:57 +00:00
|
|
|
free(mp3_info.buffer);
|
|
|
|
delete[] data;
|
|
|
|
return sample;
|
2023-07-16 10:15:44 +00:00
|
|
|
}
|
|
|
|
|
2023-07-12 13:37:16 +00:00
|
|
|
static inline int16_t Clamp16(int32_t sample) {
|
|
|
|
if (sample < -32767) return -32767;
|
|
|
|
if (sample > 32767) return 32767;
|
|
|
|
return sample;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundEffectMixer::Mix(int16_t *buffer, int sz, int sampleRateHz) {
|
2023-07-12 18:09:52 +00:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
|
|
|
if (!queue_.empty()) {
|
|
|
|
for (const auto &entry : queue_) {
|
|
|
|
plays_.push_back(entry);
|
|
|
|
}
|
|
|
|
queue_.clear();
|
|
|
|
}
|
|
|
|
if (plays_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
2023-07-12 13:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<PlayInstance>::iterator iter = plays_.begin(); iter != plays_.end(); ) {
|
|
|
|
auto sample = samples_[(int)iter->sound].get();
|
2023-07-12 17:18:56 +00:00
|
|
|
if (!sample) {
|
|
|
|
// Remove playback instance if sample invalid.
|
|
|
|
iter = plays_.erase(iter);
|
|
|
|
continue;
|
|
|
|
}
|
2023-07-12 15:54:41 +00:00
|
|
|
|
|
|
|
int64_t rateOfSample = sample->rateInHz_;
|
|
|
|
int64_t stride = (rateOfSample << 32) / sampleRateHz;
|
|
|
|
|
2023-07-12 13:37:16 +00:00
|
|
|
for (int i = 0; i < sz * 2; i += 2) {
|
2023-07-12 17:18:56 +00:00
|
|
|
if ((iter->offset >> 32) >= sample->length_ - 2) {
|
2023-07-12 13:37:16 +00:00
|
|
|
iter->done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-12 15:54:41 +00:00
|
|
|
int wholeOffset = iter->offset >> 32;
|
|
|
|
int frac = (iter->offset >> 20) & 0xFFF; // Use a 12 bit fraction to get away with 32-bit multiplies
|
2023-07-12 13:37:16 +00:00
|
|
|
|
2023-07-16 13:04:21 +00:00
|
|
|
if (sample->channels_ == 2) {
|
|
|
|
int interpolatedLeft = (sample->data_[wholeOffset * 2] * (0x1000 - frac) + sample->data_[(wholeOffset + 1) * 2] * frac) >> 12;
|
|
|
|
int interpolatedRight = (sample->data_[wholeOffset * 2 + 1] * (0x1000 - frac) + sample->data_[(wholeOffset + 1) * 2 + 1] * frac) >> 12;
|
|
|
|
|
|
|
|
// Clamping add on top per sample. Not great, we should be mixing at higher bitrate instead. Oh well.
|
|
|
|
int left = Clamp16(buffer[i] + (interpolatedLeft * iter->volume >> 8));
|
|
|
|
int right = Clamp16(buffer[i + 1] + (interpolatedRight * iter->volume >> 8));
|
2023-07-12 15:54:41 +00:00
|
|
|
|
2023-07-16 13:04:21 +00:00
|
|
|
buffer[i] = left;
|
|
|
|
buffer[i + 1] = right;
|
|
|
|
} else if (sample->channels_ == 1) {
|
|
|
|
int interpolated = (sample->data_[wholeOffset] * (0x1000 - frac) + sample->data_[wholeOffset + 1] * frac) >> 12;
|
2023-07-12 13:37:16 +00:00
|
|
|
|
2023-07-16 13:04:21 +00:00
|
|
|
// Clamping add on top per sample. Not great, we should be mixing at higher bitrate instead. Oh well.
|
|
|
|
int value = Clamp16(buffer[i] + (interpolated * iter->volume >> 8));
|
|
|
|
|
|
|
|
buffer[i] = value;
|
|
|
|
buffer[i + 1] = value;
|
|
|
|
}
|
2023-07-12 13:37:16 +00:00
|
|
|
|
2023-07-12 15:54:41 +00:00
|
|
|
iter->offset += stride;
|
2023-07-12 13:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (iter->done) {
|
|
|
|
iter = plays_.erase(iter);
|
|
|
|
} else {
|
|
|
|
iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-12 17:18:56 +00:00
|
|
|
void SoundEffectMixer::Play(UI::UISound sfx, float volume) {
|
2023-07-12 13:37:16 +00:00
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
2023-07-12 18:09:52 +00:00
|
|
|
queue_.push_back(PlayInstance{ sfx, 0, (int)(255.0f * volume), false });
|
2023-07-12 13:37:16 +00:00
|
|
|
}
|
|
|
|
|
2023-07-16 10:15:44 +00:00
|
|
|
void SoundEffectMixer::UpdateSample(UI::UISound sound, Sample *sample) {
|
|
|
|
if (sample) {
|
2023-07-16 13:04:21 +00:00
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
2023-07-16 10:15:44 +00:00
|
|
|
samples_[(size_t)sound] = std::unique_ptr<Sample>(sample);
|
|
|
|
} else {
|
|
|
|
LoadDefaultSample(sound);
|
2023-07-12 13:37:16 +00:00
|
|
|
}
|
2023-07-16 10:15:44 +00:00
|
|
|
}
|
2023-07-12 13:37:16 +00:00
|
|
|
|
2023-07-16 10:15:44 +00:00
|
|
|
void SoundEffectMixer::LoadDefaultSample(UI::UISound sound) {
|
|
|
|
const char *filename = nullptr;
|
|
|
|
switch (sound) {
|
|
|
|
case UI::UISound::BACK: filename = "sfx_back.wav"; break;
|
|
|
|
case UI::UISound::SELECT: filename = "sfx_select.wav"; break;
|
|
|
|
case UI::UISound::CONFIRM: filename = "sfx_confirm.wav"; break;
|
|
|
|
case UI::UISound::TOGGLE_ON: filename = "sfx_toggle_on.wav"; break;
|
|
|
|
case UI::UISound::TOGGLE_OFF: filename = "sfx_toggle_off.wav"; break;
|
|
|
|
case UI::UISound::ACHIEVEMENT_UNLOCKED: filename = "sfx_achievement_unlocked.wav"; break;
|
|
|
|
case UI::UISound::LEADERBOARD_SUBMITTED: filename = "sfx_leaderbord_submitted.wav"; break;
|
|
|
|
default:
|
|
|
|
return;
|
2023-07-12 13:37:16 +00:00
|
|
|
}
|
2023-07-16 12:25:46 +00:00
|
|
|
Sample *sample = Sample::Load(filename);
|
|
|
|
if (!sample) {
|
2024-07-14 12:42:59 +00:00
|
|
|
ERROR_LOG(Log::System, "Failed to load the default sample for UI sound %d", (int)sound);
|
2023-07-16 12:25:46 +00:00
|
|
|
}
|
2023-07-16 13:04:21 +00:00
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
2023-07-16 12:25:46 +00:00
|
|
|
samples_[(size_t)sound] = std::unique_ptr<Sample>(sample);
|
2023-07-12 13:37:16 +00:00
|
|
|
}
|
|
|
|
|
2024-10-11 09:31:17 +00:00
|
|
|
class SampleLoadTask : public Task {
|
|
|
|
public:
|
|
|
|
SampleLoadTask(SoundEffectMixer *mixer) : mixer_(mixer) {}
|
|
|
|
TaskType Type() const override { return TaskType::IO_BLOCKING; }
|
|
|
|
TaskPriority Priority() const override {
|
|
|
|
return TaskPriority::NORMAL;
|
|
|
|
}
|
2024-10-16 11:35:32 +00:00
|
|
|
virtual void Run() override {
|
2024-10-11 09:31:17 +00:00
|
|
|
mixer_->LoadSamplesOnThread();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
SoundEffectMixer *mixer_;
|
|
|
|
};
|
|
|
|
|
|
|
|
void SoundEffectMixer::Init() {
|
2023-07-12 13:37:16 +00:00
|
|
|
samples_.resize((size_t)UI::UISound::COUNT);
|
2024-10-11 09:31:17 +00:00
|
|
|
UI::SetSoundCallback([](UI::UISound sound, float volume) {
|
|
|
|
g_BackgroundAudio.SFX().Play(sound, volume);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Load samples in the background.
|
|
|
|
|
|
|
|
g_threadManager.EnqueueTask(new SampleLoadTask(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundEffectMixer::LoadSamplesOnThread() {
|
2023-07-16 10:15:44 +00:00
|
|
|
LoadDefaultSample(UI::UISound::BACK);
|
|
|
|
LoadDefaultSample(UI::UISound::SELECT);
|
|
|
|
LoadDefaultSample(UI::UISound::CONFIRM);
|
|
|
|
LoadDefaultSample(UI::UISound::TOGGLE_ON);
|
|
|
|
LoadDefaultSample(UI::UISound::TOGGLE_OFF);
|
|
|
|
|
|
|
|
if (!g_Config.sAchievementsUnlockAudioFile.empty()) {
|
|
|
|
UpdateSample(UI::UISound::ACHIEVEMENT_UNLOCKED, Sample::Load(g_Config.sAchievementsUnlockAudioFile));
|
|
|
|
} else {
|
|
|
|
LoadDefaultSample(UI::UISound::ACHIEVEMENT_UNLOCKED);
|
|
|
|
}
|
|
|
|
if (!g_Config.sAchievementsLeaderboardSubmitAudioFile.empty()) {
|
|
|
|
UpdateSample(UI::UISound::LEADERBOARD_SUBMITTED, Sample::Load(g_Config.sAchievementsLeaderboardSubmitAudioFile));
|
|
|
|
} else {
|
|
|
|
LoadDefaultSample(UI::UISound::LEADERBOARD_SUBMITTED);
|
|
|
|
}
|
2023-07-12 13:37:16 +00:00
|
|
|
}
|