2017-01-16 08:40:33 +00:00
|
|
|
#include "CStreamAudioManager.hpp"
|
|
|
|
#include "CStringExtras.hpp"
|
|
|
|
#include "CDvdFile.hpp"
|
|
|
|
#include "CDvdRequest.hpp"
|
|
|
|
#include "Audio/CAudioSys.hpp"
|
|
|
|
#include "amuse/DSPCodec.h"
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace urde
|
|
|
|
{
|
|
|
|
class CDSPStreamManager;
|
|
|
|
|
|
|
|
static u32 s_HandleCounter = 0;
|
|
|
|
static u32 s_HandleCounter2 = 0;
|
|
|
|
|
|
|
|
/* Standard DSPADPCM header */
|
|
|
|
struct dspadpcm_header
|
|
|
|
{
|
|
|
|
u32 x0_num_samples;
|
|
|
|
u32 x4_num_nibbles;
|
|
|
|
u32 x8_sample_rate;
|
|
|
|
u16 xc_loop_flag;
|
|
|
|
u16 xe_format; /* 0 for ADPCM */
|
|
|
|
u32 x10_loop_start_nibble;
|
|
|
|
u32 x14_loop_end_nibble;
|
|
|
|
u32 x18_ca;
|
|
|
|
s16 x1c_coef[8][2];
|
|
|
|
s16 x3c_gain;
|
|
|
|
s16 x3e_ps;
|
|
|
|
s16 x40_hist1;
|
|
|
|
s16 x42_hist2;
|
|
|
|
s16 x44_loop_ps;
|
|
|
|
s16 x46_loop_hist1;
|
|
|
|
s16 x48_loop_hist2;
|
|
|
|
u16 x4a_pad[11];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SDSPStreamInfo
|
|
|
|
{
|
|
|
|
const char* x0_fileName;
|
|
|
|
u32 x4_sampleRate;
|
|
|
|
u32 x8_headerSize = sizeof(dspadpcm_header);
|
|
|
|
u32 xc_adpcmBytes;
|
2018-06-02 00:03:31 +00:00
|
|
|
bool x10_loopFlag;
|
2017-01-16 08:40:33 +00:00
|
|
|
u32 x14_loopStartByte;
|
|
|
|
u32 x18_loopEndByte;
|
|
|
|
s16 x1c_coef[8][2];
|
|
|
|
|
|
|
|
SDSPStreamInfo() = default;
|
|
|
|
SDSPStreamInfo(const CDSPStreamManager& stream);
|
|
|
|
};
|
|
|
|
|
2017-01-18 20:57:48 +00:00
|
|
|
struct SDSPStream : boo::IAudioVoiceCallback
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
bool x0_active;
|
|
|
|
bool x1_oneshot;
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 x4_ownerId;
|
2017-01-16 08:40:33 +00:00
|
|
|
SDSPStream* x8_stereoLeft;
|
2017-01-18 20:57:48 +00:00
|
|
|
SDSPStream* xc_companionRight;
|
2017-01-16 08:40:33 +00:00
|
|
|
SDSPStreamInfo x10_info;
|
2017-01-20 03:53:32 +00:00
|
|
|
float x4c_vol;
|
|
|
|
float m_leftgain, m_rightgain;
|
2017-01-16 08:40:33 +00:00
|
|
|
//DVDFileInfo x50_dvdHandle1;
|
|
|
|
//DVDFileInfo x8c_dvdHandle2;
|
2017-01-18 20:57:48 +00:00
|
|
|
//u32 xc8_streamId = -1; // MusyX stream handle
|
2017-01-16 08:40:33 +00:00
|
|
|
u32 xcc_fileCur = 0;
|
|
|
|
std::unique_ptr<u8[]> xd4_ringBuffer;
|
|
|
|
u32 xd8_ringBytes = 0x11DC0; // 73152 4sec in ADPCM bytes
|
|
|
|
u32 xdc_ringSamples = 0x1f410; // 128016 4sec in samples
|
|
|
|
s8 xe0_curBuffer = -1;
|
2018-06-02 00:03:31 +00:00
|
|
|
bool xe8_silent = true;
|
2017-01-16 08:40:33 +00:00
|
|
|
u8 xec_readState = 0; // 0: NoRead 1: Read 2: ReadWrap
|
|
|
|
|
|
|
|
std::experimental::optional<CDvdFile> m_file;
|
|
|
|
std::shared_ptr<IDvdRequest> m_readReqs[2];
|
|
|
|
|
|
|
|
void ReadBuffer(int buf)
|
2017-01-18 20:57:48 +00:00
|
|
|
{
|
2017-01-16 08:40:33 +00:00
|
|
|
u32 halfSize = xd8_ringBytes / 2;
|
|
|
|
u8* data = xd4_ringBuffer.get() + (buf ? halfSize : 0);
|
|
|
|
|
2017-01-18 20:57:48 +00:00
|
|
|
if (x10_info.x10_loopFlag)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2017-01-18 20:57:48 +00:00
|
|
|
u32 remFileBytes = x10_info.x18_loopEndByte - xcc_fileCur;
|
2017-01-16 08:40:33 +00:00
|
|
|
|
2017-01-18 20:57:48 +00:00
|
|
|
if (remFileBytes < halfSize)
|
|
|
|
{
|
2017-01-19 02:29:49 +00:00
|
|
|
//printf("Buffering %d from %d into %d\n", remFileBytes, xcc_fileCur + x10_info.x8_headerSize, buf);
|
2017-01-18 20:57:48 +00:00
|
|
|
m_file->AsyncSeekRead(data, remFileBytes,
|
|
|
|
ESeekOrigin::Begin, xcc_fileCur + x10_info.x8_headerSize);
|
2017-01-16 08:40:33 +00:00
|
|
|
xcc_fileCur = x10_info.x14_loopStartByte;
|
2017-01-18 20:57:48 +00:00
|
|
|
u32 remBytes = halfSize - remFileBytes;
|
2017-01-19 02:29:49 +00:00
|
|
|
//printf("Loop Buffering %d from %d into %d\n", remBytes, xcc_fileCur + x10_info.x8_headerSize, buf);
|
2017-01-18 20:57:48 +00:00
|
|
|
m_readReqs[buf] = m_file->AsyncSeekRead(data + remFileBytes, remBytes,
|
|
|
|
ESeekOrigin::Begin, xcc_fileCur + x10_info.x8_headerSize);
|
|
|
|
xcc_fileCur += remBytes;
|
|
|
|
}
|
2017-01-16 08:40:33 +00:00
|
|
|
else
|
2017-01-18 20:57:48 +00:00
|
|
|
{
|
2017-01-19 02:29:49 +00:00
|
|
|
//printf("Buffering %d from %d into %d\n", halfSize, xcc_fileCur + x10_info.x8_headerSize, buf);
|
2017-01-18 20:57:48 +00:00
|
|
|
m_readReqs[buf] = m_file->AsyncSeekRead(data, halfSize,
|
|
|
|
ESeekOrigin::Begin, xcc_fileCur + x10_info.x8_headerSize);
|
|
|
|
xcc_fileCur += halfSize;
|
|
|
|
}
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-18 20:57:48 +00:00
|
|
|
if (xcc_fileCur == x10_info.xc_adpcmBytes)
|
|
|
|
{
|
|
|
|
memset(data, 0, halfSize);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 remFileBytes = x10_info.xc_adpcmBytes - xcc_fileCur;
|
|
|
|
|
|
|
|
if (remFileBytes < halfSize)
|
|
|
|
{
|
2017-01-19 02:29:49 +00:00
|
|
|
//printf("Buffering %d from %d into %d\n", remFileBytes, xcc_fileCur + x10_info.x8_headerSize, buf);
|
2017-01-18 20:57:48 +00:00
|
|
|
m_readReqs[buf] = m_file->AsyncSeekRead(data, remFileBytes,
|
|
|
|
ESeekOrigin::Begin, xcc_fileCur + x10_info.x8_headerSize);
|
|
|
|
memset(data + remFileBytes, 0, halfSize - remFileBytes);
|
|
|
|
xcc_fileCur = x10_info.xc_adpcmBytes;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-19 02:29:49 +00:00
|
|
|
//printf("Buffering %d from %d into %d\n", halfSize, xcc_fileCur + x10_info.x8_headerSize, buf);
|
2017-01-18 20:57:48 +00:00
|
|
|
m_readReqs[buf] = m_file->AsyncSeekRead(data, halfSize,
|
|
|
|
ESeekOrigin::Begin, xcc_fileCur + x10_info.x8_headerSize);
|
|
|
|
xcc_fileCur += halfSize;
|
|
|
|
}
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BufferStream()
|
|
|
|
{
|
|
|
|
if (xec_readState == 0)
|
|
|
|
{
|
|
|
|
ReadBuffer(0);
|
|
|
|
ReadBuffer(1);
|
|
|
|
xec_readState = 1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (xec_readState == 1)
|
|
|
|
{
|
|
|
|
if (m_readReqs[0]->IsComplete())
|
|
|
|
{
|
|
|
|
xe0_curBuffer = 0;
|
|
|
|
xec_readState = 2;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (xec_readState == 2)
|
|
|
|
{
|
|
|
|
if (xe0_curBuffer == 1 && m_readReqs[0]->IsComplete())
|
|
|
|
{
|
|
|
|
xe0_curBuffer = 0;
|
|
|
|
ReadBuffer(1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (xe0_curBuffer == 0 && m_readReqs[1]->IsComplete())
|
|
|
|
{
|
|
|
|
xe0_curBuffer = 1;
|
|
|
|
ReadBuffer(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
unsigned m_curSample = 0;
|
|
|
|
unsigned m_totalSamples = 0;
|
2017-01-18 20:57:48 +00:00
|
|
|
s16 m_prev1 = 0;
|
|
|
|
s16 m_prev2 = 0;
|
|
|
|
|
|
|
|
void preSupplyAudio(boo::IAudioVoice&, double) {}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
unsigned decompressChunk(unsigned readToSample, int16_t*& data)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
unsigned startSamp = m_curSample;
|
|
|
|
|
|
|
|
auto sampDiv = std::div(int(m_curSample), int(14));
|
2017-01-18 20:57:48 +00:00
|
|
|
if (sampDiv.rem)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2017-01-18 20:57:48 +00:00
|
|
|
unsigned samps = DSPDecompressFrameRanged(data, xd4_ringBuffer.get() + sampDiv.quot * 8,
|
2018-06-02 00:03:31 +00:00
|
|
|
x10_info.x1c_coef, &m_prev1, &m_prev2, unsigned(sampDiv.rem),
|
2017-01-18 20:57:48 +00:00
|
|
|
readToSample - m_curSample);
|
|
|
|
m_curSample += samps;
|
|
|
|
data += samps;
|
|
|
|
++sampDiv.quot;
|
|
|
|
}
|
2017-01-16 08:40:33 +00:00
|
|
|
|
2017-01-18 20:57:48 +00:00
|
|
|
while (m_curSample < readToSample)
|
|
|
|
{
|
|
|
|
unsigned samps = DSPDecompressFrame(data, xd4_ringBuffer.get() + sampDiv.quot * 8,
|
|
|
|
x10_info.x1c_coef, &m_prev1, &m_prev2,
|
|
|
|
readToSample - m_curSample);
|
|
|
|
m_curSample += samps;
|
|
|
|
data += samps;
|
|
|
|
++sampDiv.quot;
|
|
|
|
}
|
2018-06-02 00:03:31 +00:00
|
|
|
|
|
|
|
return m_curSample - startSamp;
|
2017-01-18 20:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t supplyAudio(boo::IAudioVoice&, size_t frames, int16_t* data)
|
|
|
|
{
|
2017-11-28 02:48:24 +00:00
|
|
|
if (!x0_active)
|
|
|
|
{
|
|
|
|
memset(data, 0, frames * 2);
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
2017-01-18 20:57:48 +00:00
|
|
|
if (xe8_silent)
|
|
|
|
{
|
|
|
|
StopStream();
|
|
|
|
memset(data, 0, frames * 2);
|
|
|
|
return frames;
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
unsigned halfRingSamples = xdc_ringSamples / 2;
|
|
|
|
|
|
|
|
size_t remFrames = frames;
|
|
|
|
while (remFrames)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
if (xec_readState != 2 || (xe0_curBuffer == 0 && m_curSample >= halfRingSamples))
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
if (!BufferStream())
|
|
|
|
{
|
|
|
|
memset(data, 0, remFrames * 2);
|
|
|
|
return frames;
|
|
|
|
}
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
unsigned readToSample = std::min(m_curSample + unsigned(remFrames),
|
|
|
|
(m_curSample / halfRingSamples + 1) * halfRingSamples);
|
|
|
|
|
|
|
|
if (!x10_info.x10_loopFlag)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
m_totalSamples += remFrames;
|
|
|
|
size_t fileSamples = x10_info.xc_adpcmBytes * 14 / 8;
|
|
|
|
if (m_totalSamples >= fileSamples)
|
|
|
|
{
|
|
|
|
size_t leftover = m_totalSamples - fileSamples;
|
|
|
|
readToSample -= leftover;
|
|
|
|
remFrames -= leftover;
|
|
|
|
memset(data + remFrames, 0, leftover * 2);
|
|
|
|
StopStream();
|
|
|
|
}
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
unsigned leftoverSamples = 0;
|
|
|
|
if (readToSample > xdc_ringSamples)
|
|
|
|
{
|
|
|
|
leftoverSamples = readToSample - xdc_ringSamples;
|
|
|
|
readToSample = xdc_ringSamples;
|
|
|
|
}
|
2017-01-16 08:40:33 +00:00
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
remFrames -= decompressChunk(readToSample, data);
|
2017-01-16 08:40:33 +00:00
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
if (leftoverSamples)
|
|
|
|
{
|
|
|
|
BufferStream();
|
|
|
|
m_curSample = 0;
|
|
|
|
remFrames -= decompressChunk(leftoverSamples, data);
|
|
|
|
}
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
2017-01-18 20:57:48 +00:00
|
|
|
|
|
|
|
return frames;
|
|
|
|
}
|
2017-12-04 03:21:35 +00:00
|
|
|
boo::ObjToken<boo::IAudioVoice> m_booVoice;
|
2017-01-16 08:40:33 +00:00
|
|
|
|
|
|
|
void DoAllocateStream()
|
|
|
|
{
|
|
|
|
xd4_ringBuffer.reset(new u8[0x11DC0]);
|
2017-01-18 20:57:48 +00:00
|
|
|
m_booVoice = CAudioSys::GetVoiceEngine()->allocateNewMonoVoice(32000.0, this);
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2017-01-16 09:14:54 +00:00
|
|
|
static void Initialize()
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
for (int i=0 ; i<4 ; ++i)
|
|
|
|
{
|
|
|
|
SDSPStream& stream = g_Streams[i];
|
|
|
|
stream.x0_active = false;
|
|
|
|
stream.xd4_ringBuffer.reset();
|
|
|
|
stream.xd8_ringBytes = 0x11DC0;
|
|
|
|
stream.xdc_ringSamples = 0x1f410;
|
|
|
|
if (i < 2)
|
|
|
|
{
|
|
|
|
stream.x1_oneshot = false;
|
|
|
|
stream.DoAllocateStream();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stream.x1_oneshot = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-22 01:40:12 +00:00
|
|
|
static void FreeAllStreams()
|
|
|
|
{
|
|
|
|
for (int i=0 ; i<4 ; ++i)
|
|
|
|
{
|
|
|
|
SDSPStream& stream = g_Streams[i];
|
|
|
|
stream.m_booVoice.reset();
|
|
|
|
stream.x0_active = false;
|
2018-06-02 00:03:31 +00:00
|
|
|
for (int j=0 ; j<2 ; ++j)
|
|
|
|
if (stream.m_readReqs[j])
|
|
|
|
{
|
|
|
|
stream.m_readReqs[j]->PostCancelRequest();
|
|
|
|
stream.m_readReqs[j].reset();
|
|
|
|
}
|
2017-01-22 01:40:12 +00:00
|
|
|
stream.xd4_ringBuffer.reset();
|
2017-11-27 05:06:53 +00:00
|
|
|
stream.m_file = std::experimental::nullopt;
|
2017-01-22 01:40:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static s32 PickFreeStream(SDSPStream*& streamOut, bool oneshot)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
for (int i=0 ; i<4 ; ++i)
|
|
|
|
{
|
|
|
|
SDSPStream& stream = g_Streams[i];
|
|
|
|
if (stream.x0_active || stream.x1_oneshot != oneshot)
|
|
|
|
continue;
|
|
|
|
stream.x0_active = true;
|
|
|
|
stream.x4_ownerId = ++s_HandleCounter2;
|
2017-01-17 01:23:19 +00:00
|
|
|
if (stream.x4_ownerId == -1)
|
|
|
|
stream.x4_ownerId = ++s_HandleCounter2;
|
2017-01-16 08:40:33 +00:00
|
|
|
stream.x8_stereoLeft = nullptr;
|
2017-01-18 20:57:48 +00:00
|
|
|
stream.xc_companionRight = nullptr;
|
2017-01-16 08:40:33 +00:00
|
|
|
streamOut = &stream;
|
|
|
|
return stream.x4_ownerId;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static s32 FindStreamIdx(s32 id)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
for (s32 i=0 ; i<4 ; ++i)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
SDSPStream& stream = g_Streams[i];
|
|
|
|
if (stream.x4_ownerId == id)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-01-20 03:53:32 +00:00
|
|
|
void UpdateStreamVolume(float vol)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
x4c_vol = vol;
|
|
|
|
if (!x0_active || xe8_silent)
|
|
|
|
return;
|
|
|
|
float coefs[8] = {};
|
2018-06-02 00:03:31 +00:00
|
|
|
coefs[int(boo::AudioChannel::FrontLeft)] = m_leftgain * vol;
|
|
|
|
coefs[int(boo::AudioChannel::FrontRight)] = m_rightgain * vol;
|
2017-01-16 08:40:33 +00:00
|
|
|
m_booVoice->setMonoChannelLevels(nullptr, coefs, true);
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static void UpdateVolume(s32 id, float vol)
|
2017-01-16 09:14:54 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 idx = FindStreamIdx(id);
|
2017-01-16 09:14:54 +00:00
|
|
|
if (idx == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SDSPStream& stream = g_Streams[idx];
|
|
|
|
stream.UpdateStreamVolume(vol);
|
|
|
|
if (SDSPStream* left = stream.x8_stereoLeft)
|
|
|
|
left->UpdateStreamVolume(vol);
|
2017-01-18 20:57:48 +00:00
|
|
|
if (SDSPStream* right = stream.xc_companionRight)
|
2017-01-16 09:14:54 +00:00
|
|
|
right->UpdateStreamVolume(vol);
|
|
|
|
}
|
|
|
|
|
2017-01-16 08:40:33 +00:00
|
|
|
void SilenceStream()
|
|
|
|
{
|
|
|
|
if (!x0_active || xe8_silent)
|
|
|
|
return;
|
|
|
|
float coefs[8] = {};
|
|
|
|
m_booVoice->setMonoChannelLevels(nullptr, coefs, true);
|
|
|
|
xe8_silent = true;
|
2017-01-19 02:29:49 +00:00
|
|
|
x0_active = false;
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static void Silence(s32 id)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 idx = FindStreamIdx(id);
|
2017-01-16 08:40:33 +00:00
|
|
|
if (idx == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SDSPStream& stream = g_Streams[idx];
|
|
|
|
stream.SilenceStream();
|
|
|
|
if (SDSPStream* left = stream.x8_stereoLeft)
|
|
|
|
left->SilenceStream();
|
2017-01-18 20:57:48 +00:00
|
|
|
if (SDSPStream* right = stream.xc_companionRight)
|
2017-01-16 08:40:33 +00:00
|
|
|
right->SilenceStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StopStream()
|
|
|
|
{
|
2017-01-16 09:14:54 +00:00
|
|
|
x0_active = false;
|
2017-01-16 08:40:33 +00:00
|
|
|
m_booVoice->stop();
|
2017-01-16 09:14:54 +00:00
|
|
|
m_file = std::experimental::nullopt;
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static bool IsStreamActive(s32 id)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 idx = FindStreamIdx(id);
|
2017-01-16 08:40:33 +00:00
|
|
|
if (idx == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SDSPStream& stream = g_Streams[idx];
|
|
|
|
return stream.x0_active;
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static bool IsStreamAvailable(s32 id)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 idx = FindStreamIdx(id);
|
2017-01-16 08:40:33 +00:00
|
|
|
if (idx == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SDSPStream& stream = g_Streams[idx];
|
|
|
|
return !stream.x0_active;
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static s32 AllocateMono(const SDSPStreamInfo& info, float vol, bool oneshot)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
SDSPStream* stream;
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 id = PickFreeStream(stream, oneshot);
|
2017-01-16 08:40:33 +00:00
|
|
|
if (id == -1)
|
|
|
|
return -1;
|
|
|
|
|
2017-01-22 21:26:58 +00:00
|
|
|
/* -3dB pan law for mono */
|
2017-01-20 03:53:32 +00:00
|
|
|
stream->AllocateStream(info, vol, 0.707f, 0.707f);
|
2017-01-16 08:40:33 +00:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static s32 AllocateStereo(const SDSPStreamInfo& linfo,
|
2017-01-16 08:40:33 +00:00
|
|
|
const SDSPStreamInfo& rinfo,
|
2017-01-20 03:53:32 +00:00
|
|
|
float vol, bool oneshot)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
SDSPStream* lstream;
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 lid = PickFreeStream(lstream, oneshot);
|
2017-01-16 08:40:33 +00:00
|
|
|
if (lid == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
SDSPStream* rstream;
|
|
|
|
if (PickFreeStream(rstream, oneshot) == -1)
|
|
|
|
return -1;
|
|
|
|
|
2017-01-18 20:57:48 +00:00
|
|
|
rstream->x8_stereoLeft = lstream;
|
|
|
|
lstream->xc_companionRight = rstream;
|
|
|
|
|
2017-01-20 03:53:32 +00:00
|
|
|
lstream->AllocateStream(linfo, vol, 1.f, 0.f);
|
|
|
|
rstream->AllocateStream(rinfo, vol, 0.f, 1.f);
|
2017-01-16 08:40:33 +00:00
|
|
|
return lid;
|
|
|
|
}
|
|
|
|
|
2017-01-20 03:53:32 +00:00
|
|
|
void AllocateStream(const SDSPStreamInfo& info, float vol, float left, float right)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
x10_info = info;
|
|
|
|
m_file.emplace(x10_info.x0_fileName);
|
|
|
|
if (!xd4_ringBuffer)
|
|
|
|
DoAllocateStream();
|
2018-06-02 00:03:31 +00:00
|
|
|
for (int j=0 ; j<2 ; ++j)
|
|
|
|
if (m_readReqs[j])
|
|
|
|
{
|
|
|
|
m_readReqs[j]->PostCancelRequest();
|
|
|
|
m_readReqs[j].reset();
|
|
|
|
}
|
2017-01-16 08:40:33 +00:00
|
|
|
x4c_vol = vol;
|
2017-01-20 03:53:32 +00:00
|
|
|
m_leftgain = left;
|
|
|
|
m_rightgain = right;
|
2017-01-16 08:40:33 +00:00
|
|
|
xe8_silent = false;
|
|
|
|
xec_readState = 0;
|
|
|
|
xe0_curBuffer = -1;
|
|
|
|
xd8_ringBytes = 0x11DC0;
|
|
|
|
xdc_ringSamples = 0x1f410;
|
2017-01-18 20:57:48 +00:00
|
|
|
xcc_fileCur = 0;
|
|
|
|
m_curSample = 0;
|
|
|
|
m_totalSamples = 0;
|
|
|
|
m_prev1 = 0;
|
|
|
|
m_prev2 = 0;
|
2017-01-16 08:40:33 +00:00
|
|
|
memset(xd4_ringBuffer.get(), 0, 0x11DC0);
|
2017-01-17 01:23:19 +00:00
|
|
|
m_booVoice->resetSampleRate(info.x4_sampleRate);
|
2017-01-16 08:40:33 +00:00
|
|
|
m_booVoice->start();
|
2017-01-18 20:57:48 +00:00
|
|
|
UpdateStreamVolume(vol);
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static SDSPStream g_Streams[4];
|
|
|
|
};
|
|
|
|
|
|
|
|
SDSPStream SDSPStream::g_Streams[4] = {};
|
|
|
|
|
|
|
|
class CDSPStreamManager
|
|
|
|
{
|
|
|
|
friend struct SDSPStreamInfo;
|
|
|
|
public:
|
|
|
|
enum class EState
|
|
|
|
{
|
2017-01-16 09:14:54 +00:00
|
|
|
Looping,
|
|
|
|
Oneshot,
|
|
|
|
Preparing
|
2017-01-16 08:40:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
dspadpcm_header x0_header;
|
|
|
|
std::string x60_fileName; // arg1
|
|
|
|
union
|
|
|
|
{
|
2018-06-02 06:06:25 +00:00
|
|
|
u32 dummy = 0;
|
2017-01-16 08:40:33 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
bool x70_24_unclaimed : 1;
|
|
|
|
bool x70_25_headerReadCancelled : 1;
|
|
|
|
u8 x70_26_headerReadState : 2; // 0: not read 1: reading 2: read
|
|
|
|
};
|
|
|
|
};
|
|
|
|
s8 x71_companionRight = -1;
|
|
|
|
s8 x72_companionLeft = -1;
|
2017-01-20 03:53:32 +00:00
|
|
|
float x73_volume = 0.f;
|
2017-01-16 08:40:33 +00:00
|
|
|
bool x74_oneshot;
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 x78_handleId = -1; // arg2
|
|
|
|
s32 x7c_streamId = -1;
|
2017-01-16 08:40:33 +00:00
|
|
|
std::shared_ptr<IDvdRequest> m_dvdReq;
|
|
|
|
//DVDFileInfo x80_dvdHandle;
|
|
|
|
static CDSPStreamManager g_Streams[4];
|
|
|
|
|
|
|
|
public:
|
|
|
|
CDSPStreamManager()
|
|
|
|
{
|
|
|
|
x70_24_unclaimed = true;
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
CDSPStreamManager(std::string_view fileName, s32 handle, float volume, bool oneshot)
|
2017-01-16 08:40:33 +00:00
|
|
|
: x60_fileName(fileName), x73_volume(volume), x74_oneshot(oneshot), x78_handleId(handle)
|
|
|
|
{
|
2017-11-13 06:19:18 +00:00
|
|
|
if (!CDvdFile::FileExists(fileName))
|
2017-01-16 08:40:33 +00:00
|
|
|
x70_24_unclaimed = true;
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static s32 FindUnclaimedStreamIdx()
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
for (s32 i=0 ; i<4 ; ++i)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
CDSPStreamManager& stream = g_Streams[i];
|
|
|
|
if (stream.x70_24_unclaimed)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static bool FindUnclaimedStereoPair(s32& left, s32& right)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 idx = FindUnclaimedStreamIdx();
|
2017-01-16 08:40:33 +00:00
|
|
|
|
|
|
|
for (u32 i=0 ; i<4 ; ++i)
|
|
|
|
{
|
|
|
|
CDSPStreamManager& stream = g_Streams[i];
|
|
|
|
if (stream.x70_24_unclaimed && idx != i)
|
|
|
|
{
|
|
|
|
left = idx;
|
|
|
|
right = i;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static s32 FindClaimedStreamIdx(s32 handle)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
for (s32 i=0 ; i<4 ; ++i)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
CDSPStreamManager& stream = g_Streams[i];
|
|
|
|
if (!stream.x70_24_unclaimed && stream.x78_handleId == handle)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static s32 GetFreeHandleId()
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 handle;
|
2017-01-16 08:40:33 +00:00
|
|
|
bool good;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
good = true;
|
|
|
|
handle = ++s_HandleCounter;
|
|
|
|
if (handle == -1)
|
|
|
|
{
|
|
|
|
good = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0 ; i<4 ; ++i)
|
|
|
|
{
|
|
|
|
CDSPStreamManager& stream = g_Streams[i];
|
|
|
|
if (!stream.x70_24_unclaimed && stream.x78_handleId == handle)
|
|
|
|
{
|
|
|
|
good = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} while(!good);
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static EState GetStreamState(s32 handle)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 idx = FindClaimedStreamIdx(handle);
|
2017-01-16 08:40:33 +00:00
|
|
|
if (idx == -1)
|
2017-01-16 09:14:54 +00:00
|
|
|
return EState::Oneshot;
|
2017-01-16 08:40:33 +00:00
|
|
|
|
|
|
|
CDSPStreamManager& stream = g_Streams[idx];
|
|
|
|
switch (stream.x70_26_headerReadState)
|
|
|
|
{
|
|
|
|
case 0:
|
2017-01-16 09:14:54 +00:00
|
|
|
return EState::Oneshot;
|
2017-01-16 08:40:33 +00:00
|
|
|
case 2:
|
|
|
|
return EState(!stream.x0_header.xc_loop_flag);
|
|
|
|
default:
|
2017-01-16 09:14:54 +00:00
|
|
|
return EState::Preparing;
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static bool CanStop(s32 handle)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 idx = FindClaimedStreamIdx(handle);
|
2017-01-16 08:40:33 +00:00
|
|
|
if (idx == -1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
CDSPStreamManager& stream = g_Streams[idx];
|
|
|
|
if (stream.x70_26_headerReadState == 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (stream.x7c_streamId == -1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return !SDSPStream::IsStreamActive(stream.x7c_streamId);
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static bool IsStreamAvailable(s32 handle)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 idx = FindClaimedStreamIdx(handle);
|
2017-01-16 08:40:33 +00:00
|
|
|
if (idx == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CDSPStreamManager& stream = g_Streams[idx];
|
|
|
|
if (stream.x70_26_headerReadState == 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (stream.x7c_streamId == -1)
|
2017-01-18 20:57:48 +00:00
|
|
|
return false;
|
2017-01-16 08:40:33 +00:00
|
|
|
|
|
|
|
return SDSPStream::IsStreamAvailable(stream.x7c_streamId);
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static void AllocateStream(s32 idx)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
CDSPStreamManager& stream = g_Streams[idx];
|
|
|
|
SDSPStreamInfo info(stream);
|
|
|
|
|
|
|
|
if (stream.x71_companionRight == -1)
|
|
|
|
{
|
|
|
|
/* Mono */
|
|
|
|
if (!stream.x70_25_headerReadCancelled)
|
2017-01-20 03:53:32 +00:00
|
|
|
stream.x7c_streamId = SDSPStream::AllocateMono(info, stream.x73_volume, stream.x74_oneshot);
|
2017-01-16 08:40:33 +00:00
|
|
|
if (stream.x7c_streamId == -1)
|
|
|
|
stream = CDSPStreamManager();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Stereo */
|
|
|
|
CDSPStreamManager& rstream = g_Streams[stream.x71_companionRight];
|
|
|
|
SDSPStreamInfo rinfo(rstream);
|
|
|
|
|
|
|
|
if (!stream.x70_25_headerReadCancelled)
|
|
|
|
stream.x7c_streamId = SDSPStream::AllocateStereo(info, rinfo, stream.x73_volume, stream.x74_oneshot);
|
|
|
|
if (stream.x7c_streamId == -1)
|
|
|
|
{
|
|
|
|
stream = CDSPStreamManager();
|
|
|
|
rstream = CDSPStreamManager();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 20:57:48 +00:00
|
|
|
void HeaderReadComplete()
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 selfIdx = -1;
|
2017-01-16 08:40:33 +00:00
|
|
|
for (int i=0 ; i<4 ; ++i)
|
|
|
|
{
|
|
|
|
if (this == &g_Streams[i])
|
|
|
|
{
|
|
|
|
selfIdx = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 20:57:48 +00:00
|
|
|
if (x70_24_unclaimed || selfIdx == -1)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
*this = CDSPStreamManager();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
x70_26_headerReadState = 2;
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 companion = -1;
|
2017-01-16 08:40:33 +00:00
|
|
|
if (x72_companionLeft != -1)
|
|
|
|
companion = x72_companionLeft;
|
|
|
|
else if (x71_companionRight != -1)
|
|
|
|
companion = x71_companionRight;
|
|
|
|
|
|
|
|
if (companion != -1)
|
|
|
|
{
|
|
|
|
/* Stereo */
|
2017-01-16 09:14:54 +00:00
|
|
|
CDSPStreamManager& companionStream = g_Streams[companion];
|
|
|
|
if (companionStream.x70_24_unclaimed ||
|
|
|
|
companionStream.x70_26_headerReadState == 0 ||
|
|
|
|
(companionStream.x71_companionRight != selfIdx &&
|
|
|
|
companionStream.x72_companionLeft != selfIdx))
|
|
|
|
{
|
2017-01-17 01:23:19 +00:00
|
|
|
/* No consistent companion available */
|
2017-01-16 09:14:54 +00:00
|
|
|
*this = CDSPStreamManager();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-17 01:23:19 +00:00
|
|
|
/* Companion is pending; its completion will continue */
|
2017-01-16 09:14:54 +00:00
|
|
|
if (companionStream.x70_26_headerReadState == 1)
|
|
|
|
return;
|
|
|
|
|
2017-01-17 01:23:19 +00:00
|
|
|
/* Use whichever stream is the left channel */
|
2017-01-16 09:14:54 +00:00
|
|
|
if (companionStream.x71_companionRight != -1)
|
|
|
|
AllocateStream(companion);
|
|
|
|
else
|
|
|
|
AllocateStream(selfIdx);
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Mono */
|
|
|
|
AllocateStream(selfIdx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 20:57:48 +00:00
|
|
|
static void PollHeaderReadCompletions()
|
|
|
|
{
|
|
|
|
for (int i=0 ; i<4 ; ++i)
|
|
|
|
{
|
|
|
|
CDSPStreamManager& stream = g_Streams[i];
|
|
|
|
if (stream.m_dvdReq && stream.m_dvdReq->IsComplete())
|
|
|
|
{
|
|
|
|
stream.m_dvdReq.reset();
|
|
|
|
stream.HeaderReadComplete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool StartMonoHeaderRead(CDSPStreamManager& stream)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
if (stream.x70_26_headerReadState != 0 || stream.x70_24_unclaimed)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CDvdFile file(stream.x60_fileName.c_str());
|
|
|
|
if (!file)
|
|
|
|
return false;
|
|
|
|
|
2017-01-17 01:23:19 +00:00
|
|
|
stream.x70_26_headerReadState = 1;
|
2017-01-18 20:57:48 +00:00
|
|
|
stream.m_dvdReq = file.AsyncRead(&stream.x0_header, sizeof(dspadpcm_header));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool StartStereoHeaderRead(CDSPStreamManager& lstream, CDSPStreamManager& rstream)
|
|
|
|
{
|
|
|
|
if (lstream.x70_26_headerReadState != 0 || lstream.x70_24_unclaimed ||
|
|
|
|
rstream.x70_26_headerReadState != 0 || rstream.x70_24_unclaimed)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CDvdFile lfile(lstream.x60_fileName.c_str());
|
|
|
|
if (!lfile)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
CDvdFile rfile(rstream.x60_fileName.c_str());
|
|
|
|
if (!rfile)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
lstream.x70_26_headerReadState = 1;
|
|
|
|
rstream.x70_26_headerReadState = 1;
|
|
|
|
|
|
|
|
lstream.m_dvdReq = lfile.AsyncRead(&lstream.x0_header, sizeof(dspadpcm_header));
|
|
|
|
rstream.m_dvdReq = rfile.AsyncRead(&rstream.x0_header, sizeof(dspadpcm_header));
|
2017-01-16 08:40:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitForReadCompletion()
|
|
|
|
{
|
|
|
|
if (std::shared_ptr<IDvdRequest> req = m_dvdReq)
|
|
|
|
req->WaitUntilComplete();
|
|
|
|
m_dvdReq.reset();
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static s32 StartStreaming(std::string_view fileName, float volume, bool oneshot)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
auto pipePos = fileName.find('|');
|
|
|
|
if (pipePos == std::string::npos)
|
|
|
|
{
|
|
|
|
/* Mono stream */
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 idx = FindUnclaimedStreamIdx();
|
2017-01-16 08:40:33 +00:00
|
|
|
if (idx == -1)
|
|
|
|
return -1;
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 handle = GetFreeHandleId();
|
2017-01-16 08:40:33 +00:00
|
|
|
CDSPStreamManager tmpStream(fileName, handle, volume, oneshot);
|
|
|
|
if (tmpStream.x70_24_unclaimed)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
CDSPStreamManager& stream = g_Streams[idx];
|
|
|
|
stream = tmpStream;
|
|
|
|
|
2017-01-18 20:57:48 +00:00
|
|
|
if (!StartMonoHeaderRead(stream))
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
stream.x70_25_headerReadCancelled = true;
|
|
|
|
stream.WaitForReadCompletion();
|
|
|
|
stream = CDSPStreamManager();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Stereo stream */
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 leftIdx = 0;
|
|
|
|
s32 rightIdx = 0;
|
2017-01-16 08:40:33 +00:00
|
|
|
if (!FindUnclaimedStereoPair(leftIdx, rightIdx))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
std::string leftFile(fileName.begin(), fileName.begin() + pipePos);
|
|
|
|
std::string rightFile(fileName.begin() + pipePos + 1, fileName.end());
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 leftHandle = GetFreeHandleId();
|
|
|
|
s32 rightHandle = GetFreeHandleId();
|
2017-01-16 08:40:33 +00:00
|
|
|
CDSPStreamManager tmpLeftStream(leftFile, leftHandle, volume, oneshot);
|
|
|
|
CDSPStreamManager tmpRightStream(rightFile, rightHandle, volume, oneshot);
|
|
|
|
if (tmpLeftStream.x70_24_unclaimed || tmpRightStream.x70_24_unclaimed)
|
|
|
|
return -1;
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
tmpLeftStream.x71_companionRight = s8(rightIdx);
|
|
|
|
tmpRightStream.x72_companionLeft = s8(leftIdx);
|
2017-01-16 08:40:33 +00:00
|
|
|
|
|
|
|
CDSPStreamManager& leftStream = g_Streams[leftIdx];
|
|
|
|
CDSPStreamManager& rightStream = g_Streams[rightIdx];
|
|
|
|
leftStream = tmpLeftStream;
|
|
|
|
rightStream = tmpRightStream;
|
|
|
|
|
2017-01-18 20:57:48 +00:00
|
|
|
if (!StartStereoHeaderRead(leftStream, rightStream))
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
leftStream.x70_25_headerReadCancelled = true;
|
|
|
|
rightStream.x70_25_headerReadCancelled = true;
|
|
|
|
leftStream.WaitForReadCompletion();
|
|
|
|
leftStream.WaitForReadCompletion();
|
|
|
|
leftStream = CDSPStreamManager();
|
|
|
|
rightStream = CDSPStreamManager();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return leftHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static void StopStreaming(s32 handle)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 idx = FindClaimedStreamIdx(handle);
|
2017-01-16 08:40:33 +00:00
|
|
|
if (idx == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CDSPStreamManager& stream = g_Streams[idx];
|
|
|
|
if (stream.x70_24_unclaimed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (stream.x70_26_headerReadState == 1)
|
|
|
|
{
|
|
|
|
stream.x70_25_headerReadCancelled = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream.x71_companionRight != -1)
|
|
|
|
g_Streams[stream.x71_companionRight] = CDSPStreamManager();
|
|
|
|
|
|
|
|
SDSPStream::Silence(stream.x7c_streamId);
|
|
|
|
|
|
|
|
stream = CDSPStreamManager();
|
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
static void UpdateVolume(s32 handle, float volume)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 idx = FindClaimedStreamIdx(handle);
|
2017-01-16 09:14:54 +00:00
|
|
|
if (idx == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CDSPStreamManager& stream = g_Streams[idx];
|
|
|
|
stream.x73_volume = volume;
|
|
|
|
if (stream.x7c_streamId == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SDSPStream::UpdateVolume(stream.x7c_streamId, volume);
|
|
|
|
}
|
2017-01-16 08:40:33 +00:00
|
|
|
|
2017-01-16 09:14:54 +00:00
|
|
|
static void Initialize()
|
|
|
|
{
|
|
|
|
SDSPStream::Initialize();
|
|
|
|
for (int i=0 ; i<4 ; ++i)
|
|
|
|
{
|
|
|
|
CDSPStreamManager& stream = g_Streams[i];
|
|
|
|
stream = CDSPStreamManager();
|
|
|
|
}
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
2017-01-22 01:40:12 +00:00
|
|
|
|
|
|
|
static void Shutdown()
|
|
|
|
{
|
|
|
|
SDSPStream::FreeAllStreams();
|
|
|
|
for (int i=0 ; i<4 ; ++i)
|
|
|
|
{
|
|
|
|
CDSPStreamManager& stream = g_Streams[i];
|
|
|
|
stream = CDSPStreamManager();
|
|
|
|
}
|
|
|
|
}
|
2017-01-16 08:40:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CDSPStreamManager CDSPStreamManager::g_Streams[4] = {};
|
|
|
|
|
|
|
|
|
|
|
|
SDSPStreamInfo::SDSPStreamInfo(const CDSPStreamManager& stream)
|
|
|
|
{
|
|
|
|
x0_fileName = stream.x60_fileName.c_str();
|
|
|
|
x4_sampleRate = hecl::SBig(stream.x0_header.x8_sample_rate);
|
|
|
|
xc_adpcmBytes = (hecl::SBig(stream.x0_header.x4_num_nibbles) / 2) & 0x7FFFFFE0;
|
|
|
|
|
|
|
|
if (stream.x0_header.xc_loop_flag)
|
|
|
|
{
|
|
|
|
u32 loopStartNibble = hecl::SBig(stream.x0_header.x10_loop_start_nibble);
|
|
|
|
u32 loopEndNibble = hecl::SBig(stream.x0_header.x14_loop_end_nibble);
|
|
|
|
x10_loopFlag = true;
|
|
|
|
x14_loopStartByte = (loopStartNibble / 2) & 0x7FFFFFE0;
|
|
|
|
x18_loopEndByte = std::min((loopEndNibble / 2) & 0x7FFFFFE0, xc_adpcmBytes);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
x10_loopFlag = false;
|
|
|
|
x14_loopStartByte = 0;
|
|
|
|
x18_loopEndByte = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0 ; i<8 ; ++i)
|
|
|
|
{
|
|
|
|
x1c_coef[i][0] = hecl::SBig(stream.x0_header.x1c_coef[i][0]);
|
|
|
|
x1c_coef[i][1] = hecl::SBig(stream.x0_header.x1c_coef[i][1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class EPlayerState
|
|
|
|
{
|
|
|
|
Stopped,
|
|
|
|
FadeIn,
|
|
|
|
Playing,
|
|
|
|
FadeOut,
|
|
|
|
FadeOutNoStop
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SDSPPlayer
|
|
|
|
{
|
|
|
|
std::string x0_fileName;
|
|
|
|
EPlayerState x10_playState = EPlayerState::Stopped;
|
2017-01-21 00:20:19 +00:00
|
|
|
float x14_volume = 0.f;
|
2017-01-16 08:40:33 +00:00
|
|
|
float x18_fadeIn = 0.f;
|
|
|
|
float x1c_fadeOut = 0.f;
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 x20_internalHandle = -1;
|
2017-01-16 08:40:33 +00:00
|
|
|
float x24_fadeFactor = 0.f;
|
|
|
|
bool x28_music = true;
|
|
|
|
|
|
|
|
SDSPPlayer() = default;
|
2017-11-13 06:19:18 +00:00
|
|
|
SDSPPlayer(EPlayerState playing, std::string_view fileName, float volume,
|
2018-06-02 00:03:31 +00:00
|
|
|
float fadeIn, float fadeOut, s32 handle, bool music)
|
2017-01-16 08:40:33 +00:00
|
|
|
: x0_fileName(fileName), x10_playState(playing), x14_volume(volume),
|
|
|
|
x18_fadeIn(fadeIn), x1c_fadeOut(fadeOut), x20_internalHandle(handle), x28_music(music) {}
|
|
|
|
};
|
|
|
|
static SDSPPlayer s_Players[2]; // looping, oneshot
|
|
|
|
static SDSPPlayer s_QueuedPlayers[2]; // looping, oneshot
|
|
|
|
|
2017-01-20 03:53:32 +00:00
|
|
|
float CStreamAudioManager::GetTargetDSPVolume(float fileVol, bool music)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
if (music)
|
2017-01-20 03:53:32 +00:00
|
|
|
return g_MusicUnmute ? (g_MusicVolume * fileVol / 127.f) : 0.f;
|
2017-01-16 08:40:33 +00:00
|
|
|
else
|
2017-01-20 03:53:32 +00:00
|
|
|
return g_SfxUnmute ? (g_SfxVolume * fileVol / 127.f) : 0.f;
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 06:19:18 +00:00
|
|
|
void CStreamAudioManager::Start(bool oneshot, std::string_view fileName,
|
2018-06-02 00:03:31 +00:00
|
|
|
float volume, bool music, float fadeIn, float fadeOut)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
SDSPPlayer& p = s_Players[oneshot];
|
|
|
|
SDSPPlayer& qp = s_QueuedPlayers[oneshot];
|
|
|
|
|
|
|
|
if (p.x10_playState != EPlayerState::Stopped &&
|
|
|
|
CStringExtras::CompareCaseInsensitive(fileName, p.x0_fileName))
|
|
|
|
{
|
|
|
|
/* Enque new stream */
|
2018-06-02 00:03:31 +00:00
|
|
|
qp = SDSPPlayer(EPlayerState::FadeIn, fileName, volume, fadeIn, fadeOut, -1, music);
|
2017-01-16 08:40:33 +00:00
|
|
|
Stop(oneshot, p.x0_fileName);
|
|
|
|
}
|
|
|
|
else if (p.x10_playState != EPlayerState::Stopped)
|
|
|
|
{
|
|
|
|
/* Fade existing stream back in */
|
|
|
|
p.x18_fadeIn = fadeIn;
|
|
|
|
p.x1c_fadeOut = fadeOut;
|
2018-06-02 00:03:31 +00:00
|
|
|
p.x14_volume = volume;
|
2017-01-16 08:40:33 +00:00
|
|
|
if (p.x18_fadeIn <= FLT_EPSILON)
|
|
|
|
{
|
|
|
|
CDSPStreamManager::UpdateVolume(p.x20_internalHandle,
|
|
|
|
GetTargetDSPVolume(p.x14_volume, p.x28_music));
|
|
|
|
p.x24_fadeFactor = 1.f;
|
|
|
|
p.x10_playState = EPlayerState::Playing;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p.x10_playState = EPlayerState::FadeIn;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Start new stream */
|
|
|
|
EPlayerState state;
|
2017-01-20 03:53:32 +00:00
|
|
|
float vol;
|
2017-01-16 08:40:33 +00:00
|
|
|
if (fadeIn > 0.f)
|
|
|
|
{
|
|
|
|
state = EPlayerState::FadeIn;
|
2017-01-20 03:53:32 +00:00
|
|
|
vol = 0.f;
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
state = EPlayerState::Playing;
|
2018-06-02 00:03:31 +00:00
|
|
|
vol = volume;
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 00:03:31 +00:00
|
|
|
s32 handle = CDSPStreamManager::StartStreaming(fileName, GetTargetDSPVolume(vol, music), oneshot);
|
2017-01-16 08:40:33 +00:00
|
|
|
if (handle != -1)
|
2018-06-02 00:03:31 +00:00
|
|
|
p = SDSPPlayer(state, fileName, volume, fadeIn, fadeOut, handle, music);
|
2017-01-16 08:40:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 06:19:18 +00:00
|
|
|
void CStreamAudioManager::Stop(bool oneshot, std::string_view fileName)
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
SDSPPlayer& p = s_Players[oneshot];
|
|
|
|
SDSPPlayer& qp = s_QueuedPlayers[oneshot];
|
|
|
|
|
|
|
|
if (!CStringExtras::CompareCaseInsensitive(fileName, qp.x0_fileName))
|
|
|
|
{
|
|
|
|
/* Cancel enqueued file */
|
|
|
|
qp = SDSPPlayer();
|
|
|
|
}
|
|
|
|
else if (!CStringExtras::CompareCaseInsensitive(fileName, p.x0_fileName) &&
|
|
|
|
p.x20_internalHandle != -1 && p.x10_playState != EPlayerState::Stopped)
|
|
|
|
{
|
|
|
|
/* Fade out or stop */
|
|
|
|
if (p.x1c_fadeOut <= FLT_EPSILON)
|
|
|
|
StopStreaming(oneshot);
|
|
|
|
else
|
|
|
|
p.x10_playState = EPlayerState::FadeOut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStreamAudioManager::FadeBackIn(bool oneshot, float fadeTime)
|
|
|
|
{
|
|
|
|
SDSPPlayer& p = s_Players[oneshot];
|
|
|
|
if (p.x10_playState == EPlayerState::Stopped ||
|
|
|
|
p.x10_playState == EPlayerState::Playing)
|
|
|
|
return;
|
|
|
|
p.x18_fadeIn = fadeTime;
|
|
|
|
p.x10_playState = EPlayerState::FadeIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStreamAudioManager::TemporaryFadeOut(bool oneshot, float fadeTime)
|
|
|
|
{
|
|
|
|
SDSPPlayer& p = s_Players[oneshot];
|
|
|
|
if (p.x10_playState == EPlayerState::FadeOut ||
|
|
|
|
p.x10_playState == EPlayerState::Stopped)
|
|
|
|
return;
|
|
|
|
p.x1c_fadeOut = fadeTime;
|
|
|
|
p.x10_playState = EPlayerState::FadeOutNoStop;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStreamAudioManager::StopStreaming(bool oneshot)
|
|
|
|
{
|
|
|
|
SDSPPlayer& p = s_Players[oneshot];
|
|
|
|
p.x10_playState = EPlayerState::Stopped;
|
|
|
|
CDSPStreamManager::StopStreaming(p.x20_internalHandle);
|
|
|
|
p.x24_fadeFactor = 0.f;
|
|
|
|
p.x20_internalHandle = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStreamAudioManager::UpdateDSP(bool oneshot, float dt)
|
|
|
|
{
|
|
|
|
SDSPPlayer& p = s_Players[oneshot];
|
|
|
|
|
|
|
|
if (p.x10_playState == EPlayerState::Stopped)
|
|
|
|
{
|
|
|
|
SDSPPlayer& qp = s_QueuedPlayers[oneshot];
|
|
|
|
if (qp.x10_playState != EPlayerState::Stopped)
|
|
|
|
{
|
|
|
|
Start(oneshot, qp.x0_fileName, qp.x14_volume, qp.x28_music,
|
|
|
|
qp.x18_fadeIn, qp.x1c_fadeOut);
|
|
|
|
qp = SDSPPlayer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (p.x10_playState != EPlayerState::Stopped &&
|
2017-01-16 09:14:54 +00:00
|
|
|
CDSPStreamManager::GetStreamState(p.x20_internalHandle) == CDSPStreamManager::EState::Oneshot &&
|
2017-01-16 08:40:33 +00:00
|
|
|
CDSPStreamManager::CanStop(p.x20_internalHandle))
|
|
|
|
{
|
|
|
|
StopStreaming(oneshot);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((p.x10_playState != EPlayerState::FadeIn &&
|
|
|
|
p.x10_playState != EPlayerState::FadeOut &&
|
2017-01-18 20:57:48 +00:00
|
|
|
p.x10_playState != EPlayerState::FadeOutNoStop))
|
2017-01-16 08:40:33 +00:00
|
|
|
{
|
|
|
|
if (p.x10_playState == EPlayerState::Playing)
|
|
|
|
CDSPStreamManager::UpdateVolume(p.x20_internalHandle,
|
|
|
|
GetTargetDSPVolume(p.x14_volume, p.x28_music));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p.x10_playState == EPlayerState::FadeIn)
|
|
|
|
{
|
|
|
|
float newFadeFactor = p.x24_fadeFactor + dt / p.x18_fadeIn;
|
|
|
|
if (newFadeFactor >= 1.f)
|
|
|
|
{
|
|
|
|
p.x24_fadeFactor = 1.f;
|
|
|
|
p.x10_playState = EPlayerState::Playing;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p.x24_fadeFactor = newFadeFactor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (p.x10_playState == EPlayerState::FadeOut ||
|
|
|
|
p.x10_playState == EPlayerState::FadeOutNoStop)
|
|
|
|
{
|
|
|
|
float newFadeFactor = p.x24_fadeFactor - dt / p.x1c_fadeOut;
|
|
|
|
if (newFadeFactor <= 0.f)
|
|
|
|
{
|
|
|
|
if (p.x10_playState == EPlayerState::FadeOutNoStop)
|
|
|
|
{
|
|
|
|
p.x24_fadeFactor = 0.f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StopStreaming(oneshot);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p.x24_fadeFactor = newFadeFactor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CDSPStreamManager::UpdateVolume(p.x20_internalHandle,
|
|
|
|
GetTargetDSPVolume(p.x14_volume * p.x24_fadeFactor,
|
|
|
|
p.x28_music));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStreamAudioManager::UpdateDSPStreamers(float dt)
|
|
|
|
{
|
|
|
|
UpdateDSP(false, dt);
|
|
|
|
UpdateDSP(true, dt);
|
|
|
|
}
|
|
|
|
|
2017-02-06 03:21:58 +00:00
|
|
|
void CStreamAudioManager::StopAllStreams()
|
|
|
|
{
|
|
|
|
for (int i=0 ; i<2 ; ++i)
|
|
|
|
{
|
2018-06-02 00:03:31 +00:00
|
|
|
StopStreaming(bool(i));
|
2017-02-06 03:21:58 +00:00
|
|
|
SDSPPlayer& p = s_Players[i];
|
|
|
|
SDSPPlayer& qp = s_QueuedPlayers[i];
|
|
|
|
p = SDSPPlayer();
|
|
|
|
qp = SDSPPlayer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 08:40:33 +00:00
|
|
|
void CStreamAudioManager::Update(float dt)
|
|
|
|
{
|
2017-01-18 20:57:48 +00:00
|
|
|
CDSPStreamManager::PollHeaderReadCompletions();
|
2017-01-16 08:40:33 +00:00
|
|
|
UpdateDSPStreamers(dt);
|
|
|
|
}
|
|
|
|
|
2017-02-06 03:21:58 +00:00
|
|
|
void CStreamAudioManager::StopAll()
|
|
|
|
{
|
|
|
|
StopAllStreams();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStreamAudioManager::SetMusicUnmute(bool unmute)
|
|
|
|
{
|
|
|
|
g_MusicUnmute = unmute;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStreamAudioManager::SetSfxVolume(u8 volume)
|
|
|
|
{
|
|
|
|
g_SfxVolume = std::min(volume, u8(127));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CStreamAudioManager::SetMusicVolume(u8 volume)
|
|
|
|
{
|
|
|
|
g_MusicVolume = std::min(volume, u8(127));
|
|
|
|
}
|
|
|
|
|
2017-01-16 09:14:54 +00:00
|
|
|
void CStreamAudioManager::Initialize()
|
|
|
|
{
|
|
|
|
CDSPStreamManager::Initialize();
|
|
|
|
}
|
|
|
|
|
2017-11-27 05:06:53 +00:00
|
|
|
void CStreamAudioManager::StopOneShot()
|
|
|
|
{
|
|
|
|
CStreamAudioManager::StopStreaming(true);
|
|
|
|
SDSPPlayer& p = s_Players[1];
|
|
|
|
p = SDSPPlayer();
|
|
|
|
SDSPPlayer& qp = s_QueuedPlayers[1];
|
|
|
|
qp = SDSPPlayer();
|
|
|
|
}
|
|
|
|
|
2017-01-22 01:40:12 +00:00
|
|
|
void CStreamAudioManager::Shutdown()
|
|
|
|
{
|
|
|
|
CDSPStreamManager::Shutdown();
|
|
|
|
}
|
|
|
|
|
2017-01-16 08:40:33 +00:00
|
|
|
u8 CStreamAudioManager::g_MusicVolume = 0x7f;
|
|
|
|
u8 CStreamAudioManager::g_SfxVolume = 0x7f;
|
|
|
|
bool CStreamAudioManager::g_MusicUnmute = true;
|
|
|
|
bool CStreamAudioManager::g_SfxUnmute = true;
|
|
|
|
|
|
|
|
}
|