mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-02-04 12:39:55 +00:00
Added Sound.clone(), implemented with OpenAL.
This commit is contained in:
parent
d22bea5eab
commit
7d36b599d0
@ -17,6 +17,11 @@ namespace Sound {
|
|||||||
may be connected to a SampleSource or read directly from a file,
|
may be connected to a SampleSource or read directly from a file,
|
||||||
and they may support 3d sounds, looping and other features
|
and they may support 3d sounds, looping and other features
|
||||||
depending on the capabilities of the backend system.
|
depending on the capabilities of the backend system.
|
||||||
|
|
||||||
|
To create multiple instances of one sound, it is recommended to
|
||||||
|
'clone' an existing instance instead of reloading it from
|
||||||
|
file. Cloned sounds will often (depending on the back-end) use
|
||||||
|
less memory due to shared buffers.
|
||||||
*/
|
*/
|
||||||
class Sound
|
class Sound
|
||||||
{
|
{
|
||||||
@ -31,14 +36,33 @@ class Sound
|
|||||||
virtual void pause() = 0;
|
virtual void pause() = 0;
|
||||||
|
|
||||||
/// Check if the sound is still playing
|
/// Check if the sound is still playing
|
||||||
virtual bool isPlaying() = 0;
|
virtual bool isPlaying() = 0 const;
|
||||||
|
|
||||||
/// Set the volume. The parameter must be between 0.0 and 1.0.
|
/// Set the volume. The parameter must be between 0.0 and 1.0.
|
||||||
virtual void setVolume(float) = 0;
|
virtual void setVolume(float) = 0;
|
||||||
|
|
||||||
/// Set the position. May not have any effect on 2D sounds.
|
/// Set left/right pan. -1.0 is left, 0.0 is center and 1.0 is right.
|
||||||
|
virtual void setPan(float) = 0;
|
||||||
|
|
||||||
|
/// Set the position. May not work with all backends.
|
||||||
virtual void setPos(float x, float y, float z) = 0;
|
virtual void setPos(float x, float y, float z) = 0;
|
||||||
|
|
||||||
|
/// Set loop mode
|
||||||
|
virtual void setRepeat(bool) = 0;
|
||||||
|
|
||||||
|
/// Set streaming mode.
|
||||||
|
/** This may be used by implementations to optimize for very large
|
||||||
|
files. If streaming mode is off (default), most implementations
|
||||||
|
will load the entire file into memory before starting playback.
|
||||||
|
*/
|
||||||
|
virtual void setStreaming(bool) = 0;
|
||||||
|
|
||||||
|
/// Create a new instance of this sound.
|
||||||
|
/** Playback status is not cloned, only the sound data
|
||||||
|
itself. Back-ends can use this as a means of sharing data and
|
||||||
|
saving memory. */
|
||||||
|
virtual Sound* clone() const = 0;
|
||||||
|
|
||||||
/// Virtual destructor
|
/// Virtual destructor
|
||||||
virtual ~Sound() {}
|
virtual ~Sound() {}
|
||||||
};
|
};
|
||||||
@ -71,12 +95,6 @@ class SoundFactory
|
|||||||
*/
|
*/
|
||||||
bool has3D;
|
bool has3D;
|
||||||
|
|
||||||
/** @brief true if 'repeat' and 'stream' can be used simultaneously.
|
|
||||||
If false, repeating a streamed sound will give undefined
|
|
||||||
behavior.
|
|
||||||
*/
|
|
||||||
bool canRepeatStream;
|
|
||||||
|
|
||||||
/// true if we can load sounds directly from file (containing encoded data)
|
/// true if we can load sounds directly from file (containing encoded data)
|
||||||
bool canLoadFile;
|
bool canLoadFile;
|
||||||
|
|
||||||
@ -99,7 +117,7 @@ class SoundFactory
|
|||||||
large files, but they are not required to.
|
large files, but they are not required to.
|
||||||
@return a new Sound object
|
@return a new Sound object
|
||||||
*/
|
*/
|
||||||
virtual Sound *load(SampleSource *input, bool stream=false) = 0;
|
virtual Sound *load(SampleSource *input) = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@brief Load a sound file from stream. Only valid if canLoadStream
|
@brief Load a sound file from stream. Only valid if canLoadStream
|
||||||
@ -109,7 +127,7 @@ class SoundFactory
|
|||||||
@param stream true if the file should be streamed
|
@param stream true if the file should be streamed
|
||||||
@see load(InputSource*,bool)
|
@see load(InputSource*,bool)
|
||||||
*/
|
*/
|
||||||
virtual Sound *load(Stream::Stream *input, bool stream=false) = 0;
|
virtual Sound *load(Stream::Stream *input) = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@brief Load a sound directly from file. Only valid if canLoadFile
|
@brief Load a sound directly from file. Only valid if canLoadFile
|
||||||
@ -119,7 +137,7 @@ class SoundFactory
|
|||||||
@param stream true if the file should be streamed
|
@param stream true if the file should be streamed
|
||||||
@see load(InputSource*,bool)
|
@see load(InputSource*,bool)
|
||||||
*/
|
*/
|
||||||
virtual Sound *load(const std::string &file, bool stream=false) = 0;
|
virtual Sound *load(const std::string &file) = 0;
|
||||||
|
|
||||||
/// Call this every frame if needsUpdate is true
|
/// Call this every frame if needsUpdate is true
|
||||||
/**
|
/**
|
||||||
|
@ -103,6 +103,28 @@ void OpenAL_Sound::setPos(float x, float y, float z)
|
|||||||
checkALError("setting position");
|
checkALError("setting position");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void OpenAL_Sound::setRepeat(bool rep)
|
||||||
|
{
|
||||||
|
alSourcei(Source, AL_LOOPING, rep?AL_TRUE:AL_FALSE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenAL_Sound::setup()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constructor used for cloned sounds
|
||||||
|
OpenAL_Sound::OpenAL_Sound(ALuint buf, int *ref)
|
||||||
|
: refCnt(ref), bufferID(buf)
|
||||||
|
{
|
||||||
|
// Increase the reference count
|
||||||
|
assert(ref != NULL);
|
||||||
|
*refCnt++;
|
||||||
|
|
||||||
|
// Create a source
|
||||||
|
alGenSources(1, &inst);
|
||||||
|
alSourcei(inst, AL_BUFFER, bufferID);
|
||||||
|
}
|
||||||
|
|
||||||
OpenAL_Sound::OpenAL_Sound(SampleSource *input)
|
OpenAL_Sound::OpenAL_Sound(SampleSource *input)
|
||||||
{
|
{
|
||||||
// Get the format
|
// Get the format
|
||||||
@ -120,7 +142,11 @@ OpenAL_Sound::OpenAL_Sound(SampleSource *input)
|
|||||||
|
|
||||||
// Create a source
|
// Create a source
|
||||||
alGenSources(1, &inst);
|
alGenSources(1, &inst);
|
||||||
alSourcei(inst, AL_BUFFER, buf);
|
alSourcei(inst, AL_BUFFER, bufferID);
|
||||||
|
|
||||||
|
// Create a cheap reference counter for the buffer
|
||||||
|
refCnt = new int;
|
||||||
|
*refCnt = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
OpenAL_Sound::~OpenAL_Sound()
|
OpenAL_Sound::~OpenAL_Sound()
|
||||||
@ -131,6 +157,12 @@ OpenAL_Sound::~OpenAL_Sound()
|
|||||||
// Return sound
|
// Return sound
|
||||||
alDeleteSources(1, &inst);
|
alDeleteSources(1, &inst);
|
||||||
|
|
||||||
// Delete buffer
|
// Decrease the reference
|
||||||
alDeleteBuffers(1, &bufferID);
|
if(--(*refCnt))
|
||||||
|
{
|
||||||
|
// We're the last owner. Delete the buffer and the counter
|
||||||
|
// itself.
|
||||||
|
alDeleteBuffers(1, &bufferID);
|
||||||
|
delete refCnt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,27 +18,26 @@ class OpenAL_Sound : public Sound
|
|||||||
ALuint inst;
|
ALuint inst;
|
||||||
ALuint bufferID;
|
ALuint bufferID;
|
||||||
|
|
||||||
|
// Poor mans reference counting. Might improve this later.
|
||||||
|
int *refCnt;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
OpenAL_Sound(SampleSource *input);
|
OpenAL_Sound(SampleSource *input);
|
||||||
|
OpenAL_Sound(ALuint buf, int *ref); // Used for cloning
|
||||||
~OpenAL_Sound();
|
~OpenAL_Sound();
|
||||||
|
|
||||||
/// Play or resume the sound
|
|
||||||
void play();
|
void play();
|
||||||
|
|
||||||
/// Stop the sound
|
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
/// Pause the sound, may be resumed later
|
|
||||||
void pause();
|
void pause();
|
||||||
|
bool isPlaying() const;
|
||||||
/// Check if the sound is still playing
|
|
||||||
bool isPlaying();
|
|
||||||
|
|
||||||
/// Set the volume. The parameter must be between 0.0 and 1.0.
|
|
||||||
void setVolume(float);
|
void setVolume(float);
|
||||||
|
|
||||||
/// Set the 3D position.
|
|
||||||
void setPos(float x, float y, float z);
|
void setPos(float x, float y, float z);
|
||||||
|
void setRepeat(bool);
|
||||||
|
void setStreaming(bool) {} // Not implemented yet
|
||||||
|
Sound* clone() const;
|
||||||
|
|
||||||
|
/// Not implemented
|
||||||
|
void setPan(float) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
class OpenAL_Factory : public SoundFactory
|
class OpenAL_Factory : public SoundFactory
|
||||||
@ -56,7 +55,6 @@ class OpenAL_Factory : public SoundFactory
|
|||||||
{
|
{
|
||||||
needsUpdate = false;
|
needsUpdate = false;
|
||||||
has3D = true;
|
has3D = true;
|
||||||
canRepeatStream = false;
|
|
||||||
canLoadFile = false;
|
canLoadFile = false;
|
||||||
canLoadStream = false;
|
canLoadStream = false;
|
||||||
canLoadSource = true;
|
canLoadSource = true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user