Browse Source

Add dsp::convert().

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
9fd506bff7
2 changed files with 67 additions and 1 deletions
  1. +65
    -0
      include/dsp/convert.hpp
  2. +2
    -1
      include/rack.hpp

+ 65
- 0
include/dsp/convert.hpp View File

@@ -0,0 +1,65 @@
#pragma once
#include <type_traits>
#include <dsp/common.hpp>


namespace rack {
namespace dsp {


/** 24-bit integer, using int32_t for conversions. */
struct __attribute__((packed)) int24_t {
int32_t i : 24;
int24_t(int32_t i) : i(i) {}
operator int32_t() {return i;}
};
static_assert(sizeof(int24_t) == 3, "int24_t type must be 3 bytes");


/** Converts between normalized types */
template <typename To, typename From>
To convert(From x);


/** Trivial conversion */
template <typename To>
To convert(To x) {return x;}


/** Integer to float */
template <>
inline float convert(int8_t x) {return x / 128.f;}
template <>
inline float convert(int16_t x) {return x / 32768.f;}
template <>
inline float convert(int24_t x) {return x / 8388608.f;}
template <>
inline float convert(int32_t x) {return x / 2147483648.f;}
template <>
inline float convert(int64_t x) {return x / 9223372036854775808.f;}


/** Float to integer */
template <>
inline int8_t convert(float x) {return std::min(std::llround(x * 128.f), 127LL);}
template <>
inline int16_t convert(float x) {return std::min(std::llround(x * 32768.f), 32767LL);}
template <>
inline int24_t convert(float x) {return std::min(std::llround(x * 8388608.f), 8388607LL);}
template <>
inline int32_t convert(float x) {return std::min(std::llround(x * 2147483648.f), 2147483647LL);}
template <>
inline int64_t convert(float x) {return std::min(std::llround(x * 9223372036854775808.f), 9223372036854775807LL);}


/** Buffer conversion */
template <typename To, typename From>
void convert(const From* in, To* out, size_t len) {
for (size_t i = 0; i < len; i++) {
out[i] = convert<To, From>(in[i]);
}
}


} // namespace dsp
} // namespace rack

+ 2
- 1
include/rack.hpp View File

@@ -94,6 +94,7 @@ Directly including Rack headers other than rack.hpp in your plugin is unsupporte
#include <plugin/Model.hpp>
#include <plugin/callbacks.hpp>

#include <dsp/common.hpp>
#include <dsp/window.hpp>
#include <dsp/ode.hpp>
#include <dsp/minblep.hpp>
@@ -106,7 +107,7 @@ Directly including Rack headers other than rack.hpp in your plugin is unsupporte
#include <dsp/vumeter.hpp>
#include <dsp/filter.hpp>
#include <dsp/digital.hpp>
#include <dsp/common.hpp>
#include <dsp/convert.hpp>

#include <simd/Vector.hpp>
#include <simd/functions.hpp>


Loading…
Cancel
Save