Sprout/sprout/compost/effects/vibrato.hpp

165 lines
5.6 KiB
C++
Raw Permalink Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2019 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-09 13:33:11 +00:00
#ifndef SPROUT_COMPOST_EFFECTS_VIBRATO_HPP
#define SPROUT_COMPOST_EFFECTS_VIBRATO_HPP
#include <iterator>
#include <sprout/config.hpp>
#include <sprout/utility/forward.hpp>
#include <sprout/container/functions.hpp>
#include <sprout/math/constants.hpp>
#include <sprout/math/sin.hpp>
#include <sprout/range/adaptor/transformed.hpp>
#include <sprout/range/adaptor/outdirected.hpp>
#include <sprout/range/adaptor/indexed.hpp>
#include <sprout/range/adaptor/valued.hpp>
namespace sprout {
namespace compost {
//
// vibrato_outdirected_value
//
template<typename Value, typename IntType>
struct vibrato_outdirected_value {
public:
typedef Value value_type;
typedef IntType int_type;
private:
value_type d_;
value_type depth_;
value_type rate_;
int_type samples_per_sec_;
private:
template<typename Outdirected>
SPROUT_CONSTEXPR typename std::iterator_traits<Outdirected>::value_type
calc_2(Outdirected const& x, typename Outdirected::index_type m, value_type const& delta) const {
return m >= 0 && (m + 1 < x.base().get() || x.base().get() < 0)
? delta * x[m + 1 - x.index()] + (1 - delta) * x[m - x.index()]
2012-11-10 23:26:58 +00:00
: 0
2012-11-09 13:33:11 +00:00
;
}
template<typename Outdirected>
SPROUT_CONSTEXPR typename std::iterator_traits<Outdirected>::value_type
2013-07-22 13:00:09 +00:00
calc_1(Outdirected const& x, value_type const& t) const {
2012-11-09 13:33:11 +00:00
return calc_2(x, static_cast<typename Outdirected::index_type>(t), t - static_cast<typename Outdirected::index_type>(t));
}
template<typename Outdirected>
SPROUT_CONSTEXPR typename std::iterator_traits<Outdirected>::value_type
calc(Outdirected const& x, value_type const& tau) const {
2013-07-22 13:00:09 +00:00
return calc_1(x, x.index() - tau);
2012-11-09 13:33:11 +00:00
}
public:
SPROUT_CONSTEXPR vibrato_outdirected_value(
value_type const& d, value_type const& depth, value_type const& rate,
int_type samples_per_sec = 44100
)
: d_(d), depth_(depth), rate_(rate), samples_per_sec_(samples_per_sec)
{}
template<typename Outdirected>
SPROUT_CONSTEXPR typename std::iterator_traits<Outdirected>::value_type
operator()(Outdirected const& x) const {
2013-04-23 10:03:03 +00:00
return calc(x, d_ + depth_ * sprout::sin(sprout::math::two_pi<Value>() * rate_ * x.index() / samples_per_sec_));
2012-11-09 13:33:11 +00:00
}
};
namespace effects {
//
// vibrato_holder
//
template<typename T, typename IntType = int>
class vibrato_holder {
public:
typedef T value_type;
typedef IntType int_type;
private:
value_type d_;
value_type depth_;
value_type rate_;
int_type samples_per_sec_;
public:
2014-01-13 15:18:06 +00:00
SPROUT_CONSTEXPR vibrato_holder() SPROUT_DEFAULTED_DEFAULT_CONSTRUCTOR_DECL
2012-11-09 13:33:11 +00:00
vibrato_holder(vibrato_holder const&) = default;
SPROUT_CONSTEXPR vibrato_holder(
value_type const& d, value_type const& depth, value_type const& rate,
int_type samples_per_sec = 44100
)
: d_(d), depth_(depth), rate_(rate), samples_per_sec_(samples_per_sec)
{}
SPROUT_CONSTEXPR value_type const& d() const {
return d_;
}
SPROUT_CONSTEXPR value_type const& depth() const {
return depth_;
}
SPROUT_CONSTEXPR value_type const& rate() const {
return rate_;
}
SPROUT_CONSTEXPR int_type const& samples_per_sec() const {
return samples_per_sec_;
}
};
//
// vibrato_forwarder
//
class vibrato_forwarder {
public:
template<typename T, typename IntType>
SPROUT_CONSTEXPR sprout::compost::effects::vibrato_holder<T, IntType>
2013-02-19 17:23:20 +00:00
operator()(T const& d, T const& depth, T const& rate, IntType samples_per_sec) const {
2012-11-09 13:33:11 +00:00
return sprout::compost::effects::vibrato_holder<T, IntType>(d, depth, rate, samples_per_sec);
}
template<typename T>
SPROUT_CONSTEXPR sprout::compost::effects::vibrato_holder<T>
2013-02-19 17:23:20 +00:00
operator()(T const& d, T const& depth, T const& rate) const {
2012-11-09 13:33:11 +00:00
return sprout::compost::effects::vibrato_holder<T>(d, depth, rate);
}
};
//
// vibrato
//
namespace {
2012-12-17 14:10:23 +00:00
SPROUT_STATIC_CONSTEXPR sprout::compost::effects::vibrato_forwarder vibrato = {};
2012-11-09 13:33:11 +00:00
} // anonymous-namespace
//
// operator|
//
template<typename Range, typename T, typename IntType>
inline SPROUT_CONSTEXPR auto
operator|(Range&& lhs, sprout::compost::effects::vibrato_holder<T, IntType> const& rhs)
-> decltype(
SPROUT_FORWARD(Range, lhs)
| sprout::adaptors::valued(sprout::size(SPROUT_FORWARD(Range, lhs)))
2012-11-09 13:33:11 +00:00
| sprout::adaptors::indexed | sprout::adaptors::outdirected
| sprout::adaptors::transformed(
sprout::compost::vibrato_outdirected_value<T, IntType>(
rhs.d(), rhs.depth(), rhs.rate(), rhs.samples_per_sec()
)
)
)
{
return SPROUT_FORWARD(Range, lhs)
| sprout::adaptors::valued(sprout::size(SPROUT_FORWARD(Range, lhs)))
2012-11-09 13:33:11 +00:00
| sprout::adaptors::indexed | sprout::adaptors::outdirected
| sprout::adaptors::transformed(
sprout::compost::vibrato_outdirected_value<T, IntType>(
rhs.d(), rhs.depth(), rhs.rate(), rhs.samples_per_sec()
)
)
;
}
} // namespace effects
using sprout::compost::effects::vibrato;
2012-11-09 13:33:11 +00:00
} // namespace compost
} // namespace sprout
#endif // #ifndef SPROUT_COMPOST_EFFECTS_VIBRATO_HPP