-
-
Notifications
You must be signed in to change notification settings - Fork 29
Playing a sound
Romain Milbert edited this page Dec 26, 2021
·
4 revisions
To be able to play sounds, an AudioSystem must be added to initialize all the necessary elements & to process audio components:
auto& audio = world.addSystem<Raz::AudioSystem>();
// An AudioSystem can take one parameter: the audio device to be opened.
// If no parameter is given, the default device is picked
// To recover the available ones, Raz::AudioSystem::recoverDevices() can be called
We are now able to import our sound file and play it:
#include <RaZ/Data/WavFormat.hpp>
// An entity must have been created for a Sound component to be added
auto& sound = entity.addComponent<Raz::Sound>(Raz::WavFormat::load("test.wav"));
sound.play();
Several other actions are possible:
// Sounds can obviously be paused & stopped
sound.pause();
sound.stop();
sound.rewind(); // Stops & resets its state to INITIAL
// A sound's state can be recovered; it can be INITIAL, PLAYING, PAUSED or STOPPED
SoundState state = sound.recoverState();
// Helper functions are available to check for individual states: isPlaying(), isPaused() & isStopped()
// The gain (volume) & the pitch can be changed, between 0 & 1
sound.setGain(1.f);
sound.setPitch(1.f);
// A sound can be set to repeat itself when finished playing
sound.repeat(true); // false to stop repeating
// The elapsed time can be recovered. This is the duration for which
// the sound has been playing since it started
float elapsedTime = sound.recoverElapsedTime();
// Finally, a sound's position & velocity can be defined. These, with the Listener's position
// & orientation, are used to make it "sound" like it is actually coming from somewhere
sound.setPosition(Raz::Vec3f(0.f, 0.f, -1.f));
sound.setVelocity(Raz::Vec3f(0.f, 0.f, 1.f));
- Home
- How to build RaZ
- Getting started
- General usage knowledge
- Some examples...
- Playground
- Tutorials
- File formats
- Modules
- Debug