Merge remote-tracking branch 'origin/Audio-update'

Conflicts:
	include/Nazara/Core/ResourceLoader.hpp
	include/Nazara/Core/ResourceLoader.inl
	src/Nazara/Audio/Loaders/sndfile/Loader.cpp
	src/Nazara/Audio/Sound.cpp
	src/Nazara/Utility/Loaders/MD2/Loader.cpp
	src/Nazara/Utility/Loaders/MD5Anim/Loader.cpp
	src/Nazara/Utility/Loaders/MD5Mesh/Loader.cpp
	src/Nazara/Utility/Loaders/PCX/Loader.cpp
	src/Nazara/Utility/Loaders/STB/Loader.cpp

Former-commit-id: e0706cc0feafb1ad182b8cc51e2b18f8f3664b97
This commit is contained in:
Lynix
2014-01-08 12:03:52 +01:00
9 changed files with 559 additions and 12 deletions

View File

@@ -4,7 +4,10 @@
#include <Nazara/Audio/Loaders/sndfile.hpp>
#include <Nazara/Audio/Audio.hpp>
#include <Nazara/Audio/Config.hpp>
#include <Nazara/Audio/Music.hpp>
#include <Nazara/Audio/SoundBuffer.hpp>
#include <Nazara/Audio/SoundStream.hpp>
#include <Nazara/Core/Endianness.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/File.hpp>
@@ -61,6 +64,109 @@ namespace
static SF_VIRTUAL_IO callbacks = {GetSize, Seek, Read, nullptr, Tell};
class sndfileStream : public NzSoundStream
{
public:
sndfileStream() :
m_file(nullptr),
m_handle(nullptr)
{
}
~sndfileStream()
{
if (m_handle)
sf_close(m_handle);
if (m_file)
delete m_file;
}
nzUInt32 GetDuration() const
{
return m_duration;
}
nzAudioFormat GetFormat() const
{
return m_format;
}
unsigned int GetSampleCount() const
{
return m_sampleCount;
}
unsigned int GetSampleRate() const
{
return m_sampleRate;
}
bool Open(const NzString& filePath)
{
m_file = new NzFile(filePath);
if (!m_file->Open(NzFile::ReadOnly))
{
NazaraError("Failed to open file " + filePath);
return false;
}
return Open(*m_file);
}
bool Open(NzInputStream& stream)
{
SF_INFO infos;
m_handle = sf_open_virtual(&callbacks, SFM_READ, &infos, &stream);
if (!m_handle)
{
NazaraError("Failed to open sound: " + NzString(sf_strerror(m_handle)));
return false;
}
m_format = NzAudio::GetAudioFormat(infos.channels);
if (m_format == nzAudioFormat_Unknown)
{
NazaraError("Channel count not handled");
sf_close(m_handle);
m_handle = nullptr;
return false;
}
m_sampleCount = infos.channels*infos.frames;
m_sampleRate = infos.samplerate;
m_duration = 1000*m_sampleCount / (m_format*m_sampleRate);
// https://github.com/LaurentGomila/SFML/issues/271
// http://www.mega-nerd.com/libsndfile/command.html#SFC_SET_SCALE_FLOAT_INT_READ
///FIXME: Seulement le Vorbis ?
if (infos.format & SF_FORMAT_VORBIS)
sf_command(m_handle, SFC_SET_SCALE_FLOAT_INT_READ, nullptr, SF_TRUE);
return true;
}
unsigned int Read(void* buffer, unsigned int sampleCount)
{
return sf_read_short(m_handle, reinterpret_cast<nzInt16*>(buffer), sampleCount);
}
void Seek(nzUInt32 offset)
{
sf_seek(m_handle, offset*m_sampleRate / 1000, SEEK_SET);
}
private:
nzAudioFormat m_format;
NzFile* m_file;
SNDFILE* m_handle;
unsigned int m_duration;
unsigned int m_sampleCount;
unsigned int m_sampleRate;
};
bool IsSupported(const NzString& extension)
{
static std::set<NzString> supportedExtensions = {
@@ -71,7 +177,7 @@ namespace
return supportedExtensions.find(extension) != supportedExtensions.end();
}
nzTernary Check(NzInputStream& stream, const NzSoundBufferParams& parameters)
nzTernary CheckMusic(NzInputStream& stream, const NzMusicParams& parameters)
{
NazaraUnused(parameters);
@@ -88,7 +194,68 @@ namespace
return nzTernary_False;
}
bool Load(NzSoundBuffer* soundBuffer, NzInputStream& stream, const NzSoundBufferParams& parameters)
bool LoadMusicFile(NzMusic* music, const NzString& filePath, const NzMusicParams& parameters)
{
NazaraUnused(parameters);
std::unique_ptr<sndfileStream> musicStream(new sndfileStream);
if (!musicStream->Open(filePath))
{
NazaraError("Failed to open music stream");
return false;
}
if (!music->Create(musicStream.get()))
{
NazaraError("Failed to create music");
return false;
}
musicStream.release();
return true;
}
bool LoadMusicStream(NzMusic* music, NzInputStream& stream, const NzMusicParams& parameters)
{
NazaraUnused(parameters);
std::unique_ptr<sndfileStream> musicStream(new sndfileStream);
if (!musicStream->Open(stream))
{
NazaraError("Failed to open music stream");
return false;
}
if (!music->Create(musicStream.get()))
{
NazaraError("Failed to create music");
return false;
}
musicStream.release();
return true;
}
nzTernary CheckSoundBuffer(NzInputStream& stream, const NzSoundBufferParams& parameters)
{
NazaraUnused(parameters);
SF_INFO info;
info.format = 0;
SNDFILE* file = sf_open_virtual(&callbacks, SFM_READ, &info, &stream);
if (file)
{
sf_close(file);
return nzTernary_True;
}
else
return nzTernary_False;
}
bool LoadSoundBuffer(NzSoundBuffer* soundBuffer, NzInputStream& stream, const NzSoundBufferParams& parameters)
{
NazaraUnused(parameters);
@@ -123,16 +290,16 @@ namespace
if (sf_read_short(file, samples.get(), sampleCount) != sampleCount)
{
sf_close(file);
NazaraError("Failed to read samples");
NazaraError("Failed to read samples");
return false;
}
if (!soundBuffer->Create(format, static_cast<unsigned int>(sampleCount), info.samplerate, samples.get()))
{
sf_close(file);
NazaraError("Failed to create sound buffer");
NazaraError("Failed to create sound buffer");
return false;
}
@@ -142,10 +309,12 @@ namespace
void NzLoaders_sndfile_Register()
{
NzSoundBufferLoader::RegisterLoader(IsSupported, Check, Load);
NzMusicLoader::RegisterLoader(IsSupported, CheckMusic, LoadMusicStream, LoadMusicFile);
NzSoundBufferLoader::RegisterLoader(IsSupported, CheckSoundBuffer, LoadSoundBuffer);
}
void NzLoaders_sndfile_Unregister()
{
NzSoundBufferLoader::UnregisterLoader(IsSupported, Check, Load);
NzMusicLoader::UnregisterLoader(IsSupported, CheckMusic, LoadMusicStream, LoadMusicFile);
NzSoundBufferLoader::UnregisterLoader(IsSupported, CheckSoundBuffer, LoadSoundBuffer);
}

273
src/Nazara/Audio/Music.cpp Normal file
View File

@@ -0,0 +1,273 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine - Audio module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Audio/Music.hpp>
#include <Nazara/Audio/Audio.hpp>
#include <Nazara/Audio/SoundStream.hpp>
#include <Nazara/Core/Thread.hpp>
#include <vector>
#include <AL/al.h>
#include <Nazara/Audio/Debug.hpp>
bool NzMusicParams::IsValid() const
{
return true;
}
struct NzMusicImpl
{
ALenum audioFormat;
std::vector<nzInt16> chunkSamples;
NzSoundStream* stream;
NzThread thread;
bool loop = false;
bool streaming = false;
bool paused = false;
unsigned int sampleRate;
};
NzMusic::~NzMusic()
{
Destroy();
}
bool NzMusic::Create(NzSoundStream* soundStream)
{
Destroy();
#if NAZARA_AUDIO_SAFE
if (!soundStream)
{
NazaraError("Sound stream must be valid");
return false;
}
#endif
nzAudioFormat format = soundStream->GetFormat();
m_impl = new NzMusicImpl;
m_impl->sampleRate = soundStream->GetSampleRate();
m_impl->audioFormat = NzAudio::GetOpenALFormat(format);
m_impl->chunkSamples.resize(format * m_impl->sampleRate); // Une seconde de samples
m_impl->stream = soundStream;
return true;
}
void NzMusic::Destroy()
{
if (m_impl)
{
Stop();
delete m_impl->stream;
delete m_impl;
m_impl = nullptr;
}
}
void NzMusic::EnableLooping(bool loop)
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Music not created");
return;
}
#endif
m_impl->loop = loop;
}
nzUInt32 NzMusic::GetDuration() const
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Music not created");
return 0;
}
#endif
return m_impl->stream->GetDuration();
}
nzUInt32 NzMusic::GetPlayingOffset() const
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Music not created");
return 0;
}
#endif
return 0;
}
nzSoundStatus NzMusic::GetStatus() const
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Music not created");
return nzSoundStatus_Stopped;
}
#endif
nzSoundStatus status = GetInternalStatus();
if (m_impl->streaming && status == nzSoundStatus_Stopped)
status = nzSoundStatus_Playing;
return status;
}
bool NzMusic::IsLooping() const
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Music not created");
return false;
}
#endif
return m_impl->loop;
}
bool NzMusic::OpenFromFile(const NzString& filePath, const NzMusicParams& params)
{
return NzMusicLoader::LoadFromFile(this, filePath, params);
}
bool NzMusic::OpenFromMemory(const void* data, std::size_t size, const NzMusicParams& params)
{
return NzMusicLoader::LoadFromMemory(this, data, size, params);
}
bool NzMusic::OpenFromStream(NzInputStream& stream, const NzMusicParams& params)
{
return NzMusicLoader::LoadFromStream(this, stream, params);
}
void NzMusic::Pause()
{
alSourcePause(m_source);
}
void NzMusic::Play()
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Music not created");
return;
}
#endif
if (m_impl->streaming)
{
if (GetStatus() != nzSoundStatus_Playing)
alSourcePlay(m_source);
return;
}
m_impl->stream->Seek(0);
m_impl->streaming = true;
m_impl->thread = NzThread(&NzMusic::MusicThread, this);
return;
}
void NzMusic::Stop()
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Music not created");
return;
}
#endif
if (m_impl->streaming)
{
m_impl->streaming = false;
m_impl->thread.Join();
}
}
bool NzMusic::FillBuffer(unsigned int buffer)
{
unsigned int sampleCount = m_impl->chunkSamples.size();
unsigned int sampleRead = 0;
for (;;)
{
sampleRead += m_impl->stream->Read(&m_impl->chunkSamples[sampleRead], sampleCount-sampleRead);
if (sampleRead != sampleCount && m_impl->loop)
m_impl->stream->Seek(0);
else
break;
}
if (sampleRead > 0)
alBufferData(buffer, m_impl->audioFormat, &m_impl->chunkSamples[0], sampleRead*sizeof(nzInt16), m_impl->sampleRate);
return sampleRead != sampleCount; // Fin du fichier (N'arrive pas en cas de loop)
}
void NzMusic::MusicThread()
{
ALuint buffers[NAZARA_AUDIO_STREAMEDBUFFERCOUNT];
alGenBuffers(NAZARA_AUDIO_STREAMEDBUFFERCOUNT, buffers);
for (unsigned int i = 0; i < NAZARA_AUDIO_STREAMEDBUFFERCOUNT; ++i)
{
bool eof = FillBuffer(buffers[i]);
alSourceQueueBuffers(m_source, 1, &buffers[i]);
if (eof)
break; // Nous avons fini, nous ne continuons pas
}
alSourcePlay(m_source);
while (m_impl->streaming)
{
nzSoundStatus status = GetInternalStatus();
if (status == nzSoundStatus_Stopped)
{
m_impl->streaming = false;
break;
}
ALint processedCount = 0;
alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &processedCount);
ALuint buffer;
while (processedCount--)
{
alSourceUnqueueBuffers(m_source, 1, &buffer);
if (!FillBuffer(buffer)) // Fin du fichier ?
alSourceQueueBuffers(m_source, 1, &buffer);
}
NzThread::Sleep(50);
}
alSourceStop(m_source);
ALint queuedBufferCount;
alGetSourcei(m_source, AL_BUFFERS_QUEUED, &queuedBufferCount);
ALuint buffer;
for (ALint i = 0; i < queuedBufferCount; ++i)
alSourceUnqueueBuffers(m_source, 1, &buffer);
alDeleteBuffers(NAZARA_AUDIO_STREAMEDBUFFERCOUNT, buffers);
}
NzMusicLoader::LoaderList NzMusic::s_loaders;

View File

@@ -138,19 +138,17 @@ void NzSound::Pause()
alSourcePause(m_source);
}
bool NzSound::Play()
void NzSound::Play()
{
#if NAZARA_AUDIO_SAFE
if (!m_buffer)
{
NazaraError("Invalid sound buffer");
return false;
return;
}
#endif
alSourcePlay(m_source);
return true;
}
void NzSound::SetBuffer(const NzSoundBuffer* buffer)

View File

@@ -0,0 +1,7 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine - Audio module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Audio/SoundStream.hpp>
NzSoundStream::~NzSoundStream() = default;