VCV Rack API v2
Loading...
Searching...
No Matches
convert.hpp
Go to the documentation of this file.
1#pragma once
2#include <type_traits>
3#include <dsp/common.hpp>
4
5
6namespace rack {
7namespace dsp {
8
9
11struct
12#ifdef __clang__
13__attribute__((packed, aligned(1)))
14#else
15__attribute__((packed, aligned(1), gcc_struct))
16#endif
17Int24 {
18 int32_t i : 24;
19 Int24() {}
20 Int24(int32_t i) : i(i) {}
21 operator int32_t() {return i;}
22};
23static_assert(sizeof(Int24) == 3, "Int24 type must be 3 bytes");
24
25
28template <typename To, typename From>
29To convert(From x) = delete;
30
31
33template <>
34inline float convert(float x) {return x;}
35
36
38template <>
39inline float convert(int8_t x) {return x / 128.f;}
40template <>
41inline float convert(int16_t x) {return x / 32768.f;}
42template <>
43inline float convert(Int24 x) {return x / 8388608.f;}
44template <>
45inline float convert(int32_t x) {return x / 2147483648.f;}
46template <>
47inline float convert(int64_t x) {return x / 9223372036854775808.f;}
48
49
51template <>
52inline int8_t convert(float x) {return std::min(std::llround(x * 128.f), 127LL);}
53template <>
54inline int16_t convert(float x) {return std::min(std::llround(x * 32768.f), 32767LL);}
55template <>
56inline Int24 convert(float x) {return std::min(std::llround(x * 8388608.f), 8388607LL);}
57template <>
58inline int32_t convert(float x) {return std::min(std::llround(x * 2147483648.f), 2147483647LL);}
59template <>
60inline int64_t convert(float x) {return std::min(std::llround(x * 9223372036854775808.f), 9223372036854775807LL);}
61
62
64template <typename To, typename From>
65void convert(const From* in, To* out, size_t len) {
66 for (size_t i = 0; i < len; i++) {
67 out[i] = convert<To, From>(in[i]);
68 }
69}
70
71
72} // namespace dsp
73} // namespace rack
To convert(From x)=delete
Converts between normalized types.
struct __attribute__((packed, aligned(1), gcc_struct)) Int24
24-bit integer, using int32_t for conversions.
Definition convert.hpp:15
Root namespace for the Rack API.
Definition AudioDisplay.hpp:9