From 9d53f7cb2b5e9d6ff4f8e90d9eae1e9343ef981c Mon Sep 17 00:00:00 2001 From: Squareys Date: Sun, 15 Nov 2015 00:57:04 +0100 Subject: [PATCH] Audio: Fix hrtfStatus() and isHrtfEnabled() and move them to Context. Since `ALC_HRTF_SOFT` is a ALC extension, we need to use `alcGetIntegerv()` instead of `alGetIntegerv()` which requires access to the OpenAL device stored in `Audio::Context` and therefore the aforementioned methods have been moved there. Signed-off-by: Squareys --- src/Magnum/Audio/Context.cpp | 15 +++++ src/Magnum/Audio/Context.h | 85 ++++++++++++++++++++++++++ src/Magnum/Audio/Renderer.cpp | 15 ----- src/Magnum/Audio/Renderer.h | 81 ------------------------ src/Magnum/Audio/Test/ContextTest.cpp | 18 +++++- src/Magnum/Audio/Test/RendererTest.cpp | 18 +----- 6 files changed, 118 insertions(+), 114 deletions(-) diff --git a/src/Magnum/Audio/Context.cpp b/src/Magnum/Audio/Context.cpp index 179e07425..9c5d0437a 100644 --- a/src/Magnum/Audio/Context.cpp +++ b/src/Magnum/Audio/Context.cpp @@ -53,6 +53,21 @@ const std::vector& Extension::extensions() { return extensions; } +Debug& operator<<(Debug& debug, const Context::HrtfStatus value) { + switch(value) { + #define _c(value) case Context::HrtfStatus::value: return debug << "Audio::Context::HrtfStatus::" #value; + _c(Disabled) + _c(Enabled) + _c(Denied) + _c(Required) + _c(Detected) + _c(UnsupportedFormat) + #undef _c + } + + return debug << "Audio::Context::HrtfStatus::(invalid)"; +} + Context* Context::_current = nullptr; Context::Context(): Context{Configuration{}} {} diff --git a/src/Magnum/Audio/Context.h b/src/Magnum/Audio/Context.h index 91094f277..8fdb16e73 100644 --- a/src/Magnum/Audio/Context.h +++ b/src/Magnum/Audio/Context.h @@ -40,6 +40,7 @@ #include "Magnum/Audio/Audio.h" #include "Magnum/Audio/Buffer.h" +#include "Magnum/Audio/Extensions.h" #include "Magnum/Audio/visibility.h" #ifndef DOXYGEN_GENERATING_OUTPUT @@ -82,6 +83,50 @@ class MAGNUM_AUDIO_EXPORT Extension { */ class MAGNUM_AUDIO_EXPORT Context { public: + + /** + * @brief HRTF status + * + * @see @ref hrtfStatus(), @ref isHrtfEnabled() + * @requires_al_extension Extension @alc_extension{SOFTX,HRTF} or + * @alc_extension{SOFT,HRTF} + */ + enum class HrtfStatus: ALenum { + Disabled = ALC_HRTF_DISABLED_SOFT, /**< HRTF is disabled */ + Enabled = ALC_HRTF_ENABLED_SOFT, /**< HRTF is enabled */ + + /** + * HRTF is disabled because it is not allowed on the device. This + * may be caused by invalid resource permissions, or an other user + * configuration that disallows HRTF. + * @requires_al_extension Extension @alc_extension{SOFT,HRTF} + */ + Denied = ALC_HRTF_DENIED_SOFT, + + /** + * HRTF is enabled because it must be used on the device. This may + * be caused by a device that can only use HRTF, or other user + * configuration that forces HRTF to be used. + * @requires_al_extension Extension @alc_extension{SOFT,HRTF} + */ + Required = ALC_HRTF_REQUIRED_SOFT, + + /** + * HRTF is enabled automatically because the device reported + * headphones. + * @requires_al_extension Extension @alc_extension{SOFT,HRTF} + */ + Detected = ALC_HRTF_HEADPHONES_DETECTED_SOFT, + + /** + * The device does not support HRTF with the current format. + * Typically this is caused by non-stereo output or an incompatible + * output frequency. + * @requires_al_extension Extension @alc_extension{SOFT,HRTF} + */ + UnsupportedFormat = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT + }; + /** @brief Current context */ static Context* current() { return _current; } @@ -106,6 +151,28 @@ class MAGNUM_AUDIO_EXPORT Context { */ ~Context(); + /** + * @brief Whether HRTFs (Head Related Transfer Functions) are enabled + * + * HRFTs may not be enabled/disabled in a running context. Instead + * create a new @ref Context with HRFTs enabled or disabled. + * @see @ref hrtfStatus(), @ref Audio::Context::Configuration::setHrtf(), + * @fn_alc{GetIntegerv} with @def_alc{HRTF_SOFT} + * @requires_al_extension Extension @alc_extension{SOFTX,HRTF} or + * @alc_extension{SOFT,HRTF} + */ + bool isHrtfEnabled() const; + + /** + * @brief HRTF status + * + * @see @ref isHrtfEnabled(), @fn_alc{GetIntegerv} with + * @def_alc{HRTF_STATUS_SOFT} + * @requires_al_extension Extension @alc_extension{SOFTX,HRTF} or + * @alc_extension{SOFT,HRTF} + */ + HrtfStatus hrtfStatus() const; + /** * @brief Vendor string * @@ -340,6 +407,24 @@ MAGNUM_ASSERT_AUDIO_EXTENSION_SUPPORTED(Extensions::ALC::SOFTX::HRTF); } while(0) #endif +/** @debugoperatorclassenum{Magnum::Audio::Context,Magnum::Audio::Context::HrtfStatus} */ +MAGNUM_AUDIO_EXPORT Debug& operator<<(Debug& debug, Context::HrtfStatus value); + +inline bool Context::isHrtfEnabled() const { + Int enabled; + alcGetIntegerv(_device, ALC_HRTF_SOFT, 1, &enabled); + return enabled == ALC_TRUE; +} + +inline Context::HrtfStatus Context::hrtfStatus() const { + if(!Context::current()->isExtensionSupported()) + return isHrtfEnabled() ? HrtfStatus::Enabled : HrtfStatus::Disabled; + + Int status; + alcGetIntegerv(_device, ALC_HRTF_STATUS_SOFT, 1, &status); + return Context::HrtfStatus(status); +} + }} #endif diff --git a/src/Magnum/Audio/Renderer.cpp b/src/Magnum/Audio/Renderer.cpp index 25dd54d0a..85214cf5d 100644 --- a/src/Magnum/Audio/Renderer.cpp +++ b/src/Magnum/Audio/Renderer.cpp @@ -60,19 +60,4 @@ Debug& operator<<(Debug& debug, const Renderer::DistanceModel value) { return debug << "Audio::Renderer::DistanceModel::(invalid)"; } -Debug& operator<<(Debug& debug, const Renderer::HrtfStatus value) { - switch(value) { - #define _c(value) case Renderer::HrtfStatus::value: return debug << "Audio::Renderer::HrtfStatus::" #value; - _c(Disabled) - _c(Enabled) - _c(Denied) - _c(Required) - _c(Detected) - _c(UnsupportedFormat) - #undef _c - } - - return debug << "Audio::Renderer::HrtfStatus::(invalid)"; -} - }} diff --git a/src/Magnum/Audio/Renderer.h b/src/Magnum/Audio/Renderer.h index 47a63c72f..41783b256 100644 --- a/src/Magnum/Audio/Renderer.h +++ b/src/Magnum/Audio/Renderer.h @@ -32,11 +32,9 @@ #include #include -#include #include "Magnum/Magnum.h" #include "Magnum/Audio/Context.h" -#include "Magnum/Audio/Extensions.h" #include "Magnum/Audio/visibility.h" #include "Magnum/Math/Vector3.h" @@ -61,85 +59,9 @@ class Renderer { OutOfMemory = AL_OUT_OF_MEMORY /**< Unable to allocate memory */ }; - /** - * @brief HRTF status - * - * @see @ref hrtfStatus(), @ref isHrtfEnabled() - * @requires_al_extension Extension @alc_extension{SOFTX,HRTF} or - * @alc_extension{SOFT,HRTF} - */ - enum class HrtfStatus: ALenum { - Disabled = ALC_HRTF_DISABLED_SOFT, /**< HRTF is disabled */ - Enabled = ALC_HRTF_ENABLED_SOFT, /**< HRTF is enabled */ - - /** - * HRTF is disabled because it is not allowed on the device. This - * may be caused by invalid resource permissions, or an other user - * configuration that disallows HRTF. - * @requires_al_extension Extension @alc_extension{SOFT,HRTF} - */ - Denied = ALC_HRTF_DENIED_SOFT, - - /** - * HRTF is enabled because it must be used on the device. This may - * be caused by a device that can only use HRTF, or other user - * configuration that forces HRTF to be used. - * @requires_al_extension Extension @alc_extension{SOFT,HRTF} - */ - Required = ALC_HRTF_REQUIRED_SOFT, - - /** - * HRTF is enabled automatically because the device reported - * headphones. - * @requires_al_extension Extension @alc_extension{SOFT,HRTF} - */ - Detected = ALC_HRTF_HEADPHONES_DETECTED_SOFT, - - /** - * The device does not support HRTF with the current format. - * Typically this is caused by non-stereo output or an incompatible - * output frequency. - * @requires_al_extension Extension @alc_extension{SOFT,HRTF} - */ - UnsupportedFormat = ALC_HRTF_UNSUPPORTED_FORMAT_SOFT - }; - /** @brief Error status */ static Error error() { return Error(alGetError()); } - /** - * @brief Whether HRTFs (Head Related Transfer Functions) are enabled - * - * HRFTs may not be enabled/disabled in a running context. Instead - * create a new @ref Context with HRFTs enabled or disabled. - * @see @ref hrtfStatus(), @ref Audio::Context::Configuration::setHrtf(), - * @fn_al{GetIntegerv} with @def_alc{HRTF_SOFT} - * @requires_al_extension Extension @alc_extension{SOFTX,HRTF} or - * @alc_extension{SOFT,HRTF} - */ - static bool isHrtfEnabled() { - Int enabled = ALC_FALSE; - alGetIntegerv(ALC_HRTF_SOFT, &enabled); - return enabled == ALC_TRUE; - } - - /** - * @brief HRTF status - * - * @see @ref isHrtfEnabled(), @fn_al{GetIntegerv} with - * @def_alc{HRTF_STATUS_SOFT} - * @requires_al_extension Extension @alc_extension{SOFTX,HRTF} or - * @alc_extension{SOFT,HRTF} - */ - static HrtfStatus hrtfStatus() { - if(!Context::current()->isExtensionSupported()) - return isHrtfEnabled() ? HrtfStatus::Enabled : HrtfStatus::Disabled; - - Int status = ALC_HRTF_DISABLED_SOFT; - alGetIntegerv(ALC_HRTF_STATUS_SOFT, &status); - return HrtfStatus(status); - } - /** @{ @name Listener positioning */ /** @@ -344,9 +266,6 @@ MAGNUM_AUDIO_EXPORT Debug& operator<<(Debug& debug, Renderer::Error value); /** @debugoperatorclassenum{Magnum::Audio::Renderer,Magnum::Audio::Renderer::DistanceModel} */ MAGNUM_AUDIO_EXPORT Debug& operator<<(Debug& debug, Renderer::DistanceModel value); -/** @debugoperatorclassenum{Magnum::Audio::Renderer,Magnum::Audio::Renderer::HrtfStatus} */ -MAGNUM_AUDIO_EXPORT Debug& operator<<(Debug& debug, Renderer::HrtfStatus value); - }} #endif diff --git a/src/Magnum/Audio/Test/ContextTest.cpp b/src/Magnum/Audio/Test/ContextTest.cpp index 0ec5ac2d7..b62099b7a 100644 --- a/src/Magnum/Audio/Test/ContextTest.cpp +++ b/src/Magnum/Audio/Test/ContextTest.cpp @@ -37,13 +37,17 @@ struct ContextTest: TestSuite::Tester { void extensionsString(); void isExtensionEnabled(); + void hrtfStatus(); + void hrtfs(); Context _context; }; ContextTest::ContextTest() { addTests({&ContextTest::extensionsString, - &ContextTest::isExtensionEnabled}); + &ContextTest::isExtensionEnabled, + &ContextTest::hrtfStatus, + &ContextTest::hrtfs}); } void ContextTest::extensionsString() { @@ -56,6 +60,18 @@ void ContextTest::isExtensionEnabled() { CORRADE_VERIFY(Context::current()->isExtensionSupported()); } +void ContextTest::hrtfStatus() { + std::ostringstream out; + Debug(&out) << Context::HrtfStatus::Denied; + CORRADE_COMPARE(out.str(), "Audio::Context::HrtfStatus::Denied\n"); +} + +void ContextTest::hrtfs() { + /* At least test that the hrtf status is `Disabled` */ + CORRADE_VERIFY(!_context.isHrtfEnabled()); + CORRADE_COMPARE(_context.hrtfStatus(), Context::HrtfStatus::Disabled); +} + }}} CORRADE_TEST_MAIN(Magnum::Audio::Test::ContextTest) diff --git a/src/Magnum/Audio/Test/RendererTest.cpp b/src/Magnum/Audio/Test/RendererTest.cpp index bb558fb8c..ff3b23ae6 100644 --- a/src/Magnum/Audio/Test/RendererTest.cpp +++ b/src/Magnum/Audio/Test/RendererTest.cpp @@ -37,7 +37,6 @@ struct RendererTest: TestSuite::Tester { void debugError(); void debugDistanceModel(); - void hrtfStatus(); void listenerOrientation(); void listenerPosition(); void listenerVelocity(); @@ -45,7 +44,6 @@ struct RendererTest: TestSuite::Tester { void speedOfSound(); void dopplerFactor(); void distanceModel(); - void hrtfs(); Context _context; }; @@ -53,15 +51,13 @@ struct RendererTest: TestSuite::Tester { RendererTest::RendererTest() { addTests({&RendererTest::debugError, &RendererTest::debugDistanceModel, - &RendererTest::hrtfStatus, &RendererTest::listenerOrientation, &RendererTest::listenerPosition, &RendererTest::listenerVelocity, &RendererTest::listenerGain, &RendererTest::speedOfSound, &RendererTest::dopplerFactor, - &RendererTest::distanceModel, - &RendererTest::hrtfs}); + &RendererTest::distanceModel}); } void RendererTest::debugError() { @@ -76,12 +72,6 @@ void RendererTest::debugDistanceModel() { CORRADE_COMPARE(out.str(), "Audio::Renderer::DistanceModel::Inverse\n"); } -void RendererTest::hrtfStatus() { - std::ostringstream out; - Debug(&out) << Renderer::HrtfStatus::Denied; - CORRADE_COMPARE(out.str(), "Audio::Renderer::HrtfStatus::Denied\n"); -} - void RendererTest::listenerOrientation() { constexpr Vector3 up{1.0f, 2.0f, 3.0f}, fwd{3.0f, 2.0f, 1.0f}; Renderer::setListenerOrientation(fwd, up); @@ -133,12 +123,6 @@ void RendererTest::distanceModel() { CORRADE_COMPARE(Renderer::distanceModel(), model); } -void RendererTest::hrtfs() { - /* At least test that the hrtf status is `Disabled` */ - CORRADE_VERIFY(!Renderer::isHrtfEnabled()); - CORRADE_COMPARE(Renderer::hrtfStatus(), Renderer::HrtfStatus::Disabled); -} - }}} CORRADE_TEST_MAIN(Magnum::Audio::Test::RendererTest)