Add a new ZMusic_GetStreamInfoEx function

This provides cleaner info about the stream format
This commit is contained in:
Chris Robinson 2021-11-05 19:01:16 -07:00 committed by Christoph Oelckers
parent dd76ab1fce
commit 3696d8fa27
14 changed files with 110 additions and 1 deletions

View file

@ -77,6 +77,15 @@ typedef enum ChannelConfig_
ChannelConfig_Stereo
} ChannelConfig;
typedef struct SoundStreamInfoEx_
{
int mBufferSize; // If mBufferSize is 0, the song doesn't use streaming but plays through a different interface.
int mSampleRate;
SampleType mSampleType;
ChannelConfig mChannelConfig;
} SoundStreamInfoEx;
typedef enum EIntConfigKey_
{
zmusic_adl_chips_count,
@ -320,6 +329,7 @@ extern "C"
DLL_IMPORT void ZMusic_VolumeChanged(ZMusic_MusicStream song);
DLL_IMPORT zmusic_bool ZMusic_WriteSMF(ZMusic_MidiSource source, const char* fn, int looplimit);
DLL_IMPORT void ZMusic_GetStreamInfo(ZMusic_MusicStream song, SoundStreamInfo *info);
DLL_IMPORT void ZMusic_GetStreamInfoEx(ZMusic_MusicStream song, SoundStreamInfoEx *info);
// Configuration interface. The return value specifies if a music restart is needed.
// RealValue should be written back to the CVAR or whatever other method the client uses to store configuration state.
DLL_IMPORT zmusic_bool ChangeMusicSettingInt(EIntConfigKey key, ZMusic_MusicStream song, int value, int* pRealValue);
@ -407,6 +417,7 @@ typedef zmusic_bool (*pfn_ZMusic_IsMIDI)(ZMusic_MusicStream song);
typedef void (*pfn_ZMusic_VolumeChanged)(ZMusic_MusicStream song);
typedef zmusic_bool (*pfn_ZMusic_WriteSMF)(ZMusic_MidiSource source, const char* fn, int looplimit);
typedef void (*pfn_ZMusic_GetStreamInfo)(ZMusic_MusicStream song, SoundStreamInfo *info);
typedef void (*pfn_ZMusic_GetStreamInfoEx)(ZMusic_MusicStream song, SoundStreamInfoEx *info);
typedef zmusic_bool (*pfn_ChangeMusicSettingInt)(EIntConfigKey key, ZMusic_MusicStream song, int value, int* pRealValue);
typedef zmusic_bool (*pfn_ChangeMusicSettingFloat)(EFloatConfigKey key, ZMusic_MusicStream song, float value, float* pRealValue);
typedef zmusic_bool (*pfn_ChangeMusicSettingString)(EStringConfigKey key, ZMusic_MusicStream song, const char* value);

View file

@ -52,6 +52,7 @@ public:
virtual int GetDeviceType() const { return MDEV_DEFAULT; }
virtual bool CanHandleSysex() const { return true; }
virtual SoundStreamInfo GetStreamInfo() const;
virtual SoundStreamInfoEx GetStreamInfoEx() const;
protected:
MidiCallback Callback;
@ -82,6 +83,7 @@ public:
virtual bool ServiceStream(void* buff, int numbytes);
int GetSampleRate() const { return SampleRate; }
SoundStreamInfo GetStreamInfo() const override;
SoundStreamInfoEx GetStreamInfoEx() const override;
protected:
double Tempo;

View file

@ -180,3 +180,14 @@ SoundStreamInfo MIDIDevice::GetStreamInfo() const
{
return { 0, 0, 0 }; // i.e. do not use streaming.
}
//==========================================================================
//
// MIDIDevice :: GetStreamInfoEx
//
//==========================================================================
SoundStreamInfoEx MIDIDevice::GetStreamInfoEx() const
{
return {}; // i.e. do not use streaming.
}

View file

@ -88,7 +88,7 @@ SoftSynthMIDIDevice::~SoftSynthMIDIDevice()
//==========================================================================
//
// SoftSynthMIDIDevice :: GwrStreamInfo
// SoftSynthMIDIDevice :: GetStreamInfo
//
//==========================================================================
@ -102,6 +102,23 @@ SoundStreamInfo SoftSynthMIDIDevice::GetStreamInfo() const
return { chunksize, SampleRate, isMono? 1:2 };
}
//==========================================================================
//
// SoftSynthMIDIDevice :: GetStreamInfoEx
//
//==========================================================================
SoundStreamInfoEx SoftSynthMIDIDevice::GetStreamInfoEx() const
{
int chunksize = (SampleRate / StreamBlockSize) * 4;
if (!isMono)
{
chunksize *= 2;
}
return { chunksize, SampleRate, SampleType_Float32,
isMono ? ChannelConfig_Mono : ChannelConfig_Stereo };
}
//==========================================================================
//
// SoftSynthMIDIDevice :: Open

View file

@ -88,6 +88,7 @@ public:
void SetMIDISource(MIDISource* _source);
bool ServiceStream(void* buff, int len) override;
SoundStreamInfo GetStreamInfo() const override;
SoundStreamInfoEx GetStreamInfoEx() const override;
int GetDeviceType() const override;
@ -452,6 +453,12 @@ SoundStreamInfo MIDIStreamer::GetStreamInfo() const
else return { 0, 0, 0 };
}
SoundStreamInfoEx MIDIStreamer::GetStreamInfoEx() const
{
if (MIDI) return MIDI->GetStreamInfoEx();
else return {};
}
//==========================================================================
//
// MIDIStreamer :: StartPlayback

View file

