Upgrade Audio (part 2)

This commit is contained in:
Jérôme Leclercq
2021-05-24 22:09:47 +02:00
parent ac57b3fbf4
commit 8cdd0b51cb
15 changed files with 158 additions and 104 deletions

View File

@@ -43,33 +43,6 @@ namespace Nz
OpenAL::Uninitialize();
}
/*!
* \brief Gets the format of the audio
* \return AudioFormat Enumeration type for the format
*
* \param channelCount Number of channels
*
* \remark Produces a NazaraError if the number of channels is erroneous (3 or 5) and AudioFormat_Unknown is returned
*/
AudioFormat Audio::GetAudioFormat(unsigned int channelCount) const
{
switch (channelCount)
{
case 1:
case 2:
case 4:
case 6:
case 7:
case 8:
return static_cast<AudioFormat>(channelCount);
default:
NazaraError("Invalid channel count: " + NumberToString(channelCount));
return AudioFormat_Unknown;
}
}
/*!
* \brief Gets the factor of the doppler effect
* \return Global factor of the doppler effect
@@ -200,10 +173,10 @@ namespace Nz
*/
bool Audio::IsFormatSupported(AudioFormat format) const
{
if (format == AudioFormat_Unknown)
if (format == AudioFormat::Unknown)
return false;
return OpenAL::AudioFormat[format] != 0;
return OpenAL::AudioFormat[UnderlyingCast(format)] != 0;
}
/*!

View File

@@ -15,17 +15,45 @@
#include <Nazara/Core/File.hpp>
#include <Nazara/Core/MemoryView.hpp>
#include <Nazara/Core/Stream.hpp>
#include <sndfile.h>
#include <memory>
#include <optional>
#include <set>
#include <string_view>
#include <vector>
#include <sndfile.h>
#include <Nazara/Audio/Debug.hpp>
namespace Nz
{
namespace Detail
{
std::optional<AudioFormat> GuessFormat(UInt32 channelCount)
{
switch (channelCount)
{
case 1:
return AudioFormat::U16_Mono;
case 2:
return AudioFormat::U16_Stereo;
case 4:
return AudioFormat::U16_Quad;
case 5:
return AudioFormat::U16_5_1;
case 6:
return AudioFormat::U16_6_1;
case 7:
return AudioFormat::U16_7_1;
default:
return std::nullopt;
}
}
sf_count_t GetSize(void* user_data)
{
Stream* stream = static_cast<Stream*>(user_data);
@@ -93,7 +121,7 @@ namespace Nz
{
// Nous avons besoin du nombre de canaux d'origine pour convertir en mono, nous trichons donc un peu...
if (m_mixToMono)
return AudioFormat_Mono;
return AudioFormat::U16_Mono;
else
return m_format;
}
@@ -136,10 +164,10 @@ namespace Nz
bool Open(Stream& stream, bool forceMono)
{
SF_INFO infos;
infos.format = 0; // Unknown format
SF_INFO info;
info.format = 0; // Unknown format
m_handle = sf_open_virtual(&callbacks, SFM_READ, &infos, &stream);
m_handle = sf_open_virtual(&callbacks, SFM_READ, &info, &stream);
if (!m_handle)
{
NazaraError("Failed to open sound: " + std::string(sf_strerror(m_handle)));
@@ -153,30 +181,30 @@ namespace Nz
m_handle = nullptr;
});
m_format = Audio::Instance()->GetAudioFormat(infos.channels);
if (m_format == AudioFormat_Unknown)
std::optional<AudioFormat> formatOpt = GuessFormat(info.channels);
if (!formatOpt)
{
NazaraError("Channel count not handled");
NazaraError("unexpected channel count: " + std::to_string(info.channels));
return false;
}
m_sampleCount = infos.channels*infos.frames;
m_sampleRate = infos.samplerate;
m_format = *formatOpt;
// Durée de la musique (s) = samples / channels*rate
m_duration = static_cast<UInt32>(1000ULL*m_sampleCount / (m_format*m_sampleRate));
m_duration = static_cast<UInt32>(1000ULL * info.frames / info.samplerate);
m_sampleCount = info.channels * info.frames;
m_sampleRate = info.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)
if (info.format & SF_FORMAT_VORBIS)
sf_command(m_handle, SFC_SET_SCALE_FLOAT_INT_READ, nullptr, SF_TRUE);
// On mixera en mono lors de la lecture
if (forceMono && m_format != AudioFormat_Mono)
if (forceMono && m_format != AudioFormat::U16_Mono)
{
m_mixToMono = true;
m_sampleCount = static_cast<UInt32>(infos.frames);
m_sampleCount = static_cast<UInt32>(info.frames);
}
else
m_mixToMono = false;
@@ -191,12 +219,14 @@ namespace Nz
// Si la musique a été demandée en mono, nous devons la convertir à la volée lors de la lecture
if (m_mixToMono)
{
// On garde un buffer sur le côté pour éviter la réallocation
m_mixBuffer.resize(m_format * sampleCount);
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);
UInt32 channelCount = GetChannelCount(m_format);
return readSampleCount / m_format;
// On garde un buffer sur le côté pour éviter la réallocation
m_mixBuffer.resize(channelCount * sampleCount);
sf_count_t readSampleCount = sf_read_short(m_handle, m_mixBuffer.data(), channelCount * sampleCount);
MixToMono(m_mixBuffer.data(), static_cast<Int16*>(buffer), channelCount, sampleCount);
return readSampleCount / channelCount;
}
else
return sf_read_short(m_handle, static_cast<Int16*>(buffer), sampleCount);
@@ -314,7 +344,7 @@ namespace Nz
if (!file)
{
NazaraError("Failed to load sound file: " + std::string(sf_strerror(file)));
return nullptr;
return {};
}
CallOnExit onExit([file]
@@ -322,13 +352,15 @@ namespace Nz
sf_close(file);
});
AudioFormat format = Audio::Instance()->GetAudioFormat(info.channels);
if (format == AudioFormat_Unknown)
std::optional<AudioFormat> formatOpt = GuessFormat(info.channels);
if (!formatOpt)
{
NazaraError("Channel count not handled");
return nullptr;
NazaraError("unexpected channel count: " + std::to_string(info.channels));
return {};
}
AudioFormat format = *formatOpt;
// https://github.com/LaurentGomila/SFML/issues/271
// http://www.mega-nerd.com/libsndfile/command.html#SFC_SET_SCALE_FLOAT_INT_READ
///FIXME: Only Vorbis?
@@ -341,15 +373,15 @@ namespace Nz
if (sf_read_short(file, samples.get(), sampleCount) != sampleCount)
{
NazaraError("Failed to read samples");
return nullptr;
return {};
}
// Convert to mono if required
if (parameters.forceMono && format != AudioFormat_Mono)
if (parameters.forceMono && format != AudioFormat::U16_Mono)
{
MixToMono(samples.get(), samples.get(), static_cast<UInt32>(info.channels), static_cast<UInt64>(info.frames));
format = AudioFormat_Mono;
format = AudioFormat::U16_Mono;
sampleCount = static_cast<unsigned int>(info.frames);
}

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Audio/Music.hpp>
#include <Nazara/Audio/Algorithm.hpp>
#include <Nazara/Audio/OpenAL.hpp>
#include <Nazara/Audio/SoundStream.hpp>
#include <atomic>
@@ -67,8 +68,8 @@ namespace Nz
m_impl = std::make_unique<MusicImpl>();
m_impl->sampleRate = soundStream->GetSampleRate();
m_impl->audioFormat = OpenAL::AudioFormat[format];
m_impl->chunkSamples.resize(format * m_impl->sampleRate); // One second of samples
m_impl->audioFormat = OpenAL::AudioFormat[UnderlyingCast(format)];
m_impl->chunkSamples.resize(GetChannelCount(format) * m_impl->sampleRate); // One second of samples
m_impl->stream = std::move(soundStream);
SetPlayingOffset(0);
@@ -147,7 +148,7 @@ namespace Nz
ALint samples = 0;
alGetSourcei(m_source, AL_SAMPLE_OFFSET, &samples);
return static_cast<UInt32>((1000ULL * (samples + (m_impl->processedSamples / m_impl->stream->GetFormat()))) / m_impl->sampleRate);
return static_cast<UInt32>((1000ULL * (samples + (m_impl->processedSamples / GetChannelCount(m_impl->stream->GetFormat())))) / m_impl->sampleRate);
}
/*!
@@ -189,8 +190,8 @@ namespace Nz
SoundStatus status = GetInternalStatus();
// To compensate any delays (or the timelaps between Play() and the thread startup)
if (m_impl->streaming && status == SoundStatus_Stopped)
status = SoundStatus_Playing;
if (m_impl->streaming && status == SoundStatus::Stopped)
status = SoundStatus::Playing;
return status;
}
@@ -289,11 +290,11 @@ namespace Nz
{
switch (GetStatus())
{
case SoundStatus_Playing:
case SoundStatus::Playing:
SetPlayingOffset(0);
break;
case SoundStatus_Paused:
case SoundStatus::Paused:
alSourcePlay(m_source);
break;
@@ -328,7 +329,7 @@ namespace Nz
Stop();
m_impl->playingOffset = offset;
m_impl->processedSamples = UInt64(offset) * m_impl->sampleRate * m_impl->stream->GetFormat() / 1000ULL;
m_impl->processedSamples = UInt64(offset) * m_impl->sampleRate * GetChannelCount(m_impl->stream->GetFormat()) / 1000ULL;
if (isPlaying)
Play();
@@ -405,7 +406,7 @@ namespace Nz
{
// The reading has stopped, we have reached the end of the stream
SoundStatus status = GetInternalStatus();
if (status == SoundStatus_Stopped)
if (status == SoundStatus::Stopped)
{
m_impl->streaming = false;
break;

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Audio/OpenAL.hpp>
#include <Nazara/Core/Algorithm.hpp>
#include <Nazara/Core/DynLib.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Log.hpp>
@@ -347,8 +348,7 @@ namespace Nz
s_library.Unload();
}
///WARNING: The integer value is the number of canals owned by the format
ALenum OpenAL::AudioFormat[AudioFormat_Max+1] = {0}; // Added values with loading of OpenAL
ALenum OpenAL::AudioFormat[AudioFormatCount] = {0}; // Added values with loading of OpenAL
/*!
* \brief Closes the device
@@ -440,19 +440,19 @@ namespace Nz
}
// We complete the formats table
AudioFormat[AudioFormat_Mono] = AL_FORMAT_MONO16;
AudioFormat[AudioFormat_Stereo] = AL_FORMAT_STEREO16;
AudioFormat[UnderlyingCast(AudioFormat::U16_Mono)] = AL_FORMAT_MONO16;
AudioFormat[UnderlyingCast(AudioFormat::U16_Stereo)] = AL_FORMAT_STEREO16;
// "The presence of an enum value does not guarantee the applicability of an extension to the current context."
if (alIsExtensionPresent("AL_EXT_MCFORMATS"))
{
AudioFormat[AudioFormat_Quad] = alGetEnumValue("AL_FORMAT_QUAD16");
AudioFormat[AudioFormat_5_1] = alGetEnumValue("AL_FORMAT_51CHN16");
AudioFormat[AudioFormat_6_1] = alGetEnumValue("AL_FORMAT_61CHN16");
AudioFormat[AudioFormat_7_1] = alGetEnumValue("AL_FORMAT_71CHN16");
AudioFormat[UnderlyingCast(AudioFormat::U16_Quad)] = alGetEnumValue("AL_FORMAT_QUAD16");
AudioFormat[UnderlyingCast(AudioFormat::U16_5_1)] = alGetEnumValue("AL_FORMAT_51CHN16");
AudioFormat[UnderlyingCast(AudioFormat::U16_6_1)] = alGetEnumValue("AL_FORMAT_61CHN16");
AudioFormat[UnderlyingCast(AudioFormat::U16_7_1)] = alGetEnumValue("AL_FORMAT_71CHN16");
}
else if (alIsExtensionPresent("AL_LOKI_quadriphonic"))
AudioFormat[AudioFormat_Quad] = alGetEnumValue("AL_FORMAT_QUAD16_LOKI");
AudioFormat[UnderlyingCast(AudioFormat::U16_Quad)] = alGetEnumValue("AL_FORMAT_QUAD16_LOKI");
return true;
}

View File

@@ -125,7 +125,7 @@ namespace Nz
*/
bool Sound::IsPlaying() const
{
return GetStatus() == SoundStatus_Playing;
return GetStatus() == SoundStatus::Playing;
}
/*!

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Audio/SoundBuffer.hpp>
#include <Nazara/Audio/Algorithm.hpp>
#include <Nazara/Audio/Audio.hpp>
#include <Nazara/Audio/Config.hpp>
#include <Nazara/Audio/OpenAL.hpp>
@@ -130,7 +131,7 @@ namespace Nz
CallOnExit clearBufferOnExit([buffer] () { alDeleteBuffers(1, &buffer); });
alBufferData(buffer, OpenAL::AudioFormat[format], samples, static_cast<ALsizei>(sampleCount*sizeof(Int16)), static_cast<ALsizei>(sampleRate));
alBufferData(buffer, OpenAL::AudioFormat[UnderlyingCast(format)], samples, static_cast<ALsizei>(sampleCount*sizeof(Int16)), static_cast<ALsizei>(sampleRate));
if (alGetError() != AL_NO_ERROR)
{
@@ -140,7 +141,7 @@ namespace Nz
m_impl = std::make_unique<SoundBufferImpl>();
m_impl->buffer = buffer;
m_impl->duration = static_cast<UInt32>((1000ULL*sampleCount / (format * sampleRate)));
m_impl->duration = static_cast<UInt32>((1000ULL*sampleCount / (GetChannelCount(format) * sampleRate)));
m_impl->format = format;
m_impl->sampleCount = sampleCount;
m_impl->sampleRate = sampleRate;

View File

@@ -326,18 +326,18 @@ namespace Nz
{
case AL_INITIAL:
case AL_STOPPED:
return SoundStatus_Stopped;
return SoundStatus::Stopped;
case AL_PAUSED:
return SoundStatus_Paused;
return SoundStatus::Paused;
case AL_PLAYING:
return SoundStatus_Playing;
return SoundStatus::Playing;
default:
NazaraInternalError("Source state unrecognized");
}
return SoundStatus_Stopped;
return SoundStatus::Stopped;
}
}