Audio: Fix some type warning

Former-commit-id: 9ed6d64e771e77d03b91060823efb4236739914b [formerly 0efc1b14cd61f3d33fc642dbe4eb6bf05d58ec7e] [formerly dbc079525b48a2efb8a7917b4b376a318f8d5fae [formerly 7c202e02ac2a8b745208e1b852ff44d2169ebaf0]]
Former-commit-id: 27a65bcbd5e499dcc741f76a5c0a31bc9ae09e60 [formerly 97ed7176fd1f0f906229d19e68aedc335a7ca420]
Former-commit-id: 1779855a4f20dc4216648aaed59542fd8c6d7bc8
This commit is contained in:
Lynix 2016-09-04 19:56:11 +02:00
parent cfba75812d
commit aabf10cc61
9 changed files with 47 additions and 147 deletions

View File

@ -11,7 +11,7 @@
namespace Nz
{
template<typename T> void MixToMono(T* input, T* output, unsigned int channelCount, unsigned int frameCount);
template<typename T> void MixToMono(T* input, T* output, UInt32 channelCount, UInt64 frameCount);
}
#include <Nazara/Audio/Algorithm.inl>

View File

@ -19,16 +19,16 @@ namespace Nz
* \remark The input buffer may be the same as the output one
*/
template<typename T>
void MixToMono(T* input, T* output, unsigned int channelCount, unsigned int frameCount)
void MixToMono(T* input, T* output, UInt32 channelCount, UInt64 frameCount)
{
// To avoid overflow, we use, as an accumulator, a type which is large enough: (u)int 64 bits for integers, double for floatings
typedef typename std::conditional<std::is_unsigned<T>::value, UInt64, Int64>::type BiggestInt;
typedef typename std::conditional<std::is_integral<T>::value, BiggestInt, double>::type Biggest;
for (unsigned int i = 0; i < frameCount; ++i)
for (UInt64 i = 0; i < frameCount; ++i)
{
Biggest acc = Biggest(0);
for (unsigned int j = 0; j < channelCount; ++j)
for (UInt32 j = 0; j < channelCount; ++j)
acc += input[i * channelCount + j];
output[i] = static_cast<T>(acc / channelCount);

View File

@ -48,7 +48,7 @@ namespace Nz
UInt32 GetDuration() const;
AudioFormat GetFormat() const;
UInt32 GetPlayingOffset() const;
UInt32 GetSampleCount() const;
UInt64 GetSampleCount() const;
UInt32 GetSampleRate() const;
SoundStatus GetStatus() const;

View File

@ -50,18 +50,18 @@ namespace Nz
public:
SoundBuffer() = default;
SoundBuffer(AudioFormat format, unsigned int sampleCount, unsigned int sampleRate, const Int16* samples);
SoundBuffer(AudioFormat format, UInt64 sampleCount, UInt32 sampleRate, const Int16* samples);
SoundBuffer(const SoundBuffer&) = delete;
SoundBuffer(SoundBuffer&&) = delete;
~SoundBuffer();
bool Create(AudioFormat format, unsigned int sampleCount, unsigned int sampleRate, const Int16* samples);
bool Create(AudioFormat format, UInt64 sampleCount, UInt32 sampleRate, const Int16* samples);
void Destroy();
UInt32 GetDuration() const;
AudioFormat GetFormat() const;
const Int16* GetSamples() const;
UInt32 GetSampleCount() const;
UInt64 GetSampleCount() const;
UInt32 GetSampleRate() const;
bool IsValid() const;

View File

@ -21,11 +21,11 @@ namespace Nz
virtual UInt32 GetDuration() const = 0;
virtual AudioFormat GetFormat() const = 0;
virtual UInt32 GetSampleCount() const = 0;
virtual UInt64 GetSampleCount() const = 0;
virtual UInt32 GetSampleRate() const = 0;
virtual unsigned int Read(void* buffer, unsigned int sampleCount) = 0;
virtual void Seek(UInt32 offset) = 0;
virtual UInt64 Read(void* buffer, UInt64 sampleCount) = 0;
virtual void Seek(UInt64 offset) = 0;
};
}

