From ffe938d422cae2fe5893c20e14a66d4d91add780 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sun, 4 Sep 2016 19:56:11 +0200 Subject: [PATCH] Audio: Fix some type warning Former-commit-id: 37c2bdbdfc591ff3cc031ee1498ae1050183b4bd [formerly e96bf359c986942fc707834c9c5992eca9c57b22] [formerly a1c53ad3ae4218aea8163f5f33245e352b820a34 [formerly a3c51b11bd591e8accad191bfa5547323436e4cb]] Former-commit-id: 5d7faba8324c26970f0ca0681d695e6650cff32f [formerly c564a0adc734737472ea066e5d362637f9697138] Former-commit-id: 7979614ff5460597b01e042e7e520ad7d55c7493 --- include/Nazara/Audio/Algorithm.hpp | 2 +- include/Nazara/Audio/Algorithm.inl | 6 +- include/Nazara/Audio/Music.hpp | 2 +- include/Nazara/Audio/SoundBuffer.hpp | 6 +- include/Nazara/Audio/SoundStream.hpp | 6 +- src/Nazara/Audio/Formats/sndfileLoader.cpp | 18 +++--- src/Nazara/Audio/Music.cpp | 46 +++----------- src/Nazara/Audio/Sound.cpp | 34 +--------- src/Nazara/Audio/SoundBuffer.cpp | 74 +++++----------------- 9 files changed, 47 insertions(+), 147 deletions(-) diff --git a/include/Nazara/Audio/Algorithm.hpp b/include/Nazara/Audio/Algorithm.hpp index ddb72a6b0..ea5bd09b6 100644 --- a/include/Nazara/Audio/Algorithm.hpp +++ b/include/Nazara/Audio/Algorithm.hpp @@ -11,7 +11,7 @@ namespace Nz { - template void MixToMono(T* input, T* output, unsigned int channelCount, unsigned int frameCount); + template void MixToMono(T* input, T* output, UInt32 channelCount, UInt64 frameCount); } #include diff --git a/include/Nazara/Audio/Algorithm.inl b/include/Nazara/Audio/Algorithm.inl index 1075a8b57..ce3c87369 100644 --- a/include/Nazara/Audio/Algorithm.inl +++ b/include/Nazara/Audio/Algorithm.inl @@ -19,16 +19,16 @@ namespace Nz * \remark The input buffer may be the same as the output one */ template - 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::value, UInt64, Int64>::type BiggestInt; typedef typename std::conditional::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(acc / channelCount); diff --git a/include/Nazara/Audio/Music.hpp b/include/Nazara/Audio/Music.hpp index d08732fa7..f31882890 100644 --- a/include/Nazara/Audio/Music.hpp +++ b/include/Nazara/Audio/Music.hpp @@ -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; diff --git a/include/Nazara/Audio/SoundBuffer.hpp b/include/Nazara/Audio/SoundBuffer.hpp index 2b02847b4..6b8e5c385 100644 --- a/include/Nazara/Audio/SoundBuffer.hpp +++ b/include/Nazara/Audio/SoundBuffer.hpp @@ -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; diff --git a/include/Nazara/Audio/SoundStream.hpp b/include/Nazara/Audio/SoundStream.hpp index 4edc7b412..8fd93f412 100644 --- a/include/Nazara/Audio/SoundStream.hpp +++ b/include/Nazara/Audio/SoundStream.hpp @@ -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; }; } diff --git a/src/Nazara/Audio/Formats/sndfileLoader.cpp b/src/Nazara/Audio/Formats/sndfileLoader.cpp index 2aca17976..055e3b6a6 100644 --- a/src/Nazara/Audio/Formats/sndfileLoader.cpp +++ b/src/Nazara/Audio/Formats/sndfileLoader.cpp @@ -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(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(buffer), m_format, sampleCount); - return static_cast(readSampleCount / m_format); + return readSampleCount / m_format; } else - return static_cast(sf_read_short(m_handle, static_cast(buffer), sampleCount)); + return sf_read_short(m_handle, static_cast(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) diff --git a/src/Nazara/Audio/Music.cpp b/src/Nazara/Audio/Music.cpp index 9e38c7114..1f0df6ba5 100644 --- a/src/Nazara/Audio/Music.cpp +++ b/src/Nazara/Audio/Music.cpp @@ -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(sampleRead*sizeof(Int16)), static_cast(m_impl->sampleRate)); alSourceQueueBuffers(m_source, 1, &buffer); } diff --git a/src/Nazara/Audio/Sound.cpp b/src/Nazara/Audio/Sound.cpp index d2b71318b..3e2e27faa 100644 --- a/src/Nazara/Audio/Sound.cpp +++ b/src/Nazara/Audio/Sound.cpp @@ -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(offset/1000.f * m_buffer->GetSampleRate())); @@ -290,7 +261,6 @@ namespace Nz /*! * \brief Stops the sound */ - void Sound::Stop() { alSourceStop(m_source); diff --git a/src/Nazara/Audio/SoundBuffer.cpp b/src/Nazara/Audio/SoundBuffer.cpp index b59e74e23..4b2b0410e 100644 --- a/src/Nazara/Audio/SoundBuffer.cpp +++ b/src/Nazara/Audio/SoundBuffer.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -40,7 +41,7 @@ namespace Nz AudioFormat format; UInt32 duration; std::unique_ptr 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(sampleCount*sizeof(Int16)), static_cast(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();