xoreos
0.0.5
|
The sound manager. More...
#include <sound.h>
Classes | |
struct | Channel |
A sound channel. More... | |
struct | Type |
A sound type. More... | |
Public Member Functions | |
SoundManager () | |
~SoundManager () | |
void | init () |
Initialize the sound subsystem. More... | |
void | deinit () |
Deinitialize the sound subsystem. More... | |
bool | ready () const |
Was the sound subsystem successfully initialized? More... | |
void | triggerUpdate () |
Signal that one of streams currently being played has changed and should be updated immediately. More... | |
bool | isValidChannel (const ChannelHandle &handle) const |
Does this channel handle point to an existing channel? More... | |
bool | isPlaying (const ChannelHandle &handle) |
Is that channel currently playing a sound? More... | |
bool | isPaused (const ChannelHandle &handle) |
Is that channel currently paused? More... | |
uint64 | getChannelSamplesPlayed (const ChannelHandle &handle) |
Return the number of samples this channel has already played. More... | |
uint64 | getChannelDurationPlayed (const ChannelHandle &handle) |
Return the time this channel has already played in milliseconds. More... | |
ChannelHandle | playSoundFile (Common::SeekableReadStream *wavStream, SoundType type, bool loop=false) |
Play a sound file. More... | |
ChannelHandle | playAudioStream (AudioStream *audStream, SoundType type, bool disposeAfterUse=true) |
Play an audio stream. More... | |
void | startChannel (ChannelHandle &handle) |
Start the channel. More... | |
void | pauseChannel (ChannelHandle &handle, bool pause) |
Pause the channel. More... | |
void | pauseChannel (ChannelHandle &handle) |
Pause toggle the channel. More... | |
void | stopChannel (ChannelHandle &handle) |
Stop and free the channel. More... | |
void | pauseAll (bool pause) |
Pause all channels. More... | |
void | stopAll () |
Stop all channels. More... | |
void | setListenerGain (float gain) |
Set the gain of the listener (= the global master volume). More... | |
void | setListenerPosition (float x, float y, float z) |
Set the position of the listener. More... | |
void | setListenerOrientation (float dirX, float dirY, float dirZ, float upX, float upY, float upZ) |
Set the orientation of the listener. More... | |
void | setChannelPosition (const ChannelHandle &handle, float x, float y, float z) |
Set the position the channel is being played. More... | |
void | getChannelPosition (const ChannelHandle &handle, float &x, float &y, float &z) |
Get the position of the channel. More... | |
void | setChannelGain (const ChannelHandle &handle, float gain) |
Set the gain/volume of the channel. More... | |
void | setChannelPitch (const ChannelHandle &handle, float pitch) |
Set the pitch of the channel. More... | |
void | setChannelRelative (const ChannelHandle &handle, bool relative) |
Set if the channel is relative. More... | |
void | setChannelDistance (const ChannelHandle &handle, float minDistance, float maxDistance) |
Set the min and max distance for the channel. More... | |
void | setTypeGain (SoundType type, float gain) |
Set the gain/volume of all channels of a specific type. More... | |
Common::UString | formatChannel (const ChannelHandle &handle) const |
Return a string representing the channel referenced by this handle. More... | |
Public Member Functions inherited from Common::Thread | |
Thread () | |
virtual | ~Thread () |
bool | createThread (const UString &name="") |
bool | destroyThread () |
Static Public Member Functions | |
static AudioStream * | makeAudioStream (Common::SeekableReadStream *stream) |
Create an audio stream from this data stream. More... | |
Static Public Member Functions inherited from Common::Singleton< SoundManager > | |
static SoundManager & | instance () |
static void | destroy () |
Private Types | |
typedef std::list< Channel * > | TypeList |
Private Member Functions | |
void | checkReady () |
Check that the SoundManager was properly initialized. More... | |
void | update () |
Update the sound information. More... | |
ChannelHandle | newChannel () |
Look for a free place in the channel vector. More... | |
void | bufferData (Channel &channel) |
Buffer more sound from the channel to the OpenAL buffers. More... | |
void | bufferData (size_t channel) |
Buffer more sound from the channel to the OpenAL buffers. More... | |
bool | isPlaying (size_t channel) const |
Is that channel currently playing a sound? More... | |
void | pauseChannel (Channel *channel, bool pause) |
Pause/Unpause a channel. More... | |
void | pauseChannel (Channel *channe) |
Pause toggle channel. More... | |
void | freeChannel (ChannelHandle &handle) |
Stop and free a channel. More... | |
void | freeChannel (size_t channel) |
Stop and free a channel. More... | |
const Channel * | getChannel (const ChannelHandle &handle) const |
Return the channel the handle refers to. More... | |
Channel * | getChannel (const ChannelHandle &handle) |
Return the channel the handle refers to. More... | |
void | threadMethod () |
bool | fillBuffer (const Channel &channel, ALuint alBuffer, AudioStream *stream, ALsizei &bufferedSize) const |
Fill the buffer with data from the audio stream. More... | |
Common::UString | formatChannel (const Channel *channel) const |
Return a string representing this channel. More... | |
Private Attributes | |
bool | _ready |
Was the sound subsystem successfully initialized? More... | |
bool | _hasSound |
Do we have working sound output? More... | |
bool | _hasMultiChannel |
Do we have the multi-channel extension? More... | |
ALenum | _format51 |
The value for the 5.1 multi-channel format. More... | |
Common::ScopedPtr< Channel > | _channels [kChannelCount] |
The sound channels. More... | |
Type | _types [kSoundTypeMAX] |
The sound types. More... | |
uint32 | _curID |
The ID the next sound will get. More... | |
Common::Mutex | _mutex |
Common::Condition | _needUpdate |
Condition to signal that an update is needed. More... | |
ALCdevice * | _dev |
ALCcontext * | _ctx |
Static Private Attributes | |
static const size_t | kChannelCount = 65535 |
Maximal number of channels. More... | |
Additional Inherited Members | |
Protected Types inherited from Common::Singleton< SoundManager > | |
typedef SoundManager | SingletonBaseType |
Protected Member Functions inherited from Common::Singleton< SoundManager > | |
Singleton () | |
virtual | ~Singleton () |
Protected Attributes inherited from Common::Thread | |
boost::atomic< bool > | _killThread |
|
private |
|
private |
Buffer more sound from the channel to the OpenAL buffers.
Definition at line 729 of file sound.cpp.
References _hasSound, Sound::SoundManager::Channel::bufferSize, error(), fillBuffer(), Sound::SoundManager::Channel::finishedBuffers, formatChannel(), Sound::SoundManager::Channel::freeBuffers, kOpenALBufferCount, Sound::SoundManager::Channel::source, and Sound::SoundManager::Channel::stream.
Referenced by bufferData(), getChannelSamplesPlayed(), and update().
|
private |
Buffer more sound from the channel to the OpenAL buffers.
Definition at line 722 of file sound.cpp.
References _channels, bufferData(), and kChannelCount.
|
private |
Check that the SoundManager was properly initialized.
Definition at line 780 of file sound.cpp.
References _ready.
Referenced by playAudioStream(), playSoundFile(), setListenerGain(), setListenerOrientation(), setListenerPosition(), and triggerUpdate().
void Sound::SoundManager::deinit | ( | ) |
Deinitialize the sound subsystem.
Definition at line 136 of file sound.cpp.
References _ctx, _dev, _hasSound, _ready, Common::Thread::destroyThread(), freeChannel(), and kChannelCount.
|
private |
Fill the buffer with data from the audio stream.
Definition at line 662 of file sound.cpp.
References _format51, _hasMultiChannel, _hasSound, Sound::AudioStream::endOfData(), error(), formatChannel(), Common::ScopedPtrBase< T, Deallocator >::get(), Sound::AudioStream::getChannels(), Sound::AudioStream::getRate(), kOpenALBufferSize, Sound::AudioStream::kSizeInvalid, Sound::AudioStream::readBuffer(), and warning().
Referenced by bufferData(), and playAudioStream().
Common::UString Sound::SoundManager::formatChannel | ( | const ChannelHandle & | handle | ) | const |
Return a string representing the channel referenced by this handle.
Definition at line 915 of file sound.cpp.
References getChannel().
Referenced by bufferData(), fillBuffer(), getChannelPosition(), isPlaying(), pauseChannel(), playAudioStream(), setChannelPosition(), startChannel(), and stopChannel().
|
private |
Return a string representing this channel.
Definition at line 908 of file sound.cpp.
References Common::composeString(), Sound::SoundManager::Channel::id, and Sound::SoundManager::Channel::index.
|
private |
Stop and free a channel.
Definition at line 860 of file sound.cpp.
References _channels, Sound::ChannelHandle::channel, Sound::ChannelHandle::id, kChannelCount, and Sound::kChannelInvalid.
Referenced by deinit(), stopAll(), stopChannel(), and update().
|
private |
Stop and free a channel.
Definition at line 871 of file sound.cpp.
References _channels, _hasSound, _types, Sound::SoundManager::Channel::buffers, kChannelCount, Sound::SoundManager::Type::list, Sound::SoundManager::Channel::source, Sound::SoundManager::Channel::stream, Sound::SoundManager::Channel::type, and Sound::SoundManager::Channel::typeIt.
|
private |
Return the channel the handle refers to.
Definition at line 424 of file sound.cpp.
References _channels, Sound::ChannelHandle::channel, Sound::ChannelHandle::id, and kChannelCount.
Referenced by formatChannel(), getChannelDurationPlayed(), getChannelPosition(), getChannelSamplesPlayed(), pauseChannel(), setChannelDistance(), setChannelGain(), setChannelPitch(), setChannelPosition(), setChannelRelative(), and startChannel().
|
private |
Return the channel the handle refers to.
Definition at line 437 of file sound.cpp.
References _channels, Sound::ChannelHandle::channel, Sound::ChannelHandle::id, and kChannelCount.
uint64 Sound::SoundManager::getChannelDurationPlayed | ( | const ChannelHandle & | handle | ) |
Return the time this channel has already played in milliseconds.
Definition at line 635 of file sound.cpp.
References _mutex, getChannel(), getChannelSamplesPlayed(), and Sound::SoundManager::Channel::stream.
void Sound::SoundManager::getChannelPosition | ( | const ChannelHandle & | handle, |
float & | x, | ||
float & | y, | ||
float & | z | ||
) |
Get the position of the channel.
Definition at line 551 of file sound.cpp.
References _hasSound, _mutex, Common::UString::c_str(), formatChannel(), getChannel(), Sound::SoundManager::Channel::source, and Sound::SoundManager::Channel::stream.
uint64 Sound::SoundManager::getChannelSamplesPlayed | ( | const ChannelHandle & | handle | ) |
Return the number of samples this channel has already played.
Definition at line 614 of file sound.cpp.
References _mutex, bufferData(), Sound::SoundManager::Channel::finishedBuffers, getChannel(), Sound::SoundManager::Channel::source, and Sound::SoundManager::Channel::stream.
Referenced by getChannelDurationPlayed().
void Sound::SoundManager::init | ( | ) |
Initialize the sound subsystem.
Definition at line 81 of file sound.cpp.
References _ctx, _curID, _dev, _format51, _hasMultiChannel, _hasSound, _ready, _types, ConfigMan, Common::Thread::createThread(), error(), Common::exceptionDispatcherWarning(), Sound::kSoundTypeMAX, Sound::kSoundTypeMusic, Sound::kSoundTypeSFX, Sound::kSoundTypeVideo, Sound::kSoundTypeVoice, setListenerGain(), and setTypeGain().
bool Sound::SoundManager::isPaused | ( | const ChannelHandle & | handle | ) |
Is that channel currently paused?
Definition at line 232 of file sound.cpp.
References _channels, _mutex, Sound::ChannelHandle::channel, and Sound::ChannelHandle::id.
bool Sound::SoundManager::isPlaying | ( | const ChannelHandle & | handle | ) |
Is that channel currently playing a sound?
Definition at line 174 of file sound.cpp.
References _channels, _mutex, Sound::ChannelHandle::channel, Sound::ChannelHandle::id, and kChannelCount.
Referenced by update().
|
private |
Is that channel currently playing a sound?
Definition at line 186 of file sound.cpp.
References _channels, _hasSound, error(), formatChannel(), and kChannelCount.
bool Sound::SoundManager::isValidChannel | ( | const ChannelHandle & | handle | ) | const |
Does this channel handle point to an existing channel?
Definition at line 164 of file sound.cpp.
References _channels, Sound::ChannelHandle::channel, Sound::ChannelHandle::id, and kChannelCount.
Referenced by stopChannel().
|
static |
Create an audio stream from this data stream.
The ownership of the data stream is transferred to the audio stream if one was created without an exception being thrown.
Definition at line 244 of file sound.cpp.
References Common::debugTag(), Sound::makeASFStream(), Sound::makeMP3Stream(), Sound::makeVorbisStream(), Sound::makeWAVStream(), MKTAG, Common::SeekableReadStream::pos(), Common::ReadStream::readUint32BE(), Common::ReadStream::readUint32LE(), Common::SeekableReadStream::seek(), Common::SeekableReadStream::size(), and Common::SeekableReadStream::skip().
Referenced by playSoundFile().
|
private |
Look for a free place in the channel vector.
Definition at line 808 of file sound.cpp.
References _channels, _curID, Sound::ChannelHandle::channel, Sound::ChannelHandle::id, kChannelCount, and Sound::kChannelInvalid.
Referenced by playAudioStream().
void Sound::SoundManager::pauseAll | ( | bool | pause | ) |
Pause all channels.
Definition at line 496 of file sound.cpp.
References _channels, _mutex, kChannelCount, and pauseChannel().
void Sound::SoundManager::pauseChannel | ( | ChannelHandle & | handle, |
bool | pause | ||
) |
Pause the channel.
Definition at line 474 of file sound.cpp.
References _mutex, debugC(), formatChannel(), getChannel(), Common::kDebugSound, and Sound::SoundManager::Channel::stream.
Referenced by pauseAll(), and pauseChannel().
void Sound::SoundManager::pauseChannel | ( | ChannelHandle & | handle | ) |
Pause toggle the channel.
Definition at line 464 of file sound.cpp.
References _mutex, getChannel(), pauseChannel(), and Sound::SoundManager::Channel::stream.
|
private |
Pause/Unpause a channel.
Definition at line 830 of file sound.cpp.
References _hasSound, error(), formatChannel(), Sound::SoundManager::Channel::id, Sound::SoundManager::Channel::source, Sound::SoundManager::Channel::state, triggerUpdate(), and warning().
|
private |
Pause toggle channel.
Definition at line 850 of file sound.cpp.
References pauseChannel(), and Sound::SoundManager::Channel::state.
ChannelHandle Sound::SoundManager::playAudioStream | ( | AudioStream * | audStream, |
SoundType | type, | ||
bool | disposeAfterUse = true |
||
) |
Play an audio stream.
This only allocate a channel for the sound, to actually start playing it, call startChannel().
audStream | The stream to play. |
type | The type of the sound. |
disposeAfterUse | Should the stream be taken over and discarded once it finished? |
Definition at line 330 of file sound.cpp.
References _channels, _hasSound, _mutex, _types, Sound::SoundManager::Channel::buffers, Sound::SoundManager::Channel::bufferSize, Sound::ChannelHandle::channel, checkReady(), debugC(), error(), fillBuffer(), formatChannel(), Sound::SoundManager::Channel::freeBuffers, Sound::SoundManager::Type::gain, Sound::ChannelHandle::id, Common::kDebugSound, kOpenALBufferCount, Sound::kSoundTypeMAX, Sound::SoundManager::Type::list, newChannel(), Sound::SoundManager::Channel::source, Sound::SoundManager::Channel::stream, Sound::SoundManager::Channel::type, and Sound::SoundManager::Channel::typeIt.
Referenced by playSoundFile().
ChannelHandle Sound::SoundManager::playSoundFile | ( | Common::SeekableReadStream * | wavStream, |
SoundType | type, | ||
bool | loop = false |
||
) |
Play a sound file.
This only allocate a channel for the sound, to actually start playing it, call startChannel().
wavStream | The stream to play. Will be taken over. |
type | The type of the sound. |
loop | Should the sound loop? |
Definition at line 402 of file sound.cpp.
References checkReady(), makeAudioStream(), Sound::makeLoopingAudioStream(), playAudioStream(), and warning().
bool Sound::SoundManager::ready | ( | ) | const |
void Sound::SoundManager::setChannelDistance | ( | const ChannelHandle & | handle, |
float | minDistance, | ||
float | maxDistance | ||
) |
Set the min and max distance for the channel.
Definition at line 601 of file sound.cpp.
References _hasSound, _mutex, getChannel(), Sound::SoundManager::Channel::source, and Sound::SoundManager::Channel::stream.
void Sound::SoundManager::setChannelGain | ( | const ChannelHandle & | handle, |
float | gain | ||
) |
Set the gain/volume of the channel.
Definition at line 566 of file sound.cpp.
References _hasSound, _mutex, _types, Sound::SoundManager::Type::gain, Sound::SoundManager::Channel::gain, getChannel(), Sound::SoundManager::Channel::source, Sound::SoundManager::Channel::stream, and Sound::SoundManager::Channel::type.
void Sound::SoundManager::setChannelPitch | ( | const ChannelHandle & | handle, |
float | pitch | ||
) |
Set the pitch of the channel.
Definition at line 579 of file sound.cpp.
References _hasSound, _mutex, getChannel(), Sound::SoundManager::Channel::source, and Sound::SoundManager::Channel::stream.
void Sound::SoundManager::setChannelPosition | ( | const ChannelHandle & | handle, |
float | x, | ||
float | y, | ||
float | z | ||
) |
Set the position the channel is being played.
Definition at line 536 of file sound.cpp.
References _hasSound, _mutex, Common::UString::c_str(), formatChannel(), getChannel(), Sound::SoundManager::Channel::source, and Sound::SoundManager::Channel::stream.
void Sound::SoundManager::setChannelRelative | ( | const ChannelHandle & | handle, |
bool | relative | ||
) |
Set if the channel is relative.
Definition at line 590 of file sound.cpp.
References _hasSound, _mutex, getChannel(), Sound::SoundManager::Channel::source, and Sound::SoundManager::Channel::stream.
void Sound::SoundManager::setListenerGain | ( | float | gain | ) |
void Sound::SoundManager::setListenerOrientation | ( | float | dirX, |
float | dirY, | ||
float | dirZ, | ||
float | upX, | ||
float | upY, | ||
float | upZ | ||
) |
Set the orientation of the listener.
Definition at line 527 of file sound.cpp.
References _mutex, and checkReady().
void Sound::SoundManager::setListenerPosition | ( | float | x, |
float | y, | ||
float | z | ||
) |
Set the position of the listener.
Definition at line 519 of file sound.cpp.
References _mutex, and checkReady().
void Sound::SoundManager::setTypeGain | ( | SoundType | type, |
float | gain | ||
) |
Set the gain/volume of all channels of a specific type.
Definition at line 645 of file sound.cpp.
References _hasSound, _mutex, _types, Sound::SoundManager::Type::gain, Sound::kSoundTypeMAX, and Sound::SoundManager::Type::list.
Referenced by init().
void Sound::SoundManager::startChannel | ( | ChannelHandle & | handle | ) |
Start the channel.
Definition at line 450 of file sound.cpp.
References _mutex, debugC(), formatChannel(), getChannel(), Common::kDebugSound, Sound::SoundManager::Channel::state, Sound::SoundManager::Channel::stream, and triggerUpdate().
void Sound::SoundManager::stopAll | ( | ) |
Stop all channels.
Definition at line 503 of file sound.cpp.
References _mutex, freeChannel(), and kChannelCount.
void Sound::SoundManager::stopChannel | ( | ChannelHandle & | handle | ) |
Stop and free the channel.
Definition at line 487 of file sound.cpp.
References _mutex, debugC(), formatChannel(), freeChannel(), isValidChannel(), and Common::kDebugSound.
|
privatevirtual |
Implements Common::Thread.
Definition at line 901 of file sound.cpp.
References Common::Thread::_killThread, _needUpdate, update(), and Common::Condition::wait().
void Sound::SoundManager::triggerUpdate | ( | ) |
Signal that one of streams currently being played has changed and should be updated immediately.
Definition at line 158 of file sound.cpp.
References _needUpdate, checkReady(), and Common::Condition::signal().
Referenced by pauseChannel(), and startChannel().
|
private |
Update the sound information.
Called regularly from within the thread method.
Definition at line 785 of file sound.cpp.
References _channels, _mutex, bufferData(), Common::composeString(), debugC(), freeChannel(), isPlaying(), kChannelCount, and Common::kDebugSound.
Referenced by threadMethod().
|
private |
The sound channels.
Definition at line 235 of file sound.h.
Referenced by bufferData(), freeChannel(), getChannel(), isPaused(), isPlaying(), isValidChannel(), newChannel(), pauseAll(), playAudioStream(), and update().
|
private |
|
private |
The ID the next sound will get.
Definition at line 238 of file sound.h.
Referenced by init(), and newChannel().
|
private |
|
private |
The value for the 5.1 multi-channel format.
Definition at line 233 of file sound.h.
Referenced by fillBuffer(), and init().
|
private |
Do we have the multi-channel extension?
Definition at line 232 of file sound.h.
Referenced by fillBuffer(), and init().
|
private |
Do we have working sound output?
Definition at line 230 of file sound.h.
Referenced by bufferData(), deinit(), fillBuffer(), freeChannel(), getChannelPosition(), init(), isPlaying(), pauseChannel(), playAudioStream(), setChannelDistance(), setChannelGain(), setChannelPitch(), setChannelPosition(), setChannelRelative(), setListenerGain(), and setTypeGain().
|
private |
Definition at line 240 of file sound.h.
Referenced by getChannelDurationPlayed(), getChannelPosition(), getChannelSamplesPlayed(), isPaused(), isPlaying(), pauseAll(), pauseChannel(), playAudioStream(), setChannelDistance(), setChannelGain(), setChannelPitch(), setChannelPosition(), setChannelRelative(), setListenerGain(), setListenerOrientation(), setListenerPosition(), setTypeGain(), startChannel(), stopAll(), stopChannel(), and update().
|
private |
Condition to signal that an update is needed.
Definition at line 243 of file sound.h.
Referenced by threadMethod(), and triggerUpdate().
|
private |
|
private |
The sound types.
Definition at line 236 of file sound.h.
Referenced by freeChannel(), init(), playAudioStream(), setChannelGain(), and setTypeGain().
|
staticprivate |
Maximal number of channels.
Definition at line 190 of file sound.h.
Referenced by bufferData(), deinit(), freeChannel(), getChannel(), isPlaying(), isValidChannel(), newChannel(), pauseAll(), stopAll(), and update().