LWJGL Forum

Programming => OpenAL => Topic started by: Dikill on March 02, 2016, 00:29:03

Title: [SOLVED] OpenAl HRTF
Post by: Dikill on March 02, 2016, 00:29:03
Hi everybody !  :)

I'd like to know if the openal part of the library supports the HRTF tech (aka 3D binaural sound).
https://en.wikipedia.org/wiki/Head-related_transfer_function (https://en.wikipedia.org/wiki/Head-related_transfer_function)
I made some research on internet but I found contrary answers.  :-\
If it's included in openAl, is it enabled by default or do we have to make some manipulations to make it work.

Thanks a lot for helping
Title: Re: OpenAl HRTF
Post by: spasi on March 02, 2016, 00:55:02
LWJGL includes OpenAL-Soft (http://openal-soft.org/), which is a software OpenAL implementation with HRTF support. LWJGL also supports the ALC_SOFT_HRTF (http://openal-soft.org/openal-extensions/SOFT_HRTF.txt) extension. There's information in there on how to request HRTF functionality.

OpenAL-Soft can also be configured with a config file (https://github.com/kcat/openal-soft/blob/master/alsoftrc.sample), you can use the ALSOFT_CONF environment variable to point to it. Other useful environments variables, here (https://github.com/kcat/openal-soft/blob/master/env-vars.txt).
Title: Re: OpenAl HRTF
Post by: Dikill on March 02, 2016, 19:59:34
Thank you for the quick answer
I didn't find a way to enable it in my java code.
The 3.0.0a version of LWJGL contains a java file SOFTHRTF that is not in the last version (3.0.0b) and nothing in the doc seem to loke like HRTF.
How do I initialise the device and/or openAl context in java to have HRTF functioning ?

I tried:
AlDevice.create(ALDevice.create, 44100, 60, false);
but I'm not sure it worked :s
Title: Re: OpenAl HRTF
Post by: spasi on March 02, 2016, 20:20:00
Support for ALC_SOFT_HRTF was added after the release of 3.0.0b. Please download the latest nightly build, it's there.

For OpenAL sample code, see these demos (https://github.com/LWJGL/lwjgl3/tree/master/modules/core/src/test/java/org/lwjgl/demo/openal). To create a context with HRTF support, you must use the ALContext.create(ALDevice, IntBuffer) method. The attributes buffer must contain the values [ALC_HRTF_SOFT, ALC_TRUE, 0], as explained in the extension.
Title: Re: OpenAl HRTF
Post by: Dikill on March 02, 2016, 22:43:21
I read the demo and tried to do the same
but when I ask for the state I got 0x5 which is ALC_HRTF_UNSUPPORTED_FORMAT_SOFT ...
Here's the code


      ALDevice device = ALDevice.create(null);
      if(device == null)
          throw new IllegalStateException("Failed to open device");
     
      ALCCapabilities caps = device.getCapabilities();


      System.out.println("OpenALC10: " + caps.OpenALC10);
      System.out.println("OpenALC11: " + caps.OpenALC11);
      System.out.println("caps.ALC_EXT_EFX = " + caps.ALC_EXT_EFX);
     
      if ( caps.OpenALC11 ) {
          List<String> devices = ALC.getStringList(NULL, ALC_ALL_DEVICES_SPECIFIER);
          if ( devices == null )
              ALUtil.checkALCError(null);
          else {
              for ( int i = 0; i < devices.size(); i++ )
                  System.out.println(i + ": " + devices.get(i));
          }
      }
     
      String defaultDeviceSpecifier = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);

      System.out.println("Default device: " + defaultDeviceSpecifier);
     
      IntBuffer buffer = (IntBuffer) BufferUtils.createIntBuffer(3).put(new int[]{HRTF.ALC_HRTF_SOFT, ALC10.ALC_TRUE, 0} ).rewind();
      context = ALContext.create(device, buffer);
      int response = alcGetInteger(device.address(), HRTF.ALC_HRTF_STATUS_SOFT);
     
      System.out.println(response == HRTF.ALC_HRTF_ENABLED_SOFT);
      System.out.println("HRTF STATE: 0x" + Integer.toHexString(response));
      System.out.println("Frequency :" + alcGetInteger(device.address(), ALC10.ALC_FREQUENCY));
     
      System.out.println(ALC10.alcGetString(context.getDevice().address(), HRTF.ALC_HRTF_SPECIFIER_SOFT));


And the result

QuoteOpenALC10: true
OpenALC11: true
caps.ALC_EXT_EFX = true
0: Casque (2- Périphérique High Definition Audio) on OpenAL Soft
1: Haut-parleurs (2- Périphérique High Definition Audio) on OpenAL Soft
Default device: OpenAL Soft
false
HRTF STATE: 0x5
Frequency :44100
Title: Re: OpenAl HRTF
Post by: spasi on March 02, 2016, 23:57:03
OpenAL-Soft needs HRTF data for the extension to work. On Windows, by default OpenAL-Soft uses the %APPDATA% folder for this (download the binary distribution and see readme.txt for details). But you can easily override it with the ALSOFT_CONF environment variable:

- Download the sample config file (https://github.com/kcat/openal-soft/blob/master/alsoftrc.sample), rename it (say alsoft.conf), put it with your app.
- Download the sample HRTFs (https://github.com/kcat/openal-soft/tree/master/hrtf), put them in a folder.
- Set the hrtf-paths property in the config file to the folder where you put the HRTFs.
- Set the ALSOFT_CONF environment variable to the config file path.

If you now query ALC_NUM_HRTF_SPECIFIERS_SOFT, before creating the context, it should return 2. Querying ALC_HRTF_SPECIFIER_SOFT with alcGetStringiSOFT, using indices 0 and 1, should return the two HRTFs. If you get to that point, HRTF should be enabled when you create the context with ALC_HRTF_SOFT set to ALC_TRUE.
Title: Re: OpenAl HRTF
Post by: Dikill on March 08, 2016, 19:54:32
Problem Solved, Thank you !!!  ;D
Title: Re: [SOLVED] OpenAl HRTF
Post by: CosmicNeanderthal on July 03, 2016, 16:12:53
Hi guys,

I'm following you up until you mention setting the ALSOFT_CONF environment variable to the config file path. I can't seem to figure out how or where exactly to do this. I'm kind of lost here.

If someone could point me in the right direction I would really appreciate it.

Thanks.
Title: Re: [SOLVED] OpenAl HRTF
Post by: Kai on July 03, 2016, 17:08:54
"Environment variables" are an operating-system feature. Every operating system has its ways to set environment variables either via a system setting or/and as part of the command shell session. Use Google to find the way for your operating system.

The GitHub openal-soft link mentioned by Spasi's "Other useful environments variables, here." also updated:
- https://github.com/kcat/openal-soft/blob/master/docs/env-vars.txt
Title: Re: [SOLVED] OpenAl HRTF
Post by: spasi on July 04, 2016, 13:12:28
I have added an HRTF demo (https://github.com/LWJGL/lwjgl3/blob/a9ec74f2d7c831b4d6600af0860f49be236f255b/modules/core/src/test/java/org/lwjgl/demo/openal/HRTFDemo.java) to the LWJGL repository.
Title: Re: [SOLVED] OpenAl HRTF
Post by: CosmicNeanderthal on July 04, 2016, 21:38:25
Oh I see. Thank you very much, managed to get it working now!

Apologies for my ignorance.  ;D