tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
numeric.cpp
// SPDX-License-Identifier: BSL-1.0
// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
#include <etl/cassert.hpp>
#if defined(TETL_ENABLE_CXX_MODULES)
import etl;
#else
#include <etl/array.hpp>
#include <etl/iterator.hpp>
#include <etl/numbers.hpp>
#include <etl/numeric.hpp>
#include <etl/span.hpp>
#include <etl/vector.hpp>
#endif
#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(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;
}
constexpr auto next(InputIt it, typename iterator_traits< InputIt >::difference_type n=1) -> InputIt
Return the nth successor of iterator it.
Definition next.hpp:15
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:19
constexpr double pi
Definition constants.hpp:33
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:14
TETL_BUILTIN_PTRDIFF ptrdiff_t
etl::ptrdiff_t is the signed integer type of the result of subtracting two pointers.
Definition ptrdiff_t.hpp:15
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:15
The class template bitset represents a fixed-size sequence of Bits bits. Bitsets can be manipulated b...
Definition bitset.hpp:23