tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
numeric.cpp
// 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;
}
};
auto main() -> int
{
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;
}
constexpr auto next(InputIt it, typename iterator_traits< InputIt >::difference_type n=1) -> InputIt
Return the nth successor of iterator it.
Definition next.hpp:14
constexpr auto size(C const &c) noexcept(noexcept(c.size())) -> decltype(c.size())
Returns the size of the given container c or array array. Returns c.size(), converted to the return t...
Definition size.hpp:18
constexpr double pi
Definition constants.hpp:32
constexpr auto accumulate(InputIt first, InputIt last, Type init) noexcept -> Type
Computes the sum of the given value init and the elements in the range [first, last).
Definition accumulate.hpp:13
TETL_BUILTIN_PTRDIFF ptrdiff_t
etl::ptrdiff_t is the signed integer type of the result of subtracting two pointers.
Definition ptrdiff_t.hpp:14
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:14
A container that encapsulates fixed size arrays.
Definition array.hpp:48
array(T, U...) -> array< T, 1+sizeof...(U)>
One deduction guide is provided for array to provide an equivalent of experimental::make_array for co...
A non-owning view over a contiguous sequence of objects.
Definition span.hpp:83
Dynamically-resizable fixed-capacity vector.
Definition static_vector.hpp:329
constexpr auto push_back(U &&value) noexcept(noexcept(emplace_back(etl::forward< U >(value)))) -> void
Appends value at the end of the vector.
Definition static_vector.hpp:423
constexpr auto begin() noexcept -> iterator
Definition static_vector.hpp:376
constexpr auto end() noexcept -> iterator
Definition static_vector.hpp:380