Sound spatialization

I am new to openfl and I want to play a sound with some spatialization effect (so that the user can differentiate if the sound “comes” from left, right, top or bottom). As native targets are using openAL, it should probably be possible (openAL supports 3D sound spatialization) but I don’t know how.
If somebody could help and post a small example, it would be highly appreciated.

The OpenFL (Flash) API supports right/left positioning, but does not expose the 3D sound positioning directly. However, when you are in native, you can use the OpenAL bindings directly so long as you did not compile with -Dlegacy, allowing you to use OpenAL yourself to do this :slight_smile:

1 Like

can u give a short example on how to access directly OpenAL methods?
For example if I want to play a sound simulated as coming from top of the screen, how should I call my openAL function?
Also, -Dlegacy is an option that is false by default right? So if I understand well, if I compile with lime without any option flag for my native targets it should be OK?

Yep :smile:

Here’s an example where we use the OpenAL API in Lime:

I suppose another option would be for us to extend the AudioSource API in Lime to expose this, should make this easier to use

Sorry, but this “example” doesn’t really help me.
I will probably sound like a complete noob (which i am, actually :wink: ) but, if I have a Sound or SoundChannel object, how did I access the openAL properties?
In your example, I see that to access al, you do:

switch (AudioManager.context) {
      case OPENAL (alc, al):

but actually I don’t understand these lines (I don’t see where OPENAL, alc and al are defined)
So, from a Sound or SoundChannel, how can I get the linked “al” object so that I can do something like:

al.listener3f (AL.POSITION, 0, 0, 0);

or any other openAL parametter modification ?

You might be able to do something like:

var soundChannel = ();
var audioSource = @:privateAccess (soundChannel.__source);
var alSourceID = @:privateAccess (;
AL.source3f (alSourceID, AL_POSITION, 0, 0, 0);

These use private objects, so I would not recommend it thoroughly, but this help get the ball rolling in terms of touching the OpenAL API with your audio, and understanding what you want to do with it, and how.

In the future, I think that we should look adding to the Lime AudioSource class in order to set 3D position, something like:

audioSource.location = new Vec3 (0, 0, 0);

This would skip the last two lines of code. When the OpenFL soundChannel sets the pan value, perhaps it would be setting a location value internally. However, for full access to 3D sound, all someone would need to is use the Lime audio API directly instead of the OpenFL API in order to do this

Does this make some sense? I apologize to dive too deeply into source files too quickly :slight_smile:

As soon as I do:


my android application is not working anymore (black screen at launch). Whereas if I do not import the AL class (in wich case I can not directly access to openAL properties with your “trick”), everything is working fine.
So How can I do? Did I need to define something specific in my project.xml file to be able to use lime classes?

Any idea on how to fix it? (what I don’t understand is that some classes like also import and it doesn’t seem to pose any problem to android when the import is in those classes…)

Are you using -Dlegacy when targeting Android? Or using HaxeFlixel? (which defaults to -Dlegacy)?

If you use Legacy, the Lime API is unavailable :frowning:

Yes I am using HaxeFlixel. Didn’t knew it was using -Dlegacy by default… is there any way to use HaxeFlixel with the new openfl? In all my code, I am using openfl standard classes (not the openfl._legacy ones) and it seems to be just working fine though (exept for the openAL ones) so it might be possible to use HaxeFlixel with the new openfl build only. No?

I think Flixel uses -Dnext to use OpenFL 3 / Lime 2 without legacy

Thanks for the reply. Actually I tried it but it seems that the next openfl version is not mature enaugh to be used in my project. For example the SharedObject, used by the haxeflixel game save slots system, is not correctly implemented. Also, some class seems to be missing (for example, trying to import openfl.Lib results in an “Unexpected import” error at building).
So I guess I would have to stick with the legacy openfl for now and forget the 3D sound spatialization in my game :frowning:

All in good time, we’ll all keep working on stabilizing things :slight_smile:

Hi. Is “panning” not working on -Dlegacy for windows target?

Have you tried mono sound?

Thanks. It works with both Wav and Ogg

Yeah, I think OpenAL does not support panning of stereo sound :slight_smile:

Is the idea of exposing openAL source3f() function, for ccp and html5 targets, with something like: audioSource.location = new Vec3(0,0,0) still on the TODO list or as this been abandoned?
Also, do you know what is the default listener location set by openFL in openAL? is it (0,0,0) or is is something else?