Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio: how to set distance attenuation/fall-off? #402

Closed
CallumCVM opened this issue Sep 17, 2023 · 4 comments · Fixed by #403
Closed

Audio: how to set distance attenuation/fall-off? #402

CallumCVM opened this issue Sep 17, 2023 · 4 comments · Fixed by #403
Labels
audio Related to DirectX Tool Kit for Audio documentation

Comments

@CallumCVM
Copy link

No description provided.

@walbourn walbourn added question audio Related to DirectX Tool Kit for Audio labels Sep 18, 2023
@walbourn
Copy link
Member

walbourn commented Sep 18, 2023

You control this through the standard X3DAUDIO_LISTENER and X3DAUDIO_EMITTER structures. DirectX Tool Kit for Audio has derived helper classes AudioListener and AudioEmitter.

The fall-off curves are provided via the X3DAUDIO_EMITTER structure per Microsoft Learn.

  X3DAUDIO_DISTANCE_CURVE *pVolumeCurve;
  X3DAUDIO_DISTANCE_CURVE *pLFECurve;
  X3DAUDIO_DISTANCE_CURVE *pLPFDirectCurve;
  X3DAUDIO_DISTANCE_CURVE *pLPFReverbCurve;
  X3DAUDIO_DISTANCE_CURVE *pReverbCurve;

Default curves are used if these pointers are nullptr per the documentation which are generally 'square-of-distance'. There is a helper AudioEmitter::EnableDefaultCurves which will instead set a number of built-in curves which are consistent with those used by legacy XACT which uses linear falloff. You can also provide pointers to your own custom curves.

https://github.com/microsoft/DirectXTK/wiki/AudioEmitter#custom-distance-curves

@CallumCVM
Copy link
Author

@walbourn I checked the function EnableDefaultCurves that you mentioned and it sets a volume curve that is 1.0f at a distance of both 0.0f AND 1.0f (aka max volume regardless of distance). Is this intentional? It doesn't seem to match the description you gave of it having linear falloff.

@walbourn
Copy link
Member

EnableDefaultCurves is implemented based on this code from XACT:

        if (SUCCEEDED(hr)) {
            static X3DAUDIO_DISTANCE_CURVE_POINT DefaultCurvePoints[2] = { 0.0f, 1.0f, 1.0f, 1.0f };
            static X3DAUDIO_DISTANCE_CURVE       DefaultCurve          = { (X3DAUDIO_DISTANCE_CURVE_POINT*)&DefaultCurvePoints[0], 2 };
            if (pEmitter->pVolumeCurve == NULL) {
                pEmitter->pVolumeCurve = &DefaultCurve;
            }
            if (pEmitter->pLFECurve == NULL) {
                pEmitter->pLFECurve = &DefaultCurve;
            }

            X3DAudioCalculate(X3DInstance, pListener, pEmitter, (X3DAUDIO_CALCULATE_MATRIX | X3DAUDIO_CALCULATE_DOPPLER | X3DAUDIO_CALCULATE_EMITTER_ANGLE), pDSPSettings);
        }

I see that my description of the default curve is probably wrong since it seems to turn off distance attenuation as you note. I'm not sure why as its' difficult to locate the old XACT docs these days and when you go look at them, they aren't really that detailed.

That said, the easy fix is to just use X3DAudioDefault_LinearCurve from the x3daudio.h header. I'll look at adding a EnableLinearCurves helper as well.

@CallumCVM
Copy link
Author

Thanks @walbourn, I did indeed implement it using the curves myself already as you suggested. Thanks for the help, issue resolved.

Possibly also worth noting that the default curves (those used when not calling EnableDefaultCurves or setting curves manually) do also not have any attenuation/falloff (that I could notice). Hope this helps.

@walbourn walbourn reopened this Sep 25, 2023
@walbourn walbourn linked a pull request Sep 25, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
audio Related to DirectX Tool Kit for Audio documentation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants