2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTILITY_H__
|
|
|
|
#define __UTILITY_H__
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstddef>
|
2016-01-27 15:09:36 -05:00
|
|
|
#include <algorithm>
|
2015-08-04 17:08:15 -04:00
|
|
|
#include <complex>
|
2015-07-08 11:39:24 -04:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#define LOCATE_IN_RAM __attribute__((section(".ramtext")))
|
|
|
|
|
2016-12-07 14:49:42 -05:00
|
|
|
inline uint16_t fb_to_uint16(const std::string& fb) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return (fb[1] << 8) + fb[0];
|
2016-12-07 14:49:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t fb_to_uint32(const std::string& fb) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return (fb[3] << 24) + (fb[2] << 16) + (fb[1] << 8) + fb[0];
|
2016-12-07 14:49:42 -05:00
|
|
|
}
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
constexpr size_t operator"" _KiB(unsigned long long v) {
|
|
|
|
return v * 1024;
|
2015-08-20 20:15:51 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
constexpr size_t operator"" _MiB(unsigned long long v) {
|
|
|
|
return v * 1024 * 1024;
|
2015-08-20 20:15:51 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
template <typename E>
|
2015-07-08 11:39:24 -04:00
|
|
|
constexpr typename std::underlying_type<E>::type toUType(E enumerator) noexcept {
|
2023-05-18 16:16:05 -04:00
|
|
|
/* Thanks, Scott Meyers! */
|
|
|
|
return static_cast<typename std::underlying_type<E>::type>(enumerator);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t flp2(uint32_t v) {
|
2023-05-18 16:16:05 -04:00
|
|
|
v |= v >> 1;
|
|
|
|
v |= v >> 2;
|
|
|
|
v |= v >> 4;
|
|
|
|
v |= v >> 8;
|
|
|
|
v |= v >> 16;
|
|
|
|
return v - (v >> 1);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t gcd(const uint32_t u, const uint32_t v);
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
template <class T>
|
2015-07-08 11:39:24 -04:00
|
|
|
inline constexpr T pow(const T base, unsigned const exponent) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return (exponent == 0) ? 1 : (base * pow(base, exponent - 1));
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2016-02-10 13:15:42 -05:00
|
|
|
constexpr bool power_of_two(const size_t n) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return (n & (n - 1)) == 0;
|
2016-02-10 13:15:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t log_2(const size_t n, const size_t p = 0) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return (n <= 1) ? p : log_2(n / 2, p + 1);
|
2016-02-10 13:15:42 -05:00
|
|
|
}
|
|
|
|
|
2016-02-13 14:18:15 -05:00
|
|
|
float fast_log2(const float val);
|
2016-02-13 14:18:34 -05:00
|
|
|
float fast_pow2(const float val);
|
2016-02-13 14:18:15 -05:00
|
|
|
|
2016-02-14 15:38:50 -05:00
|
|
|
float mag2_to_dbv_norm(const float mag2);
|
2015-08-04 15:03:10 -04:00
|
|
|
|
2015-08-04 17:08:15 -04:00
|
|
|
inline float magnitude_squared(const std::complex<float> c) {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto r = c.real();
|
|
|
|
const auto r2 = r * r;
|
|
|
|
const auto i = c.imag();
|
|
|
|
const auto i2 = i * i;
|
|
|
|
return r2 + i2;
|
2015-08-04 17:08:15 -04:00
|
|
|
}
|
|
|
|
|
2023-06-17 16:01:46 -04:00
|
|
|
/* Compute the duration in ms of a buffer. */
|
|
|
|
inline uint32_t ms_duration(
|
|
|
|
uint64_t buffer_size,
|
|
|
|
uint32_t sample_rate,
|
|
|
|
uint32_t bytes_per_sample) {
|
|
|
|
/* bytes * sample * second * ms
|
|
|
|
* ------ ------- ------
|
|
|
|
* bytes samples seconds
|
|
|
|
*/
|
|
|
|
if (sample_rate == 0 || bytes_per_sample == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return buffer_size / bytes_per_sample / sample_rate * 1000;
|
|
|
|
}
|
|
|
|
|
2023-02-28 14:02:05 -05:00
|
|
|
int fast_int_magnitude(int y, int x);
|
|
|
|
int int_atan2(int y, int x);
|
|
|
|
int32_t int16_sin_s4(int32_t x);
|
|
|
|
|
2023-06-11 14:47:13 -04:00
|
|
|
template <typename TEnum>
|
2023-06-12 13:40:32 -04:00
|
|
|
struct is_flags_type {
|
|
|
|
static constexpr bool value = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename TEnum>
|
|
|
|
constexpr bool is_flags_type_v = is_flags_type<TEnum>::value;
|
|
|
|
|
|
|
|
#define ENABLE_FLAGS_OPERATORS(type) \
|
|
|
|
template <> \
|
|
|
|
struct is_flags_type<type> { static constexpr bool value = true; };
|
|
|
|
|
|
|
|
template <typename TEnum>
|
|
|
|
constexpr std::enable_if_t<is_flags_type_v<TEnum>, TEnum> operator|(TEnum a, TEnum b) {
|
|
|
|
using under_t = std::underlying_type_t<TEnum>;
|
|
|
|
return static_cast<TEnum>(static_cast<under_t>(a) | static_cast<under_t>(b));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename TEnum>
|
|
|
|
constexpr std::enable_if_t<is_flags_type_v<TEnum>, bool> flags_enabled(TEnum value, TEnum flags) {
|
2023-06-11 14:47:13 -04:00
|
|
|
auto i_value = static_cast<std::underlying_type_t<TEnum>>(value);
|
|
|
|
auto i_flags = static_cast<std::underlying_type_t<TEnum>>(flags);
|
|
|
|
|
|
|
|
return (i_value & i_flags) == i_flags;
|
|
|
|
}
|
|
|
|
|
2023-06-04 15:56:46 -04:00
|
|
|
/* Returns value constrained to min and max. */
|
|
|
|
template <class T>
|
|
|
|
constexpr const T& clip(const T& value, const T& minimum, const T& maximum) {
|
|
|
|
return std::max(std::min(value, maximum), minimum);
|
|
|
|
}
|
|
|
|
|
2023-06-14 03:57:20 -04:00
|
|
|
/* Saves state on construction and reverts it when destroyed. */
|
|
|
|
template <typename T>
|
|
|
|
struct Stash {
|
|
|
|
Stash(T& target)
|
|
|
|
: target_{target}, prev_{target} {
|
|
|
|
}
|
|
|
|
|
|
|
|
~Stash() {
|
|
|
|
target_ = std::move(prev_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T& target_;
|
|
|
|
T prev_;
|
|
|
|
};
|
|
|
|
|
2023-06-04 15:56:46 -04:00
|
|
|
// TODO: need to decide if this is inclusive or exclusive.
|
|
|
|
// The implementations are different and cause the subtle
|
|
|
|
// bugs mentioned below.
|
2023-05-18 16:16:05 -04:00
|
|
|
template <class T>
|
2016-01-27 15:09:36 -05:00
|
|
|
struct range_t {
|
2023-05-18 16:16:05 -04:00
|
|
|
const T minimum;
|
|
|
|
const T maximum;
|
|
|
|
|
|
|
|
constexpr const T& clip(const T& value) const {
|
2023-06-04 15:56:46 -04:00
|
|
|
return ::clip(value, minimum, maximum);
|
2023-05-18 16:16:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr void reset_if_outside(T& value, const T& reset_value) const {
|
|
|
|
if ((value < minimum) ||
|
|
|
|
(value > maximum)) {
|
|
|
|
value = reset_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool below_range(const T& value) const {
|
|
|
|
return value < minimum;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool contains(const T& value) const {
|
|
|
|
// TODO: Subtle gotcha here! Range test doesn't include maximum!
|
|
|
|
return (value >= minimum) && (value < maximum);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr bool out_of_range(const T& value) const {
|
|
|
|
// TODO: Subtle gotcha here! Range test in contains() doesn't include maximum!
|
|
|
|
return !contains(value);
|
|
|
|
}
|
2016-01-27 15:09:36 -05:00
|
|
|
};
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
#endif /*__UTILITY_H__*/
|