LMMS
Loading...
Searching...
No Matches
SampleFrame.h
Go to the documentation of this file.
1/*
2 * SampleFrame.h - Representation of a stereo sample
3 *
4 * Copyright (c) 2004-2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
5 * Copyright (c) 2024- Michael Gregorius
6 *
7 * This file is part of LMMS - https://lmms.io
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program (see COPYING); if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301 USA.
23 *
24 */
25
26#ifndef LMMS_SAMPLEFRAME_H
27#define LMMS_SAMPLEFRAME_H
28
29#include "LmmsTypes.h"
30#include "lmms_constants.h"
31
32#include <algorithm>
33#include <array>
34#include <cmath>
35
36
37namespace lmms
38{
39
41{
42public:
44 {
45 }
46
50
55
57 {
58 return m_samples.data();
59 }
60
61 const sample_t* data() const
62 {
63 return m_samples.data();
64 }
65
67 {
68 return m_samples[0];
69 }
70
71 const sample_t& left() const
72 {
73 return m_samples[0];
74 }
75
76 void setLeft(const sample_t& value)
77 {
78 m_samples[0] = value;
79 }
80
82 {
83 return m_samples[1];
84 }
85
86 const sample_t& right() const
87 {
88 return m_samples[1];
89 }
90
91 void setRight(const sample_t& value)
92 {
93 m_samples[1] = value;
94 }
95
96 sample_t& operator[](size_t index)
97 {
98 return m_samples[index];
99 }
100
101 const sample_t& operator[](size_t index) const
102 {
103 return m_samples[index];
104 }
105
107 {
108 return SampleFrame(left() + other.left(), right() + other.right());
109 }
110
112 {
113 auto & l = left();
114 auto & r = right();
115
116 l += other.left();
117 r += other.right();
118
119 return *this;
120 }
121
123 {
124 return SampleFrame(left() * value, right() * value);
125 }
126
128 {
129 setLeft(left() * value);
130 setRight(right() * value);
131
132 return *this;
133 }
134
136 {
137 return SampleFrame(left() * other.left(), right() * other.right());
138 }
139
140 void operator*=(const SampleFrame& other)
141 {
142 left() *= other.left();
143 right() *= other.right();
144 }
145
147 {
148 return left() * left() + right() * right();
149 }
150
152 {
153 return SampleFrame{std::abs(this->left()), std::abs(this->right())};
154 }
155
157 {
158 const auto a = abs();
159 const auto b = other.abs();
160
161 return SampleFrame(std::max(a.left(), b.left()), std::max(a.right(), b.right()));
162 }
163
165 {
166 return (left() + right()) / 2;
167 }
168
169 void clamp(sample_t low, sample_t high)
170 {
171 auto & l = left();
172 l = std::clamp(l, low, high);
173
174 auto & r = right();
175 r = std::clamp(r, low, high);
176 }
177
178 bool containsInf() const
179 {
180 return std::isinf(left()) || std::isinf(right());
181 }
182
183 bool containsNaN() const
184 {
185 return std::isnan(left()) || std::isnan(right());
186 }
187
188private:
189 std::array<sample_t, DEFAULT_CHANNELS> m_samples;
190};
191
192inline void zeroSampleFrames(SampleFrame* buffer, size_t frames)
193{
194 // The equivalent of the following operation which yields compiler warnings
195 // memset(buffer, 0, sizeof(SampleFrame) * frames);
196
197 std::fill(buffer, buffer + frames, SampleFrame());
198}
199
200inline SampleFrame getAbsPeakValues(SampleFrame* buffer, size_t frames)
201{
202 SampleFrame peaks;
203
204 for (f_cnt_t i = 0; i < frames; ++i)
205 {
206 peaks = peaks.absMax(buffer[i]);
207 }
208
209 return peaks;
210}
211
212inline void copyToSampleFrames(SampleFrame* target, const float* source, size_t frames)
213{
214 for (size_t i = 0; i < frames; ++i)
215 {
216 target[i].setLeft(source[2*i]);
217 target[i].setRight(source[2*i + 1]);
218 }
219}
220
221inline void copyFromSampleFrames(float* target, const SampleFrame* source, size_t frames)
222{
223 for (size_t i = 0; i < frames; ++i)
224 {
225 target[2*i] = source[i].left();
226 target[2*i + 1] = source[i].right();
227 }
228}
229
230} // namespace lmms
231
232#endif // LMMS_SAMPLEFRAME_H
uint8_t a
Definition Spc_Cpu.h:141
Definition SampleFrame.h:41
void clamp(sample_t low, sample_t high)
Definition SampleFrame.h:169
SampleFrame()
Definition SampleFrame.h:43
sample_t & right()
Definition SampleFrame.h:81
const sample_t & operator[](size_t index) const
Definition SampleFrame.h:101
SampleFrame absMax(const SampleFrame &other)
Definition SampleFrame.h:156
sample_t & left()
Definition SampleFrame.h:66
bool containsNaN() const
Definition SampleFrame.h:183
SampleFrame(sample_t value)
Definition SampleFrame.h:47
const sample_t & right() const
Definition SampleFrame.h:86
SampleFrame operator*(const SampleFrame &other) const
Definition SampleFrame.h:135
sample_t * data()
Definition SampleFrame.h:56
SampleFrame operator+(const SampleFrame &other) const
Definition SampleFrame.h:106
SampleFrame & operator*=(float value)
Definition SampleFrame.h:127
const sample_t * data() const
Definition SampleFrame.h:61
std::array< sample_t, DEFAULT_CHANNELS > m_samples
Definition SampleFrame.h:189
sample_t sumOfSquaredAmplitudes() const
Definition SampleFrame.h:146
const sample_t & left() const
Definition SampleFrame.h:71
sample_t & operator[](size_t index)
Definition SampleFrame.h:96
SampleFrame(sample_t left, sample_t right)
Definition SampleFrame.h:51
void setLeft(const sample_t &value)
Definition SampleFrame.h:76
void operator*=(const SampleFrame &other)
Definition SampleFrame.h:140
sample_t average() const
Definition SampleFrame.h:164
SampleFrame abs() const
Definition SampleFrame.h:151
void setRight(const sample_t &value)
Definition SampleFrame.h:91
SampleFrame & operator+=(const SampleFrame &other)
Definition SampleFrame.h:111
SampleFrame operator*(float value) const
Definition SampleFrame.h:122
bool containsInf() const
Definition SampleFrame.h:178
int * l
Definition inflate.c:1579
register unsigned i
Definition inflate.c:1575
static PuglViewHint int value
Definition pugl.h:1708
Definition AudioAlsa.cpp:35
void copyFromSampleFrames(float *target, const SampleFrame *source, size_t frames)
Definition SampleFrame.h:221
void copyToSampleFrames(SampleFrame *target, const float *source, size_t frames)
Definition SampleFrame.h:212
float sample_t
Definition LmmsTypes.h:39
std::uint64_t f_cnt_t
Definition LmmsTypes.h:43
void zeroSampleFrames(SampleFrame *buffer, size_t frames)
Definition SampleFrame.h:192
SampleFrame getAbsPeakValues(SampleFrame *buffer, size_t frames)
Definition SampleFrame.h:200
int r
Definition crypt.c:458
b
Definition crypt.c:628