Skip to content

Setting up a 3D sound source

zedraken edited this page Oct 20, 2019 · 5 revisions

Overview

This howto explains you how to setup a 3D sound source. Such sound types are very useful if you want to add more realism to your 3D scene, adding some sound (or noise) that seems to come from a specific point in the 3D space. Moreover, when you move, the sound you will hear will also change, being amplified as long as you get closer to the sound source, or fading out if you move far away from the sound.

Principle

Basically speaking, you need to create two components:

  • a sound source
  • a listener

Each component is created within its own parent node so you first need to create nodes. By this way, each component inherits from its parent position. It becomes then possible to compute a sound attenuation depending on the distance between the sound source and the Listener.

Code example

Here is a basic code snippet that creates both the 3D sound source and the listener.

First, we create our 3D sound source.

    // Create the sound source node
    Node *sound_node = scene_->CreateChild("Sound source node");
    // Set the node position (sound will come from that position also)
    sound_node->SetPosition(Vector3(10,-20,45));
    // Create the sound source
    SoundSource3D *snd_source_3d = sound_node->CreateComponent<SoundSource3D>();
    // Set near distance (if listener is within that distance, no attenuation is computed)
    snd_source_3d->SetNearDistance(100);
    // Set far distance (distance after which no sound is heard)
    snd_source_3d->SetFarDistance(1000);

We also need to load a sound.

    // Load a sound. That sound will be the one that will be broadcast by the 3D sound source.
    Sound *my_sound = GetSubsystem<ResourceCache>()->GetResource<Sound>("Sounds/a_nice_sound.wav");
    // That sound shall be played continuously.
    my_sound->SetLooped(true);

Then, we can create the listener.

    // Create the listener node
    Node *listener_node = scene_->CreateChild("Listener node");
    // Set listener node position
    listener_node->SetPosition(Vector3(0, 0, 0));
    // Create the listener itself
    SoundListener *listener = listener_node->CreateComponent<SoundListener>();

Once a listener has been created, we need to tell the audio system that the listener is going to hear sounds coming from the 3D source.

    // Set the listener for that audio subsystem
    GetSubsystem<Audio>()->SetListener(listener);

Do not forget to play the sound, or you will hear nothing !

    // Play sound
    snd_source_3d->Play(mysound);

And that's it !

Now that all components are created, you can move either the sound source, or the listener, or both and listen at the results. You will even be able to know if the sound comes from your right or from your left depending on your orientation in the 3D space. You can also create several sound sources and set their position individually. You will then be able to build a full featured sound environment which is very important for a game for example.

Enjoy !