change static sound API

main
cosmonaut 2021-10-28 17:25:41 -07:00
parent 2568e9869b
commit 8fee2bcba9
2 changed files with 166 additions and 206 deletions

View File

@ -535,72 +535,6 @@ double FAudioGMS_StaticSound_LoadWAV(char *filePath)
return (double)sound->id;
}
static void FAudioGMS_INTERNAL_SoundInstance_SetLowPassFilter(FAudioGMS_SoundInstance* instance, float lowPassFilter, float Q)
{
FAudioFilterParameters p;
p.Type = FAudioLowPassFilter;
p.Frequency = SDL_max(0.0, SDL_min(1.0, lowPassFilter));
p.OneOverQ = 1.0f / Q;
FAudioVoice_SetFilterParameters(instance->handle, &p, 0);
instance->lowPassFilter = lowPassFilter;
}
void FAudioGMS_SoundInstance_SetLowPassFilter(double soundInstanceID, double lowPassFilter, double Q)
{
FAudioGMS_SoundInstance *instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID);
if (instance != NULL)
{
FAudioGMS_INTERNAL_SoundInstance_SetLowPassFilter(instance, lowPassFilter, Q);
}
}
static void FAudioGMS_INTERNAL_SoundInstance_SetHighPassFilter(FAudioGMS_SoundInstance* instance, float highPassFilter, float Q)
{
FAudioFilterParameters p;
p.Type = FAudioHighPassFilter;
p.Frequency = SDL_max(0.0, SDL_min(1.0, highPassFilter));
p.OneOverQ = 1.0f / Q;
FAudioVoice_SetFilterParameters(instance->handle, &p, 0);
instance->highPassFilter = highPassFilter;
}
void FAudioGMS_SoundInstance_SetHighPassFilter(double soundInstanceID, double highPassFilter, double Q)
{
FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID);
if (instance != NULL)
{
FAudioGMS_INTERNAL_SoundInstance_SetHighPassFilter(instance, highPassFilter, Q);
}
}
static void FAudioGMS_INTERNAL_SoundInstance_SetBandPassFilter(FAudioGMS_SoundInstance* instance, float bandPassFilter, float Q)
{
FAudioFilterParameters p;
p.Type = FAudioBandPassFilter;
p.Frequency = SDL_max(0.0, SDL_min(1.0, bandPassFilter));
p.OneOverQ = 1.0f / Q;
FAudioVoice_SetFilterParameters(instance->handle, &p, 0);
instance->bandPassFilter = bandPassFilter;
}
void FAudioGMS_SoundInstance_SetBandPassFilter(double soundInstanceID, double bandPassFilter, double Q)
{
FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID);
if (instance != NULL)
{
FAudioGMS_INTERNAL_SoundInstance_SetBandPassFilter(instance, bandPassFilter, Q);
}
}
static void FAudioGMS_INTERNAL_SoundInstance_SetPan(FAudioGMS_SoundInstance* instance, float pan)
{
instance->pan = pan;
@ -656,35 +590,6 @@ static void FAudioGMS_INTERNAL_SoundInstance_SetProperties(FAudioGMS_SoundInstan
FAudioGMS_INTERNAL_SoundInstance_SetVolume(instance, volume);
}
static void FAudioGMS_INTERNAL_Apply3D(FAudioGMS_SoundInstance* instance)
{
F3DAUDIO_EMITTER* emitter = instance->emitter;
if (emitter == NULL)
{
Log("Sound instance does not have 3D data! Did you forget to initialize?");
return;
}
F3DAudioCalculate(
device->handle3D,
&device->listener,
emitter,
F3DAUDIO_CALCULATE_MATRIX | F3DAUDIO_CALCULATE_DOPPLER,
&instance->dspSettings
);
FAudioGMS_INTERNAL_SoundInstance_UpdatePitch(instance);
FAudioVoice_SetOutputMatrix(
instance->handle,
device->masteringVoice,
instance->dspSettings.SrcChannelCount,
instance->dspSettings.DstChannelCount,
instance->dspSettings.pMatrixCoefficients,
0
);
}
static FAudioGMS_SoundInstance* FAudioGMS_INTERNAL_SoundInstance_Init(
uint32_t channelCount,
uint32_t samplesPerSecond,
@ -771,7 +676,7 @@ static FAudioGMS_SoundInstance* FAudioGMS_INTERNAL_SoundInstance_Init(
}
static FAudioGMS_SoundInstance* FAudioGMS_INTERNAL_SoundInstance_CreateFromStaticSound(
FAudioGMS_StaticSound *staticSound
FAudioGMS_StaticSound* staticSound
) {
FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_SoundInstance_Init(
staticSound->channels,
@ -785,6 +690,118 @@ static FAudioGMS_SoundInstance* FAudioGMS_INTERNAL_SoundInstance_CreateFromStati
return instance;
}
double FAudioGMS_StaticSound_CreateSoundInstance(double staticSoundID)
{
RETURN_ON_NULL_DEVICE()
FAudioGMS_StaticSound* staticSound = FAudioGMS_INTERNAL_LookupStaticSound((uint32_t)staticSoundID);
if (staticSound != NULL)
{
FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_SoundInstance_CreateFromStaticSound(staticSound);
return instance->id;
}
else
{
Log("Invalid static sound ID!");
return -1.0;
}
}
static void FAudioGMS_INTERNAL_SoundInstance_SetLowPassFilter(FAudioGMS_SoundInstance* instance, float lowPassFilter, float Q)
{
FAudioFilterParameters p;
p.Type = FAudioLowPassFilter;
p.Frequency = SDL_max(0.0, SDL_min(1.0, lowPassFilter));
p.OneOverQ = 1.0f / Q;
FAudioVoice_SetFilterParameters(instance->handle, &p, 0);
instance->lowPassFilter = lowPassFilter;
}
void FAudioGMS_SoundInstance_SetLowPassFilter(double soundInstanceID, double lowPassFilter, double Q)
{
FAudioGMS_SoundInstance *instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID);
if (instance != NULL)
{
FAudioGMS_INTERNAL_SoundInstance_SetLowPassFilter(instance, lowPassFilter, Q);
}
}
static void FAudioGMS_INTERNAL_SoundInstance_SetHighPassFilter(FAudioGMS_SoundInstance* instance, float highPassFilter, float Q)
{
FAudioFilterParameters p;
p.Type = FAudioHighPassFilter;
p.Frequency = SDL_max(0.0, SDL_min(1.0, highPassFilter));
p.OneOverQ = 1.0f / Q;
FAudioVoice_SetFilterParameters(instance->handle, &p, 0);
instance->highPassFilter = highPassFilter;
}
void FAudioGMS_SoundInstance_SetHighPassFilter(double soundInstanceID, double highPassFilter, double Q)
{
FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID);
if (instance != NULL)
{
FAudioGMS_INTERNAL_SoundInstance_SetHighPassFilter(instance, highPassFilter, Q);
}
}
static void FAudioGMS_INTERNAL_SoundInstance_SetBandPassFilter(FAudioGMS_SoundInstance* instance, float bandPassFilter, float Q)
{
FAudioFilterParameters p;
p.Type = FAudioBandPassFilter;
p.Frequency = SDL_max(0.0, SDL_min(1.0, bandPassFilter));
p.OneOverQ = 1.0f / Q;
FAudioVoice_SetFilterParameters(instance->handle, &p, 0);
instance->bandPassFilter = bandPassFilter;
}
void FAudioGMS_SoundInstance_SetBandPassFilter(double soundInstanceID, double bandPassFilter, double Q)
{
FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID);
if (instance != NULL)
{
FAudioGMS_INTERNAL_SoundInstance_SetBandPassFilter(instance, bandPassFilter, Q);
}
}
static void FAudioGMS_INTERNAL_Apply3D(FAudioGMS_SoundInstance* instance)
{
F3DAUDIO_EMITTER* emitter = instance->emitter;
if (emitter == NULL)
{
Log("Sound instance does not have 3D data! Did you forget to initialize?");
return;
}
F3DAudioCalculate(
device->handle3D,
&device->listener,
emitter,
F3DAUDIO_CALCULATE_MATRIX | F3DAUDIO_CALCULATE_DOPPLER,
&instance->dspSettings
);
FAudioGMS_INTERNAL_SoundInstance_UpdatePitch(instance);
FAudioVoice_SetOutputMatrix(
instance->handle,
device->masteringVoice,
instance->dspSettings.SrcChannelCount,
instance->dspSettings.DstChannelCount,
instance->dspSettings.pMatrixCoefficients,
0
);
}
static void FAudioGMS_INTERNAL_SoundInstance_AddBuffer(FAudioGMS_SoundInstance* instance)
{
uint32_t requestedSampleCount = instance->format.nSamplesPerSec / 4;
@ -867,7 +884,7 @@ double FAudioGMS_StreamingSound_LoadOGG(char* filePath)
return instance->id;
}
static void FAudioGMS_INTERNAL_StaticSound_AddEmitter(FAudioGMS_SoundInstance* instance, float x, float y, float z)
static void FAudioGMS_INTERNAL_SoundInstance_AddEmitter(FAudioGMS_SoundInstance* instance, float x, float y, float z)
{
instance->is3D = 1;
@ -937,84 +954,6 @@ static void FAudioGMS_INTERNAL_SoundInstance_Play(FAudioGMS_SoundInstance* insta
instance->soundState = SoundState_Playing;
}
void FAudioGMS_StaticSound_PlayOneOff(double staticSoundID, double pan, double pitch, double volume)
{
RETURN_ON_NULL_DEVICE()
FAudioGMS_StaticSound* staticSound = FAudioGMS_INTERNAL_LookupStaticSound((uint32_t)staticSoundID);
if (staticSound != NULL)
{
FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_SoundInstance_CreateFromStaticSound(staticSound);
instance->destroyOnFinish = 1;
FAudioGMS_INTERNAL_SoundInstance_SetProperties(instance, pan, pitch, volume);
FAudioGMS_INTERNAL_SoundInstance_Play(instance);
}
else
{
Log("StaticSound_PlayOneOff: Invalid static sound ID! Did you destroy this sound?");
}
}
void FAudioGMS_StaticSound_PlayOneOffSpatial(double staticSoundID, double x, double y, double z, double pitch, double volume)
{
RETURN_ON_NULL_DEVICE()
FAudioGMS_StaticSound* staticSound = FAudioGMS_INTERNAL_LookupStaticSound((uint32_t)staticSoundID);
if (staticSound != NULL)
{
FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_SoundInstance_CreateFromStaticSound(staticSound);
instance->destroyOnFinish = 1;
FAudioGMS_INTERNAL_SoundInstance_SetProperties(instance, 0, pitch, volume);
FAudioGMS_INTERNAL_StaticSound_AddEmitter(instance, x, y, z);
FAudioGMS_INTERNAL_SoundInstance_Play(instance);
}
else
{
Log("StaticSound_PlayOneOffSpatial: Invalid static sound ID! Did you destroy this sound?");
}
}
double FAudioGMS_StaticSound_Play(double staticSoundID, double pan, double pitch, double volume, double loop)
{
RETURN_ON_NULL_DEVICE(-1.0)
FAudioGMS_StaticSound* staticSound = FAudioGMS_INTERNAL_LookupStaticSound((uint32_t)staticSoundID);
if (staticSound != NULL)
{
FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_SoundInstance_CreateFromStaticSound(staticSound);
instance->loop = (uint8_t)loop;
FAudioGMS_INTERNAL_SoundInstance_SetProperties(instance, pan, pitch, volume);
FAudioGMS_INTERNAL_SoundInstance_Play(instance);
return (double)instance->id;
}
else
{
Log("StaticSound_Play: Invalid static sound ID! Did you destroy this sound?");
return -1;
}
}
double FAudioGMS_StaticSound_PlaySpatial(double staticSoundID, double x, double y, double z, double pitch, double volume, double loop)
{
RETURN_ON_NULL_DEVICE(-1.0)
FAudioGMS_StaticSound* staticSound = FAudioGMS_INTERNAL_LookupStaticSound((uint32_t)staticSoundID);
if (staticSound != NULL)
{
FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_SoundInstance_CreateFromStaticSound(staticSound);
instance->loop = (uint8_t)loop;
FAudioGMS_INTERNAL_SoundInstance_SetProperties(instance, 0, pitch, volume);
FAudioGMS_INTERNAL_StaticSound_AddEmitter(instance, x, y, z);
FAudioGMS_INTERNAL_SoundInstance_Play(instance);
return (double)instance->id;
}
else
{
Log("StaticSound_PlaySpatial: Invalid static sound ID! Did you destroy this sound?");
return -1;
}
}
void FAudioGMS_SoundInstance_Play(double soundInstanceID, double loop)
{
RETURN_ON_NULL_DEVICE()
@ -1079,7 +1018,7 @@ void FAudioGMS_SoundInstance_SetPan(double soundInstanceID, double pan)
RETURN_ON_NULL_DEVICE()
FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID);
if (instance != NULL)
if (instance != NULL && !instance->is3D)
{
FAudioGMS_INTERNAL_SoundInstance_SetPan(instance, pan);
}
@ -1117,20 +1056,35 @@ void FAudioGMS_SoundInstance_Set3DPosition(double soundInstanceID, double x, dou
{
if (!instance->is3D)
{
Log("Not a 3D sound!");
return;
FAudioGMS_INTERNAL_SoundInstance_AddEmitter(instance, x, y, z);
}
instance->emitter->Velocity.x = x - instance->emitter->Position.x;
instance->emitter->Velocity.y = y - instance->emitter->Position.y;
instance->emitter->Velocity.z = z - instance->emitter->Position.z;
instance->emitter->Position.x = x;
instance->emitter->Position.y = y;
instance->emitter->Position.z = z;
}
}
void FAudioGMS_SoundInstance_Set3DVelocity(double soundInstanceID, double xVelocity, double yVelocity, double zVelocity)
{
RETURN_ON_NULL_DEVICE()
FAudioGMS_SoundInstance* instance = FAudioGMS_INTERNAL_LookupSoundInstance((uint32_t)soundInstanceID);
if (instance != NULL)
{
if (!instance->is3D)
{
Log("Not a 3D sound!");
return;
}
instance->emitter->Velocity.x;
instance->emitter->Velocity.y;
instance->emitter->Velocity.z;
}
}
/* FIXME: this will die horribly if position is greater than total length */
void FAudioGMS_SoundInstance_SetTrackPositionInSeconds(double soundInstanceID, double trackPositionInSeconds)
{
RETURN_ON_NULL_DEVICE()
@ -1252,15 +1206,19 @@ double FAudioGMS_SoundInstance_GetTrackPositionInSeconds(double soundInstanceID)
void FAudioGMS_SetListenerPosition(double x, double y, double z)
{
RETURN_ON_NULL_DEVICE()
device->listener.Velocity.x = x - device->listener.Position.x;
device->listener.Velocity.y = y - device->listener.Position.y;
device->listener.Velocity.z = z - device->listener.Position.z;
device->listener.Position.x = x;
device->listener.Position.y = y;
device->listener.Position.z = z;
}
void FAudioGMS_SetListenerVelocity(double xVelocity, double yVelocity, double zVelocity)
{
RETURN_ON_NULL_DEVICE()
device->listener.Velocity.x = xVelocity;
device->listener.Velocity.y = yVelocity;
device->listener.Velocity.z = zVelocity;
}
static void FAudioGMS_INTERNAL_SoundInstance_Destroy(FAudioGMS_SoundInstance* instance)
{
if (instance != NULL)
@ -1459,6 +1417,8 @@ void FAudioGMS_EffectChain_AddReverb(
static void FAudioGMS_INTERNAL_SoundInstance_SetEffectGain(FAudioGMS_SoundInstance *instance, float effectGain)
{
if (instance->effectChainAttached)
{
float* outputMatrix = instance->dspSettings.pMatrixCoefficients;
outputMatrix[0] = effectGain;
@ -1476,6 +1436,7 @@ static void FAudioGMS_INTERNAL_SoundInstance_SetEffectGain(FAudioGMS_SoundInstan
0);
instance->effectGain = effectGain;
}
}
void FAudioGMS_SoundInstance_SetEffectGain(double soundInstanceID, double effectGain)

View File

@ -42,13 +42,10 @@ extern "C" {
FAUDIOGMSAPI void FAudioGMS_Init(double spatialDistanceScale, double timestep);
FAUDIOGMSAPI double FAudioGMS_StaticSound_LoadWAV(char *filePath); /* returns a static sound ID */
FAUDIOGMSAPI void FAudioGMS_StaticSound_PlayOneOff(double staticSoundID, double pan, double pitch, double volume); /* automatically frees itself when done! */
FAUDIOGMSAPI void FAudioGMS_StaticSound_PlayOneOffSpatial(double staticSoundID, double x, double y, double z, double pitch, double volume); /* automatically frees itself when done! */
FAUDIOGMSAPI double FAudioGMS_StaticSound_Play(double staticSoundID, double pan, double pitch, double volume, double loop); /* returns a sound instance ID. must be freed! */
FAUDIOGMSAPI double FAudioGMS_StaticSound_PlaySpatial(double staticSoundID, double x, double y, double z, double pitch, double volume, double loop); /* returns a sound instance ID. must be freed! */
FAUDIOGMSAPI double FAudioGMS_StaticSound_CreateSoundInstance(double staticSoundID); /* returns a sound instance ID */
FAUDIOGMSAPI void FAudioGMS_StaticSound_Destroy(double staticSoundID);
FAUDIOGMSAPI double FAudioGMS_StreamingSound_LoadOGG(char* filepath); /* returns a sound instance */
FAUDIOGMSAPI double FAudioGMS_StreamingSound_LoadOGG(char* filepath); /* returns a sound instance ID */
FAUDIOGMSAPI void FAudioGMS_SoundInstance_Play(double soundInstanceID, double loop);
FAUDIOGMSAPI void FAudioGMS_SoundInstance_Pause(double soundInstanceID);
@ -58,6 +55,7 @@ FAUDIOGMSAPI void FAudioGMS_SoundInstance_SetPan(double soundInstanceID, double
FAUDIOGMSAPI void FAudioGMS_SoundInstance_SetPitch(double soundInstanceID, double pitch);
FAUDIOGMSAPI void FAudioGMS_SoundInstance_SetVolume(double soundInstanceID, double volume);
FAUDIOGMSAPI void FAudioGMS_SoundInstance_Set3DPosition(double soundInstanceID, double x, double y, double z);
FAUDIOGMSAPI void FAudioGMS_SoundInstance_Set3DVelocity(double soundInstanceID, double xVelocity, double yVelocity, double zVelocity);
FAUDIOGMSAPI void FAudioGMS_SoundInstance_SetTrackPositionInSeconds(double soundInstanceID, double trackPositionInSeconds);
FAUDIOGMSAPI void FAudioGMS_SoundInstance_SetVolumeOverTime(double soundInstanceID, double volume, double milliseconds);
FAUDIOGMSAPI void FAudioGMS_SoundInstance_SetLowPassFilter(double soundInstanceID, double lowPassFilter, double Q);
@ -101,6 +99,7 @@ FAUDIOGMSAPI void FAudioGMS_SoundInstance_SetEffectChain(double soundInstanceID,
FAUDIOGMSAPI void FAudioGMS_SoundInstance_SetEffectGain(double soundInstanceID, double effectGain);
FAUDIOGMSAPI void FAudioGMS_SetListenerPosition(double x, double y, double z);
FAUDIOGMSAPI void FAudioGMS_SetListenerVelocity(double xVelocity, double yVelocity, double zVelocity);
FAUDIOGMSAPI void FAudioGMS_StopAll();