Browse Source

New AnyAudioImporter plugin.

Similar to AnyImageImporter. Currently knows only WAV files, more to
come with upcoming plugins.
pull/205/head
Vladimír Vondruš 11 years ago
parent
commit
81c485dd62
  1. 0
      src/MagnumPlugins/AnyAudioImporter/AnyAudioImporter.conf
  2. 80
      src/MagnumPlugins/AnyAudioImporter/AnyImporter.cpp
  3. 85
      src/MagnumPlugins/AnyAudioImporter/AnyImporter.h
  4. 61
      src/MagnumPlugins/AnyAudioImporter/CMakeLists.txt
  5. 33
      src/MagnumPlugins/AnyAudioImporter/Test/CMakeLists.txt
  6. 79
      src/MagnumPlugins/AnyAudioImporter/Test/Test.cpp
  7. 32
      src/MagnumPlugins/AnyAudioImporter/Test/configure.h.cmake
  8. BIN
      src/MagnumPlugins/AnyAudioImporter/Test/stereo8.wav
  9. 29
      src/MagnumPlugins/AnyAudioImporter/pluginRegistration.cpp

0
src/MagnumPlugins/AnyAudioImporter/AnyAudioImporter.conf

80
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š <mosra@centrum.cz>
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 <Corrade/Containers/Array.h>
#include <Corrade/PluginManager/Manager.h>
#include <Corrade/Utility/Assert.h>
#include <Corrade/Utility/String.h>
namespace Magnum { namespace Audio {
AnyImporter::AnyImporter(PluginManager::Manager<AbstractImporter>& 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<AbstractImporter> importer = static_cast<PluginManager::Manager<AbstractImporter>*>(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<char> AnyImporter::doData() { return _in->data(); }
}}

85
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š <mosra@centrum.cz>
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 <memory>
#include <Magnum/Audio/AbstractImporter.h>
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<AbstractImporter>& 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<char> doData() override;
std::unique_ptr<AbstractImporter> _in;
};
}}
#endif

61
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š <mosra@centrum.cz>
#
# 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
$<TARGET_OBJECTS:AnyAudioImporterObjects>
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_OBJECTS:AnyAudioImporterObjects>)
target_link_libraries(MagnumAnyAudioImporterTestLib ${MAGNUM_LIBRARIES} ${MAGNUM_AUDIO_LIBRARIES})
add_subdirectory(Test)
endif()

33
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š <mosra@centrum.cz>
#
# 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)

79
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š <mosra@centrum.cz>
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 <sstream>
#include <Corrade/TestSuite/Tester.h>
#include <Corrade/PluginManager/Manager.h>
#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<AbstractImporter> _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)

32
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š <mosra@centrum.cz>
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}"

BIN
src/MagnumPlugins/AnyAudioImporter/Test/stereo8.wav

Binary file not shown.

29
src/MagnumPlugins/AnyAudioImporter/pluginRegistration.cpp

@ -0,0 +1,29 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015
Vladimír Vondruš <mosra@centrum.cz>
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")
Loading…
Cancel
Save