Remove sf::AudioResource

This commit is contained in:
vittorioromeo 2024-05-04 19:30:40 +02:00
parent ca0a231b35
commit 2d51fefb72
11 changed files with 93 additions and 273 deletions

View File

@ -1,68 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#pragma once
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio/Export.hpp>
#include <memory>
namespace sf
{
////////////////////////////////////////////////////////////
/// \brief Base class for classes that require an audio device
///
////////////////////////////////////////////////////////////
class SFML_AUDIO_API AudioResource
{
protected:
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
////////////////////////////////////////////////////////////
AudioResource();
private:
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
const std::shared_ptr<void> m_device; //!< Sound device
};
} // namespace sf
////////////////////////////////////////////////////////////
/// \class sf::AlResource
/// \ingroup audio
///
/// This class is for internal use only, it must be the base
/// of every class that requires a valid audio device in
/// order to work.
///
////////////////////////////////////////////////////////////

View File

@ -29,8 +29,6 @@
////////////////////////////////////////////////////////////
#include <SFML/Audio/Export.hpp>
#include <SFML/Audio/AudioResource.hpp>
#include <SFML/System/Angle.hpp>
#include <SFML/System/Vector3.hpp>
@ -42,7 +40,7 @@ namespace sf
/// \brief Base class defining a sound's properties
///
////////////////////////////////////////////////////////////
class SFML_AUDIO_API SoundSource : protected AudioResource
class SFML_AUDIO_API SoundSource
{
public:
////////////////////////////////////////////////////////////

View File

@ -38,10 +38,6 @@ namespace sf::priv
////////////////////////////////////////////////////////////
AudioDevice::AudioDevice()
{
// Ensure we only ever have a single AudioDevice instance
assert(getInstance() == nullptr);
getInstance() = this;
// Create the log
m_log.emplace();
@ -136,31 +132,33 @@ AudioDevice::AudioDevice()
}
// Set master volume, position, velocity, cone and world up vector
if (const auto result = ma_device_set_master_volume(ma_engine_get_device(&*m_engine),
getListenerProperties().volume * 0.01f);
if (const auto result = ma_device_set_master_volume(ma_engine_get_device(&*m_engine), m_listenerProperties.volume * 0.01f);
result != MA_SUCCESS)
err() << "Failed to set audio device master volume: " << ma_result_description(result) << std::endl;
ma_engine_listener_set_position(&*m_engine,
0,
getListenerProperties().position.x,
getListenerProperties().position.y,
getListenerProperties().position.z);
m_listenerProperties.position.x,
m_listenerProperties.position.y,
m_listenerProperties.position.z);
ma_engine_listener_set_velocity(&*m_engine,
0,
getListenerProperties().velocity.x,
getListenerProperties().velocity.y,
getListenerProperties().velocity.z);
m_listenerProperties.velocity.x,
m_listenerProperties.velocity.y,
m_listenerProperties.velocity.z);
ma_engine_listener_set_cone(&*m_engine,
0,
getListenerProperties().cone.innerAngle.asRadians(),
getListenerProperties().cone.outerAngle.asRadians(),
getListenerProperties().cone.outerGain);
m_listenerProperties.cone.innerAngle.asRadians(),
m_listenerProperties.cone.outerAngle.asRadians(),
m_listenerProperties.cone.outerGain);
ma_engine_listener_set_world_up(&*m_engine,
0,
getListenerProperties().upVector.x,
getListenerProperties().upVector.y,
getListenerProperties().upVector.z);
m_listenerProperties.upVector.x,
m_listenerProperties.upVector.y,
m_listenerProperties.upVector.z);
}
@ -182,20 +180,14 @@ AudioDevice::~AudioDevice()
// Destroy the log
if (m_log)
ma_log_uninit(&*m_log);
// Ensure we only ever have a single AudioDevice instance
assert(getInstance() != nullptr);
getInstance() = nullptr;
}
////////////////////////////////////////////////////////////
ma_engine* AudioDevice::getEngine()
{
auto* instance = getInstance();
if (instance && instance->m_engine)
return &*instance->m_engine;
if (m_engine.has_value())
return &*m_engine;
return nullptr;
}
@ -205,23 +197,21 @@ ma_engine* AudioDevice::getEngine()
void AudioDevice::setGlobalVolume(float volume)
{
// Store the volume in case no audio device exists yet
getListenerProperties().volume = volume;
m_listenerProperties.volume = volume;
auto* instance = getInstance();
if (!instance || !instance->m_engine)
if (!m_engine.has_value())
return;
if (const auto result = ma_device_set_master_volume(ma_engine_get_device(&*instance->m_engine), volume * 0.01f);
if (const auto result = ma_device_set_master_volume(ma_engine_get_device(&*m_engine), volume * 0.01f);
result != MA_SUCCESS)
err() << "Failed to set audio device master volume: " << ma_result_description(result) << std::endl;
}
////////////////////////////////////////////////////////////
float AudioDevice::getGlobalVolume()
float AudioDevice::getGlobalVolume() const
{
return getListenerProperties().volume;
return m_listenerProperties.volume;
}
@ -229,21 +219,19 @@ float AudioDevice::getGlobalVolume()
void AudioDevice::setPosition(const Vector3f& position)
{
// Store the position in case no audio device exists yet
getListenerProperties().position = position;
m_listenerProperties.position = position;
auto* instance = getInstance();
if (!instance || !instance->m_engine)
if (!m_engine.has_value())
return;
ma_engine_listener_set_position(&*instance->m_engine, 0, position.x, position.y, position.z);
ma_engine_listener_set_position(&*m_engine, 0, position.x, position.y, position.z);
}
////////////////////////////////////////////////////////////
Vector3f AudioDevice::getPosition()
Vector3f AudioDevice::getPosition() const
{
return getListenerProperties().position;
return m_listenerProperties.position;
}
@ -251,21 +239,19 @@ Vector3f AudioDevice::getPosition()
void AudioDevice::setDirection(const Vector3f& direction)
{
// Store the direction in case no audio device exists yet
getListenerProperties().direction = direction;
m_listenerProperties.direction = direction;
auto* instance = getInstance();
if (!instance || !instance->m_engine)
if (!m_engine.has_value())
return;
ma_engine_listener_set_direction(&*instance->m_engine, 0, direction.x, direction.y, direction.z);
ma_engine_listener_set_direction(&*m_engine, 0, direction.x, direction.y, direction.z);
}
////////////////////////////////////////////////////////////
Vector3f AudioDevice::getDirection()
Vector3f AudioDevice::getDirection() const
{
return getListenerProperties().direction;
return m_listenerProperties.direction;
}
@ -273,21 +259,19 @@ Vector3f AudioDevice::getDirection()
void AudioDevice::setVelocity(const Vector3f& velocity)
{
// Store the velocity in case no audio device exists yet
getListenerProperties().velocity = velocity;
m_listenerProperties.velocity = velocity;
auto* instance = getInstance();
if (!instance || !instance->m_engine)
if (!m_engine.has_value())
return;
ma_engine_listener_set_velocity(&*instance->m_engine, 0, velocity.x, velocity.y, velocity.z);
ma_engine_listener_set_velocity(&*m_engine, 0, velocity.x, velocity.y, velocity.z);
}
////////////////////////////////////////////////////////////
Vector3f AudioDevice::getVelocity()
Vector3f AudioDevice::getVelocity() const
{
return getListenerProperties().velocity;
return m_listenerProperties.velocity;
}
@ -295,14 +279,12 @@ Vector3f AudioDevice::getVelocity()
void AudioDevice::setCone(const Listener::Cone& cone)
{
// Store the cone in case no audio device exists yet
getListenerProperties().cone = cone;
m_listenerProperties.cone = cone;
auto* instance = getInstance();
if (!instance || !instance->m_engine)
if (!m_engine.has_value())
return;
ma_engine_listener_set_cone(&*instance->m_engine,
ma_engine_listener_set_cone(&*m_engine,
0,
std::clamp(cone.innerAngle.asRadians(), 0.f, sf::degrees(360).asRadians()),
std::clamp(cone.outerAngle.asRadians(), 0.f, sf::degrees(360).asRadians()),
@ -311,9 +293,9 @@ void AudioDevice::setCone(const Listener::Cone& cone)
////////////////////////////////////////////////////////////
Listener::Cone AudioDevice::getCone()
Listener::Cone AudioDevice::getCone() const
{
return getListenerProperties().cone;
return m_listenerProperties.cone;
}
@ -321,37 +303,27 @@ Listener::Cone AudioDevice::getCone()
void AudioDevice::setUpVector(const Vector3f& upVector)
{
// Store the up vector in case no audio device exists yet
getListenerProperties().upVector = upVector;
m_listenerProperties.upVector = upVector;
auto* instance = getInstance();
if (!instance || !instance->m_engine)
if (!m_engine.has_value())
return;
ma_engine_listener_set_world_up(&*instance->m_engine, 0, upVector.x, upVector.y, upVector.z);
ma_engine_listener_set_world_up(&*m_engine, 0, upVector.x, upVector.y, upVector.z);
}
////////////////////////////////////////////////////////////
Vector3f AudioDevice::getUpVector()
Vector3f AudioDevice::getUpVector() const
{
return getListenerProperties().upVector;
return m_listenerProperties.upVector;
}
////////////////////////////////////////////////////////////
AudioDevice*& AudioDevice::getInstance()
AudioDevice& AudioDevice::get()
{
static AudioDevice* instance{};
static AudioDevice instance;
return instance;
}
////////////////////////////////////////////////////////////
AudioDevice::ListenerProperties& AudioDevice::getListenerProperties()
{
static ListenerProperties properties;
return properties;
}
} // namespace sf::priv

View File

@ -46,7 +46,7 @@ namespace sf::priv
////////////////////////////////////////////////////////////
class AudioDevice
{
public:
private:
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
@ -59,17 +59,16 @@ public:
////////////////////////////////////////////////////////////
~AudioDevice();
public:
////////////////////////////////////////////////////////////
/// \brief Get the audio engine
///
/// There should only be a single instance of AudioDevice.
/// As long as an AudioResource exists, this function should
/// always return a valid pointer to the audio engine.
///
/// \return The audio engine
///
////////////////////////////////////////////////////////////
static ma_engine* getEngine();
ma_engine* getEngine();
////////////////////////////////////////////////////////////
/// \brief Change the global volume of all the sounds and musics
@ -83,7 +82,7 @@ public:
/// \see getGlobalVolume
///
////////////////////////////////////////////////////////////
static void setGlobalVolume(float volume);
void setGlobalVolume(float volume);
////////////////////////////////////////////////////////////
/// \brief Get the current value of the global volume
@ -93,7 +92,7 @@ public:
/// \see setGlobalVolume
///
////////////////////////////////////////////////////////////
static float getGlobalVolume();
float getGlobalVolume() const;
////////////////////////////////////////////////////////////
/// \brief Set the position of the listener in the scene
@ -105,7 +104,7 @@ public:
/// \see getPosition, setDirection
///
////////////////////////////////////////////////////////////
static void setPosition(const Vector3f& position);
void setPosition(const Vector3f& position);
////////////////////////////////////////////////////////////
/// \brief Get the current position of the listener in the scene
@ -115,7 +114,7 @@ public:
/// \see setPosition
///
////////////////////////////////////////////////////////////
static Vector3f getPosition();
Vector3f getPosition() const;
////////////////////////////////////////////////////////////
/// \brief Set the forward vector of the listener in the scene
@ -132,7 +131,7 @@ public:
/// \see getDirection, setUpVector, setPosition
///
////////////////////////////////////////////////////////////
static void setDirection(const Vector3f& direction);
void setDirection(const Vector3f& direction);
////////////////////////////////////////////////////////////
/// \brief Get the current forward vector of the listener in the scene
@ -142,7 +141,7 @@ public:
/// \see setDirection
///
////////////////////////////////////////////////////////////
static Vector3f getDirection();
Vector3f getDirection() const;
////////////////////////////////////////////////////////////
/// \brief Set the velocity of the listener in the scene
@ -154,7 +153,7 @@ public:
/// \see getVelocity, getDirection, setUpVector, setPosition
///
////////////////////////////////////////////////////////////
static void setVelocity(const Vector3f& velocity);
void setVelocity(const Vector3f& velocity);
////////////////////////////////////////////////////////////
/// \brief Get the current forward vector of the listener in the scene
@ -164,7 +163,7 @@ public:
/// \see setVelocity
///
////////////////////////////////////////////////////////////
static Vector3f getVelocity();
Vector3f getVelocity() const;
////////////////////////////////////////////////////////////
/// \brief Set the cone properties of the listener in the audio scene
@ -177,7 +176,7 @@ public:
/// \see getCone
///
////////////////////////////////////////////////////////////
static void setCone(const Listener::Cone& cone);
void setCone(const Listener::Cone& cone);
////////////////////////////////////////////////////////////
/// \brief Get the cone properties of the listener in the audio scene
@ -187,7 +186,7 @@ public:
/// \see setCone
///
////////////////////////////////////////////////////////////
static Listener::Cone getCone();
Listener::Cone getCone() const;
////////////////////////////////////////////////////////////
/// \brief Set the upward vector of the listener in the scene
@ -204,7 +203,7 @@ public:
/// \see getUpVector, setDirection, setPosition
///
////////////////////////////////////////////////////////////
static void setUpVector(const Vector3f& upVector);
void setUpVector(const Vector3f& upVector);
////////////////////////////////////////////////////////////
/// \brief Get the current upward vector of the listener in the scene
@ -214,17 +213,17 @@ public:
/// \see setUpVector
///
////////////////////////////////////////////////////////////
static Vector3f getUpVector();
Vector3f getUpVector() const;
////////////////////////////////////////////////////////////
/// \brief Gets the audio device singleton, initializing it if necessary
///
/// \return The audio device
///
////////////////////////////////////////////////////////////
static AudioDevice& get();
private:
////////////////////////////////////////////////////////////
/// \brief This function makes sure the instance pointer is initialized before using it
///
/// \return The instance pointer
///
////////////////////////////////////////////////////////////
static AudioDevice*& getInstance();
struct ListenerProperties
{
float volume{100.f};
@ -235,17 +234,10 @@ private:
sf::Vector3f upVector{0, 1, 0};
};
////////////////////////////////////////////////////////////
/// \brief Get the listener properties
///
/// \return The listener properties
///
////////////////////////////////////////////////////////////
static ListenerProperties& getListenerProperties();
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////
ListenerProperties m_listenerProperties; //!< The listener properties
std::optional<ma_log> m_log; //!< The miniaudio log
std::optional<ma_context> m_context; //!< The miniaudio context
std::optional<ma_device> m_playbackDevice; //!< The miniaudio playback device

View File

@ -1,62 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Audio/AudioDevice.hpp>
#include <SFML/Audio/AudioResource.hpp>
#include <memory>
#include <mutex>
namespace sf
{
////////////////////////////////////////////////////////////
AudioResource::AudioResource() :
m_device(
[]()
{
// Ensure we only ever create a single instance of an
// AudioDevice that is shared between all AudioResources
static std::mutex mutex;
static std::weak_ptr<sf::priv::AudioDevice> weakAudioDevice;
const std::lock_guard lock(mutex);
auto audioDevice = weakAudioDevice.lock();
if (audioDevice == nullptr)
{
audioDevice = std::make_shared<priv::AudioDevice>();
weakAudioDevice = audioDevice;
}
return audioDevice;
}())
{
}
} // namespace sf

View File

@ -3,8 +3,6 @@ set(SRCROOT ${PROJECT_SOURCE_DIR}/src/SFML/Audio)
# all source files
set(SRC
${SRCROOT}/AudioResource.cpp
${INCROOT}/AudioResource.hpp
${SRCROOT}/AudioDevice.cpp
${SRCROOT}/AudioDevice.hpp
${INCROOT}/Export.hpp

View File

@ -34,84 +34,84 @@ namespace sf
////////////////////////////////////////////////////////////
void Listener::setGlobalVolume(float volume)
{
priv::AudioDevice::setGlobalVolume(volume);
priv::AudioDevice::get().setGlobalVolume(volume);
}
////////////////////////////////////////////////////////////
float Listener::getGlobalVolume()
{
return priv::AudioDevice::getGlobalVolume();
return priv::AudioDevice::get().getGlobalVolume();
}
////////////////////////////////////////////////////////////
void Listener::setPosition(const Vector3f& position)
{
priv::AudioDevice::setPosition(position);
priv::AudioDevice::get().setPosition(position);
}
////////////////////////////////////////////////////////////
Vector3f Listener::getPosition()
{
return priv::AudioDevice::getPosition();
return priv::AudioDevice::get().getPosition();
}
////////////////////////////////////////////////////////////
void Listener::setDirection(const Vector3f& direction)
{
priv::AudioDevice::setDirection(direction);
priv::AudioDevice::get().setDirection(direction);
}
////////////////////////////////////////////////////////////
Vector3f Listener::getDirection()
{
return priv::AudioDevice::getDirection();
return priv::AudioDevice::get().getDirection();
}
////////////////////////////////////////////////////////////
void Listener::setVelocity(const Vector3f& velocity)
{
priv::AudioDevice::setVelocity(velocity);
priv::AudioDevice::get().setVelocity(velocity);
}
////////////////////////////////////////////////////////////
Vector3f Listener::getVelocity()
{
return priv::AudioDevice::getVelocity();
return priv::AudioDevice::get().getVelocity();
}
////////////////////////////////////////////////////////////
void Listener::setCone(const Listener::Cone& cone)
{
priv::AudioDevice::setCone(cone);
priv::AudioDevice::get().setCone(cone);
}
////////////////////////////////////////////////////////////
Listener::Cone Listener::getCone()
{
return priv::AudioDevice::getCone();
return priv::AudioDevice::get().getCone();
}
////////////////////////////////////////////////////////////
void Listener::setUpVector(const Vector3f& upVector)
{
priv::AudioDevice::setUpVector(upVector);
priv::AudioDevice::get().setUpVector(upVector);
}
////////////////////////////////////////////////////////////
Vector3f Listener::getUpVector()
{
return priv::AudioDevice::getUpVector();
return priv::AudioDevice::get().getUpVector();
}
} // namespace sf

View File

@ -61,7 +61,7 @@ struct Sound::Impl
void initialize()
{
// Initialize the sound
auto* engine = priv::AudioDevice::getEngine();
auto* engine = priv::AudioDevice::get().getEngine();
if (engine == nullptr)
{

View File

@ -61,7 +61,7 @@ struct SoundStream::Impl
void initialize()
{
// Initialize the sound
auto* engine = priv::AudioDevice::getEngine();
auto* engine = priv::AudioDevice::get().getEngine();
if (engine == nullptr)
{

View File

@ -1,9 +0,0 @@
#include <SFML/Audio/AudioResource.hpp>
#include <type_traits>
static_assert(!std::is_constructible_v<sf::AudioResource>);
static_assert(std::is_copy_constructible_v<sf::AudioResource>);
static_assert(!std::is_copy_assignable_v<sf::AudioResource>);
static_assert(std::is_nothrow_move_constructible_v<sf::AudioResource>);
static_assert(!std::is_nothrow_move_assignable_v<sf::AudioResource>);

View File

@ -130,7 +130,6 @@ set(NETWORK_SRC
sfml_add_test(test-sfml-network "${NETWORK_SRC}" SFML::Network)
set(AUDIO_SRC
Audio/AudioResource.test.cpp
Audio/InputSoundFile.test.cpp
Audio/Music.test.cpp
Audio/OutputSoundFile.test.cpp