Documentation for module: Audio
Former-commit-id: acb9e00a7af8bf1c36f42b45fe7e7df99e03c0f9
This commit is contained in:
@@ -14,6 +14,19 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
/*!
|
||||
* \ingroup audio
|
||||
* \class Nz::Music
|
||||
* \brief Audio class that represents a music
|
||||
*
|
||||
* \remark Module Audio needs to be initialized to use this class
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Checks whether the parameters for the loading of the music are correct
|
||||
* \return true If parameters are valid
|
||||
*/
|
||||
|
||||
bool MusicParams::IsValid() const
|
||||
{
|
||||
return true;
|
||||
@@ -32,11 +45,26 @@ namespace Nz
|
||||
unsigned int sampleRate;
|
||||
};
|
||||
|
||||
/*!
|
||||
* \brief Destructs the object and calls Destroy
|
||||
*
|
||||
* \see Destroy
|
||||
*/
|
||||
|
||||
Music::~Music()
|
||||
{
|
||||
Destroy();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Creates a music with a sound stream
|
||||
* \return true if creation was succesful
|
||||
*
|
||||
* \param soundStream Sound stream which is the source for the music
|
||||
*
|
||||
* \remark Produces a NazaraError if soundStream is invalid with NAZARA_AUDIO_SAFE defined
|
||||
*/
|
||||
|
||||
bool Music::Create(SoundStream* soundStream)
|
||||
{
|
||||
NazaraAssert(soundStream, "Invalid stream");
|
||||
@@ -48,7 +76,7 @@ namespace Nz
|
||||
m_impl = new MusicImpl;
|
||||
m_impl->sampleRate = soundStream->GetSampleRate();
|
||||
m_impl->audioFormat = OpenAL::AudioFormat[format];
|
||||
m_impl->chunkSamples.resize(format * m_impl->sampleRate); // Une seconde de samples
|
||||
m_impl->chunkSamples.resize(format * m_impl->sampleRate); // One second of samples
|
||||
m_impl->stream.reset(soundStream);
|
||||
|
||||
SetPlayingOffset(0);
|
||||
@@ -56,6 +84,10 @@ namespace Nz
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Destroys the current music and frees resources
|
||||
*/
|
||||
|
||||
void Music::Destroy()
|
||||
{
|
||||
if (m_impl)
|
||||
@@ -67,6 +99,14 @@ namespace Nz
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Enables the looping of the music
|
||||
*
|
||||
* \param loop Should music loop
|
||||
*
|
||||
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
|
||||
*/
|
||||
|
||||
void Music::EnableLooping(bool loop)
|
||||
{
|
||||
#if NAZARA_AUDIO_SAFE
|
||||
@@ -80,6 +120,13 @@ namespace Nz
|
||||
m_impl->loop = loop;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the duration of the music
|
||||
* \return Duration of the music in milliseconds
|
||||
*
|
||||
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
|
||||
*/
|
||||
|
||||
UInt32 Music::GetDuration() const
|
||||
{
|
||||
#if NAZARA_AUDIO_SAFE
|
||||
@@ -93,6 +140,13 @@ namespace Nz
|
||||
return m_impl->stream->GetDuration();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the format of the music
|
||||
* \return Enumeration of type AudioFormat (mono, stereo, ...)
|
||||
*
|
||||
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
|
||||
*/
|
||||
|
||||
AudioFormat Music::GetFormat() const
|
||||
{
|
||||
#if NAZARA_AUDIO_SAFE
|
||||
@@ -106,6 +160,13 @@ namespace Nz
|
||||
return m_impl->stream->GetFormat();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the current offset in the music
|
||||
* \return Offset in milliseconds (works with entire seconds)
|
||||
*
|
||||
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
|
||||
*/
|
||||
|
||||
UInt32 Music::GetPlayingOffset() const
|
||||
{
|
||||
#if NAZARA_AUDIO_SAFE
|
||||
@@ -125,6 +186,13 @@ namespace Nz
|
||||
return static_cast<UInt32>((1000ULL * (samples + (m_impl->processedSamples / m_impl->stream->GetFormat()))) / m_impl->sampleRate);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the number of samples in the music
|
||||
* \return Count of samples (number of seconds * sample rate * channel count)
|
||||
*
|
||||
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
|
||||
*/
|
||||
|
||||
UInt32 Music::GetSampleCount() const
|
||||
{
|
||||
#if NAZARA_AUDIO_SAFE
|
||||
@@ -138,6 +206,13 @@ namespace Nz
|
||||
return m_impl->stream->GetSampleCount();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the rates of sample in the music
|
||||
* \return Rate of sample in Hertz (Hz)
|
||||
*
|
||||
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
|
||||
*/
|
||||
|
||||
UInt32 Music::GetSampleRate() const
|
||||
{
|
||||
#if NAZARA_AUDIO_SAFE
|
||||
@@ -151,6 +226,14 @@ namespace Nz
|
||||
return m_impl->sampleRate;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the status of the music
|
||||
* \return Enumeration of type SoundStatus (Playing, Stopped, ...)
|
||||
*
|
||||
* \remark If the music is not playing, Stopped is returned
|
||||
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
|
||||
*/
|
||||
|
||||
SoundStatus Music::GetStatus() const
|
||||
{
|
||||
#if NAZARA_AUDIO_SAFE
|
||||
@@ -163,13 +246,20 @@ namespace Nz
|
||||
|
||||
SoundStatus status = GetInternalStatus();
|
||||
|
||||
// Pour compenser les éventuels retards (ou le laps de temps entre Play() et la mise en route du thread)
|
||||
// To compensate any delays (or the timelaps between Play() and the thread startup)
|
||||
if (m_impl->streaming && status == SoundStatus_Stopped)
|
||||
status = SoundStatus_Playing;
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether the music is looping
|
||||
* \return true if it is the case
|
||||
*
|
||||
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
|
||||
*/
|
||||
|
||||
bool Music::IsLooping() const
|
||||
{
|
||||
#if NAZARA_AUDIO_SAFE
|
||||
@@ -183,26 +273,61 @@ namespace Nz
|
||||
return m_impl->loop;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Loads the music from file
|
||||
* \return true if loading is successful
|
||||
*
|
||||
* \param filePath Path to the file
|
||||
* \param params Parameters for the music
|
||||
*/
|
||||
|
||||
bool Music::OpenFromFile(const String& filePath, const MusicParams& params)
|
||||
{
|
||||
return MusicLoader::LoadFromFile(this, filePath, params);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Loads the music from memory
|
||||
* \return true if loading is successful
|
||||
*
|
||||
* \param data Raw memory
|
||||
* \param size Size of the memory
|
||||
* \param params Parameters for the music
|
||||
*/
|
||||
|
||||
bool Music::OpenFromMemory(const void* data, std::size_t size, const MusicParams& params)
|
||||
{
|
||||
return MusicLoader::LoadFromMemory(this, data, size, params);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Loads the music from stream
|
||||
* \return true if loading is successful
|
||||
*
|
||||
* \param stream Stream to the music
|
||||
* \param params Parameters for the music
|
||||
*/
|
||||
|
||||
bool Music::OpenFromStream(Stream& stream, const MusicParams& params)
|
||||
{
|
||||
return MusicLoader::LoadFromStream(this, stream, params);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Pauses the music
|
||||
*/
|
||||
|
||||
void Music::Pause()
|
||||
{
|
||||
alSourcePause(m_source);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Plays the music
|
||||
*
|
||||
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
|
||||
*/
|
||||
|
||||
void Music::Play()
|
||||
{
|
||||
#if NAZARA_AUDIO_SAFE
|
||||
@@ -238,6 +363,14 @@ namespace Nz
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the playing offset for the music
|
||||
*
|
||||
* \param offset Offset in the music in milliseconds
|
||||
*
|
||||
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
|
||||
*/
|
||||
|
||||
void Music::SetPlayingOffset(UInt32 offset)
|
||||
{
|
||||
#if NAZARA_AUDIO_SAFE
|
||||
@@ -260,6 +393,12 @@ namespace Nz
|
||||
Play();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Stops the music
|
||||
*
|
||||
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
|
||||
*/
|
||||
|
||||
void Music::Stop()
|
||||
{
|
||||
#if NAZARA_AUDIO_SAFE
|
||||
@@ -277,6 +416,13 @@ namespace Nz
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Fills the buffer and queues it up
|
||||
* \return true if operation was successful
|
||||
*
|
||||
* \param buffer Index of the buffer
|
||||
*/
|
||||
|
||||
bool Music::FillAndQueueBuffer(unsigned int buffer)
|
||||
{
|
||||
unsigned int sampleCount = m_impl->chunkSamples.size();
|
||||
@@ -304,27 +450,31 @@ namespace Nz
|
||||
alSourceQueueBuffers(m_source, 1, &buffer);
|
||||
}
|
||||
|
||||
return sampleRead != sampleCount; // Fin du stream (N'arrive pas en cas de loop)
|
||||
return sampleRead != sampleCount; // End of stream (Does not happen when looping)
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Thread function for the music
|
||||
*/
|
||||
|
||||
void Music::MusicThread()
|
||||
{
|
||||
// Allocation des buffers de streaming
|
||||
// Allocation of streaming buffers
|
||||
ALuint buffers[NAZARA_AUDIO_STREAMED_BUFFER_COUNT];
|
||||
alGenBuffers(NAZARA_AUDIO_STREAMED_BUFFER_COUNT, buffers);
|
||||
|
||||
for (unsigned int i = 0; i < NAZARA_AUDIO_STREAMED_BUFFER_COUNT; ++i)
|
||||
{
|
||||
if (FillAndQueueBuffer(buffers[i]))
|
||||
break; // Nous avons atteint la fin du stream, inutile de rajouter des buffers
|
||||
break; // We have reached the end of the stream, there is no use to add new buffers
|
||||
}
|
||||
|
||||
alSourcePlay(m_source);
|
||||
|
||||
// Boucle de lecture (remplissage de nouveaux buffers au fur et à mesure)
|
||||
// Reading loop (Filling new buffers as playing)
|
||||
while (m_impl->streaming)
|
||||
{
|
||||
// La lecture s'est arrêtée, nous avons atteint la fin du stream
|
||||
// The reading has stopped, we have reached the end of the stream
|
||||
SoundStatus status = GetInternalStatus();
|
||||
if (status == SoundStatus_Stopped)
|
||||
{
|
||||
@@ -334,7 +484,7 @@ namespace Nz
|
||||
|
||||
Nz::LockGuard lock(m_impl->bufferLock);
|
||||
|
||||
// On traite les buffers lus
|
||||
// We treat read buffers
|
||||
ALint processedCount = 0;
|
||||
alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &processedCount);
|
||||
while (processedCount--)
|
||||
@@ -355,14 +505,14 @@ namespace Nz
|
||||
|
||||
lock.Unlock();
|
||||
|
||||
// On retourne dormir un peu
|
||||
// We go back to sleep
|
||||
Thread::Sleep(50);
|
||||
}
|
||||
|
||||
// Arrêt de la lecture du son (dans le cas où ça ne serait pas déjà fait)
|
||||
// Stop playing of the sound (in the case where it has not been already done)
|
||||
alSourceStop(m_source);
|
||||
|
||||
// On supprime les buffers du stream
|
||||
// We delete buffers from the stream
|
||||
ALint queuedBufferCount;
|
||||
alGetSourcei(m_source, AL_BUFFERS_QUEUED, &queuedBufferCount);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user