2014-06-22 15:02:04 +00:00
|
|
|
#include <string>
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/timeutil.h"
|
2014-06-22 15:26:15 +00:00
|
|
|
#include "base/mutex.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)
|
2016-02-13 09:48:11 +00:00
|
|
|
: file_((const uint8_t *)&data[0], (int32_t)data.size()),
|
2014-06-22 15:02:04 +00:00
|
|
|
raw_data_(0),
|
|
|
|
raw_data_size_(0),
|
2014-07-16 14:49:48 +00:00
|
|
|
raw_offset_(0),
|
2014-06-22 15:02:04 +00:00
|
|
|
buffer_(0),
|
|
|
|
decoder_(0) {
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
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:
|
2014-06-22 16:32:57 +00:00
|
|
|
codec = PSP_CODEC_AT3;
|
|
|
|
break;
|
2014-06-22 15:02:04 +00:00
|
|
|
default:
|
2014-06-22 16:55:14 +00:00
|
|
|
ERROR_LOG(HLE, "Unexpected SND0.AT3 format %04x", format);
|
2014-06-22 15:02:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
num_channels = temp >> 16;
|
|
|
|
|
|
|
|
samplesPerSec = file_.readInt();
|
|
|
|
avgBytesPerSec = file_.readInt();
|
|
|
|
|
|
|
|
temp = file_.readInt();
|
|
|
|
raw_bytes_per_frame_ = temp & 0xFFFF;
|
|
|
|
Nothing = temp >> 16;
|
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.
|
|
|
|
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
|
|
|
file_.ascend();
|
|
|
|
// ILOG("got fmt data: %i", samplesPerSec);
|
|
|
|
} else {
|
|
|
|
ELOG("Error - no format chunk in wav");
|
|
|
|
file_.ascend();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_.descend('data')) { //enter the data chunk
|
|
|
|
int numBytes = file_.getCurrentChunkSize();
|
|
|
|
numFrames = numBytes / raw_bytes_per_frame_; // numFrames
|
|
|
|
|
|
|
|
raw_data_ = (uint8_t *)malloc(numBytes);
|
|
|
|
raw_data_size_ = numBytes;
|
2015-10-03 21:18:54 +00:00
|
|
|
if (/*raw_bytes_per_frame_ == 280 && */ num_channels == 1 || num_channels == 2) {
|
2014-06-22 15:02:04 +00:00
|
|
|
file_.readData(raw_data_, numBytes);
|
|
|
|
} else {
|
|
|
|
ELOG("Error - bad blockalign or channels");
|
|
|
|
free(raw_data_);
|
|
|
|
raw_data_ = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
file_.ascend();
|
|
|
|
} else {
|
|
|
|
ELOG("Error - no data chunk in wav");
|
|
|
|
file_.ascend();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
file_.ascend();
|
|
|
|
} 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_);
|
|
|
|
raw_data_ = 0;
|
|
|
|
delete[] buffer_;
|
|
|
|
buffer_ = 0;
|
|
|
|
delete decoder_;
|
|
|
|
decoder_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsOK() { return raw_data_ != 0; }
|
|
|
|
|
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)) {
|
2014-06-22 15:02:04 +00:00
|
|
|
int outBytes;
|
|
|
|
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
|
|
|
|
|
|
|
for (int i = 0; i < outBytes / 2; i++) {
|
|
|
|
bgQueue.push(buffer_[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop!
|
|
|
|
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:
|
|
|
|
ChunkFile file_;
|
|
|
|
uint8_t *raw_data_;
|
|
|
|
int raw_data_size_;
|
|
|
|
int raw_offset_;
|
|
|
|
int raw_bytes_per_frame_;
|
|
|
|
FixedSizeQueue<s16, 128 * 1024> bgQueue;
|
|
|
|
short *buffer_;
|
|
|
|
SimpleAudio *decoder_;
|
|
|
|
};
|
|
|
|
|
2014-06-22 15:26:15 +00:00
|
|
|
static recursive_mutex 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];
|
2014-06-22 15:02:04 +00:00
|
|
|
|
2015-02-28 20:42:32 +00:00
|
|
|
static void ClearBackgroundAudio() {
|
|
|
|
if (at3Reader) {
|
|
|
|
at3Reader->Shutdown();
|
|
|
|
delete at3Reader;
|
|
|
|
at3Reader = 0;
|
|
|
|
}
|
|
|
|
playbackOffset = 0;
|
2015-10-03 21:18:54 +00:00
|
|
|
gameLastChanged = 0;
|
2015-02-28 20:42:32 +00:00
|
|
|
}
|
|
|
|
|
2014-06-22 15:02:04 +00:00
|
|
|
void SetBackgroundAudioGame(const std::string &path) {
|
2014-06-22 15:26:15 +00:00
|
|
|
time_update();
|
|
|
|
|
|
|
|
lock_guard lock(bgMutex);
|
2014-06-22 15:02:04 +00:00
|
|
|
if (path == bgGamePath) {
|
|
|
|
// Do nothing
|
|
|
|
return;
|
|
|
|
}
|
2014-06-22 15:26:15 +00:00
|
|
|
|
2015-02-28 20:42:32 +00:00
|
|
|
ClearBackgroundAudio();
|
2014-06-22 15:02:04 +00:00
|
|
|
gameLastChanged = time_now_d();
|
|
|
|
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
|
|
|
|
|
|
|
lock_guard lock(bgMutex);
|
2015-02-28 20:42:32 +00:00
|
|
|
|
|
|
|
// Immediately stop the sound if it is turned off while playing.
|
|
|
|
if (!g_Config.bEnableSound) {
|
|
|
|
ClearBackgroundAudio();
|
|
|
|
__PushExternalAudio(0, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-22 15:02:04 +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)
|
|
|
|
if (!at3Reader && bgGamePath.size() && (time_now_d() - gameLastChanged > 0.5)) {
|
|
|
|
// Grab some audio from the current game and play it.
|
2014-08-17 19:29:36 +00:00
|
|
|
GameInfo *gameInfo = g_gameInfoCache.GetInfo(NULL, bgGamePath, GAMEINFO_WANTSND);
|
2014-06-22 15:02:04 +00:00
|
|
|
if (!gameInfo)
|
|
|
|
return 0;
|
|
|
|
|
2015-10-03 21:18:54 +00:00
|
|
|
if (gameInfo->pending) {
|
|
|
|
// Should try again shortly..
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-22 15:02:04 +00:00
|
|
|
if (gameInfo->sndFileData.size()) {
|
|
|
|
const std::string &data = gameInfo->sndFileData;
|
|
|
|
at3Reader = new AT3PlusReader(data);
|
2015-01-29 13:58:03 +00:00
|
|
|
lastPlaybackTime = 0.0;
|
2014-06-22 15:02:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-01-29 13:58:03 +00:00
|
|
|
if (at3Reader->Read(buffer, sz))
|
|
|
|
__PushExternalAudio(buffer, sz);
|
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
|
|
|
}
|