GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
grem::audio::Sound Class Reference

Container for a particular sound wave that can be played in a SoundStage. More...

#include <GREM/audio/Sound.hpp>

Inheritance diagram for grem::audio::Sound:

Public Member Functions

 Sound () noexcept=default
 Construct an invalid sound that must be reassigned before use.
 Sound (const Filesystem &filesystem, CStringView filepath, const SoundOptions &options={})
 Load a sound from a virtual file.
 Sound (Span< const byte > fileContents, const SoundOptions &options={})
 Load a sound from an in-memory file.
 Sound (Span< const uint8_t > samples, float sampleRate, size_t channelCount=1, const SoundOptions &options={})
 Construct a sound wave from a raw array of samples.
 Sound (Span< const int16_t > samples, float sampleRate, size_t channelCount=1, const SoundOptions &options={})
 Construct a sound wave from a raw array of samples.
 Sound (Span< const float32_t > samples, float sampleRate, size_t channelCount=1, const SoundOptions &options={})
 Construct a sound wave from a raw array of samples.
 operator bool () const noexcept
 Check if the sound has a value.
template<filter F>
void setFilter (size_t filterSlotIndex, const F &newFilter)
 Set the filter at a particular filter slot of the sound.
void removeFilter (size_t filterSlotIndex) noexcept
 Remove the filter at a particular filter slot of the sound.
Duration getDuration () const noexcept
 Get the duration of the sound.
void * get () noexcept
 Get an opaque handle to the internal representation of the sound.
const void * get () const noexcept
 Get an opaque handle to the internal representation of the sound.

Static Public Attributes

static constexpr size_t MAX_FILTER_COUNT = 8
 Maximum number of filters that can be applied simultaneously to a sound.

Protected Member Functions

 Sound (void *handle, const SoundOptions &options)

Detailed Description

Container for a particular sound wave that can be played in a SoundStage.

A single loaded sound can be used to spawn multiple sound instances that play the same sound at different times or in parallel, and with potentially varying volumes, positions and velocities.

Constructor & Destructor Documentation

◆ Sound() [1/7]

grem::audio::Sound::Sound ( )
defaultnoexcept

Construct an invalid sound that must be reassigned before use.

◆ Sound() [2/7]

grem::audio::Sound::Sound ( const Filesystem & filesystem,
CStringView filepath,
const SoundOptions & options = {} )

Load a sound from a virtual file.

The supported file formats are:

  • Vorbis (.ogg)
  • RIFF (.wav)
  • FLAC (.flac)
  • MP3 (.mp3)
Parameters
filesystemfilesystem to load the file from.
filepathvirtual filepath of the sound file to load.
optionssound options, see SoundOptions.
Exceptions
File::Erroron failure to open the file.
audio::Erroron failure to load a sound from the file.
std::length_errorif the maximum sound file size was exceeded.
std::bad_allocon allocation failure.
Note
The file format is determined entirely from the file contents; the filename extension is not taken into account.

◆ Sound() [3/7]

grem::audio::Sound::Sound ( Span< const byte > fileContents,
const SoundOptions & options = {} )
explicit

Load a sound from an in-memory file.

The supported file formats are:

  • Vorbis (.ogg)
  • RIFF (.wav)
  • FLAC (.flac)
  • MP3 (.mp3)
Parameters
fileContentscontents of the sound file to load.
optionssound options, see SoundOptions.
Exceptions
audio::Erroron failure to load a sound from the file.
std::length_errorif the maximum sound file size was exceeded.
std::bad_allocon allocation failure.

◆ Sound() [4/7]

grem::audio::Sound::Sound ( Span< const uint8_t > samples,
float sampleRate,
size_t channelCount = 1,
const SoundOptions & options = {} )

Construct a sound wave from a raw array of samples.

Parameters
samplesarray of unsigned 8-bit samples.
sampleRatesample rate of the sound, in Hertz. For example: 44100.0f, 48000.0f or 96000.0f.
channelCountnumber of sound channels. The number of samples must be divisible by this number.
optionssound options, see SoundOptions.
Exceptions
audio::Erroron failure to create the sound.
std::length_errorif the maximum sound wave size was exceeded.
std::bad_allocon allocation failure.

