-
Notifications
You must be signed in to change notification settings - Fork 2
/
numeric.cpp
76 lines (57 loc) · 2.29 KB
/
numeric.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// SPDX-License-Identifier: BSL-1.0
#undef NDEBUG
#include <etl/array.hpp>
#include <etl/iterator.hpp>
#include <etl/numbers.hpp>
#include <etl/numeric.hpp>
#include <etl/span.hpp>
#include <etl/vector.hpp>
#include <stdio.h>
template <typename T, unsigned Channels, unsigned Frames>
struct fixed_audio_buffer {
using value_type = T;
using size_type = etl::size_t;
using frame_type = etl::array<T, Channels>;
using const_frame_type = etl::array<T const, Channels>;
using channel_type = etl::span<T, Frames>;
using const_channel_type = etl::span<T const, Frames>;
fixed_audio_buffer() = default;
[[nodiscard]] auto size_channels() const -> size_type { return Channels; }
[[nodiscard]] auto size_frames() const -> size_type { return Frames; }
[[nodiscard]] auto size_samples() const -> size_type { return size_channels() * size_frames(); }
[[nodiscard]] auto frame(size_type index) { return make_frame(index); }
[[nodiscard]] auto frame(size_type index) const { return make_frame(index); }
[[nodiscard]] auto channel(size_type ch)
{
return channel_type(etl::next(data(_buffer), static_cast<etl::ptrdiff_t>(ch * Frames)), Frames);
}
[[nodiscard]] auto channel(size_type ch) const
{
return const_channel_type(etl::next(data(_buffer), static_cast<etl::ptrdiff_t>(ch * Frames)), Frames);
}
[[nodiscard]] auto operator()(size_type ch, size_type s) -> value_type& { return channel(ch)[s]; }
[[nodiscard]] auto operator()(size_type ch, size_type s) const -> value_type const& { return channel(ch)[s]; }
private:
[[nodiscard]] auto make_frame(size_type s) const
{
auto frame = frame_type{};
for (size_type ch{0}; ch < size_channels(); ++ch) {
frame[ch] = (*this)(ch, s);
}
return frame;
}
etl::array<value_type, static_cast<etl::size_t>(Channels* Frames)> _buffer{};
};
auto main() -> int
{
etl::static_vector<double, 16> vec;
vec.push_back(etl::numbers::pi);
vec.push_back(2.0);
vec.push_back(3.0);
vec.push_back(4.0);
auto sum = etl::accumulate(vec.begin(), vec.end(), 0.0);
printf("%f\n", sum);
auto buffer = fixed_audio_buffer<float, 2, 32>{};
printf("%zu\n", etl::size(buffer.channel(0)));
return 0;
}