CUGL 2.0
Cornell University Game Library
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cugl::SoundLoader Class Reference

#include <CUSoundLoader.h>

Inheritance diagram for cugl::SoundLoader:
cugl::Loader< Sound > cugl::BaseLoader

Public Member Functions

 SoundLoader ()
 
void dispose () override
 
float getVolume () const
 
void setVolume (float volume)
 
- Public Member Functions inherited from cugl::Loader< Sound >
 Loader ()
 
std::shared_ptr< Soundget (const std::string &key) const
 
std::shared_ptr< Soundget (const char *key) const
 
std::shared_ptr< Soundoperator[] (const std::string &key) const
 
std::shared_ptr< Soundoperator[] (const char *key) const
 
size_t loadCount () const override
 
size_t waitCount () const override
 
void unloadAll () override
 
- Public Member Functions inherited from cugl::BaseLoader
 BaseLoader ()
 
 ~BaseLoader ()
 
virtual bool init ()
 
virtual bool init (const std::shared_ptr< ThreadPool > &threads)
 
std::shared_ptr< BaseLoadergetHook ()
 
std::shared_ptr< ThreadPoolgetThreadPool () const
 
void setThreadPool (const std::shared_ptr< ThreadPool > &threads)
 
void setManager (AssetManager *manager)
 
const AssetManagergetManager () const
 
bool load (const std::string &key, const std::string &source)
 
bool load (const char *key, const std::string &source)
 
bool load (const std::string &key, const char *source)
 
bool load (const char *key, const char *source)
 
bool load (const std::shared_ptr< JsonValue > &json)
 
void loadAsync (const std::string &key, const std::string &source, LoaderCallback callback)
 
void loadAsync (const char *key, const std::string &source, LoaderCallback callback)
 
void loadAsync (const std::string &key, const char *source, LoaderCallback callback)
 
void loadAsync (const char *key, const char *source, LoaderCallback callback)
 
void loadAsync (const std::shared_ptr< JsonValue > &json, LoaderCallback callback)
 
bool unload (const std::string &key)
 
bool unload (const char *key)
 
bool unload (const std::shared_ptr< JsonValue > &json)
 
bool contains (const std::string &key) const
 
bool contains (const char *key) const
 
bool complete () const
 
float progress () const
 

Static Public Member Functions

static std::shared_ptr< SoundLoaderalloc ()
 
static std::shared_ptr< SoundLoaderalloc (const std::shared_ptr< ThreadPool > &threads)
 

Protected Member Functions

void materialize (const std::string &key, const std::shared_ptr< Sound > &sound, LoaderCallback callback)
 
virtual bool read (const std::string &key, const std::string &source, LoaderCallback callback, bool async) override
 
virtual bool read (const std::shared_ptr< JsonValue > &json, LoaderCallback callback, bool async) override
 
- Protected Member Functions inherited from cugl::Loader< Sound >
bool purge (const std::string &key) override
 
bool verify (const std::string &key) const override
 
- Protected Member Functions inherited from cugl::BaseLoader
virtual bool purge (const std::shared_ptr< JsonValue > &json)
 

Protected Attributes

float _volume
 
- Protected Attributes inherited from cugl::Loader< Sound >
std::unordered_map< std::string, std::shared_ptr< Sound > > _assets
 
std::unordered_set< std::string > _queue
 
- Protected Attributes inherited from cugl::BaseLoader
std::shared_ptr< ThreadPool_loader
 
AssetManager_manager
 

Detailed Description

This class is a implementation of Loader<Sound>

This asset loader allows us to allocate sound assets. See Sound for the supported audio files. As a general rule, sound assets should be WAV files as there is no benefit for compression.

Note that this implementation uses a two phase loading system. First, it loads as much of the asset as possible without accessing the audio engine. This allows us to load the sound asset in a separate thread. It then finishes off the remainder of asset loading using Application#schedule. This is a good template for asset loaders in general.

As with all of our loaders, this loader is designed to be attached to an asset manager. Use the method getHook() to get the appropriate pointer for attaching the loader.

Constructor & Destructor Documentation

◆ SoundLoader()

cugl::SoundLoader::SoundLoader ( )

Creates a new, uninitialized sound loader

NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate a loader on the heap, use one of the static constructors instead.

