From 6efb83f72971592e3a7715f18c54d9d3705d5084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 11 Aug 2013 11:15:18 +0200 Subject: [PATCH] Audio: added Renderer class. --- src/Audio/Audio.h | 2 + src/Audio/CMakeLists.txt | 12 +- src/Audio/Renderer.cpp | 46 +++++++ src/Audio/Renderer.h | 202 ++++++++++++++++++++++++++++++ src/Audio/Test/CMakeLists.txt | 25 ++++ src/Audio/Test/RendererTest.cpp | 51 ++++++++ src/Audio/magnumAudioVisibility.h | 35 ++++++ src/Renderer.h | 2 +- 8 files changed, 372 insertions(+), 3 deletions(-) create mode 100644 src/Audio/Renderer.cpp create mode 100644 src/Audio/Renderer.h create mode 100644 src/Audio/Test/CMakeLists.txt create mode 100644 src/Audio/Test/RendererTest.cpp create mode 100644 src/Audio/magnumAudioVisibility.h diff --git a/src/Audio/Audio.h b/src/Audio/Audio.h index 63960eeca..d0cabf47e 100644 --- a/src/Audio/Audio.h +++ b/src/Audio/Audio.h @@ -30,6 +30,8 @@ namespace Magnum { namespace Audio { +/* Renderer used only statically */ + }} #endif diff --git a/src/Audio/CMakeLists.txt b/src/Audio/CMakeLists.txt index 8bdc6b9f8..b5ea399a1 100644 --- a/src/Audio/CMakeLists.txt +++ b/src/Audio/CMakeLists.txt @@ -27,13 +27,21 @@ find_package(OpenAL REQUIRED) include_directories(${OPENAL_INCLUDE_DIR}) set(MagnumAudio_SOURCES - Audio.cpp) + Audio.cpp + Renderer.cpp) set(MagnumAudio_HEADERS - Audio.h) + Audio.h + Renderer.h + + magnumAudioVisibility.h) add_library(MagnumAudio ${SHARED_OR_STATIC} ${MagnumAudio_SOURCES}) target_link_libraries(MagnumAudio ${OPENAL_LIBRARY}) install(TARGETS MagnumAudio DESTINATION ${MAGNUM_LIBRARY_INSTALL_DIR}) install(FILES ${MagnumAudio_HEADERS} DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/Audio) + +if(BUILD_TESTS) + add_subdirectory(Test) +endif() diff --git a/src/Audio/Renderer.cpp b/src/Audio/Renderer.cpp new file mode 100644 index 000000000..4dbeae53f --- /dev/null +++ b/src/Audio/Renderer.cpp @@ -0,0 +1,46 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include "Renderer.h" + +#include + +namespace Magnum { namespace Audio { + +Debug operator<<(Debug debug, const Renderer::Error value) { + switch(value) { + #define _c(value) case Renderer::Error::value: return debug << "Audio::Renderer::Error::" #value; + _c(NoError) + _c(InvalidName) + _c(InvalidEnum) + _c(InvalidValue) + _c(InvalidOperation) + _c(OutOfMemory) + #undef _c + } + + return debug << "Audio::Renderer::Error::(invalid)"; +} + +}} diff --git a/src/Audio/Renderer.h b/src/Audio/Renderer.h new file mode 100644 index 000000000..f8041e28a --- /dev/null +++ b/src/Audio/Renderer.h @@ -0,0 +1,202 @@ +#ifndef Magnum_Audio_Renderer_h +#define Magnum_Audio_Renderer_h +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +/** @file + * @brief Class Magnum::Audio::Renderer + */ + +#include + +#include "Math/Vector3.h" +#include "Magnum.h" +#include "Audio/magnumAudioVisibility.h" + +namespace Magnum { namespace Audio { + +/** @brief Global renderer configuration */ +class Renderer { + public: + Renderer() = delete; + + /** + * @brief Error status + * + * @see error() + */ + enum class Error: ALenum { + NoError = AL_NO_ERROR, /**< No error occured */ + InvalidName = AL_INVALID_NAME, /**< Invalid name parameter */ + InvalidEnum = AL_INVALID_ENUM, /**< Invalid enum parameter */ + InvalidValue = AL_INVALID_VALUE, /**< Invalid enum value parameter */ + InvalidOperation = AL_INVALID_OPERATION, /**< Illegal call */ + OutOfMemory = AL_OUT_OF_MEMORY /**< Unable to allocate memory */ + }; + + /** @brief Error status */ + static Error error() { return Error(alGetError()); } + + /** @{ @name Listener positioning */ + + /** + * @brief Set listener position + * + * Default is `{0.0f, 0.0f, 0.0f}`. + * @see @fn_al{Listenerfv} with @def_al{POSITION} + */ + static void setListenerPosition(const Vector3& position) { + alListenerfv(AL_POSITION, position.data()); + } + + /** @overload + * @see @fn_al{Listeneriv} with @def_al{POSITION} + */ + static void setListenerPosition(const Vector3i& position) { + alListeneriv(AL_POSITION, position.data()); + } + + /** + * @brief Set listener orientation + * + * The values must be linearly independent and don't need to be + * normalized. Default is -Z and +Y. + * @see @fn_al{Listenerfv} with @def_al{ORIENTATION} + */ + static void setListenerOrientation(const Vector3& forward, const Vector3& up); + + /** @overload + * @see @fn_al{Listeneriv} with @def_al{ORIENTATION} + */ + static void setListenerOrientation(const Vector3i& forward, const Vector3i& up); + + /** + * @brief Set listener velocity + * + * Default is `{0.0f, 0.0f, 0.0f}`. + * @see @fn_al{Listenerfv} with @def_al{VELOCITY} + */ + static void setListenerVelocity(const Vector3& velocity) { + alListenerfv(AL_VELOCITY, velocity.data()); + } + + /** @overload + * @see @fn_al{Listeneriv} with @def_al{VELOCITY} + */ + static void setListenerVelocity(const Vector3i& velocity) { + alListeneriv(AL_VELOCITY, velocity.data()); + } + + /*@}*/ + + /** @{ @name Global behavior */ + + /** + * @brief Distance model + * + * @see setDistanceModel() + */ + enum class DistanceModel: ALenum { + /** No distance attenuation calculation */ + None = AL_NONE, + + /** Inverse distance */ + Inverse = AL_INVERSE_DISTANCE, + + /** Inverse distance, clamped */ + InverseClamped = AL_INVERSE_DISTANCE_CLAMPED, + + /** Linear distance */ + Linear = AL_LINEAR_DISTANCE, + + /** Linear distance, clamped */ + LinearClamped = AL_LINEAR_DISTANCE_CLAMPED, + + /** Exponential distance */ + Exponent = AL_EXPONENT_DISTANCE, + + /** Exponential distance, clamped */ + ExponentClamped = AL_EXPONENT_DISTANCE_CLAMPED + }; + + /** + * @brief Set listener gain + * + * Default is `1.0f`, which means that the sound is unattenuated. + * If set to `0.0f`, all sound is muted. + * @see @fn_al{Listenerf} with @def_al{GAIN} + */ + static void setListenerGain(Float gain) { + alListenerf(AL_GAIN, gain); + } + + /** + * @brief Set Doppler factor + * + * Default is `1.0f`. If set to `0.0f`, the effect is disabled. + * @see @ref setSpeedOfSound(), @fn_al{DopplerFactor} + */ + static void setDopplerFactor(Float factor) { + alDopplerFactor(factor); + } + + /** + * @brief Set speed of sound + * + * Default is `343.3f` (meters per second). + * @see @ref setDopplerFactor(), @fn_al{SpeedOfSound} + */ + static void setSpeedOfSound(Float speed) { + alSpeedOfSound(speed); + } + + /** + * @brief Set distance model + * + * Default is @ref DistanceModel "DistanceModel::InverseClamped". + * @see @fn_al{DistanceModel} + */ + static void setDistanceModel(DistanceModel model) { + alDistanceModel(ALenum(model)); + } + + /*@}*/ +}; + +/** @debugoperator{Magnum::Audio::Renderer} */ +Debug MAGNUM_AUDIO_EXPORT operator<<(Debug debug, Renderer::Error value); + +inline void Renderer::setListenerOrientation(const Vector3& forward, const Vector3& up) { + const Vector3 data[] = {forward, up}; + alListenerfv(AL_ORIENTATION, data[0].data()); +} + +inline void Renderer::setListenerOrientation(const Vector3i& forward, const Vector3i& up) { + const Vector3i data[] = {forward, up}; + alListeneriv(AL_ORIENTATION, data[0].data()); +} + +}} + +#endif diff --git a/src/Audio/Test/CMakeLists.txt b/src/Audio/Test/CMakeLists.txt new file mode 100644 index 000000000..3a13c2df8 --- /dev/null +++ b/src/Audio/Test/CMakeLists.txt @@ -0,0 +1,25 @@ +# +# This file is part of Magnum. +# +# Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš +# +# Permission is hereby granted, free of charge, to any person obtaining a +# copy of this software and associated documentation files (the "Software"), +# to deal in the Software without restriction, including without limitation +# the rights to use, copy, modify, merge, publish, distribute, sublicense, +# and/or sell copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +# DEALINGS IN THE SOFTWARE. +# + +corrade_add_test(AudioRendererTest RendererTest.cpp LIBRARIES MagnumAudio) diff --git a/src/Audio/Test/RendererTest.cpp b/src/Audio/Test/RendererTest.cpp new file mode 100644 index 000000000..9be69593d --- /dev/null +++ b/src/Audio/Test/RendererTest.cpp @@ -0,0 +1,51 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include +#include + +#include "Audio/Renderer.h" + +namespace Magnum { namespace Audio { namespace Test { + +class RendererTest: public TestSuite::Tester { + public: + explicit RendererTest(); + + void debugError(); +}; + +RendererTest::RendererTest() { + addTests({&RendererTest::debugError}); +} + +void RendererTest::debugError() { + std::ostringstream out; + Debug(&out) << Renderer::Error::InvalidOperation; + CORRADE_COMPARE(out.str(), "Audio::Renderer::Error::InvalidOperation\n"); +} + +}}} + +CORRADE_TEST_MAIN(Magnum::Audio::Test::RendererTest) diff --git a/src/Audio/magnumAudioVisibility.h b/src/Audio/magnumAudioVisibility.h new file mode 100644 index 000000000..e4a7c0f67 --- /dev/null +++ b/src/Audio/magnumAudioVisibility.h @@ -0,0 +1,35 @@ +#ifndef Magnum_Audio_magnumAudioVisibility_h +#define Magnum_Audio_magnumAudioVisibility_h +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#include + +#ifdef MagnumAudio_EXPORTS + #define MAGNUM_AUDIO_EXPORT CORRADE_VISIBILITY_EXPORT +#else + #define MAGNUM_AUDIO_EXPORT CORRADE_VISIBILITY_IMPORT +#endif + +#endif diff --git a/src/Renderer.h b/src/Renderer.h index 7bf87ed64..1e345f3ea 100644 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -24,7 +24,7 @@ DEALINGS IN THE SOFTWARE. */ -/** @file +/** @file /Renderer.h * @brief Class Magnum::Renderer */