@ -55,6 +55,7 @@ public:
void ChangeSettingString(const char *name, const char *value) override { if(m_Source) m_Source->ChangeSettingString(name, value); }
bool ServiceStream(void* buff, int len) override;
SoundStreamInfo GetStreamInfo() const override { return m_Source->GetFormat(); }
SoundStreamInfoEx GetStreamInfoEx() const override { return m_Source->GetFormatEx(); }
protected:

View file

@ -65,6 +65,7 @@ public:
bool SetSubsong(int subsong) override;
bool Start() override;
SoundStreamInfo GetFormat() override;
SoundStreamInfoEx GetFormatEx() override;
void ChangeSettingNum(const char* setting, double val) override;
std::string GetStats() override;
@ -1039,6 +1040,17 @@ SoundStreamInfo DumbSong::GetFormat()
return { 32*1024, srate, 2 };
}
//==========================================================================
//
// DumbSong GetFormatEx
//
//==========================================================================
SoundStreamInfoEx DumbSong::GetFormatEx()
{
return { 32*1024, srate, SampleType_Float32, ChannelConfig_Stereo };
}
//==========================================================================
//
// DumbSong :: Play

View file

@ -58,6 +58,7 @@ public:
std::string GetStats() override;
bool GetData(void *buffer, size_t len) override;
SoundStreamInfo GetFormat() override;
SoundStreamInfoEx GetFormatEx() override;
protected:
Music_Emu *Emu;
@ -172,6 +173,11 @@ SoundStreamInfo GMESong::GetFormat()
return { 32*1024, SampleRate, -2 };
}
SoundStreamInfoEx GMESong::GetFormatEx()
{
return { 32*1024, SampleRate, SampleType_Int16, ChannelConfig_Stereo };
}
//==========================================================================
//
// GMESong - Destructor

View file

@ -51,6 +51,7 @@ public:
~SndFileSong();
std::string GetStats() override;
SoundStreamInfo GetFormat() override;
SoundStreamInfoEx GetFormatEx() override;
bool GetData(void *buffer, size_t len) override;
protected:
@ -455,6 +456,16 @@ SoundStreamInfo SndFileSong::GetFormat()
return { 64/*snd_streambuffersize*/ * 1024, SampleRate, -Channels };
}
SoundStreamInfoEx SndFileSong::GetFormatEx()
{
ChannelConfig chans;
SampleType stype;
int srate;
Decoder->getInfo(&srate, &chans, &stype);
return { 64/*snd_streambuffersize*/ * 1024, srate, stype, chans };
}
//==========================================================================
//
// SndFileSong - Destructor

View file

@ -55,6 +55,7 @@ public:
bool Start() override;
void ChangeSettingInt(const char *name, int value) override;
SoundStreamInfo GetFormat() override;
SoundStreamInfoEx GetFormatEx() override;
protected:
bool GetData(void *buffer, size_t len) override;
@ -105,6 +106,19 @@ SoundStreamInfo OPLMUSSong::GetFormat()
//
//==========================================================================
SoundStreamInfoEx OPLMUSSong::GetFormatEx()
{
int samples = int(OPL_SAMPLE_RATE / 14);
return { samples * 4, int(OPL_SAMPLE_RATE), SampleType_Float32,
current_opl_core == 0? ChannelConfig_Mono:ChannelConfig_Stereo };
}
//==========================================================================
//
//
//
//==========================================================================
OPLMUSSong::~OPLMUSSong ()
{
if (Music != NULL)

View file

@ -236,6 +236,7 @@ class XASong : public StreamSource
public:
XASong(MusicIO::FileInterface *readr);
SoundStreamInfo GetFormat() override;
SoundStreamInfoEx GetFormatEx() override;
bool Start() override;
bool GetData(void *buffer, size_t len) override;
@ -266,6 +267,12 @@ SoundStreamInfo XASong::GetFormat()
return { 64*1024, SampleRate, 2};
}
SoundStreamInfoEx XASong::GetFormatEx()
{
auto SampleRate = xad.blockIs18K? 18900 : 37800;
return { 64*1024, SampleRate, SampleType_Float32, ChannelConfig_Stereo };
}
//==========================================================================
//
// XASong :: Play

View file

@ -22,6 +22,7 @@ public:
virtual bool SetSubsong(int subsong) { return false; }
virtual bool GetData(void *buffer, size_t len) = 0;
virtual SoundStreamInfo GetFormat() { return {65536, m_OutputRate, 2 }; } // Default format is: System's output sample rate, 32 bit float, stereo
virtual SoundStreamInfoEx GetFormatEx() = 0;
virtual std::string GetStats() { return ""; }
virtual void ChangeSettingInt(const char *name, int value) { }
virtual void ChangeSettingNum(const char *name, double value) { }

View file

@ -32,6 +32,7 @@ public:
virtual void ChangeSettingString(const char* setting, const char* value) {} // "
virtual bool ServiceStream(void *buff, int len) { return false; }
virtual SoundStreamInfo GetStreamInfo() const { return { 0,0,0 }; }
virtual SoundStreamInfoEx GetStreamInfoEx() const = 0;
enum EState
{

View file

@ -460,6 +460,14 @@ DLL_EXPORT void ZMusic_GetStreamInfo(MusInfo *song, SoundStreamInfo *fmt)
*fmt = song->GetStreamInfo();
}
DLL_EXPORT void ZMusic_GetStreamInfoEx(MusInfo *song, SoundStreamInfoEx *fmt)
{
if (!fmt) return;
if (!song) *fmt = {};
std::lock_guard<FCriticalSection> lock(song->CritSec);
*fmt = song->GetStreamInfoEx();
}
DLL_EXPORT void ZMusic_Close(MusInfo *song)
{
if (!song) return;