Improved the API documentation in the audio module

Added sf::SoundSource as a base class of both sf::Sound and sf::SoundStream

git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1253 4e206d99-4929-0410-ac5d-dfc041789085
This commit is contained in:
LaurentGom 2009-10-30 16:14:26 +00:00
parent 3c0d42fdd0
commit 7d5f354850
14 changed files with 682 additions and 386 deletions

View file

@ -91,34 +91,29 @@ AudioDevice::~AudioDevice()
////////////////////////////////////////////////////////////
AudioDevice& AudioDevice::GetInstance()
bool AudioDevice::IsExtensionSupported(const std::string& extension)
{
return globalDevice;
if ((extension.length() > 2) && (extension.substr(0, 3) == "ALC"))
return alcIsExtensionPresent(globalDevice.myDevice, extension.c_str()) != AL_FALSE;
else
return alIsExtensionPresent(extension.c_str()) != AL_FALSE;
}
////////////////////////////////////////////////////////////
ALCdevice* AudioDevice::GetDevice() const
{
return myDevice;
}
////////////////////////////////////////////////////////////
ALenum AudioDevice::GetFormatFromChannelsCount(unsigned int channelsCount) const
ALenum AudioDevice::GetFormatFromChannelsCount(unsigned int channelsCount)
{
// Find the good format according to the number of channels
switch (channelsCount)
{
case 1 : return AL_FORMAT_MONO16;
case 2 : return AL_FORMAT_STEREO16;
case 4 : return alGetEnumValue("AL_FORMAT_QUAD16");
case 6 : return alGetEnumValue("AL_FORMAT_51CHN16");
case 7 : return alGetEnumValue("AL_FORMAT_61CHN16");
case 8 : return alGetEnumValue("AL_FORMAT_71CHN16");
case 1 : return AL_FORMAT_MONO16;
case 2 : return AL_FORMAT_STEREO16;
case 4 : return alGetEnumValue("AL_FORMAT_QUAD16");
case 6 : return alGetEnumValue("AL_FORMAT_51CHN16");
case 7 : return alGetEnumValue("AL_FORMAT_61CHN16");
case 8 : return alGetEnumValue("AL_FORMAT_71CHN16");
default : return 0;
}
return 0;
}
} // namespace priv

View file

@ -60,20 +60,18 @@ public :
~AudioDevice();
////////////////////////////////////////////////////////////
/// \brief Get the unique instance of the class
/// \brief Check if an OpenAL extension is supported
///
/// \return Unique instance of the class
/// This functions automatically finds whether it
/// is an AL or ALC extension, and calls the corresponding
/// function.
///
/// \param extension Name of the extension to test
///
/// \return True if the extension is supported, false if not
///
////////////////////////////////////////////////////////////
static AudioDevice& GetInstance();
////////////////////////////////////////////////////////////
/// \brief Get the OpenAL audio device
///
/// \return OpenAL device (cannot be NULL)
///
////////////////////////////////////////////////////////////
ALCdevice* GetDevice() const;
static bool IsExtensionSupported(const std::string& extension);
////////////////////////////////////////////////////////////
/// \brief Get the OpenAL format that matches the given number of channels
@ -83,7 +81,7 @@ public :
/// \return Corresponding format
///
////////////////////////////////////////////////////////////
ALenum GetFormatFromChannelsCount(unsigned int channelsCount) const;
static ALenum GetFormatFromChannelsCount(unsigned int channelsCount);
private :

View file

