HephAudio v3.0.6
A cross-platform C++ library for recording, playing, and processing audio on Windows, Android, Linux, iOS, and macOS.
Loading...
Searching...
No Matches
ArithmeticBuffer.h
Go to the documentation of this file.
1#pragma once
2#include "HephShared.h"
3#include "BufferBase.h"
4#include "HephMath.h"
5#include "BufferOperators.h"
6#include <initializer_list>
7#include <limits>
8
11namespace Heph
12{
19 template <class Tself, typename Tdata>
21 public BufferBase<Tself, Tdata>,
22 public BufferArithmeticOperators<Tself, Tdata>,
23 public BufferArithmeticOperators<Tself, Tdata, Tself, Tdata>
24 {
25 static_assert(std::is_arithmetic<Tdata>::value, "Tdata must be an arithmetic type");
26
27 protected:
32 static constexpr Tdata MIN_ELEMENT = std::numeric_limits<Tdata>::lowest();
33
38 static constexpr Tdata MAX_ELEMENT = std::numeric_limits<Tdata>::max();
39
40 protected:
42 ArithmeticBuffer() : BufferBase<Tself, Tdata>() { ArithmeticBuffer::AddEventHandlers(); }
43
45 explicit ArithmeticBuffer(size_t size) : BufferBase<Tself, Tdata>(size) { ArithmeticBuffer::AddEventHandlers(); }
46
48 ArithmeticBuffer(size_t size, BufferFlags flags) : BufferBase<Tself, Tdata>(size, flags) { ArithmeticBuffer::AddEventHandlers(); }
49
51 ArithmeticBuffer(const std::initializer_list<Tdata>& rhs) : BufferBase<Tself, Tdata>(rhs) { ArithmeticBuffer::AddEventHandlers(); }
52
54 ArithmeticBuffer(const ArithmeticBuffer& rhs) : BufferBase<Tself, Tdata>(rhs) { ArithmeticBuffer::AddEventHandlers(); }
55
57 ArithmeticBuffer(ArithmeticBuffer&& rhs) noexcept : BufferBase<Tself, Tdata>(std::move(rhs)) { ArithmeticBuffer::AddEventHandlers(); }
58
59 public:
61 virtual ~ArithmeticBuffer() = default;
62
67 Tdata Min() const
68 {
69 Tdata result = ArithmeticBuffer::MAX_ELEMENT;
70 for (size_t i = 0; i < this->size; ++i)
71 {
72 if (this->pData[i] < result)
73 {
74 result = this->pData[i];
75 }
76 }
77 return result;
78 }
79
84 Tdata Max() const
85 {
86 Tdata result = ArithmeticBuffer::MIN_ELEMENT;
87 for (size_t i = 0; i < this->size; ++i)
88 {
89 if (this->pData[i] > result)
90 {
91 result = this->pData[i];
92 }
93 }
94 return result;
95 }
96
101 double Rms() const
102 {
103 if (this->size > 0)
104 {
105 double sumSquared = 0;
106 for (size_t i = 0; i < this->size; ++i)
107 {
108 sumSquared += this->pData[i] * this->pData[i];
109 }
110 return std::sqrt(sumSquared / this->size);
111 }
112 return 0;
113 }
114
115 private:
116 static inline bool ADD_EVENT_HANDLERS = false;
117 static void AddEventHandlers()
118 {
119 if (!ArithmeticBuffer::ADD_EVENT_HANDLERS)
120 {
121 BufferOperatorEvents<Tself, Tdata>::OnResultCreated = ArithmeticBuffer::ResultCreatedEventHandler<Tdata>;
122 BufferOperatorEvents<Tself, Tself>::OnResultCreated = ArithmeticBuffer::ResultCreatedEventHandler<Tself>;
123 ArithmeticBuffer::ADD_EVENT_HANDLERS = true;
124 }
125 }
126
127 template<typename Rhs>
128 static void ResultCreatedEventHandler(const EventParams& params)
129 {
130 BufferOperatorResultCreatedEventArgs<Tself, Rhs>* pArgs = (BufferOperatorResultCreatedEventArgs<Tself, Rhs>*)params.pArgs;
131
132 pArgs->result.pData = ArithmeticBuffer::AllocateUninitialized(pArgs->lhs.SizeAsByte());
133 pArgs->result.size = pArgs->lhs.size;
134 }
135 };
136
143 template <class Tself, typename Tdata>
145 public ArithmeticBuffer<Tself, Tdata>,
146 public BufferUnaryMinusOperator<Tself, Tdata>
147 {
148 static_assert(std::is_signed<Tdata>::value, "Tdata must be a signed type");
149
150 protected:
153
155 explicit SignedArithmeticBuffer(size_t size) : ArithmeticBuffer<Tself, Tdata>(size) {}
156
158 SignedArithmeticBuffer(size_t size, BufferFlags flags) : ArithmeticBuffer<Tself, Tdata>(size, flags) {}
159
161 SignedArithmeticBuffer(const std::initializer_list<Tdata>& rhs) : ArithmeticBuffer<Tself, Tdata>(rhs) {}
162
165
168
169 public:
171 virtual ~SignedArithmeticBuffer() = default;
172
177 void Invert()
178 {
179 for (size_t i = 0; i < this->size; ++i)
180 {
181 this->pData[i] = -this->pData[i];
182 }
183 }
184
189 Tdata AbsMax() const
190 {
191 Tdata result = 0;
192 for (size_t i = 0; i < this->size; ++i)
193 {
194 const Tdata abs_current = std::abs(this->pData[i]);
195 if (abs_current > result)
196 {
197 result = abs_current;
198 }
199 }
200 return result;
201 }
202 };
203}
BufferFlags
Definition BufferBase.h:13
#define HEPH_API
Definition HephShared.h:132
base class for buffers that store arithmetic types. Provides operations and methods for arithmetic da...
Definition ArithmeticBuffer.h:24
ArithmeticBuffer(ArithmeticBuffer &&rhs) noexcept
Definition ArithmeticBuffer.h:57
Tdata Max() const
Definition ArithmeticBuffer.h:84
ArithmeticBuffer(const std::initializer_list< Tdata > &rhs)
Definition ArithmeticBuffer.h:51
ArithmeticBuffer(size_t size)
Definition ArithmeticBuffer.h:45
double Rms() const
Definition ArithmeticBuffer.h:101
ArithmeticBuffer(size_t size, BufferFlags flags)
Definition ArithmeticBuffer.h:48
ArithmeticBuffer()
Definition ArithmeticBuffer.h:42
Tdata Min() const
Definition ArithmeticBuffer.h:67
ArithmeticBuffer(const ArithmeticBuffer &rhs)
Definition ArithmeticBuffer.h:54
virtual ~ArithmeticBuffer()=default
provides arithmetic operators to the buffer.
Definition BufferOperators.h:595
base class for buffers. Provides basic buffer operations and methods.
Definition BufferBase.h:31
provides unary minus operator to the buffer.
Definition BufferOperators.h:609
base class for buffers that store signed arithmetic types. Provides operations and methods for signed...
Definition ArithmeticBuffer.h:147
SignedArithmeticBuffer(size_t size, BufferFlags flags)
Definition ArithmeticBuffer.h:158
SignedArithmeticBuffer()
Definition ArithmeticBuffer.h:152
virtual ~SignedArithmeticBuffer()=default
SignedArithmeticBuffer(SignedArithmeticBuffer &&rhs) noexcept
Definition ArithmeticBuffer.h:167
Tdata AbsMax() const
Definition ArithmeticBuffer.h:189
SignedArithmeticBuffer(const SignedArithmeticBuffer &rhs)
Definition ArithmeticBuffer.h:164
SignedArithmeticBuffer(const std::initializer_list< Tdata > &rhs)
Definition ArithmeticBuffer.h:161
SignedArithmeticBuffer(size_t size)
Definition ArithmeticBuffer.h:155
void Invert()
Definition ArithmeticBuffer.h:177
Definition BufferOperators.h:28