Hangman / TuningFork

Advanced 3D audio features for libGDX desktop users.
Apache License 2.0
31 stars 4 forks source link

TuningFork:4.2.0 - AudioDevice#disableHrtf() throws exception #14

Closed kumskov closed 10 months ago

kumskov commented 10 months ago

Enabling HRTF works, but the disable method crashes with

Caused by: java.lang.IllegalArgumentException: Missing termination

    at org.lwjgl.system.Checks.checkNTSafe(Checks.java:275)
    at org.lwjgl.openal.SOFTHRTF.alcResetDeviceSOFT(SOFTHRTF.java:130)
    at de.pottgames.tuningfork.AudioDevice.disableHrtf(AudioDevice.java:488)

Changing method code to

public void disableHrtf() {
        if (this.isHrtfSupported()) {
            // SET NEW DEVICE ATTRIBUTES
            final ContextAttributes oldAttributes = this.contextAttributes;
            final int[] attributes = new int[9];
            attributes[0] = SOFTHRTF.ALC_HRTF_SOFT;
            attributes[1] = ALC10.ALC_FALSE;
            attributes[2] = EXTEfx.ALC_MAX_AUXILIARY_SENDS;
            attributes[3] = this.config.getEffectSlots();
            attributes[4] = SOFTOutputLimiter.ALC_OUTPUT_LIMITER_SOFT;
            attributes[5] = this.config.isEnableOutputLimiter() ? ALC10.ALC_TRUE : ALC10.ALC_FALSE;
            attributes[6] = SOFTOutputMode.ALC_OUTPUT_MODE_SOFT;
            attributes[7] = this.config.getOutputMode().getAlId();
            attributes[8] = 0;
            this.contextAttributes = new ContextAttributes(attributes);

            // RESET DEVICE
            if (SOFTHRTF.alcResetDeviceSOFT(this.deviceHandle, attributes)) {
                this.hrtfEnabled = false;
                this.logger.info(this.getClass(), "HRTF disabled.");
            } else {
                this.contextAttributes = oldAttributes;
                this.logger.error(this.getClass(), "Failed to reset device: " + ALC10.alcGetString(this.deviceHandle, ALC10.alcGetError(this.deviceHandle)));
            }
        } else {
            this.logger.warn(this.getClass(), "HRTF is not supported by this device and was therefore never enabled.");
        }
    }

seems to fix the issue (Tried this with reflection), but I'm not sure if this solution is the correct one.

My dependencies are TuningFork:4.2.0, libgdx:1.12.1 and lwjgl-openal:3.3.3, running on MacOS 10.15.7

Hangman commented 10 months ago

Yep, that's the right fix. Refactored it a bit to use the actual ContextAttributes buffer, which I had created to prevent such issues. Thanks for the report! ❤️