◆ Sound() [5/7]

grem::audio::Sound::Sound ( Span< const int16_t > samples,
float sampleRate,
size_t channelCount = 1,
const SoundOptions & options = {} )

Construct a sound wave from a raw array of samples.

Parameters
samplesarray of signed 16-bit samples.
sampleRatesample rate of the sound, in Hertz. For example: 44100.0f, 48000.0f or 96000.0f.
channelCountnumber of sound channels. The number of samples must be divisible by this number.
optionssound options, see SoundOptions.
Exceptions
audio::Erroron failure to create the sound.
std::length_errorif the maximum sound wave size was exceeded.
std::bad_allocon allocation failure.

◆ Sound() [6/7]

grem::audio::Sound::Sound ( Span< const float32_t > samples,
float sampleRate,
size_t channelCount = 1,
const SoundOptions & options = {} )

Construct a sound wave from a raw array of samples.

Parameters
samplesarray of floating-point 32-bit samples.
sampleRatesample rate of the sound, in Hertz. For example: 44100.0f, 48000.0f or 96000.0f.
channelCountnumber of sound channels. The number of samples must be divisible by this number.
optionssound options, see SoundOptions.
Exceptions
audio::Erroron failure to create the sound.
std::length_errorif the maximum sound wave size was exceeded.
std::bad_allocon allocation failure.

◆ Sound() [7/7]

grem::audio::Sound::Sound ( void * handle,
const SoundOptions & options )
protected

Member Function Documentation

◆ operator bool()

grem::audio::Sound::operator bool ( ) const
inlineexplicitnoexcept

Check if the sound has a value.

Returns
true if the sound has a value, false otherwise.

◆ setFilter()

template<filter F>
void grem::audio::Sound::setFilter ( size_t filterSlotIndex,
const F & newFilter )
inline

Set the filter at a particular filter slot of the sound.

This replaces any existing filter at the given slot.

Parameters
filterSlotIndexfilter slot index to set the filter of. Must be less than Sound::MAX_FILTER_COUNT.
newFilternew filter to insert.
Exceptions
audio::Erroron failure to create the filter.
std::length_errorif the maximum filter size was exceeded.
std::bad_allocon allocation failure.
Remarks
A custom filter is any trivially copyable and trivially destructible type that provides the following public member function: cpp void filter(Span<float> samples, size_t channelCount, size_t sampleCountPerChannel, float sampleRate, double time); Filters may also provide a field named parameters of an aggregate type containing a scalar field for each filter parameter that should be adjustable on-the-fly.

◆ removeFilter()

void grem::audio::Sound::removeFilter ( size_t filterSlotIndex)
noexcept

Remove the filter at a particular filter slot of the sound.

Parameters
filterSlotIndexfilter slot index to clear the filter of. Must be less than Sound::MAX_FILTER_COUNT.

◆ getDuration()

Duration grem::audio::Sound::getDuration ( ) const
nodiscardnoexcept

Get the duration of the sound.

Returns
the time duration between the start and end of the sound when played at 1x speed.

◆ get() [1/2]

void * grem::audio::Sound::get ( )
inlinenodiscardnoexcept

Get an opaque handle to the internal representation of the sound.

Returns
an untyped non-owning pointer to the internal representation of the sound.
Note
This function is used internally by the SoundStage implementation and is not intended to be used outside of it. The returned handle has no meaning to application code.

◆ get() [2/2]

const void * grem::audio::Sound::get ( ) const
inlinenodiscardnoexcept

Get an opaque handle to the internal representation of the sound.

Returns
an untyped non-owning read-only pointer to the internal representation of the sound.
Note
This function is used internally by the SoundStage implementation and is not intended to be used outside of it. The returned handle has no meaning to application code.

Member Data Documentation

◆ MAX_FILTER_COUNT

size_t grem::audio::Sound::MAX_FILTER_COUNT = 8
staticconstexpr

Maximum number of filters that can be applied simultaneously to a sound.


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