Calamity Engine 1.0.0
A cross-platform 2D game engine written in C++ and SDL3.
Loading...
Searching...
No Matches
AudioSource Class Reference

#include <audio.hpp>

Inheritance diagram for AudioSource:
Component

Public Member Functions

 AudioSource (const std::string &path)
 
float getVolume () const
 
float getPitch () const
 
bool getPlaying () const
 
void setPitch (float pitch)
 
void setVolume (float volume)
 
void play ()
 
void stop ()
 
void pause ()
 
bool loadAudio ()
 
void update (float deltaTime)
 
void exit ()
 
template<class Archive >
void save (Archive &ar) const
 
template<class Archive >
void load (Archive &ar)
 
- Public Member Functions inherited from Component
virtual ~Component ()=default
 
virtual void render (std::shared_ptr< Window > window)
 
virtual void initialize ()
 
virtual void physicsUpdate ()
 
virtual void input (InputEvent &event)
 
virtual void postLoad ()
 
NodegetNode () const
 
void setNode (Node *n)
 
template<class Archive >
void save (Archive &ar) const
 
template<class Archive >
void load (Archive &ar)
 

Public Attributes

std::string path
 
bool loop = false
 
Signal finished
 
Signal looped
 
Signal stopped
 
Signal paused
 

Detailed Description

AudioSource

The source of a one-dimensional sound. For now, you can only load WAV files because of SDL audio streams. You can modify its pitch and volume and loop it. It also has a Signal for when the sound finishes playing.

Example Usage:

std::shared_ptr<Node> node = std::make_shared<Node>();
node->addComponent(std::make_shared<AudioSource>("res://assets/sound.wav"));
// play sound (make sure to do this AFTER the audio source was initialized)
sound->play();

Properties and usages

You can set and get its pitch:

sound->setPitch(sound->getPitch() + 0.2f); // up the pitch by 0.2

You can also modify its volume:

sound->setVolume(0.4f); // set the volume to 40%;

It also has a loop attribute:

sound->loop = true; // The sound now loops!

You can also register callbacks to all of the events it has for when the sound is finished, looped, stopped and paused.

sound->paused.connect([]() {
Logger::info("sound paused!");
});
sound->pause();
// Output: sound paused!
static void info(fmt::format_string< Args... > fmt, Args &&...args)
Definition logger.hpp:28

Make sure to also check out the audio example for more information!

Constructor & Destructor Documentation

◆ AudioSource()

AudioSource::AudioSource ( const std::string &  path)

Member Function Documentation

◆ exit()

void AudioSource::exit ( )
virtual

Reimplemented from Component.

◆ getPitch()

float AudioSource::getPitch ( ) const

◆ getPlaying()

bool AudioSource::getPlaying ( ) const

◆ getVolume()

float AudioSource::getVolume ( ) const

◆ load()

template<class Archive >
void AudioSource::load ( Archive &  ar)
inline

◆ loadAudio()

bool AudioSource::loadAudio ( )

◆ pause()

void AudioSource::pause ( )

◆ play()

void AudioSource::play ( )

◆ save()

template<class Archive >
void AudioSource::save ( Archive &  ar) const
inline

◆ setPitch()

void AudioSource::setPitch ( float  pitch)

◆ setVolume()

void AudioSource::setVolume ( float  volume)

◆ stop()

void AudioSource::stop ( )

◆ update()

void AudioSource::update ( float  deltaTime)
virtual

Reimplemented from Component.

Member Data Documentation

◆ finished

Signal AudioSource::finished

◆ loop

bool AudioSource::loop = false

◆ looped

Signal AudioSource::looped

◆ path

std::string AudioSource::path

◆ paused

Signal AudioSource::paused

◆ stopped

Signal AudioSource::stopped

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