View File

@ -97,7 +97,7 @@ namespace Nz
return m_format;
}
UInt32 GetSampleCount() const override
UInt64 GetSampleCount() const override
{
return m_sampleCount;
}
@ -131,7 +131,7 @@ namespace Nz
bool Open(Stream& stream, bool forceMono)
{
SF_INFO infos;
infos.format = 0; // Format inconnu
infos.format = 0; // Unknown format
m_handle = sf_open_virtual(&callbacks, SFM_READ, &infos, &stream);
if (!m_handle)
@ -154,7 +154,7 @@ namespace Nz
return false;
}
m_sampleCount = static_cast<UInt32>(infos.channels*infos.frames);
m_sampleCount = infos.channels*infos.frames;
m_sampleRate = infos.samplerate;
// Durée de la musique (s) = samples / channels*rate
@ -180,7 +180,7 @@ namespace Nz
return true;
}
unsigned int Read(void* buffer, unsigned int sampleCount) override
UInt64 Read(void* buffer, UInt64 sampleCount) override
{
// Si la musique a été demandée en mono, nous devons la convertir à la volée lors de la lecture
if (m_mixToMono)
@ -190,13 +190,13 @@ namespace Nz
sf_count_t readSampleCount = sf_read_short(m_handle, m_mixBuffer.data(), m_format * sampleCount);
MixToMono(m_mixBuffer.data(), static_cast<Int16*>(buffer), m_format, sampleCount);
return static_cast<unsigned int>(readSampleCount / m_format);
return readSampleCount / m_format;
}
else
return static_cast<unsigned int>(sf_read_short(m_handle, static_cast<Int16*>(buffer), sampleCount));
return sf_read_short(m_handle, static_cast<Int16*>(buffer), sampleCount);
}
void Seek(UInt32 offset) override
void Seek(UInt64 offset) override
{
sf_seek(m_handle, offset*m_sampleRate / 1000, SEEK_SET);
}
@ -208,8 +208,8 @@ namespace Nz
SNDFILE* m_handle;
bool m_mixToMono;
UInt32 m_duration;
unsigned int m_sampleCount;
unsigned int m_sampleRate;
UInt32 m_sampleRate;
UInt64 m_sampleCount;
};
bool IsSupported(const String& extension)

View File

