JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
SoundPanning Class Reference

A sound which can be positioned in the stereo field. More...

#include <jngl/SoundPanning.hpp>

Public Member Functions

 SoundPanning ()
 Creates an empty object on which all methods do nothing.
 
 ~SoundPanning ()
 Stops the sound.
 
 SoundPanning (const SoundPanning &)=delete
 
SoundPanning & operator= (const SoundPanning &)=delete
 
 SoundPanning (SoundPanning &&) noexcept
 
SoundPanning & operator= (SoundPanning &&) noexcept
 Stops the sound this object has been playing and takes over the other one.
 
void setPanning (float panning)
 Set the stereo position, the first call starts playing the sound.
 
void stop ()
 Stops the sound. If it hasn't been started yet, it won't start anymore.
 
bool isPlaying () const
 Whether the sound has been started (using setPanning) and is still playing.
 
void setVolume (float volume)
 Set volume in [0, ∞].
 

Friends

class SoundFile
 

Detailed Description

A sound which can be positioned in the stereo field.

Returned by SoundFile::playWithPanning and SoundFile::loopWithPanning. The sound only starts playing once setPanning has been called for the first time, so that it never starts at the wrong position.

The sound is tied to the lifetime of this object: Destroying it (or assigning another SoundPanning to it) stops the sound. This makes it a good fit as a member of an entity in your game world.

Example:

class Enemy {
public:
Enemy() : engineSound(jngl::SoundFile::get("sfx/engine.ogg")->loopWithPanning()) {}
void step() {
// ... move the enemy ...
engineSound.setPanning(position.x / jngl::getScreenSize().x * 2);
}
private:
jngl::Vec2 position;
jngl::SoundPanning engineSound; // stops when the Enemy gets destroyed
};
Sound loaded from an OGG or MP3 file.
Definition SoundFile.hpp:34
A sound which can be positioned in the stereo field.
Two-dimensional vector.
Definition Vec2.hpp:36
double x
x component
Definition Vec2.hpp:45
JNGL's main namespace.
Vec2 getScreenSize()
Returns the size of the useable draw area (excluding letter-boxing) in screen pixels.
Note
The Channel on which the sound plays must not be moved while this object is alive.

Definition at line 44 of file SoundPanning.hpp.

Member Function Documentation

◆ setPanning()

void setPanning ( float panning)

Set the stereo position, the first call starts playing the sound.

Parameters
panning-1.0f = left, 0.0f = center, 1.0f = right. Values outside of [-1, 1] will be clamped.

Like with a real head, the channel further away from the sound source gets quieter (-10 dB at ±1) and slightly delayed. This can be called every frame, e.g. to follow a moving object. Changes are smoothed, so a fast moving sound will also get a slight Doppler effect.

Once the sound has finished playing (or has been stopped) this does nothing.

◆ setVolume()

void setVolume ( float volume)

Set volume in [0, ∞].

Default is 1.0f

Can be called before the sound has been started.

Friends And Related Symbol Documentation

◆ SoundFile

friend class SoundFile
friend

Definition at line 82 of file SoundPanning.hpp.


The documentation for this class was generated from the following file: