microsoft / DirectXTK

The DirectX Tool Kit (aka DirectXTK) is a collection of helper classes for writing DirectX 11.x code in C++
https://walbourn.github.io/directxtk/
MIT License
2.55k stars 506 forks source link

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

Closed CallumCVM closed 11 months ago

walbourn commented 11 months ago

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 commented 11 months ago

@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 commented 11 months ago

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 commented 11 months ago

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.