forked from MoonsideGames/FAudioGMS
fix effect chains making sounds mono
parent
22d759ea11
commit
0e04346b8c
119
src/FAudioGMS.c
119
src/FAudioGMS.c
|
@ -506,6 +506,57 @@ void FAudioGMS_Init(double spatialDistanceScale, double timestep)
|
|||
fflush(stdout);
|
||||
}
|
||||
|
||||
double FAudioGMS_StaticSound_LoadWAV(char *filePath)
|
||||
{
|
||||
RETURN_ON_NULL_DEVICE(-1.0)
|
||||
drwav_uint64 frameCount;
|
||||
|
||||
FAudioGMS_StaticSound *sound = SDL_malloc(sizeof(FAudioGMS_StaticSound));
|
||||
float *pSampleData = drwav_open_file_and_read_pcm_frames_f32(
|
||||
filePath,
|
||||
&sound->channels,
|
||||
&sound->samplesPerSecond,
|
||||
&frameCount,
|
||||
NULL);
|
||||
if (pSampleData == NULL)
|
||||
{
|
||||
Log("Error opening WAV file: ");
|
||||
Log(filePath);
|
||||
SDL_free(sound);
|
||||
return -1;
|
||||
}
|
||||
|
||||
sound->buffer.AudioBytes = (uint32_t)(frameCount * sound->channels * sizeof(float));
|
||||
sound->buffer.Flags = 0;
|
||||
sound->buffer.LoopBegin = 0;
|
||||
sound->buffer.LoopCount = 0;
|
||||
sound->buffer.LoopLength = 0;
|
||||
sound->buffer.PlayBegin = 0;
|
||||
sound->buffer.PlayLength = frameCount;
|
||||
sound->buffer.pAudioData = (uint8_t *)pSampleData;
|
||||
sound->buffer.pContext = NULL;
|
||||
|
||||
sound->lengthInSeconds = frameCount / sound->samplesPerSecond;
|
||||
|
||||
if (device->staticSoundIndexStack.count > 0)
|
||||
{
|
||||
sound->id = IdStack_Pop(&device->staticSoundIndexStack);
|
||||
}
|
||||
else
|
||||
{
|
||||
sound->id = device->staticSoundCount;
|
||||
|
||||
device->staticSounds = SDL_realloc(
|
||||
device->staticSounds,
|
||||
(device->staticSoundCount + 1) * sizeof(FAudioGMS_StaticSound *));
|
||||
device->staticSoundCount += 1;
|
||||
}
|
||||
|
||||
device->staticSounds[sound->id] = sound;
|
||||
|
||||
return (double)sound->id;
|
||||
}
|
||||
|
||||
/* Taken from
|
||||
* https://github.com/FNA-XNA/FNA/blob/master/src/Audio/SoundEffectInstance.cs
|
||||
*/
|
||||
|
@ -563,61 +614,8 @@ static void SetPanMatrixCoefficients(FAudioGMS_SoundInstance *instance)
|
|||
}
|
||||
}
|
||||
|
||||
double FAudioGMS_StaticSound_LoadWAV(char *filePath)
|
||||
static void FAudioGMS_INTERNAl_SoundInstance_ApplyPan(FAudioGMS_SoundInstance* instance)
|
||||
{
|
||||
RETURN_ON_NULL_DEVICE(-1.0)
|
||||
drwav_uint64 frameCount;
|
||||
|
||||
FAudioGMS_StaticSound *sound = SDL_malloc(sizeof(FAudioGMS_StaticSound));
|
||||
float *pSampleData = drwav_open_file_and_read_pcm_frames_f32(
|
||||
filePath,
|
||||
&sound->channels,
|
||||
&sound->samplesPerSecond,
|
||||
&frameCount,
|
||||
NULL);
|
||||
if (pSampleData == NULL)
|
||||
{
|
||||
Log("Error opening WAV file: ");
|
||||
Log(filePath);
|
||||
SDL_free(sound);
|
||||
return -1;
|
||||
}
|
||||
|
||||
sound->buffer.AudioBytes = (uint32_t)(frameCount * sound->channels * sizeof(float));
|
||||
sound->buffer.Flags = 0;
|
||||
sound->buffer.LoopBegin = 0;
|
||||
sound->buffer.LoopCount = 0;
|
||||
sound->buffer.LoopLength = 0;
|
||||
sound->buffer.PlayBegin = 0;
|
||||
sound->buffer.PlayLength = frameCount;
|
||||
sound->buffer.pAudioData = (uint8_t *)pSampleData;
|
||||
sound->buffer.pContext = NULL;
|
||||
|
||||
sound->lengthInSeconds = frameCount / sound->samplesPerSecond;
|
||||
|
||||
if (device->staticSoundIndexStack.count > 0)
|
||||
{
|
||||
sound->id = IdStack_Pop(&device->staticSoundIndexStack);
|
||||
}
|
||||
else
|
||||
{
|
||||
sound->id = device->staticSoundCount;
|
||||
|
||||
device->staticSounds = SDL_realloc(
|
||||
device->staticSounds,
|
||||
(device->staticSoundCount + 1) * sizeof(FAudioGMS_StaticSound *));
|
||||
device->staticSoundCount += 1;
|
||||
}
|
||||
|
||||
device->staticSounds[sound->id] = sound;
|
||||
|
||||
return (double)sound->id;
|
||||
}
|
||||
|
||||
static void FAudioGMS_INTERNAL_SoundInstance_SetPan(FAudioGMS_SoundInstance *instance, float pan)
|
||||
{
|
||||
pan = SDL_max(-1.0, SDL_min(1.0, pan));
|
||||
instance->pan = pan;
|
||||
SetPanMatrixCoefficients(instance);
|
||||
|
||||
FAudioVoice_SetOutputMatrix(
|
||||
|
@ -629,6 +627,13 @@ static void FAudioGMS_INTERNAL_SoundInstance_SetPan(FAudioGMS_SoundInstance *ins
|
|||
0);
|
||||
}
|
||||
|
||||
static void FAudioGMS_INTERNAL_SoundInstance_SetPan(FAudioGMS_SoundInstance *instance, float pan)
|
||||
{
|
||||
pan = SDL_max(-1.0, SDL_min(1.0, pan));
|
||||
instance->pan = pan;
|
||||
FAudioGMS_INTERNAl_SoundInstance_ApplyPan(instance);
|
||||
}
|
||||
|
||||
static void FAudioGMS_INTERNAL_SoundInstance_UpdatePitch(FAudioGMS_SoundInstance *instance)
|
||||
{
|
||||
float doppler = 1.0f;
|
||||
|
@ -1750,7 +1755,7 @@ static void FAudioGMS_INTERNAL_Voice_SetEffectGain(FAudioGMS_Voice *voice, float
|
|||
float *outputMatrix = SDL_stack_alloc(float, voice->effectSends.SendCount);
|
||||
|
||||
outputMatrix[0] = effectGain;
|
||||
if (voice->effectSends.SendCount == 2)
|
||||
if (voice->handle->outputChannels == 2)
|
||||
{
|
||||
outputMatrix[1] = effectGain;
|
||||
}
|
||||
|
@ -1758,7 +1763,7 @@ static void FAudioGMS_INTERNAL_Voice_SetEffectGain(FAudioGMS_Voice *voice, float
|
|||
FAudioVoice_SetOutputMatrix(
|
||||
voice->handle,
|
||||
voice->effectVoice,
|
||||
voice->effectSends.SendCount,
|
||||
voice->handle->outputChannels,
|
||||
1,
|
||||
outputMatrix,
|
||||
0);
|
||||
|
@ -1926,6 +1931,8 @@ void FAudioGMS_SoundInstance_SetEffectChain(
|
|||
if (instance != NULL && effectChain != NULL)
|
||||
{
|
||||
FAudioGMS_INTERNAL_SetEffectChain(&instance->voice, effectChain, effectGain);
|
||||
/* re-apply pan after setting chain because sends may have changed */
|
||||
FAudioGMS_INTERNAl_SoundInstance_ApplyPan(instance);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue