Urho3D Wiki
Register
Advertisement

Urho3D supports two kinds of sounds:

  • SoundSource for 2D background music or ambient sounds
  • SoundSource3D for 3D sound sources placed in the world

This article may be also interesting: Sound Recording and Editing with Audacity

General[]

Often needed include files:

#include <Urho3D/Audio/Sound.h>
#include <Urho3D/Audio/SoundSource3D.h>
#include <Urho3D/Audio/SoundListener.h>
#include <Urho3D/Audio/Audio.h>

Preparing sound playback (inside your Urho3D::Application class):

// for 3D sounds to work
SoundListener* listener=cameraNode_->CreateComponent<SoundListener>();
GetSubsystem<Audio>()->SetListener(listener);
// you can set master volumes for the different kinds if sounds, here 30% for music GetSubsystem<Audio>()->SetMasterGain(SOUND_MUSIC,0.3);

Urho has four different kinds of sounds (and master, defined in AudioDefs.h, they are only relevant if you want to use the global volume feature):

  • SOUND_MASTER
  • SOUND_EFFECT
  • SOUND_AMBIENT
  • SOUND_VOICE
  • SOUND_MUSIC

SoundSource[]

API Link

Loading a sound and playing a SoundSource:

Sound* sound=cache->GetResource<Sound>("Music/Ninja Gods.ogg");
sound->SetLooped(true); // sound can be set to be repeated // you can use an existing or a new node to append the sound to Node* node=scene_->CreateChild("Sound");
SoundSource* sound_source=node->CreateComponent<SoundSource>();
sound_source->SetSoundType(SOUND_MUSIC); // optional
sound_source->Play(sound);

(appending a non 3D sound source to a 3D scene node seems a bit weird, though the Urho "Sound Effect" sample is also doing that)

SoundSource3D[]

API Link

Attention: Urho does only support mono sound files to be used as a 3D sound source with panning. Stereo music files will be played as they are (in stereo) without panning at all. (Panning means hearing the sound only from the left if the sound source is on the left side).

This example shows a 3D sound source that is triggered by an event. You could logically also make it looping or playing from the start.

In header file:

Urho3D::SoundSource3D* sound_source_flag;
Urho3D::Sound* sound_flag;

in source file.

// loading the sound
sound_flag=cache->GetResource<Sound>("Sounds/jingle.ogg");
sound_source_flag=node_model->CreateComponent<SoundSource3D>();
sound_source_flag->SetNearDistance(1); // distance up to where the volume is 100%
sound_source_flag->SetFarDistance(55); // distance from where the volume is at 0%
sound_source_flag->SetSoundType(SOUND_EFFECT); ... // playing the sound sound_source_flag->Play(sound_flag);

You can also modify stuff like the frequency and volume for each sound individually:

// this randomises the pitch a bit so that not every sound
// source with this sound does not sound exactly the same
sound_torch_source->SetFrequency(sound_torch->GetFrequency()*Random(0.7f,1.3f));
Advertisement