2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2017-07-29 05:20:01 +00:00
|
|
|
Copyright (c) 2011-2017 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2012-11-08 16:09:49 +00:00
|
|
|
#ifndef SPROUT_COMPOST_FORMATS_AS_PCM_WAVE_HPP
|
|
|
|
#define SPROUT_COMPOST_FORMATS_AS_PCM_WAVE_HPP
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
2013-08-06 15:15:09 +00:00
|
|
|
#include <sprout/limits.hpp>
|
2012-11-08 16:09:49 +00:00
|
|
|
#include <sprout/algorithm/clamp.hpp>
|
|
|
|
#include <sprout/utility/forward.hpp>
|
2014-03-15 03:06:51 +00:00
|
|
|
#include <sprout/type_traits/is_sint.hpp>
|
2012-11-08 16:09:49 +00:00
|
|
|
#include <sprout/type_traits/is_uint.hpp>
|
|
|
|
#include <sprout/range/adaptor/transformed.hpp>
|
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
namespace compost {
|
2012-11-15 02:54:57 +00:00
|
|
|
namespace detail {
|
2012-11-08 16:09:49 +00:00
|
|
|
template<typename IntType, typename = void>
|
|
|
|
struct normalized_to_pcm_wave;
|
|
|
|
template<typename IntType>
|
|
|
|
struct normalized_to_pcm_wave<
|
|
|
|
IntType,
|
2014-03-15 03:06:51 +00:00
|
|
|
typename std::enable_if<sprout::is_sint<IntType>::value>::type
|
2012-11-08 16:09:49 +00:00
|
|
|
> {
|
|
|
|
public:
|
|
|
|
typedef IntType result_type;
|
|
|
|
public:
|
|
|
|
template<typename T>
|
|
|
|
SPROUT_CONSTEXPR result_type operator()(T const& x) const {
|
2013-08-06 15:15:09 +00:00
|
|
|
return static_cast<result_type>(sprout::clamp(x, -1, 1) * sprout::numeric_limits<result_type>::max());
|
2012-11-08 16:09:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
template<typename IntType>
|
|
|
|
struct normalized_to_pcm_wave<
|
|
|
|
IntType,
|
|
|
|
typename std::enable_if<sprout::is_uint<IntType>::value>::type
|
|
|
|
> {
|
|
|
|
public:
|
|
|
|
typedef IntType result_type;
|
|
|
|
public:
|
|
|
|
template<typename T>
|
|
|
|
SPROUT_CONSTEXPR result_type operator()(T const& x) const {
|
2013-08-06 15:15:09 +00:00
|
|
|
return static_cast<result_type>(sprout::clamp((x + 1) / 2, 0, 1) * sprout::numeric_limits<result_type>::max());
|
2012-11-08 16:09:49 +00:00
|
|
|
}
|
|
|
|
};
|
2012-11-15 02:54:57 +00:00
|
|
|
} // namespace detail
|
|
|
|
//
|
|
|
|
// normalized_to_pcm_wave
|
|
|
|
//
|
|
|
|
template<typename IntType>
|
|
|
|
struct normalized_to_pcm_wave
|
|
|
|
: public sprout::compost::detail::normalized_to_pcm_wave<IntType>
|
|
|
|
{};
|
2012-11-08 16:09:49 +00:00
|
|
|
|
2012-11-15 02:54:57 +00:00
|
|
|
namespace formats {
|
2012-11-08 16:09:49 +00:00
|
|
|
//
|
|
|
|
// as_pcm_wave_forwarder
|
|
|
|
//
|
|
|
|
template<typename IntType>
|
|
|
|
class as_pcm_wave_forwarder {};
|
|
|
|
|
|
|
|
//
|
|
|
|
// as_pcm_wave
|
|
|
|
//
|
|
|
|
template<typename IntType>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::compost::formats::as_pcm_wave_forwarder<IntType>
|
|
|
|
as_pcm_wave() {
|
|
|
|
return sprout::compost::formats::as_pcm_wave_forwarder<IntType>();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// operator|
|
|
|
|
//
|
|
|
|
template<typename Range, typename IntType>
|
|
|
|
inline SPROUT_CONSTEXPR auto
|
2013-07-22 13:00:09 +00:00
|
|
|
operator|(Range&& lhs, sprout::compost::formats::as_pcm_wave_forwarder<IntType> const&)
|
2012-11-08 16:09:49 +00:00
|
|
|
-> decltype(
|
2014-02-22 07:32:51 +00:00
|
|
|
SPROUT_FORWARD(Range, lhs)
|
2012-11-15 02:54:57 +00:00
|
|
|
| sprout::adaptors::transformed(sprout::compost::normalized_to_pcm_wave<IntType>())
|
2012-11-08 16:09:49 +00:00
|
|
|
)
|
|
|
|
{
|
2014-02-22 07:32:51 +00:00
|
|
|
return SPROUT_FORWARD(Range, lhs)
|
2012-11-15 02:54:57 +00:00
|
|
|
| sprout::adaptors::transformed(sprout::compost::normalized_to_pcm_wave<IntType>())
|
2012-11-08 16:09:49 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// as_pcm_wave8
|
|
|
|
// as_pcm_wave16
|
|
|
|
//
|
|
|
|
namespace {
|
2012-12-17 14:10:23 +00:00
|
|
|
SPROUT_STATIC_CONSTEXPR sprout::compost::formats::as_pcm_wave_forwarder<std::uint8_t> as_pcm_wave8 = {};
|
|
|
|
SPROUT_STATIC_CONSTEXPR sprout::compost::formats::as_pcm_wave_forwarder<std::int16_t> as_pcm_wave16 = {};
|
2012-11-08 16:09:49 +00:00
|
|
|
} // anonymous-namespace
|
|
|
|
} // namespace formats
|
2012-11-24 04:04:02 +00:00
|
|
|
|
|
|
|
using sprout::compost::formats::as_pcm_wave;
|
|
|
|
using sprout::compost::formats::as_pcm_wave8;
|
|
|
|
using sprout::compost::formats::as_pcm_wave16;
|
2012-11-08 16:09:49 +00:00
|
|
|
} // namespace compost
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_COMPOST_FORMATS_AS_PCM_WAVE_HPP
|