2014-06-22 15:02:04 +00:00
|
|
|
#include <string>
|
2017-02-27 20:57:46 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
2014-06-22 15:02:04 +00:00
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/timeutil.h"
|
2015-09-06 19:19:33 +00:00
|
|
|
#include "file/chunk_file.h"
|
2014-06-22 15:02:04 +00:00
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
#include "Core/HW/SimpleAudioDec.h"
|
2015-01-29 13:58:03 +00:00
|
|
|
#include "Core/HLE/__sceAudio.h"
|
2014-06-22 15:02:04 +00:00
|
|
|
#include "Common/FixedSizeQueue.h"
|
|
|
|
#include "GameInfoCache.h"
|
2015-02-23 08:07:34 +00:00
|
|
|
#include "Core/Config.h"
|
2014-06-22 15:02:04 +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:
|
|
|
|
AT3PlusReader(const std::string &data)
|
2019-08-17 20:31:52 +00:00
|
|
|
: file_((const uint8_t *)&data[0], (int32_t)data.size()) {
|
2014-06-22 15:02:04 +00:00
|
|
|
// Normally 8k but let's be safe.
|
|
|
|
buffer_ = new short[32 * 1024];
|
|
|
|
|
|
|
|
int codec = PSP_CODEC_AT3PLUS;
|
2014-06-22 16:55:14 +00:00
|
|
|
u8 at3_extradata[16];
|
2014-06-22 15:02:04 +00:00
|
|
|
|
|
|
|
int num_channels, sample_rate, numFrames, samplesPerSec, avgBytesPerSec, Nothing;
|
2017-12-04 16:33:42 +00:00
|
|
|
if (file_.Descend('RIFF')) {
|
|
|
|
file_.ReadInt(); //get past 'WAVE'
|
|
|
|
if (file_.Descend('fmt ')) { //enter the format chunk
|
|
|
|
int temp = file_.ReadInt();
|
2014-06-22 15:02:04 +00:00
|
|
|
int format = temp & 0xFFFF;
|
|
|
|
switch (format) {
|
|
|
|
case 0xFFFE:
|
|
|
|
codec = PSP_CODEC_AT3PLUS;
|
|
|
|
break;
|
|
|
|
case 0x270:
|
2014-06-22 16:32:57 +00:00
|
|
|
codec = PSP_CODEC_AT3;
|
|
|
|
break;
|
2014-06-22 15:02:04 +00:00
|
|
|
default:
|
2017-03-06 12:10:23 +00:00
|
|
|
ERROR_LOG(SCEAUDIO, "Unexpected SND0.AT3 format %04x", format);
|
2014-06-22 15:02:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_channels = temp >> 16;
|
|
|
|
|
2017-12-04 16:33:42 +00:00
|
|
|
samplesPerSec = file_.ReadInt();
|
|
|
|
avgBytesPerSec = file_.ReadInt();
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2017-12-04 16:33:42 +00:00
|
|
|
temp = file_.ReadInt();
|
2014-06-22 15:02:04 +00:00
|
|
|
raw_bytes_per_frame_ = temp & 0xFFFF;
|
|
|
|
Nothing = temp >> 16;
|
2014-06-22 16:55:14 +00:00
|
|
|
|
2016-08-05 17:48:18 +00:00
|
|
|
// Not currently used, but part of the format.
|
|
|
|
(void)avgBytesPerSec;
|
|
|
|
(void)Nothing;
|
|
|
|
|
2014-06-22 16:55:14 +00:00
|
|
|
if (codec == PSP_CODEC_AT3) {
|
2014-11-02 18:53:17 +00:00
|
|
|
// The first two bytes are actually not a useful part of the extradata.
|
2014-06-22 17:05:27 +00:00
|
|
|
// We already read 16 bytes, so make sure there's enough left.
|
2017-12-04 16:33:42 +00:00
|
|
|
if (file_.GetCurrentChunkSize() >= 32) {
|
|
|
|
file_.ReadData(at3_extradata, 16);
|
2014-06-22 17:05:27 +00:00
|
|
|
} else {
|
|
|
|
memset(at3_extradata, 0, sizeof(at3_extradata));
|
|
|
|
}
|
2014-06-22 16:55:14 +00:00
|
|
|
}
|
2017-12-04 16:33:42 +00:00
|
|
|
file_.Ascend();
|
2014-06-22 15:02:04 +00:00
|
|
|
// ILOG("got fmt data: %i", samplesPerSec);
|
|
|
|
} else {
|
|
|
|
ELOG("Error - no format chunk in wav");
|
2017-12-04 16:33:42 +00:00
|
|
|
file_.Ascend();
|
2014-06-22 15:02:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-17 20:31:52 +00:00
|
|
|
// If we have no loop info, we'll just loop the entire audio.
|
|
|
|
raw_offset_loop_start_ = 0;
|
|
|
|
raw_offset_loop_end_ = 0;
|
|
|
|
skip_next_samples_ = 0;
|
|
|
|
|
|
|
|
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.
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file_.Ascend();
|
|
|
|
}
|
|
|
|
|
2017-12-04 16:33:42 +00:00
|
|
|
if (file_.Descend('data')) { //enter the data chunk
|
|
|
|
int numBytes = file_.GetCurrentChunkSize();
|
2014-06-22 15:02:04 +00:00
|
|
|
numFrames = numBytes / raw_bytes_per_frame_; // numFrames
|
|
|
|
|
|
|
|
raw_data_ = (uint8_t *)malloc(numBytes);
|
|
|
|
raw_data_size_ = numBytes;
|
2019-08-17 20:31:52 +00:00
|
|
|
if (num_channels == 1 || num_channels == 2) {
|
2017-12-04 16:33:42 +00:00
|
|
|
file_.ReadData(raw_data_, numBytes);
|
2014-06-22 15:02:04 +00:00
|
|
|
} else {
|
|
|
|
ELOG("Error - bad blockalign or channels");
|
|
|
|
free(raw_data_);
|
2019-08-17 20:31:52 +00:00
|
|
|
raw_data_ = nullptr;
|
2014-06-22 15:02:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-04 16:33:42 +00:00
|
|
|
file_.Ascend();
|
2014-06-22 15:02:04 +00:00
|
|
|
} else {
|
|
|
|
ELOG("Error - no data chunk in wav");
|
2017-12-04 16:33:42 +00:00
|
|
|
file_.Ascend();
|
2014-06-22 15:02:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-12-04 16:33:42 +00:00
|
|
|
file_.Ascend();
|
2014-06-22 15:02:04 +00:00
|
|
|
} else {
|
2016-02-13 09:48:11 +00:00
|
|
|
ELOG("Could not descend into RIFF file. Data size=%d", (int32_t)data.size());
|
2014-06-22 15:02:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
sample_rate = samplesPerSec;
|
2014-06-22 16:32:57 +00:00
|
|
|
decoder_ = new SimpleAudio(codec, sample_rate, num_channels);
|
2014-06-22 16:55:14 +00:00
|
|
|
if (codec == PSP_CODEC_AT3) {
|
|
|
|
decoder_->SetExtraData(&at3_extradata[2], 14, raw_bytes_per_frame_);
|
|
|
|
}
|
2014-06-22 15:02:04 +00:00
|
|
|
ILOG("read ATRAC, frames: %i, rate %i", numFrames, sample_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AT3PlusReader() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Shutdown() {
|
|
|
|
free(raw_data_);
|
2019-08-17 20:31:52 +00:00
|
|
|
raw_data_ = nullptr;
|
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
|
|
|
}
|
|
|
|
|
2019-08-17 20:31:52 +00:00
|
|
|
bool IsOK() { return 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) {
|
2014-06-22 15:57:12 +00:00
|
|
|
if (!raw_data_)
|
|
|
|
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)) {
|
2019-08-17 20:31:52 +00:00
|
|
|
int outBytes = 0;
|
2014-06-22 15:02:04 +00:00
|
|
|
decoder_->Decode(raw_data_ + raw_offset_, raw_bytes_per_frame_, (uint8_t *)buffer_, &outBytes);
|
|
|
|
if (!outBytes)
|
2014-06-22 15:57:12 +00:00
|
|
|
return false;
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2019-08-17 20:31:52 +00:00
|
|
|
if (raw_offset_loop_end_ != 0 && raw_offset_ == raw_offset_loop_end_) {
|
|
|
|
// Only take the remaining bytes, but convert to stereo s16.
|
|
|
|
outBytes = std::min(outBytes, loop_end_offset_ * 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
2019-08-17 20:31:52 +00:00
|
|
|
if (raw_offset_loop_end_ != 0 && raw_offset_ == raw_offset_loop_end_) {
|
|
|
|
// Time to loop. Account for the addition below.
|
|
|
|
raw_offset_ = raw_offset_loop_start_ - raw_bytes_per_frame_;
|
|
|
|
// This time we're counting each stereo sample.
|
|
|
|
skip_next_samples_ = loop_start_offset_ * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle loops when there's no loop info.
|
2014-06-22 15:02:04 +00:00
|
|
|
raw_offset_ += raw_bytes_per_frame_;
|
|
|
|
if (raw_offset_ >= raw_data_size_) {
|
|
|
|
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_;
|
2019-08-17 20:31:52 +00:00
|
|
|
uint8_t *raw_data_ = nullptr;
|
|
|
|
int raw_data_size_ = 0;
|
|
|
|
int raw_offset_ = 0;
|
2014-06-22 15:02:04 +00:00
|
|
|
int raw_bytes_per_frame_;
|
2019-08-17 20:31:52 +00:00
|
|
|
int raw_offset_loop_start_ = 0;
|
|
|
|
int raw_offset_loop_end_ = 0;
|
|
|
|
int loop_start_offset_ = 0;
|
|
|
|
int loop_end_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;
|
|
|
|
SimpleAudio *decoder_ = nullptr;
|
2014-06-22 15:02:04 +00:00
|
|
|
};
|
|
|
|
|
2019-09-27 21:53:31 +00:00
|
|
|
static std::mutex g_bgMutex;
|
2014-06-22 15:02:04 +00:00
|
|
|
static std::string bgGamePath;
|
|
|
|
static int playbackOffset;
|
|
|
|
static AT3PlusReader *at3Reader;
|
|
|
|
static double gameLastChanged;
|
2015-01-29 13:58:03 +00:00
|
|
|
static double lastPlaybackTime;
|
|
|
|
static int buffer[44100];
|
2017-01-28 09:38:38 +00:00
|
|
|
static bool fadingOut = true;
|
|
|
|
static float volume;
|
|
|
|
static float delta = -0.0001f;
|
|
|
|
|
|
|
|
static void ClearBackgroundAudio(bool hard) {
|
|
|
|
if (!hard) {
|
|
|
|
fadingOut = true;
|
|
|
|
volume = 1.0f;
|
|
|
|
return;
|
|
|
|
}
|
2015-02-28 20:42:32 +00:00
|
|
|
if (at3Reader) {
|
|
|
|
at3Reader->Shutdown();
|
|
|
|
delete at3Reader;
|
2017-01-28 09:38:38 +00:00
|
|
|
at3Reader = nullptr;
|
2015-02-28 20:42:32 +00:00
|
|
|
}
|
|
|
|
playbackOffset = 0;
|
|
|
|
}
|
|
|
|
|
2014-06-22 15:02:04 +00:00
|
|
|
void SetBackgroundAudioGame(const std::string &path) {
|
2014-06-22 15:26:15 +00:00
|
|
|
time_update();
|
|
|
|
|
2019-09-27 21:53:31 +00:00
|
|
|
std::lock_guard<std::mutex> lock(g_bgMutex);
|
2014-06-22 15:02:04 +00:00
|
|
|
if (path == bgGamePath) {
|
|
|
|
// Do nothing
|
|
|
|
return;
|
|
|
|
}
|
2014-06-22 15:26:15 +00:00
|
|
|
|
2017-01-28 09:38:38 +00:00
|
|
|
if (path.size() == 0) {
|
|
|
|
ClearBackgroundAudio(false);
|
|
|
|
fadingOut = true;
|
|
|
|
} else {
|
|
|
|
ClearBackgroundAudio(true);
|
|
|
|
gameLastChanged = time_now_d();
|
|
|
|
fadingOut = false;
|
|
|
|
}
|
|
|
|
volume = 1.0f;
|
2014-06-22 15:02:04 +00:00
|
|
|
bgGamePath = path;
|
|
|
|
}
|
|
|
|
|
2015-01-29 13:58:03 +00:00
|
|
|
int PlayBackgroundAudio() {
|
2014-06-22 15:02:04 +00:00
|
|
|
time_update();
|
2014-06-22 15:26:15 +00:00
|
|
|
|
2019-09-27 21:53:31 +00:00
|
|
|
std::lock_guard<std::mutex> lock(g_bgMutex);
|
2015-02-28 20:42:32 +00:00
|
|
|
|
|
|
|
// Immediately stop the sound if it is turned off while playing.
|
|
|
|
if (!g_Config.bEnableSound) {
|
2017-01-28 09:38:38 +00:00
|
|
|
ClearBackgroundAudio(true);
|
2015-02-28 20:42:32 +00:00
|
|
|
__PushExternalAudio(0, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-29 13:58:03 +00:00
|
|
|
double now = time_now();
|
|
|
|
if (at3Reader) {
|
2015-02-28 21:24:35 +00:00
|
|
|
int sz = lastPlaybackTime <= 0.0 ? 44100 / 60 : (int)((now - lastPlaybackTime) * 44100);
|
|
|
|
sz = std::min((int)ARRAY_SIZE(buffer) / 2, sz);
|
|
|
|
if (sz >= 16) {
|
2017-01-28 09:38:38 +00:00
|
|
|
if (at3Reader->Read(buffer, sz)) {
|
|
|
|
if (!fadingOut) {
|
|
|
|
__PushExternalAudio(buffer, sz);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < sz*2; i += 2) {
|
|
|
|
buffer[i] *= volume;
|
|
|
|
buffer[i + 1] *= volume;
|
|
|
|
volume += delta;
|
|
|
|
}
|
|
|
|
__PushExternalAudio(buffer, sz);
|
|
|
|
if (volume <= 0.0f) {
|
|
|
|
ClearBackgroundAudio(true);
|
|
|
|
fadingOut = false;
|
|
|
|
gameLastChanged = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-28 21:24:35 +00:00
|
|
|
lastPlaybackTime = now;
|
2015-01-29 13:58:03 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
__PushExternalAudio(0, 0);
|
2015-02-28 21:24:35 +00:00
|
|
|
lastPlaybackTime = now;
|
2014-06-22 15:02:04 +00:00
|
|
|
}
|
2015-01-29 13:58:03 +00:00
|
|
|
|
2014-06-22 15:02:04 +00:00
|
|
|
return 0;
|
2014-07-16 14:49:48 +00:00
|
|
|
}
|
2019-09-27 21:53:31 +00:00
|
|
|
|
|
|
|
// Stuff that should be on the UI thread only, like anything to do with
|
|
|
|
// g_gameInfoCache.
|
|
|
|
void UpdateBackgroundAudio() {
|
|
|
|
// 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)
|
|
|
|
if (bgGamePath.size() && (time_now_d() - gameLastChanged > 0.5)) {
|
|
|
|
std::lock_guard<std::mutex> lock(g_bgMutex);
|
|
|
|
if (!at3Reader) {
|
|
|
|
// Grab some audio from the current game and play it.
|
|
|
|
if (!g_gameInfoCache)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::shared_ptr<GameInfo> gameInfo = g_gameInfoCache->GetInfo(NULL, bgGamePath, GAMEINFO_WANTSND);
|
|
|
|
if (!gameInfo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (gameInfo->pending) {
|
|
|
|
// Should try again shortly..
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gameInfo->sndFileData.size()) {
|
|
|
|
const std::string &data = gameInfo->sndFileData;
|
|
|
|
at3Reader = new AT3PlusReader(data);
|
|
|
|
lastPlaybackTime = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|