SFX API Changes

DSound has since been deprecated and xaudio2 would require us to write our own 3d spatialization and mixer
Load devices the same way we load in the gfx end
setup sfx provider
run sfx devices on startup
various fixes around sfx null device
added the bitrate and samplerate globals
added the hrtf global code is in to use this but not setup yet
Adds speed of sound to the sound system
SFXAmbience now has a property for speed of sound for different mediums, can also be set directly
This commit is contained in:
marauder2k7 2026-03-13 08:05:42 +00:00
parent 1c6409a485
commit 974f217b96
53 changed files with 1885 additions and 6409 deletions

View file

@ -33,46 +33,127 @@ bool SFXSndStream::_readHeader()
dMemset(&sfinfo, 0, sizeof(SF_INFO));
vio.get_filelen = sndFileLen;
vio.seek = sndSeek;
vio.read = sndRead;
vio.write = sndWrite;
vio.tell = sndTell;
vio.get_filelen = sndFileLen;
vio.seek = sndSeek;
vio.read = sndRead;
vio.write = sndWrite;
vio.tell = sndTell;
vio_data.length = 0;
vio_data.offset = 0;
vio_data.data = mStream;
vio_data.sampleBlockAlign = 1;
vio_data.byteBlockAlign = 0;
if ((sndFile = sf_open_virtual(&vio, SFM_READ, &sfinfo, &vio_data)) == NULL)
{
Con::printf("SFXSndStream - _readHeader failed: %s", sf_strerror(sndFile));
return false;
}
S32 bitsPerSample = 0;
switch ((sfinfo.format & SF_FORMAT_SUBMASK))
S32 bitsPerSample = 16;
switch (sfinfo.format & SF_FORMAT_SUBMASK)
{
case SF_FORMAT_PCM_S8:
case SF_FORMAT_PCM_U8:
bitsPerSample = 8;
mSampleType = Sample_Int8; // Still decode using sf_readf_short()
break;
case SF_FORMAT_PCM_16:
bitsPerSample = 16;
mSampleType = Sample_Int16;
break;
case SF_FORMAT_VORBIS:
case SF_FORMAT_PCM_24:
case SF_FORMAT_PCM_32:
case SF_FORMAT_FLOAT:
bitsPerSample = 16;
sf_command(sndFile, SFC_SET_SCALE_FLOAT_INT_READ, NULL, SF_TRUE);
case SF_FORMAT_DOUBLE:
case SF_FORMAT_VORBIS:
case SF_FORMAT_OPUS:
case SF_FORMAT_ALAC_20:
case SF_FORMAT_ALAC_24:
case SF_FORMAT_ALAC_32:
case 0x0080/*SF_FORMAT_MPEG_LAYER_I*/:
case 0x0081/*SF_FORMAT_MPEG_LAYER_II*/:
case 0x0082/*SF_FORMAT_MPEG_LAYER_III*/:
bitsPerSample = 32;
mSampleType = Sample_Float;
break;
default:
// missed, set it to 16 anyway.
case SF_FORMAT_IMA_ADPCM:
bitsPerSample = 16;
mSampleType = Sample_IMA4;
break;
case SF_FORMAT_MS_ADPCM:
bitsPerSample = 16;
mSampleType = Sample_MSADPCM;
break;
default:
bitsPerSample = 16;
mSampleType = Sample_Int16;
break;
}
mFormat.set(sfinfo.channels, bitsPerSample * sfinfo.channels, sfinfo.samplerate);
//------------------------------------------------------------
// Block alignment logic for ADPCM formats
//------------------------------------------------------------
int byteBlockAlign = 0;
int sampleBlockAlign = 1;
if (mSampleType == Sample_IMA4 || mSampleType == Sample_MSADPCM)
{
SF_CHUNK_INFO inf = { "fmt ", 4, 0, NULL };
SF_CHUNK_ITERATOR* iter = sf_get_chunk_iterator(sndFile, &inf);
if (!iter || sf_get_chunk_size(iter, &inf) != SF_ERR_NO_ERROR || inf.datalen < 14)
mSampleType = Sample_Int16;
else
{
uint8_t* fmtbuf = (uint8_t*)malloc(inf.datalen);
inf.data = fmtbuf;
if (sf_get_chunk_data(iter, &inf) != SF_ERR_NO_ERROR)
mSampleType = Sample_Int16;
else
{
byteBlockAlign = fmtbuf[12] | (fmtbuf[13] << 8);
int ch = sfinfo.channels;
if (mSampleType == Sample_IMA4)
sampleBlockAlign = (byteBlockAlign / ch - 4) / 4 * 8 + 1;
else
sampleBlockAlign = (byteBlockAlign / ch - 7) * 2 + 2;
}
dFree(fmtbuf);
}
}
//------------------------------------------------------------
// Commit alignment to class + VIO data
//------------------------------------------------------------
if (mSampleType == Sample_Int16)
{
sampleBlockAlign = 1;
byteBlockAlign = sfinfo.channels * 2;
}
else if (mSampleType == Sample_Float)
{
sampleBlockAlign = 1;
byteBlockAlign = sfinfo.channels * 4;
}
vio_data.sampleBlockAlign = sampleBlockAlign;
vio_data.byteBlockAlign = byteBlockAlign;
mSampleBlockAlign = sampleBlockAlign;
mByteBlockAlign = byteBlockAlign;
mFormat.set(sfinfo.channels, bitsPerSample * sfinfo.channels, sfinfo.samplerate, mSampleType);
mSamples = sfinfo.frames;
@ -110,25 +191,32 @@ U32 SFXSndStream::read(U8* buffer, U32 length)
return 0;
}
U32 framesToRead = length / mFormat.getBytesPerSample();
const U32 bytesPerFrame = mFormat.getBytesPerSample();
U32 framesToRead = length / bytesPerFrame;
// Enforce sample block alignment (important for ADPCM / compressed)
if (mSampleBlockAlign > 1)
framesToRead -= (framesToRead % mSampleBlockAlign);
if (framesToRead == 0)
return 0;
U32 framesRead = 0;
switch (sfinfo.format & SF_FORMAT_SUBMASK)
switch (mSampleType)
{
case SF_FORMAT_PCM_S8:
case SF_FORMAT_PCM_U8:
framesRead = sf_readf_int(sndFile, (int*)buffer, framesToRead);
case SFXSampleType::Sample_Int8: framesRead = sf_readf_int(sndFile, (int*)buffer, framesToRead);
break;
case SF_FORMAT_PCM_16:
case SF_FORMAT_VORBIS:
case SF_FORMAT_PCM_24:
case SF_FORMAT_PCM_32:
case SF_FORMAT_FLOAT:
framesRead = sf_readf_short(sndFile, (short*)buffer, framesToRead);
case SFXSampleType::Sample_Int16: framesRead = sf_readf_short(sndFile, (short*)buffer, framesToRead);
break;
case SFXSampleType::Sample_Float: framesRead = sf_readf_float(sndFile, (float*)buffer, framesToRead);
break;
case SFXSampleType::Sample_IMA4:
case SFXSampleType::Sample_MSADPCM:
framesRead = sf_read_raw(sndFile, buffer, framesToRead);
break;
default:
Con::errorf("SFXSndStream - read: Unsupported format.");
return 0;
break;
}
if (framesRead != framesToRead)
@ -142,7 +230,6 @@ U32 SFXSndStream::read(U8* buffer, U32 length)
// reset stream
setPosition(0);
}
return framesRead * mFormat.getBytesPerSample();
}
@ -221,4 +308,3 @@ sf_count_t SFXSndStream::sndFileLen(void* user_data)
return vf->length;
}