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

Sound loaded from an OGG or MP3 file. More...

#include <jngl/SoundFile.hpp>

Public Member Functions

 SoundFile (const std::string &filename, std::launch policy=std::launch::async)
 Load an OGG or MP3 file called filename.
 
 SoundFile (const SoundFile &)=delete
 
SoundFile & operator= (const SoundFile &)=delete
 
 SoundFile (SoundFile &&) noexcept
 
SoundFile & operator= (SoundFile &&) noexcept
 
void play ()
 Play the sound once. If called twice the sound would also play twice.
 
void play (Channel &)
 Play the sound once on the Channel.
 
void stop ()
 Stop the last started sound.
 
void stop (Channel &)
 Stop the last started sound of this SoundFile started on the Channel.
 
bool isPlaying ()
 Whether the sound is still playing at least once.
 
void loop ()
 Play the sound in a loop. Can also be stopped using stop()
 
void loop (Channel &)
 
SoundPanning playWithPanning ()
 Like play(), but the sound can be positioned in the stereo field.
 
SoundPanning playWithPanning (Channel &)
 
SoundPanning loopWithPanning ()
 Like loop(), but the sound can be positioned in the stereo field.
 
SoundPanning loopWithPanning (Channel &)
 
void setVolume (float v)
 Set volume in [0, ∞]. Default is 1.0f.
 
void load ()
 Block until the sound file has been fully decompressed and loaded.
 
std::chrono::milliseconds length () const
 Returns the duration in ms.
 
float progress () const
 Returns playing progress in [0, 1], can be used with length() to determine how much time has passed.
 
std::span< float > getSamples ()
 Returns the raw audio samples of this SoundFile.
 

Static Public Member Functions

static std::shared_ptr< SoundFile > get (std::string_view filename)
 Returns a shared pointer to a SoundFile from JNGL's internal cache, creates it with async loading if necessary.
 

Detailed Description

Sound loaded from an OGG or MP3 file.

JNGL keeps a list of loaded sound files, so there's no need for you to use this class directly - you can just use jngl::play or Channel::play. If you want to access the internally SoundFile from JNGL's cache before playing, use SoundFile::get.

Definition at line 34 of file SoundFile.hpp.

Constructor & Destructor Documentation

◆ SoundFile()

SoundFile ( const std::string & filename,
std::launch policy = std::launch::async )
explicit

Load an OGG or MP3 file called filename.

Loading can either happen on its own thread (std::launch::async) or the first time you try to play the file (std::launch::deferred).

Note
If the file doesn't exist this will not throw, but calling SoundFile::play, SoundFile::loop or SoundFile::load will.

Member Function Documentation

◆ get()

static std::shared_ptr< SoundFile > get ( std::string_view filename)
static

Returns a shared pointer to a SoundFile from JNGL's internal cache, creates it with async loading if necessary.

Example:

auto sound = jngl::SoundFile::get("sfx/foo.ogg");
auto other = jngl::SoundFile::get("sfx/foo.ogg");
assert(sound == other);
assert(!sound->isPlaying());
static std::shared_ptr< SoundFile > get(std::string_view filename)
Returns a shared pointer to a SoundFile from JNGL's internal cache, creates it with async loading if ...

◆ stop()

void stop ( Channel & )

Stop the last started sound of this SoundFile started on the Channel.

Note
If not using the main Channel (i.e. Channel::main()), this method should be used instead of stop().

◆ playWithPanning()

SoundPanning playWithPanning ( )
nodiscard

Like play(), but the sound can be positioned in the stereo field.

The sound only starts playing once SoundPanning::setPanning has been called for the first time and stops when the returned object gets destroyed. Use the returned object to control the sound, as stop(), isPlaying(), setVolume() and progress() of this SoundFile won't affect it.

Note
The Channel must not be moved while the returned object is still alive.

◆ loopWithPanning()

SoundPanning loopWithPanning ( )
nodiscard

Like loop(), but the sound can be positioned in the stereo field.

Unlike loop() this will always start a new sound, even if this SoundFile is already looping. See playWithPanning() for details.

◆ load()

void load ( )

Block until the sound file has been fully decompressed and loaded.

Exceptions
std::runtime_errorFile not found or decoding errors

◆ progress()

float progress ( ) const

Returns playing progress in [0, 1], can be used with length() to determine how much time has passed.

If the sound hasn't been started yet, returns 0.0f.


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