@ -35,8 +35,6 @@ namespace sf
////////////////////////////////////////////////////////////
Sound::Sound()
{
ALCheck(alGenSources(1, &mySource));
ALCheck(alSourcei(mySource, AL_BUFFER, 0));
}
@ -44,8 +42,6 @@ Sound::Sound()
Sound::Sound(const SoundBuffer& buffer, bool loop, float pitch, float volume, const Vector3f& position) :
myBuffer(NULL)
{
ALCheck(alGenSources(1, &mySource));
SetBuffer(buffer);
SetLoop(loop);
SetPitch(pitch);
@ -56,34 +52,19 @@ myBuffer(NULL)
////////////////////////////////////////////////////////////
Sound::Sound(const Sound& copy) :
myBuffer(NULL)
SoundSource(copy),
myBuffer (NULL)
{
ALCheck(alGenSources(1, &mySource));
if (copy.myBuffer)
SetBuffer(*copy.myBuffer);
SetLoop(copy.GetLoop());
SetPitch(copy.GetPitch());
SetVolume(copy.GetVolume());
SetPosition(copy.GetPosition());
SetRelativeToListener(copy.IsRelativeToListener());
SetMinDistance(copy.GetMinDistance());
SetAttenuation(copy.GetAttenuation());
}
////////////////////////////////////////////////////////////
Sound::~Sound()
{
if (mySource)
{
if (myBuffer)
{
Stop();
ALCheck(alSourcei(mySource, AL_BUFFER, 0));
}
ALCheck(alDeleteSources(1, &mySource));
}
Stop();
}
@ -123,54 +104,6 @@ void Sound::SetLoop(bool Loop)
}
////////////////////////////////////////////////////////////
void Sound::SetPitch(float pitch)
{
ALCheck(alSourcef(mySource, AL_PITCH, pitch));
}
////////////////////////////////////////////////////////////
void Sound::SetVolume(float volume)
{
ALCheck(alSourcef(mySource, AL_GAIN, volume * 0.01f));
}
////////////////////////////////////////////////////////////
void Sound::SetPosition(float x, float y, float z)
{
ALCheck(alSource3f(mySource, AL_POSITION, x, y, z));
}
////////////////////////////////////////////////////////////
void Sound::SetPosition(const Vector3f& position)
{
SetPosition(position.x, position.y, position.z);
}
////////////////////////////////////////////////////////////
void Sound::SetRelativeToListener(bool relative)
{
ALCheck(alSourcei(mySource, AL_SOURCE_RELATIVE, relative));
}
////////////////////////////////////////////////////////////
void Sound::SetMinDistance(float distance)
{
ALCheck(alSourcef(mySource, AL_REFERENCE_DISTANCE, distance));
}
////////////////////////////////////////////////////////////
void Sound::SetAttenuation(float attenuation)
{
ALCheck(alSourcef(mySource, AL_ROLLOFF_FACTOR, attenuation));
}
////////////////////////////////////////////////////////////
void Sound::SetPlayingOffset(float timeOffset)
{
@ -195,66 +128,6 @@ bool Sound::GetLoop() const
}
////////////////////////////////////////////////////////////
float Sound::GetPitch() const
{
ALfloat pitch;
ALCheck(alGetSourcef(mySource, AL_PITCH, &pitch));
return pitch;
}
////////////////////////////////////////////////////////////
float Sound::GetVolume() const
{
ALfloat gain;
ALCheck(alGetSourcef(mySource, AL_GAIN, &gain));
return gain * 100.f;
}
////////////////////////////////////////////////////////////
Vector3f Sound::GetPosition() const
{
Vector3f position;
ALCheck(alGetSource3f(mySource, AL_POSITION, &position.x, &position.y, &position.z));
return position;
}
////////////////////////////////////////////////////////////
bool Sound::IsRelativeToListener() const
{
ALint relative;
ALCheck(alGetSourcei(mySource, AL_SOURCE_RELATIVE, &relative));
return relative != 0;
}
////////////////////////////////////////////////////////////
float Sound::GetMinDistance() const
{
ALfloat distance;
ALCheck(alGetSourcef(mySource, AL_REFERENCE_DISTANCE, &distance));
return distance;
}
////////////////////////////////////////////////////////////
float Sound::GetAttenuation() const
{
ALfloat attenuation;
ALCheck(alGetSourcef(mySource, AL_ROLLOFF_FACTOR, &attenuation));
return attenuation;
}
////////////////////////////////////////////////////////////
float Sound::GetPlayingOffset() const
{
@ -268,25 +141,14 @@ float Sound::GetPlayingOffset() const
////////////////////////////////////////////////////////////
Sound::Status Sound::GetStatus() const
{
ALint status;
ALCheck(alGetSourcei(mySource, AL_SOURCE_STATE, &status));
switch (status)
{
case AL_INITIAL :
case AL_STOPPED : return Stopped;
case AL_PAUSED : return Paused;
case AL_PLAYING : return Playing;
}
return Stopped;
return SoundSource::GetStatus();
}
////////////////////////////////////////////////////////////
Sound& Sound::operator =(const Sound& other)
Sound& Sound::operator =(const Sound& right)
{
Sound temp(other);
Sound temp(right);
std::swap(mySource, temp.mySource);
std::swap(myBuffer, temp.myBuffer);

View file

@ -237,7 +237,7 @@ bool SoundBuffer::Update(unsigned int channelsCount, unsigned int sampleRate)
return false;
// Find the good format according to the number of channels
ALenum format = priv::AudioDevice::GetInstance().GetFormatFromChannelsCount(channelsCount);
ALenum format = priv::AudioDevice::GetFormatFromChannelsCount(channelsCount);
// Check if the format is valid
if (format == 0)

View file

@ -167,11 +167,11 @@ private :
private :
static sf_count_t GetLength(void* UserData);
static sf_count_t Read(void* Ptr, sf_count_t Count, void* UserData);
static sf_count_t Seek(sf_count_t Offset, int Whence, void* UserData);
static sf_count_t Tell(void* UserData);
static sf_count_t Write(const void* Ptr, sf_count_t Count, void* UserData);
static sf_count_t GetLength(void* userData);
static sf_count_t Read(void* ptr, sf_count_t count, void* userData);
static sf_count_t Seek(sf_count_t offset, int whence, void* userData);
static sf_count_t Tell(void* userData);
static sf_count_t Write(const void* ptr, sf_count_t count, void* userData);
const char* myDataStart;
const char* myDataPtr;

View file

@ -83,7 +83,7 @@ void SoundRecorder::Start(unsigned int sampleRate)
return;
}
// Clear the sample array
// Clear the array of samples
mySamples.clear();
// Store the sample rate
@ -121,9 +121,7 @@ unsigned int SoundRecorder::GetSampleRate() const
////////////////////////////////////////////////////////////
bool SoundRecorder::CanCapture()
{
ALCdevice* device = priv::AudioDevice::GetInstance().GetDevice();
return alcIsExtensionPresent(device, "ALC_EXT_CAPTURE") != AL_FALSE;
return priv::AudioDevice::IsExtensionSupported("ALC_EXT_CAPTURE");
}

View file

@ -0,0 +1,190 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio/SoundSource.hpp>
#include <SFML/Audio/ALCheck.hpp>
namespace sf
{
////////////////////////////////////////////////////////////
SoundSource::SoundSource()
{
ALCheck(alGenSources(1, &mySource));
ALCheck(alSourcei(mySource, AL_BUFFER, 0));
}
////////////////////////////////////////////////////////////
SoundSource::SoundSource(const SoundSource& copy)
{
ALCheck(alGenSources(1, &mySource));
ALCheck(alSourcei(mySource, AL_BUFFER, 0));
SetPitch(copy.GetPitch());
SetVolume(copy.GetVolume());
SetPosition(copy.GetPosition());
SetRelativeToListener(copy.IsRelativeToListener());
SetMinDistance(copy.GetMinDistance());
SetAttenuation(copy.GetAttenuation());
}
////////////////////////////////////////////////////////////
SoundSource::~SoundSource()
{
ALCheck(alSourcei(mySource, AL_BUFFER, 0));
ALCheck(alDeleteSources(1, &mySource));
}
////////////////////////////////////////////////////////////
void SoundSource::SetPitch(float pitch)
{
ALCheck(alSourcef(mySource, AL_PITCH, pitch));
}
////////////////////////////////////////////////////////////
void SoundSource::SetVolume(float volume)
{
ALCheck(alSourcef(mySource, AL_GAIN, volume * 0.01f));
}
////////////////////////////////////////////////////////////
void SoundSource::SetPosition(float x, float y, float z)
{
ALCheck(alSource3f(mySource, AL_POSITION, x, y, z));
}
////////////////////////////////////////////////////////////
void SoundSource::SetPosition(const Vector3f& position)
{
SetPosition(position.x, position.y, position.z);
}
////////////////////////////////////////////////////////////
void SoundSource::SetRelativeToListener(bool relative)
{
ALCheck(alSourcei(mySource, AL_SOURCE_RELATIVE, relative));
}
////////////////////////////////////////////////////////////
void SoundSource::SetMinDistance(float distance)
{
ALCheck(alSourcef(mySource, AL_REFERENCE_DISTANCE, distance));
}
////////////////////////////////////////////////////////////
void SoundSource::SetAttenuation(float attenuation)
{
ALCheck(alSourcef(mySource, AL_ROLLOFF_FACTOR, attenuation));
}
////////////////////////////////////////////////////////////
float SoundSource::GetPitch() const
{
ALfloat pitch;
ALCheck(alGetSourcef(mySource, AL_PITCH, &pitch));
return pitch;
}
////////////////////////////////////////////////////////////
float SoundSource::GetVolume() const
{
ALfloat gain;
ALCheck(alGetSourcef(mySource, AL_GAIN, &gain));
return gain * 100.f;
}
////////////////////////////////////////////////////////////
Vector3f SoundSource::GetPosition() const
{
Vector3f position;
ALCheck(alGetSource3f(mySource, AL_POSITION, &position.x, &position.y, &position.z));
return position;
}
////////////////////////////////////////////////////////////
bool SoundSource::IsRelativeToListener() const
{
ALint relative;
ALCheck(alGetSourcei(mySource, AL_SOURCE_RELATIVE, &relative));
return relative != 0;
}
////////////////////////////////////////////////////////////
float SoundSource::GetMinDistance() const
{
ALfloat distance;
ALCheck(alGetSourcef(mySource, AL_REFERENCE_DISTANCE, &distance));
return distance;
}
////////////////////////////////////////////////////////////
float SoundSource::GetAttenuation() const
{
ALfloat attenuation;
ALCheck(alGetSourcef(mySource, AL_ROLLOFF_FACTOR, &attenuation));
return attenuation;
}
////////////////////////////////////////////////////////////
SoundSource::Status SoundSource::GetStatus() const
{
ALint status;
ALCheck(alGetSourcei(mySource, AL_SOURCE_STATE, &status));
switch (status)
{
case AL_INITIAL :
case AL_STOPPED : return Stopped;
case AL_PAUSED : return Paused;
case AL_PLAYING : return Playing;
}
return Stopped;
}
} // namespace sf

View file

@ -61,7 +61,7 @@ void SoundStream::Initialize(unsigned int channelsCount, unsigned int sampleRate
mySampleRate = sampleRate;
// Deduce the format from the number of channels
myFormat = priv::AudioDevice::GetInstance().GetFormatFromChannelsCount(channelsCount);
myFormat = priv::AudioDevice::GetFormatFromChannelsCount(channelsCount);
// Check if the format is valid
if (myFormat == 0)
@ -79,14 +79,14 @@ void SoundStream::Play()
// Check if the sound parameters have been set
if (myFormat == 0)
{
std::cerr << "Failed to play audio stream : sound parameters have not been initialized (call Initialize first)" << std::endl;
std::cerr << "Failed to play audio stream: sound parameters have not been initialized (call Initialize first)" << std::endl;
return;
}
// If the sound is already playing (probably paused), just resume it
if (myIsStreaming)
{
Sound::Play();
ALCheck(alSourcePlay(mySource));
return;
}
@ -100,6 +100,13 @@ void SoundStream::Play()
}
////////////////////////////////////////////////////////////
void SoundStream::Pause()
{
ALCheck(alSourcePause(mySource));
}
////////////////////////////////////////////////////////////
void SoundStream::Stop()
{
@ -124,9 +131,9 @@ unsigned int SoundStream::GetSampleRate() const
////////////////////////////////////////////////////////////
Sound::Status SoundStream::GetStatus() const
SoundStream::Status SoundStream::GetStatus() const
{
Status status = Sound::GetStatus();
Status status = SoundSource::GetStatus();
// To compensate for the lag between Play() and alSourcePlay()
if ((status == Stopped) && myIsStreaming)
@ -155,7 +162,10 @@ void SoundStream::SetPlayingOffset(float timeOffset)
////////////////////////////////////////////////////////////
float SoundStream::GetPlayingOffset() const
{
return Sound::GetPlayingOffset() + static_cast<float>(mySamplesProcessed) / mySampleRate / myChannelsCount;
ALfloat seconds = 0.f;
ALCheck(alGetSourcef(mySource, AL_SEC_OFFSET, &seconds));
return seconds + static_cast<float>(mySamplesProcessed) / mySampleRate / myChannelsCount;
}
@ -184,17 +194,17 @@ void SoundStream::Run()
bool requestStop = FillQueue();
// Play the sound
Sound::Play();
ALCheck(alSourcePlay(mySource));
while (myIsStreaming)
{
// The stream has been interrupted !
if (Sound::GetStatus() == Stopped)
// The stream has been interrupted!
if (SoundSource::GetStatus() == Stopped)
{
if (!requestStop)
{
// Just continue
Sound::Play();
ALCheck(alSourcePlay(mySource));
}
else
{
@ -205,13 +215,13 @@ void SoundStream::Run()
// Get the number of buffers that have been processed (ie. ready for reuse)
ALint nbProcessed;
ALCheck(alGetSourcei(Sound::mySource, AL_BUFFERS_PROCESSED, &nbProcessed));
ALCheck(alGetSourcei(mySource, AL_BUFFERS_PROCESSED, &nbProcessed));
while (nbProcessed--)
{
// Pop the first unused buffer from the queue
ALuint buffer;
ALCheck(alSourceUnqueueBuffers(Sound::mySource, 1, &buffer));
ALCheck(alSourceUnqueueBuffers(mySource, 1, &buffer));
// Retrieve its size and add it to the samples count
if (buffer == endBuffer)
@ -250,18 +260,18 @@ void SoundStream::Run()
}
// Leave some time for the other threads if the stream is still playing
if (Sound::GetStatus() != Stopped)
if (SoundSource::GetStatus() != Stopped)
Sleep(0.1f);
}
// Stop the playback
Sound::Stop();
ALCheck(alSourceStop(mySource));
// Unqueue any buffer left in the queue
ClearQueue();
// Delete the buffers
ALCheck(alSourcei(Sound::mySource, AL_BUFFER, 0));
ALCheck(alSourcei(mySource, AL_BUFFER, 0));
ALCheck(alDeleteBuffers(BuffersCount, myBuffers));
}
@ -284,7 +294,7 @@ bool SoundStream::FillAndPushBuffer(unsigned int buffer)
ALCheck(alBufferData(buffer, myFormat, data.Samples, size, mySampleRate));
// Push it into the sound queue
ALCheck(alSourceQueueBuffers(Sound::mySource, 1, &buffer));
ALCheck(alSourceQueueBuffers(mySource, 1, &buffer));
}
return requestStop;
@ -311,12 +321,12 @@ void SoundStream::ClearQueue()
{
// Get the number of buffers still in the queue
ALint nbQueued;
ALCheck(alGetSourcei(Sound::mySource, AL_BUFFERS_QUEUED, &nbQueued));
ALCheck(alGetSourcei(mySource, AL_BUFFERS_QUEUED, &nbQueued));
// Unqueue them all
ALuint buffer;
for (ALint i = 0; i < nbQueued; ++i)
ALCheck(alSourceUnqueueBuffers(Sound::mySource, 1, &buffer));
ALCheck(alSourceUnqueueBuffers(mySource, 1, &buffer));
}
} // namespace sf