Member Function Documentation

◆ alloc() [1/2]

static std::shared_ptr<SoundLoader> cugl::SoundLoader::alloc ( )
inlinestatic

Returns a newly allocated sound loader.

This method bootstraps the loader with any initial resources that it needs to load assets. In particular, the audio engine must be active. Attempts to load an asset before this method is called will fail.

This loader will have no associated threads. That means any asynchronous loading will fail until a thread is provided via setThreadPool.

Returns
a newly allocated sound loader.

◆ alloc() [2/2]

static std::shared_ptr<SoundLoader> cugl::SoundLoader::alloc ( const std::shared_ptr< ThreadPool > &  threads)
inlinestatic

Returns a newly allocated sound loader.

This method bootstraps the loader with any initial resources that it needs to load assets. In particular, the audio engine must be active. Attempts to load an asset before this method is called will fail.

This loader will have no associated threads. That means any asynchronous loading will fail until a thread is provided via setThreadPool.

Parameters
threadsThe thread pool for asynchronous loading
Returns
a newly allocated sound loader.

◆ dispose()

void cugl::SoundLoader::dispose ( )
inlineoverridevirtual

Disposes all resources and assets of this loader

Any assets loaded by this object will be immediately released by the loader. However, a sound may still be available if it is referenced by another smart pointer. The audio engine will only release a sound buffer once all smart pointer attached to the asset are null.

Once the loader is disposed, any attempts to load a new asset will fail. You must reinitialize the loader to begin loading assets again.

Reimplemented from cugl::BaseLoader.

◆ getVolume()

float cugl::SoundLoader::getVolume ( ) const
inline

Returns the default volume

Once set, any future sound asset processed by this loader will have this volume unless otherwise specified. The default is 1.0 (max volume).

Returns
the default volume

◆ materialize()

void cugl::SoundLoader::materialize ( const std::string &  key,
const std::shared_ptr< Sound > &  sound,
LoaderCallback  callback 
)
protected

Finishes loading the sound file, setting its default volume.

Allocating a sound asset can be done safely in a separate thread. However, setting the default volume requires the audio engine, and so this step is not safe to be done in a separate thread. Instead, it takes place in the main CUGL thread via Application#schedule.

This method supports an optional callback function which reports whether the asset was successfully materialized.

Parameters
keyThe key to access the asset after loading
soundThe sound asset partially loaded
callbackAn optional callback for asynchronous loading

◆ read() [1/2]

virtual bool cugl::SoundLoader::read ( const std::shared_ptr< JsonValue > &  json,
LoaderCallback  callback,
bool  async 
)
overrideprotectedvirtual

Internal method to support asset loading.

This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.

This method will split the loading across the Sound allocation and the internal materialize method. This ensures that asynchronous loading is safe.

This version of read provides support for JSON directories. A soundfx directory entry has the following values

 "file":         The path to the asset
 "volume":       This default sound volume (float)
Parameters
jsonThe directory entry for the asset
callbackAn optional callback for asynchronous loading
asyncWhether the asset was loaded asynchronously
Returns
true if the asset was successfully loaded

Reimplemented from cugl::BaseLoader.

◆ read() [2/2]

virtual bool cugl::SoundLoader::read ( const std::string &  key,
const std::string &  source,
LoaderCallback  callback,
bool  async 
)
overrideprotectedvirtual

Internal method to support asset loading.

This method supports either synchronous or asynchronous loading, as specified by the given parameter. If the loading is asynchronous, the user may specify an optional callback function.

This method will split the loading across the Sound allocation and the internal materialize method. This ensures that asynchronous loading is safe.

Parameters
keyThe key to access the asset after loading
sourceThe pathname to the asset
callbackAn optional callback for asynchronous loading
asyncWhether the asset was loaded asynchronously
Returns
true if the asset was successfully loaded

Reimplemented from cugl::BaseLoader.

◆ setVolume()

void cugl::SoundLoader::setVolume ( float  volume)
inline

Sets the default volume

Once set, any future sound asset processed by this loader will have this volume unless otherwise specified. The default is 1.0 (max volume).

Parameters
volumeThe default volume

Member Data Documentation

◆ _volume

float cugl::SoundLoader::_volume
protected

The default volume for all music assets


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