Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
transport.hpp
Go to the documentation of this file.
1/**
2 * @file transport.hpp
3 * @brief The processing call's shape as a checked contract
4 *
5 * A component declares its ports, in the order its processing call takes
6 * them, and takes them as a parameter pack after the frame count:
7 *
8 * static constexpr transport::Port PORTS[] = {{"pitch"}, {"wave", transport::Spelling::Scalar}};
9 *
10 * template<class... Ports>
11 * void process(float* out, uint16_t frames, Ports... ports); // a source
12 * template<class... Ports>
13 * void process(const float* in, float* out, uint16_t frames, Ports... ports); // a processor
14 *
15 * Each position is one declared port. An argument is a block, `const float*`
16 * with one value per frame, or a scalar, any arithmetic type; a port's
17 * Spelling says which it accepts. Trailing ports may be left off, and arg()
18 * gives Omitted for them; the component documents what a port left off
19 * does. A component with no ports declares none and takes none.
20 *
21 * fits_v checks a call's port arguments against the declaration: no more
22 * arguments than ports, and each one a spelling its port accepts. A
23 * component static_asserts it at the top of process(), with its own message.
24 * is_source_v and is_processor_v tell the two kinds apart by the call they
25 * accept. PARAMETERS, also optional, declares the setters and their units for
26 * tools such as sbl-render.
27 *
28 * Everything here is resolved at compile time; nothing is stored.
29 */
30
31#ifndef SBL_COMMON_TRANSPORT_HPP_
32#define SBL_COMMON_TRANSPORT_HPP_
33
34#include <cstddef>
35#include <cstdint>
36#include <iterator>
37#include <tuple>
38#include <type_traits>
39#include <utility>
40
41namespace sbl::transport {
42
43/** Which spellings a port accepts (section 7.2). */
44enum class Spelling : uint8_t {
45 Either, /**< a block or a scalar */
46 Scalar, /**< a scalar only */
47 Block, /**< a block only */
48};
49
50/** What a port's or a parameter's value is, so a tool can convert to it. */
51enum class Unit : uint8_t {
52 Number, /**< used as given */
53 CyclesPerFrame, /**< a frequency; a tool takes it in Hz and divides by its sample rate */
54};
55
56/** One port, named for the quantity it controls. Below the transport its unit can say what a tool converts. */
57struct Port {
58 const char* name = nullptr;
59 Spelling spelling = Spelling::Either;
60 Unit unit = Unit::Number;
61};
62
63/** One parameter: its setter on T and the unit the setter takes. */
64template<class T>
65struct Parameter {
66 const char* name;
67 Unit unit;
68 void (T::*set)(float);
69};
70
71/** A port the call left off. */
72struct Omitted {};
73
74template<class A>
75inline constexpr bool is_block_v =
76 std::is_same_v<std::decay_t<A>, const float*> || std::is_same_v<std::decay_t<A>, float*>;
77
78template<class A>
79inline constexpr bool is_scalar_v = std::is_arithmetic_v<std::decay_t<A>>;
80
81template<class A>
82inline constexpr bool is_omitted_v = std::is_same_v<std::decay_t<A>, Omitted>;
83
84/** The number of ports T declares: none without PORTS. */
85template<class T, class = void>
86struct port_count : std::integral_constant<std::size_t, 0> {};
87template<class T>
88struct port_count<T, std::void_t<decltype(T::PORTS)>> : std::integral_constant<std::size_t, std::size(T::PORTS)> {};
89template<class T>
90inline constexpr std::size_t port_count_v = port_count<T>::value;
91
92/** The number of parameters T declares: none without PARAMETERS. */
93template<class T, class = void>
94struct parameter_count : std::integral_constant<std::size_t, 0> {};
95template<class T>
96struct parameter_count<T, std::void_t<decltype(T::PARAMETERS)>>
97 : std::integral_constant<std::size_t, std::size(T::PARAMETERS)> {};
98template<class T>
99inline constexpr std::size_t parameter_count_v = parameter_count<T>::value;
100
101namespace detail {
102
103template<class T, std::size_t I, class A>
104constexpr bool accepts() {
105 if constexpr (I < port_count_v<T> && (is_block_v<A> || is_scalar_v<A>)) {
106 return (is_block_v<A>) ? T::PORTS[I].spelling != Spelling::Scalar : T::PORTS[I].spelling != Spelling::Block;
107 } else {
108 return false; // past the declared ports, or neither a block nor a scalar
109 }
110}
111
112template<class T, class... A, std::size_t... I>
113constexpr bool fits(std::index_sequence<I...> /*positions*/) {
114 return (accepts<T, I, A>() && ...);
115}
116
117} // namespace detail
118
119/** Whether port arguments of types A... fit T's declared ports. */
120template<class T, class... A>
121inline constexpr bool fits_v = detail::fits<T, A...>(std::index_sequence_for<A...>{});
122
123/** The I-th port argument, or Omitted when the call left it off. */
124template<std::size_t I, class... A>
125constexpr auto arg(A... args) {
126 if constexpr (I < sizeof...(A)) {
127 return std::get<I>(std::tuple<A...>(args...));
128 } else {
129 return Omitted{};
130 }
131}
132
133/** A source: no signal in, `process(float* out, uint16_t frames, ports...)`. */
134template<class T, class = void>
135struct is_source : std::false_type {};
136template<class T>
137struct is_source<T, std::void_t<decltype(std::declval<T&>().process(std::declval<float*>(), std::declval<uint16_t>()))>>
138 : std::true_type {};
139template<class T>
140inline constexpr bool is_source_v = is_source<T>::value;
141
142/** A processor: a signal in and out, `process(const float* in, float* out, uint16_t frames, ports...)`. */
143template<class T, class = void>
144struct is_processor : std::false_type {};
145template<class T>
146struct is_processor<T, std::void_t<decltype(std::declval<T&>().process(
147 std::declval<const float*>(), std::declval<float*>(), std::declval<uint16_t>()))>>
148 : std::true_type {};
149template<class T>
150inline constexpr bool is_processor_v = is_processor<T>::value;
151
152} // namespace sbl::transport
153
154#endif // SBL_COMMON_TRANSPORT_HPP_
The signal transport trait: a component's declared ports and the check of a processing call against t...
Definition transport.hpp:41
constexpr bool fits_v
constexpr auto arg(A... args)