Audio
The audio system provides abstractions to platform specific implementations. We have four main concepts for the lower level:
- Audio
- AudioBuffer
- AudioSource
- AudioStream
Audio
The Audio interface is rather simple. It provides a factory methods for creating implementation specific AudioBuffer objects and manages a thread for playback and updating AudioStream objects.
AudioBuffer
AudioBuffer is an abstraction of an implementation specific buffer that can be played by the implementation API. The most simple way to play sound with Echo, if you're not worried about formats or streaming is to use Audio to create an AudioBuffer, fill the buffer with data then call Play().
AudioSource
AudioSource classes implement a way to acquire audio to use in a buffer. An AudioSource might read data from a specific file format or read data from a network stream.
AudioStream
AudioStream is a Task that has an AudioSource and an AudioBuffer. It updates the AudioBuffer periodically with new data from the AudioSource.
Higher level audio
An AudioPlayer is a higher level object that utilises the above objects and manages more simple playback.
Normally you would have to create a specific AudioSource object then use it to fill a buffer. For example, to playback a .wav file you could write something like this:
shared_ptr<WavAudioSource> wavAudio(new WavAudioSource(GetFileSystem()->Open("resources/MySound.wav"))); shared_ptr<AudioBuffer> buffer = GetAudio()->LoadSample(wavAudio); buffer->Play();
AudioPlayer takes away the need to know the file type in advance. It allows you to register the available source types then and play audio by name from a mapped list of files.
AudioPlayer audioPlayer(audio,fileSystem); audioPlayer.AddSound("MySound","resources/MySound.wav"); audioPlayer.PlaySound("MySound");
It looks like a similar amount of work but when you have 100's of sounds and many music tracks the following can be much more effective:
AudioPlayer audioPlayer(audio,fileSystem); audioPlayer.LoadAudioList("resources/AudioList.txt"); audioPlayer.PlayMusic("MyMusic"); audioPlayer.PlaySound("MySound"); audioPlayer.PlaySound("MySound2"); audioPlayer.PlaySound("MySound3"); audioPlayer.PlaySound("MySound4");
Later the sounds can be changed by changing the mapping in the audio list rather than the code.
AudioPlayer also supports simple fading in and out of music.
- Last Author
- 0xseantasker
- Last Edited
- Oct 23 2015, 12:48 PM