From 81c485dd624a4930e131baa0d6d2a8582a53d9f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 8 Sep 2015 00:09:44 +0200 Subject: [PATCH] New AnyAudioImporter plugin. Similar to AnyImageImporter. Currently knows only WAV files, more to come with upcoming plugins. --- .../AnyAudioImporter/AnyAudioImporter.conf | 0 .../AnyAudioImporter/AnyImporter.cpp | 80 +++++++++++++++++ .../AnyAudioImporter/AnyImporter.h | 85 ++++++++++++++++++ .../AnyAudioImporter/CMakeLists.txt | 61 +++++++++++++ .../AnyAudioImporter/Test/CMakeLists.txt | 33 +++++++ .../AnyAudioImporter/Test/Test.cpp | 79 ++++++++++++++++ .../AnyAudioImporter/Test/configure.h.cmake | 32 +++++++ .../AnyAudioImporter/Test/stereo8.wav | Bin 0 -> 48 bytes .../AnyAudioImporter/pluginRegistration.cpp | 29 ++++++ 9 files changed, 399 insertions(+) create mode 100644 src/MagnumPlugins/AnyAudioImporter/AnyAudioImporter.conf create mode 100644 src/MagnumPlugins/AnyAudioImporter/AnyImporter.cpp create mode 100644 src/MagnumPlugins/AnyAudioImporter/AnyImporter.h create mode 100644 src/MagnumPlugins/AnyAudioImporter/CMakeLists.txt create mode 100644 src/MagnumPlugins/AnyAudioImporter/Test/CMakeLists.txt create mode 100644 src/MagnumPlugins/AnyAudioImporter/Test/Test.cpp create mode 100644 src/MagnumPlugins/AnyAudioImporter/Test/configure.h.cmake create mode 100644 src/MagnumPlugins/AnyAudioImporter/Test/stereo8.wav create mode 100644 src/MagnumPlugins/AnyAudioImporter/pluginRegistration.cpp diff --git a/src/MagnumPlugins/AnyAudioImporter/AnyAudioImporter.conf b/src/MagnumPlugins/AnyAudioImporter/AnyAudioImporter.conf new file mode 100644 index 000000000..e69de29bb diff --git a/src/MagnumPlugins/AnyAudioImporter/AnyImporter.cpp b/src/MagnumPlugins/AnyAudioImporter/AnyImporter.cpp new file mode 100644 index 000000000..2c994aed7 --- /dev/null +++ b/src/MagnumPlugins/AnyAudioImporter/AnyImporter.cpp @@ -0,0 +1,80 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015 + 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 "AnyImporter.h" + +#include +#include +#include +#include + +namespace Magnum { namespace Audio { + +AnyImporter::AnyImporter(PluginManager::Manager& manager): AbstractImporter{manager} {} + +AnyImporter::AnyImporter(PluginManager::AbstractManager& manager, std::string plugin): AbstractImporter{manager, std::move(plugin)} {} + +AnyImporter::~AnyImporter() = default; + +auto AnyImporter::doFeatures() const -> Features { return {}; } + +bool AnyImporter::doIsOpened() const { return !!_in; } + +void AnyImporter::doClose() { _in = nullptr; } + +void AnyImporter::doOpenFile(const std::string& filename) { + CORRADE_INTERNAL_ASSERT(manager()); + + /* Detect type from extension */ + std::string plugin; + if(Utility::String::endsWith(filename, ".wav")) + plugin = "WavAudioImporter"; + else { + Error() << "Audio::AnyImporter::openFile(): cannot determine type of file" << filename; + return; + } + + /* Try to load the plugin */ + if(!(manager()->load(plugin) & PluginManager::LoadState::Loaded)) { + Error() << "Audio::AnyImporter::openFile(): cannot load" << plugin << "plugin"; + return; + } + + /* Try to open the file (error output should be printed by the plugin + itself) */ + std::unique_ptr importer = static_cast*>(manager())->instance(plugin); + if(!importer->openFile(filename)) return; + + /* Success, save the instance */ + _in = std::move(importer); +} + +Buffer::Format AnyImporter::doFormat() const { return _in->format(); } + +UnsignedInt AnyImporter::doFrequency() const { return _in->frequency(); } + +Containers::Array AnyImporter::doData() { return _in->data(); } + +}} diff --git a/src/MagnumPlugins/AnyAudioImporter/AnyImporter.h b/src/MagnumPlugins/AnyAudioImporter/AnyImporter.h new file mode 100644 index 000000000..39a5f2da4 --- /dev/null +++ b/src/MagnumPlugins/AnyAudioImporter/AnyImporter.h @@ -0,0 +1,85 @@ +#ifndef Magnum_Trade_AnyImporter_h +#define Magnum_Trade_AnyImporter_h +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015 + 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 @ref Magnum::Audio::AnyImporter + */ + +#include +#include + +namespace Magnum { namespace Audio { + +/** +@brief Any audio importer plugin + +Detects file type based on file extension, loads corresponding plugin and then +tries to open the file with it. + +This plugin is built if `WITH_ANYAUDIOIMPORTER` is enabled when building +Magnum Plugins. To use dynamic plugin, you need to load `AnyAudioImporter` +plugin from `MAGNUM_PLUGINS_IMPORTER_DIR`. To use static plugin, you need to +request `AnyAudioImporter` component of `MagnumPlugins` package in CMake and +link to `${MAGNUMPLUGINS_ANYAUDIOIMPORTER_LIBRARIES}`. To use this as a +dependency of another plugin, you additionally need to add +`${MAGNUMPLUGINS_ANYAUDIOIMPORTER_INCLUDE_DIRS}` to include path. See +@ref building-plugins, @ref cmake-plugins and @ref plugins for more +information. + +Supported formats: + +- WAV (`*.wav`), loaded with @ref WavImporter "WavAudioImporter" or any other + plugin that provides it + +Only loading from files is supported. +*/ +class AnyImporter: public AbstractImporter { + public: + /** @brief Constructor with access to plugin manager */ + explicit AnyImporter(PluginManager::Manager& manager); + + /** @brief Plugin manager constructor */ + explicit AnyImporter(PluginManager::AbstractManager& manager, std::string plugin); + + ~AnyImporter(); + + private: + Features doFeatures() const override; + bool doIsOpened() const override; + void doClose() override; + void doOpenFile(const std::string& filename) override; + + Buffer::Format doFormat() const override; + UnsignedInt doFrequency() const override; + Containers::Array doData() override; + + std::unique_ptr _in; +}; + +}} + +#endif diff --git a/src/MagnumPlugins/AnyAudioImporter/CMakeLists.txt b/src/MagnumPlugins/AnyAudioImporter/CMakeLists.txt new file mode 100644 index 000000000..9a2e3a319 --- /dev/null +++ b/src/MagnumPlugins/AnyAudioImporter/CMakeLists.txt @@ -0,0 +1,61 @@ +# +# This file is part of Magnum. +# +# Copyright © 2010, 2011, 2012, 2013, 2014, 2015 +# 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. +# + +find_package(Magnum REQUIRED Audio) + +include_directories(${MAGNUM_AUDIO_INCLUDE_DIRS}) + +set(AnyAudioImporter_SRCS + AnyImporter.cpp) + +set(AnyAudioImporter_HEADERS + AnyImporter.h) + +# Objects shared between plugin and test library +add_library(AnyAudioImporterObjects OBJECT + ${AnyAudioImporter_SRCS} + ${AnyAudioImporter_HEADERS}) +if(NOT BUILD_STATIC OR BUILD_STATIC_PIC) + set_target_properties(AnyAudioImporterObjects PROPERTIES POSITION_INDEPENDENT_CODE ON) +endif() + +# AnyAudioImporter plugin +add_plugin(AnyAudioImporter ${MAGNUM_PLUGINS_AUDIOIMPORTER_DEBUG_INSTALL_DIR} ${MAGNUM_PLUGINS_AUDIOIMPORTER_RELEASE_INSTALL_DIR} + AnyAudioImporter.conf + $ + pluginRegistration.cpp) +if(BUILD_STATIC_PIC) + set_target_properties(AnyAudioImporter PROPERTIES POSITION_INDEPENDENT_CODE ON) +endif() + +target_link_libraries(AnyAudioImporter ${MAGNUM_LIBRARIES} ${MAGNUM_AUDIO_LIBRARIES}) + +install(FILES ${AnyAudioImporter_HEADERS} DESTINATION ${MAGNUM_PLUGINS_INCLUDE_INSTALL_DIR}/AnyAudioImporter) + +if(BUILD_TESTS) + add_library(MagnumAnyAudioImporterTestLib STATIC $) + target_link_libraries(MagnumAnyAudioImporterTestLib ${MAGNUM_LIBRARIES} ${MAGNUM_AUDIO_LIBRARIES}) + add_subdirectory(Test) +endif() diff --git a/src/MagnumPlugins/AnyAudioImporter/Test/CMakeLists.txt b/src/MagnumPlugins/AnyAudioImporter/Test/CMakeLists.txt new file mode 100644 index 000000000..dfed4c423 --- /dev/null +++ b/src/MagnumPlugins/AnyAudioImporter/Test/CMakeLists.txt @@ -0,0 +1,33 @@ +# +# This file is part of Magnum. +# +# Copyright © 2010, 2011, 2012, 2013, 2014, 2015 +# 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_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}) + +set(WAV_FILE ${CMAKE_CURRENT_SOURCE_DIR}/stereo8.wav) + +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configure.h.cmake + ${CMAKE_CURRENT_BINARY_DIR}/configure.h) + +corrade_add_test(AnyAudioImporterTest Test.cpp LIBRARIES MagnumAnyAudioImporterTestLib) diff --git a/src/MagnumPlugins/AnyAudioImporter/Test/Test.cpp b/src/MagnumPlugins/AnyAudioImporter/Test/Test.cpp new file mode 100644 index 000000000..af87d1de2 --- /dev/null +++ b/src/MagnumPlugins/AnyAudioImporter/Test/Test.cpp @@ -0,0 +1,79 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015 + 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 + +#include "Magnum/Trade/ImageData.h" + +#include "MagnumPlugins/AnyAudioImporter/AnyImporter.h" + +#include "configure.h" + +namespace Magnum { namespace Audio { namespace Test { + +struct AnyImporterTest: TestSuite::Tester { + explicit AnyImporterTest(); + + void wav(); + + void unknown(); + +private: + PluginManager::Manager _manager; +}; + +AnyImporterTest::AnyImporterTest(): _manager{MAGNUM_PLUGINS_AUDIOIMPORTER_DIR} { + addTests({&AnyImporterTest::wav, + + &AnyImporterTest::unknown}); +} + +void AnyImporterTest::wav() { + if(_manager.loadState("WavAudioImporter") == PluginManager::LoadState::NotFound) + CORRADE_SKIP("WavAudioImporter plugin not found, cannot test"); + + AnyImporter importer{_manager}; + CORRADE_VERIFY(importer.openFile(WAV_FILE)); + + /* Check only parameters, as it is good enough proof that it is working */ + CORRADE_COMPARE(importer.format(), Buffer::Format::Stereo8); + CORRADE_COMPARE(importer.frequency(), 96000); +} + +void AnyImporterTest::unknown() { + std::ostringstream output; + Error::setOutput(&output); + + AnyImporter importer{_manager}; + CORRADE_VERIFY(!importer.openFile("sound.mid")); + + CORRADE_COMPARE(output.str(), "Audio::AnyImporter::openFile(): cannot determine type of file sound.mid\n"); +} + +}}} + +CORRADE_TEST_MAIN(Magnum::Audio::Test::AnyImporterTest) diff --git a/src/MagnumPlugins/AnyAudioImporter/Test/configure.h.cmake b/src/MagnumPlugins/AnyAudioImporter/Test/configure.h.cmake new file mode 100644 index 000000000..5bec68853 --- /dev/null +++ b/src/MagnumPlugins/AnyAudioImporter/Test/configure.h.cmake @@ -0,0 +1,32 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015 + 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. +*/ + +#ifdef CORRADE_IS_DEBUG_BUILD +#define MAGNUM_PLUGINS_AUDIOIMPORTER_DIR "${MAGNUM_PLUGINS_AUDIOIMPORTER_DEBUG_DIR}" +#else +#define MAGNUM_PLUGINS_AUDIOIMPORTER_DIR "${MAGNUM_PLUGINS_AUDIOIMPORTER_DIR}" +#endif + +#define WAV_FILE "${WAV_FILE}" diff --git a/src/MagnumPlugins/AnyAudioImporter/Test/stereo8.wav b/src/MagnumPlugins/AnyAudioImporter/Test/stereo8.wav new file mode 100644 index 0000000000000000000000000000000000000000..02298ef3b1b44a5f186092b15dd675ac457ea6b4 GIT binary patch literal 48 zcmWIYbaT^SU| + + 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 "MagnumPlugins/AnyAudioImporter/AnyImporter.h" + +CORRADE_PLUGIN_REGISTER(AnyAudioImporter, Magnum::Audio::AnyImporter, + "cz.mosra.magnum.Audio.AbstractImporter/0.1")