Skip to content

Instantly share code, notes, and snippets.

@tomspilman
Last active November 24, 2016 03:34
Show Gist options
  • Save tomspilman/9464d6646c9dbe059f6c2e516a18df7e to your computer and use it in GitHub Desktop.
Save tomspilman/9464d6646c9dbe059f6c2e516a18df7e to your computer and use it in GitHub Desktop.
loat* SoundVoice::CalculatePanMatrix(float pan, float scale, float *matrix, int srcChannels)
{
if (srcChannels == 1)
{
matrix[0] = (pan >= 0 ? ( 1.f - pan ) : 1.f) * scale; // Left
matrix[1] = (pan <= 0 ? ( -pan - 1.f ) : 1.f) * scale; // Right
}
else if (srcChannels == 2)
{
if ( -1.0f <= pan && pan <= 0.0f )
{
matrix[0] = (0.5f * pan + 1.0f) * scale; // .5 when pan is -1, 1 when pan is 0
matrix[1] = (0.5f * -pan) * scale; // .5 when pan is -1, 0 when pan is 0
matrix[2] = 0.0f; // 0 when pan is -1, 0 when pan is 0
matrix[3] = (pan + 1.0f) * scale; // 0 when pan is -1, 1 when pan is 0
}
else
{
matrix[0] = (-pan + 1.0f) * scale; // 1 when pan is 0, 0 when pan is 1
matrix[1] = 0.0f; // 0 when pan is 0, 0 when pan is 1
matrix[2] = (0.5f * pan) * scale; // 0 when pan is 0, .5f when pan is 1
matrix[3] = (0.5f * -pan + 1.0f) * scale; // 1 when pan is 0. .5f when pan is 1
}
}
return matrix;
}
void SoundVoice::UpdateOutputMatrix(SoundSystem* system)
{
XAUDIO2_VOICE_DETAILS details;
memset(&details, 0, sizeof(details));
_voice->GetVoiceDetails(&details);
int srcChannelCount = details.InputChannels;
system->GetMasterVoice()->GetVoiceDetails(&details);
int dstChannelCount = details.InputChannels;
// Default to zero volume on all channels.
float panMatrix[16];
memset(panMatrix, 0, sizeof(panMatrix));
// Set the pan on the correct channels based on the reverb mix.
if (!(_reverbMix > 0.0f))
_voice->SetOutputMatrix(nullptr, srcChannelCount, dstChannelCount, CalculatePanMatrix(_pan, 1.0f, panMatrix, srcChannelCount));
else
{
_voice->SetOutputMatrix(system->GetReverbVoice(), srcChannelCount, dstChannelCount, CalculatePanMatrix(_pan, _reverbMix, panMatrix, srcChannelCount));
_voice->SetOutputMatrix(system->GetMasterVoice(), srcChannelCount, dstChannelCount, CalculatePanMatrix(_pan, 1.0f - (_reverbMix > 1.0f ? 1.0f : _reverbMix), panMatrix, srcChannelCount));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment