Big f***ing cleanup part 1
This commit is contained in:
@@ -14,9 +14,7 @@
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Core/File.hpp>
|
||||
#include <Nazara/Core/MemoryView.hpp>
|
||||
#include <Nazara/Core/Mutex.hpp>
|
||||
#include <Nazara/Core/Stream.hpp>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <set>
|
||||
#include <vector>
|
||||
@@ -99,7 +97,7 @@ namespace Nz
|
||||
return m_format;
|
||||
}
|
||||
|
||||
Mutex& GetMutex() override
|
||||
std::mutex& GetMutex() override
|
||||
{
|
||||
return m_mutex;
|
||||
}
|
||||
@@ -114,11 +112,11 @@ namespace Nz
|
||||
return m_sampleRate;
|
||||
}
|
||||
|
||||
bool Open(const String& filePath, bool forceMono)
|
||||
bool Open(const std::filesystem::path& filePath, bool forceMono)
|
||||
{
|
||||
// Nous devons gérer nous-même le flux car il doit rester ouvert après le passage du loader
|
||||
// (les flux automatiquement ouverts par le ResourceLoader étant fermés après celui-ci)
|
||||
std::unique_ptr<File> file(new File);
|
||||
std::unique_ptr<File> file = std::make_unique<File>();
|
||||
if (!file->Open(filePath, OpenMode_ReadOnly))
|
||||
{
|
||||
NazaraError("Failed to open stream from file: " + Error::GetLastError());
|
||||
@@ -219,15 +217,15 @@ namespace Nz
|
||||
AudioFormat m_format;
|
||||
SNDFILE* m_handle;
|
||||
bool m_mixToMono;
|
||||
Mutex m_mutex;
|
||||
std::mutex m_mutex;
|
||||
UInt32 m_duration;
|
||||
UInt32 m_sampleRate;
|
||||
UInt64 m_sampleCount;
|
||||
};
|
||||
|
||||
bool IsSupported(const String& extension)
|
||||
bool IsSupported(const std::string& extension)
|
||||
{
|
||||
static std::set<String> supportedExtensions = {
|
||||
static std::set<std::string> supportedExtensions = {
|
||||
"aiff", "au", "avr", "caf", "flac", "htk", "ircam", "mat4", "mat5", "mpc2k",
|
||||
"nist","ogg", "pvf", "raw", "rf64", "sd2", "sds", "svx", "voc", "w64", "wav", "wve"
|
||||
};
|
||||
@@ -253,9 +251,9 @@ namespace Nz
|
||||
return Ternary_False;
|
||||
}
|
||||
|
||||
SoundStreamRef LoadSoundStreamFile(const String& filePath, const SoundStreamParams& parameters)
|
||||
SoundStreamRef LoadSoundStreamFile(const std::filesystem::path& filePath, const SoundStreamParams& parameters)
|
||||
{
|
||||
std::unique_ptr<sndfileStream> soundStream(new sndfileStream);
|
||||
std::unique_ptr<sndfileStream> soundStream = std::make_unique<sndfileStream>();
|
||||
if (!soundStream->Open(filePath, parameters.forceMono))
|
||||
{
|
||||
NazaraError("Failed to open sound stream");
|
||||
|
||||
@@ -5,12 +5,11 @@
|
||||
#include <Nazara/Audio/Music.hpp>
|
||||
#include <Nazara/Audio/OpenAL.hpp>
|
||||
#include <Nazara/Audio/SoundStream.hpp>
|
||||
#include <Nazara/Core/LockGuard.hpp>
|
||||
#include <Nazara/Core/Mutex.hpp>
|
||||
#include <Nazara/Core/Thread.hpp>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <Nazara/Audio/Debug.hpp>
|
||||
|
||||
@@ -29,9 +28,9 @@ namespace Nz
|
||||
ALenum audioFormat;
|
||||
std::atomic<UInt64> processedSamples;
|
||||
std::vector<Int16> chunkSamples;
|
||||
Mutex bufferLock;
|
||||
std::mutex bufferLock;
|
||||
SoundStreamRef stream;
|
||||
Thread thread;
|
||||
std::thread thread;
|
||||
UInt64 playingOffset;
|
||||
bool loop = false;
|
||||
bool streaming = false;
|
||||
@@ -142,7 +141,7 @@ namespace Nz
|
||||
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);
|
||||
std::lock_guard<std::mutex> lock(m_impl->bufferLock);
|
||||
|
||||
ALint samples = 0;
|
||||
alGetSourcei(m_source, AL_SAMPLE_OFFSET, &samples);
|
||||
@@ -215,7 +214,7 @@ namespace Nz
|
||||
* \param filePath Path to the file
|
||||
* \param params Parameters for the music
|
||||
*/
|
||||
bool Music::OpenFromFile(const String& filePath, const SoundStreamParams& params)
|
||||
bool Music::OpenFromFile(const std::filesystem::path& filePath, const SoundStreamParams& params)
|
||||
{
|
||||
if (SoundStreamRef soundStream = SoundStream::OpenFromFile(filePath, params))
|
||||
return Create(soundStream);
|
||||
@@ -305,7 +304,7 @@ namespace Nz
|
||||
{
|
||||
// Starting streaming's thread
|
||||
m_impl->streaming = true;
|
||||
m_impl->thread = Thread(&Music::MusicThread, this);
|
||||
m_impl->thread = std::thread(&Music::MusicThread, this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,30 +350,29 @@ namespace Nz
|
||||
{
|
||||
std::size_t sampleCount = m_impl->chunkSamples.size();
|
||||
std::size_t sampleRead = 0;
|
||||
|
||||
Nz::LockGuard lock(m_impl->stream->GetMutex());
|
||||
|
||||
m_impl->stream->Seek(m_impl->playingOffset);
|
||||
|
||||
// Fill the buffer by reading from the stream
|
||||
for (;;)
|
||||
{
|
||||
sampleRead += m_impl->stream->Read(&m_impl->chunkSamples[sampleRead], sampleCount - sampleRead);
|
||||
if (sampleRead < sampleCount && m_impl->loop)
|
||||
std::lock_guard<std::mutex> lock(m_impl->stream->GetMutex());
|
||||
|
||||
m_impl->stream->Seek(m_impl->playingOffset);
|
||||
|
||||
// Fill the buffer by reading from the stream
|
||||
for (;;)
|
||||
{
|
||||
// In case we read less than expected, assume we reached the end of the stream and seek back to the beginning
|
||||
m_impl->stream->Seek(0);
|
||||
continue;
|
||||
sampleRead += m_impl->stream->Read(&m_impl->chunkSamples[sampleRead], sampleCount - sampleRead);
|
||||
if (sampleRead < sampleCount && m_impl->loop)
|
||||
{
|
||||
// In case we read less than expected, assume we reached the end of the stream and seek back to the beginning
|
||||
m_impl->stream->Seek(0);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Either we read the size we wanted, either we're not looping
|
||||
break;
|
||||
}
|
||||
|
||||
// Either we read the size we wanted, either we're not looping
|
||||
break;
|
||||
m_impl->playingOffset = m_impl->stream->Tell();
|
||||
}
|
||||
|
||||
m_impl->playingOffset = m_impl->stream->Tell();
|
||||
|
||||
lock.Unlock();
|
||||
|
||||
// Update the buffer (send it to OpenAL) and queue it if we got any data
|
||||
if (sampleRead > 0)
|
||||
{
|
||||
@@ -410,31 +408,31 @@ namespace Nz
|
||||
break;
|
||||
}
|
||||
|
||||
Nz::LockGuard lock(m_impl->bufferLock);
|
||||
|
||||
// We treat read buffers
|
||||
ALint processedCount = 0;
|
||||
alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &processedCount);
|
||||
while (processedCount--)
|
||||
{
|
||||
ALuint buffer;
|
||||
alSourceUnqueueBuffers(m_source, 1, &buffer);
|
||||
std::lock_guard<std::mutex> lock(m_impl->bufferLock);
|
||||
|
||||
ALint bits, size;
|
||||
alGetBufferi(buffer, AL_BITS, &bits);
|
||||
alGetBufferi(buffer, AL_SIZE, &size);
|
||||
// We treat read buffers
|
||||
ALint processedCount = 0;
|
||||
alGetSourcei(m_source, AL_BUFFERS_PROCESSED, &processedCount);
|
||||
while (processedCount--)
|
||||
{
|
||||
ALuint buffer;
|
||||
alSourceUnqueueBuffers(m_source, 1, &buffer);
|
||||
|
||||
if (bits != 0)
|
||||
m_impl->processedSamples += (8 * size) / bits;
|
||||
ALint bits, size;
|
||||
alGetBufferi(buffer, AL_BITS, &bits);
|
||||
alGetBufferi(buffer, AL_SIZE, &size);
|
||||
|
||||
if (FillAndQueueBuffer(buffer))
|
||||
break;
|
||||
if (bits != 0)
|
||||
m_impl->processedSamples += (8 * size) / bits;
|
||||
|
||||
if (FillAndQueueBuffer(buffer))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
lock.Unlock();
|
||||
|
||||
// We go back to sleep
|
||||
Thread::Sleep(50);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
}
|
||||
|
||||
// Stop playing of the sound (in the case where it has not been already done)
|
||||
@@ -456,7 +454,7 @@ namespace Nz
|
||||
if (m_impl->streaming)
|
||||
{
|
||||
m_impl->streaming = false;
|
||||
m_impl->thread.Join();
|
||||
m_impl->thread.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,7 +41,7 @@ namespace Nz
|
||||
std::size_t length;
|
||||
while ((length = std::strlen(deviceString)) > 0)
|
||||
{
|
||||
devices.push_back(String(deviceString, length));
|
||||
devices.emplace_back(deviceString, length);
|
||||
deviceString += length + 1;
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@ namespace Nz
|
||||
bool succeeded = false;
|
||||
for (const char* path : libs)
|
||||
{
|
||||
String libPath(path);
|
||||
std::filesystem::path libPath(path);
|
||||
if (!s_library.Load(libPath))
|
||||
continue;
|
||||
|
||||
@@ -248,7 +248,7 @@ namespace Nz
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
NazaraWarning(libPath + " loading failed: " + String(e.what()));
|
||||
NazaraWarning(libPath.generic_u8string() + " loading failed: " + std::string(e.what()));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,12 +149,12 @@ namespace Nz
|
||||
*
|
||||
* \remark Produces a NazaraError if loading failed
|
||||
*/
|
||||
bool Sound::LoadFromFile(const String& filePath, const SoundBufferParams& params)
|
||||
bool Sound::LoadFromFile(const std::filesystem::path& filePath, const SoundBufferParams& params)
|
||||
{
|
||||
SoundBufferRef buffer = SoundBuffer::LoadFromFile(filePath, params);
|
||||
if (!buffer)
|
||||
{
|
||||
NazaraError("Failed to load buffer from file (" + filePath + ')');
|
||||
NazaraError("Failed to load buffer from file (" + filePath.generic_u8string() + ')');
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -269,7 +269,7 @@ namespace Nz
|
||||
* \param filePath Path to the file
|
||||
* \param params Parameters for the sound buffer
|
||||
*/
|
||||
SoundBufferRef SoundBuffer::LoadFromFile(const String& filePath, const SoundBufferParams& params)
|
||||
SoundBufferRef SoundBuffer::LoadFromFile(const std::filesystem::path& filePath, const SoundBufferParams& params)
|
||||
{
|
||||
return SoundBufferLoader::LoadFromFile(filePath, params);
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace Nz
|
||||
|
||||
SoundStream::~SoundStream() = default;
|
||||
|
||||
SoundStreamRef SoundStream::OpenFromFile(const String& filePath, const SoundStreamParams& params)
|
||||
SoundStreamRef SoundStream::OpenFromFile(const std::filesystem::path& filePath, const SoundStreamParams& params)
|
||||
{
|
||||
return SoundStreamLoader::LoadFromFile(filePath, params);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user