@ -166,17 +166,10 @@ namespace Nz
*
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
*/
UInt32 Music::GetPlayingOffset() const
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Music not created");
return 0;
}
#endif
NazaraAssert(m_impl, "Music not created");
// Prevent music thread from enqueing new buffers while we're getting the count
Nz::LockGuard lock(m_impl->bufferLock);
@ -192,16 +185,9 @@ namespace Nz
*
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
*/
UInt32 Music::GetSampleCount() const
UInt64 Music::GetSampleCount() const
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Music not created");
return 0;
}
#endif
NazaraAssert(m_impl, "Music not created");
return m_impl->stream->GetSampleCount();
}
@ -212,16 +198,9 @@ namespace Nz
*
* \remark Produces a NazaraError if there is no music with NAZARA_AUDIO_SAFE defined
*/
UInt32 Music::GetSampleRate() const
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Music not created");
return 0;
}
#endif
NazaraAssert(m_impl, "Music not created");
return m_impl->sampleRate;
}
@ -233,16 +212,9 @@ namespace Nz
* \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
if (!m_impl)
{
NazaraError("Music not created");
return SoundStatus_Stopped;
}
#endif
NazaraAssert(m_impl, "Music not created");
SoundStatus status = GetInternalStatus();
@ -425,8 +397,8 @@ namespace Nz
bool Music::FillAndQueueBuffer(unsigned int buffer)
{
unsigned int sampleCount = m_impl->chunkSamples.size();
unsigned int sampleRead = 0;
std::size_t sampleCount = m_impl->chunkSamples.size();
std::size_t sampleRead = 0;
// Fill the buffer by reading from the stream
for (;;)
@ -446,7 +418,7 @@ namespace Nz
// Update the buffer (send it to OpenAL) and queue it if we got any data
if (sampleRead > 0)
{
alBufferData(buffer, m_impl->audioFormat, &m_impl->chunkSamples[0], sampleRead*sizeof(Int16), m_impl->sampleRate);
alBufferData(buffer, m_impl->audioFormat, &m_impl->chunkSamples[0], static_cast<ALsizei>(sampleRead*sizeof(Int16)), static_cast<ALsizei>(m_impl->sampleRate));
alSourceQueueBuffers(m_source, 1, &buffer);
}

View File

@ -27,7 +27,6 @@ namespace Nz
*
* \param soundBuffer Buffer to read sound from
*/
Sound::Sound(const SoundBuffer* soundBuffer)
{
SetBuffer(soundBuffer);
@ -38,7 +37,6 @@ namespace Nz
*
* \param sound Sound to copy
*/
Sound::Sound(const Sound& sound) :
SoundEmitter(sound)
{
@ -50,7 +48,6 @@ namespace Nz
*
* \see Stop
*/
Sound::~Sound()
{
Stop();
@ -61,7 +58,6 @@ namespace Nz
*
* \param loop Should sound loop
*/
void Sound::EnableLooping(bool loop)
{
alSourcei(m_source, AL_LOOPING, loop);
@ -71,7 +67,6 @@ namespace Nz
* \brief Gets the internal buffer
* \return Internal buffer
*/
const SoundBuffer* Sound::GetBuffer() const
{
return m_buffer;
@ -83,7 +78,6 @@ namespace Nz
*
* \remark Produces a NazaraError if there is no buffer
*/
UInt32 Sound::GetDuration() const
{
NazaraAssert(m_buffer, "Invalid sound buffer");
@ -95,7 +89,6 @@ namespace Nz
* \brief Gets the current offset in the sound
* \return Offset in milliseconds (works with entire seconds)
*/
UInt32 Sound::GetPlayingOffset() const
{
ALint samples = 0;
@ -108,7 +101,6 @@ namespace Nz
* \brief Gets the status of the music
* \return Enumeration of type SoundStatus (Playing, Stopped, ...)
*/
SoundStatus Sound::GetStatus() const
{
return GetInternalStatus();
@ -118,7 +110,6 @@ namespace Nz
* \brief Checks whether the sound is looping
* \return true if it is the case
*/
bool Sound::IsLooping() const
{
ALint loop;
@ -141,7 +132,6 @@ namespace Nz
* \brief Checks whether the sound is playing
* \return true if it is the case
*/
bool Sound::IsPlaying() const
{
return GetStatus() == SoundStatus_Playing;
@ -156,7 +146,6 @@ namespace Nz
*
* \remark Produces a NazaraError if loading failed
*/
bool Sound::LoadFromFile(const String& filePath, const SoundBufferParams& params)
{
SoundBufferRef buffer = SoundBuffer::New();
@ -180,7 +169,6 @@ namespace Nz
*
* \remark Produces a NazaraError if loading failed
*/
bool Sound::LoadFromMemory(const void* data, std::size_t size, const SoundBufferParams& params)
{
SoundBufferRef buffer = SoundBuffer::New();
@ -203,7 +191,6 @@ namespace Nz
*
* \remark Produces a NazaraError if loading failed
*/
bool Sound::LoadFromStream(Stream& stream, const SoundBufferParams& params)
{
SoundBufferRef buffer = SoundBuffer::New();
@ -220,7 +207,6 @@ namespace Nz
/*!
* \brief Pauses the sound
*/
void Sound::Pause()
{
alSourcePause(m_source);
@ -231,16 +217,9 @@ namespace Nz
*
* \remark Produces a NazaraError if the sound is not playable with NAZARA_AUDIO_SAFE defined
*/
void Sound::Play()
{
#if NAZARA_AUDIO_SAFE
if (!IsPlayable())
{
NazaraError("Invalid sound buffer");
return;
}
#endif
NazaraAssert(IsPlayable(), "Music is not playable");
alSourcePlay(m_source);
}
@ -252,16 +231,9 @@ namespace Nz
*
* \remark Produces a NazaraError if buffer is invalid with NAZARA_AUDIO_SAFE defined
*/
void Sound::SetBuffer(const SoundBuffer* buffer)
{
#if NAZARA_AUDIO_SAFE
if (buffer && !buffer->IsValid())
{
NazaraError("Invalid sound buffer");
return;
}
#endif
NazaraAssert(!buffer || buffer->IsValid(), "Invalid sound buffer");
if (m_buffer == buffer)
return;
@ -281,7 +253,6 @@ namespace Nz
*
* \param offset Offset in the sound in milliseconds
*/
void Sound::SetPlayingOffset(UInt32 offset)
{
alSourcei(m_source, AL_SAMPLE_OFFSET, static_cast<ALint>(offset/1000.f * m_buffer->GetSampleRate()));
@ -290,7 +261,6 @@ namespace Nz
/*!
* \brief Stops the sound
*/
void Sound::Stop()
{
alSourceStop(m_source);

View File

@ -6,6 +6,7 @@
#include <Nazara/Audio/Audio.hpp>
#include <Nazara/Audio/Config.hpp>
#include <Nazara/Audio/OpenAL.hpp>
#include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/Error.hpp>
#include <cstring>
#include <memory>
@ -40,7 +41,7 @@ namespace Nz
AudioFormat format;
UInt32 duration;
std::unique_ptr<Int16[]> samples;
UInt32 sampleCount;
UInt64 sampleCount;
UInt32 sampleRate;
};
@ -57,8 +58,7 @@ namespace Nz
*
* \see Create
*/
SoundBuffer::SoundBuffer(AudioFormat format, unsigned int sampleCount, unsigned int sampleRate, const Int16* samples)
SoundBuffer::SoundBuffer(AudioFormat format, UInt64 sampleCount, UInt32 sampleRate, const Int16* samples)
{
Create(format, sampleCount, sampleRate, samples);
@ -76,7 +76,6 @@ namespace Nz
*
* \see Destroy
*/
SoundBuffer::~SoundBuffer()
{
OnSoundBufferRelease(this);
@ -96,8 +95,7 @@ namespace Nz
* \remark Produces a NazaraError if creation went wrong with NAZARA_AUDIO_SAFE defined,
* this could happen if parameters are invalid or creation of OpenAL buffers failed
*/
bool SoundBuffer::Create(AudioFormat format, unsigned int sampleCount, unsigned int sampleRate, const Int16* samples)
bool SoundBuffer::Create(AudioFormat format, UInt64 sampleCount, UInt32 sampleRate, const Int16* samples)
{
Destroy();
@ -132,19 +130,18 @@ namespace Nz
ALuint buffer;
alGenBuffers(1, &buffer);
if (alGetError() != AL_NO_ERROR)
{
NazaraError("Failed to create OpenAL buffer");
return false;
}
alBufferData(buffer, OpenAL::AudioFormat[format], samples, sampleCount*sizeof(Int16), sampleRate);
CallOnExit clearBufferOnExit([buffer] () { alDeleteBuffers(1, &buffer); });
alBufferData(buffer, OpenAL::AudioFormat[format], samples, static_cast<ALsizei>(sampleCount*sizeof(Int16)), static_cast<ALsizei>(sampleRate));
if (alGetError() != AL_NO_ERROR)
{
alDeleteBuffers(1, &buffer);
NazaraError("Failed to set OpenAL buffer");
return false;
}
@ -158,6 +155,8 @@ namespace Nz
m_impl->samples.reset(new Int16[sampleCount]);
std::memcpy(&m_impl->samples[0], samples, sampleCount*sizeof(Int16));
clearBufferOnExit.Reset();
return true;
}
@ -182,16 +181,9 @@ namespace Nz
*
* \remark Produces a NazaraError if there is no sound buffer with NAZARA_AUDIO_SAFE defined
*/
UInt32 SoundBuffer::GetDuration() const
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Sound buffer not created");
return 0;
}
#endif
NazaraAssert(m_impl, "Sound buffer not created");
return m_impl->duration;
}
@ -205,13 +197,7 @@ namespace Nz
AudioFormat SoundBuffer::GetFormat() const
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Sound buffer not created");
return AudioFormat_Unknown;
}
#endif
NazaraAssert(m_impl, "Sound buffer not created");
return m_impl->format;
}
@ -222,16 +208,9 @@ namespace Nz
*
* \remark Produces a NazaraError if there is no sound buffer with NAZARA_AUDIO_SAFE defined
*/
const Int16* SoundBuffer::GetSamples() const
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Sound buffer not created");
return nullptr;
}
#endif
NazaraAssert(m_impl, "Sound buffer not created");
return m_impl->samples.get();
}
@ -242,16 +221,9 @@ namespace Nz
*
* \remark Produces a NazaraError if there is no sound buffer with NAZARA_AUDIO_SAFE defined
*/
unsigned int SoundBuffer::GetSampleCount() const
UInt64 SoundBuffer::GetSampleCount() const
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Sound buffer not created");
return 0;
}
#endif
NazaraAssert(m_impl, "Sound buffer not created");
return m_impl->sampleCount;
}
@ -262,16 +234,9 @@ namespace Nz
*
* \remark Produces a NazaraError if there is no sound buffer with NAZARA_AUDIO_SAFE defined
*/
unsigned int SoundBuffer::GetSampleRate() const
UInt32 SoundBuffer::GetSampleRate() const
{
#if NAZARA_AUDIO_SAFE
if (!m_impl)
{
NazaraError("Sound buffer not created");
return 0;
}
#endif
NazaraAssert(m_impl, "Sound buffer not created");
return m_impl->sampleRate;
}
@ -293,7 +258,6 @@ namespace Nz
* \param filePath Path to the file
* \param params Parameters for the sound buffer
*/
bool SoundBuffer::LoadFromFile(const String& filePath, const SoundBufferParams& params)
{
return SoundBufferLoader::LoadFromFile(this, filePath, params);
@ -307,7 +271,6 @@ namespace Nz
* \param size Size of the memory
* \param params Parameters for the sound buffer
*/
bool SoundBuffer::LoadFromMemory(const void* data, std::size_t size, const SoundBufferParams& params)
{
return SoundBufferLoader::LoadFromMemory(this, data, size, params);
@ -320,7 +283,6 @@ namespace Nz
* \param stream Stream to the sound buffer
* \param params Parameters for the sound buffer
*/
bool SoundBuffer::LoadFromStream(Stream& stream, const SoundBufferParams& params)
{
return SoundBufferLoader::LoadFromStream(this, stream, params);
@ -332,7 +294,6 @@ namespace Nz
*
* \param format Format to check
*/
bool SoundBuffer::IsFormatSupported(AudioFormat format)
{
return Audio::IsFormatSupported(format);
@ -344,7 +305,6 @@ namespace Nz
*
* \remark Produces a NazaraError if there is no sound buffer with NAZARA_AUDIO_SAFE defined
*/
unsigned int SoundBuffer::GetOpenALBuffer() const
{
#ifdef NAZARA_DEBUG
@ -364,7 +324,6 @@ namespace Nz
*
* \remark Produces a NazaraError if sub-initialization failed
*/
bool SoundBuffer::Initialize()
{
if (!SoundBufferLibrary::Initialize())
@ -385,7 +344,6 @@ namespace Nz
/*!
* \brief Uninitializes the libraries and managers
*/
void SoundBuffer::Uninitialize()
{
SoundBufferManager::Uninitialize();