Handle the SoundStream in MvePlayer instead of InterplayDecoder

This commit is contained in:
Chris Robinson 2022-10-06 11:08:47 -07:00 committed by Christoph Oelckers
parent bf933b3904
commit 573784b37f
3 changed files with 65 additions and 40 deletions

View file

@ -260,8 +260,16 @@ public:
class MvePlayer : public MoviePlayer
{
InterplayDecoder decoder;
SoundStream* stream = nullptr;
bool failed = false;
bool StreamCallback(SoundStream *stream, void *buff, int len)
{
return decoder.FillSamples(buff, len);
}
static bool StreamCallbackC(SoundStream *stream, void *buff, int len, void *userdata)
{ return static_cast<MvePlayer*>(userdata)->StreamCallback(stream, buff, len); }
public:
bool isvalid() { return !failed; }
@ -279,12 +287,29 @@ public:
bool Frame(uint64_t clock) override
{
if (failed) return false;
bool playon = decoder.RunFrame(clock);
if (playon)
{
if (!stream && decoder.HasAudio())
{
S_StopMusic(true);
// start audio playback
stream = S_CreateCustomStream(6000, decoder.GetSampleRate(), decoder.NumChannels(), MusicSamples16bit, StreamCallbackC, this);
if (!stream)
decoder.DisableAudio();
}
}
return playon;
}
~MvePlayer()
{
if (stream)
S_StopCustomStream(stream);
stream = nullptr;
decoder.Close();
}

View file

@ -95,7 +95,7 @@ static const int16_t delta_table[] = {
#define LE_64(x) (LE_32(x) | ((uint64_t)LE_32(x+4) << 32))
bool InterplayDecoder::StreamCallback(SoundStream *stream, void *buff, int len)
bool InterplayDecoder::FillSamples(void *buff, int len)
{
for (int i = 0; i < len;)
{
@ -174,10 +174,20 @@ bool InterplayDecoder::StreamCallback(SoundStream *stream, void *buff, int len)
return true;
}
void InterplayDecoder::DisableAudio()
{
if (bAudioEnabled)
{
std::unique_lock plock(PacketMutex);
bAudioEnabled = false;
audio.Packets.clear();
}
}
InterplayDecoder::InterplayDecoder(bool soundenabled)
{
bIsPlaying = false;
bAudioStarted = false;
bAudioEnabled = soundenabled;
nWidth = 0;
@ -485,9 +495,6 @@ int InterplayDecoder::ProcessNextChunk()
void InterplayDecoder::Close()
{
bIsPlaying = false;
if (stream)
S_StopCustomStream(stream);
stream = nullptr;
fr.Close();
if (decodeMap.pData) {
@ -562,14 +569,6 @@ bool InterplayDecoder::RunFrame(uint64_t clock)
}
nNextFrameTime += nFrameDuration;
if (!bAudioStarted && bAudioEnabled)
{
S_StopMusic(true);
// start audio playback
stream = S_CreateCustomStream(6000, audio.nSampleRate, audio.nChannels, MusicSamples16bit, StreamCallbackC, this);
bAudioStarted = true;
}
bool doFrame = false;
do
{

View file

@ -58,21 +58,6 @@
class InterplayDecoder
{
struct AudioPacket
{
size_t nSize = 0;
std::unique_ptr<uint8_t[]> pData;
};
struct VideoPacket
{
uint16_t nPalStart=0, nPalCount=0;
uint32_t nDecodeMapSize = 0;
uint32_t nVideoDataSize = 0;
bool bSendFlag = false;
std::unique_ptr<uint8_t[]> pData;
};
public:
enum
{
@ -123,8 +108,34 @@ public:
bool Open(FileReader &fr);
void Close();
bool RunFrame(uint64_t clock);
bool FillSamples(void *buff, int len);
bool HasAudio() const noexcept { return bAudioEnabled; }
int NumChannels() const noexcept { return audio.nChannels; }
int GetSampleRate() const noexcept { return audio.nSampleRate; }
void DisableAudio();
AnimTextures& animTex() { return animtex; }
private:
struct AudioPacket
{
size_t nSize = 0;
std::unique_ptr<uint8_t[]> pData;
};
struct VideoPacket
{
uint16_t nPalStart=0, nPalCount=0;
uint32_t nDecodeMapSize = 0;
uint32_t nVideoDataSize = 0;
bool bSendFlag = false;
std::unique_ptr<uint8_t[]> pData;
};
struct AudioData
{
int nChannels = 0;
@ -138,16 +149,7 @@ public:
std::deque<AudioPacket> Packets;
};
AudioData audio;
AnimTextures animtex;
AnimTextures& animTex() { return animtex; }
private:
bool StreamCallback(SoundStream *stream, void *buff, int len);
static bool StreamCallbackC(SoundStream *stream, void *buff, int len, void *userdata)
{ return static_cast<InterplayDecoder*>(userdata)->StreamCallback(stream, buff, len); }
struct DecodeMap
{
@ -185,8 +187,7 @@ private:
std::mutex PacketMutex;
FileReader fr;
bool bIsPlaying, bAudioStarted;
bool bAudioEnabled;
bool bIsPlaying, bAudioEnabled;
uint32_t nTimerRate, nTimerDiv;
uint32_t nWidth, nHeight, nFrame;
@ -204,9 +205,9 @@ private:
const uint8_t *ChunkPtr = nullptr;
DecodeMap decodeMap;
AnimTextures animtex;
Palette palette[256];
uint64_t nNextFrameTime = 0;
SoundStream* stream = nullptr;
};
#endif