lmms
Cross-platform music production software
Loading...
Searching...
No Matches
AudioBuffer.h
Go to the documentation of this file.
1/*
2 * AudioBuffer.h
3 *
4 * Copyright (c) 2026 Dalton Messmer <messmer.dalton/at/gmail.com>
5 *
6 * This file is part of LMMS - https://lmms.io
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program (see COPYING); if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301 USA.
22 *
23 */
24
25#ifndef LMMS_AUDIO_BUFFER_H
26#define LMMS_AUDIO_BUFFER_H
27
28#include <bitset>
29#include <memory_resource>
30#include <vector>
31
32#include "AudioBufferView.h"
33#include "ArrayVector.h"
34#include "LmmsTypes.h"
35#include "lmms_constants.h"
36#include "lmms_export.h"
37
38namespace lmms
39{
40
79class LMMS_EXPORT AudioBuffer
80{
81public:
82 using ChannelFlags = std::bitset<MaxChannelsPerAudioBuffer>;
83
86 {
87 public:
88 ChannelGroup() = default;
93
94 auto buffers() const -> const float* const* { return m_buffers; }
95 auto buffers() -> float** { return m_buffers; }
96
97 auto buffer(ch_cnt_t channel) const -> const float*
98 {
99 assert(channel < m_channels);
100 return m_buffers[channel];
101 }
102
103 auto buffer(ch_cnt_t channel) -> float*
104 {
105 assert(channel < m_channels);
106 return m_buffers[channel];
107 }
108
109 auto channels() const -> ch_cnt_t { return m_channels; }
110
111 void setBuffers(float** newBuffers) { m_buffers = newBuffers; }
113
114 // TODO: Future additions: Group names, type (main/aux), speaker arrangements (for surround sound), ...
115
116 private:
121 float** m_buffers = nullptr;
122
125 };
126
127 AudioBuffer() = delete;
128
129 AudioBuffer(const AudioBuffer&) = delete;
130 AudioBuffer(AudioBuffer&&) noexcept = default;
131 auto operator=(const AudioBuffer&) -> AudioBuffer& = delete;
132 auto operator=(AudioBuffer&&) noexcept -> AudioBuffer& = default;
133
144 std::pmr::memory_resource* resource = std::pmr::get_default_resource());
145
157 template<class F>
159 std::pmr::memory_resource* resource, F&& groupVisitor)
160 : AudioBuffer{frames, channels, resource}
161 {
162 setGroups(groups, std::forward<F>(groupVisitor));
163 }
164
166 void allocateInterleavedBuffer();
167
168 auto hasInterleavedBuffer() const -> bool { return !m_interleavedBuffer.empty(); }
169
174 static auto allocationSize(f_cnt_t frames, ch_cnt_t channels,
175 bool withInterleavedBuffer = false) -> std::size_t;
176
178 auto groupCount() const -> group_cnt_t { return static_cast<group_cnt_t>(m_groups.size()); }
179
180 auto group(group_cnt_t index) const -> const ChannelGroup& { return m_groups[index]; }
181 auto group(group_cnt_t index) -> ChannelGroup& { return m_groups[index]; }
182
184 auto allBuffers() const -> PlanarBufferView<const float>
185 {
186 return {m_accessBuffer.data(), totalChannels(), m_frames};
187 }
188
191 {
192 return {m_accessBuffer.data(), totalChannels(), m_frames};
193 }
194
197 {
198 assert(index < groupCount());
199 const ChannelGroup& g = m_groups[index];
200 return {g.buffers(), g.channels(), m_frames};
201 }
202
205 {
206 assert(index < groupCount());
207 ChannelGroup& g = m_groups[index];
208 return {g.buffers(), g.channels(), m_frames};
209 }
210
212 auto buffer(ch_cnt_t channel) const -> std::span<const float>
213 {
214 return {m_accessBuffer[channel], m_frames};
215 }
216
218 auto buffer(ch_cnt_t channel) -> std::span<float>
219 {
220 return {m_accessBuffer[channel], m_frames};
221 }
222
224 auto totalChannels() const -> ch_cnt_t { return static_cast<ch_cnt_t>(m_accessBuffer.size()); }
225
227 auto frames() const -> f_cnt_t { return m_frames; }
228
230 auto interleavedBuffer() const -> InterleavedBufferView<const float, 2>
231 {
232 assert(hasInterleavedBuffer());
233 return {m_interleavedBuffer.data(), m_frames};
234 }
235
242
253 auto addGroup(ch_cnt_t channels) -> ChannelGroup*;
254
264 template<class F>
265 void setGroups(group_cnt_t groups, F&& groupVisitor)
266 {
267 static_assert(std::is_invocable_r_v<ch_cnt_t, F, group_cnt_t, ChannelGroup&>,
268 "groupVisitor is passed the group index + group reference and must return the group's channel count");
269
270 m_groups.clear();
271 ch_cnt_t ch = 0;
272 for (group_cnt_t idx = 0; idx < groups; ++idx)
273 {
274 auto& group = m_groups.emplace_back();
275
276 const auto channels = groupVisitor(idx, group);
277 if (channels == 0) { throw std::runtime_error{"group cannot have zero channels"}; }
278
279 group.setBuffers(&m_accessBuffer[ch]);
280 group.setChannels(channels);
281
282 ch += channels;
283 if (ch > this->totalChannels())
284 {
285 throw std::runtime_error{"sum of group channel counts exceeds total channels"};
286 }
287 }
288 }
289
300 auto silenceFlags() const -> const ChannelFlags& { return m_silenceFlags; }
301
303 void assumeNonSilent(ch_cnt_t channel) { m_silenceFlags[channel] = false; }
304
316 void enableSilenceTracking(bool enabled);
317 auto silenceTrackingEnabled() const -> bool { return m_silenceTrackingEnabled; }
318
320 void mixSilenceFlags(const AudioBuffer& other);
321
334 auto hasSignal(const ChannelFlags& channels) const -> bool;
335
337 auto hasAnySignal() const -> bool;
338
350 auto sanitize(const ChannelFlags& channels, ch_cnt_t upperBound = MaxChannelsPerAudioBuffer) -> bool;
351
354 auto sanitizeAll() -> bool;
355
363 auto updateSilenceFlags(const ChannelFlags& channels, ch_cnt_t upperBound = MaxChannelsPerAudioBuffer) -> bool;
364
366 auto updateAllSilenceFlags() -> bool;
367
374 void silenceChannels(const ChannelFlags& channels, ch_cnt_t upperBound = MaxChannelsPerAudioBuffer);
375
377 void silenceAllChannels();
378
380 auto absPeakValue(ch_cnt_t channel) const -> float;
381
382private:
388 std::pmr::vector<float> m_sourceBuffer;
389
395 std::pmr::vector<float*> m_accessBuffer;
396
402 std::pmr::vector<float> m_interleavedBuffer;
403
406
409
422
424};
425
426} // namespace lmms
427
428#endif // LMMS_AUDIO_BUFFER_H
Definition ArrayVector.h:51
Non-owning collection of audio channels + metadata.
Definition AudioBuffer.h:86
auto buffer(ch_cnt_t channel) -> float *
Definition AudioBuffer.h:103
auto buffer(ch_cnt_t channel) const -> const float *
Definition AudioBuffer.h:97
ch_cnt_t m_channels
Number of channels in m_buffers - currently only 2 is used.
Definition AudioBuffer.h:124
void setBuffers(float **newBuffers)
Definition AudioBuffer.h:111
void setChannels(ch_cnt_t channels)
Definition AudioBuffer.h:112
float ** m_buffers
Definition AudioBuffer.h:121
ChannelGroup(float **buffers, ch_cnt_t channels)
Definition AudioBuffer.h:89
auto buffers() const -> const float *const *
Definition AudioBuffer.h:94
auto channels() const -> ch_cnt_t
Definition AudioBuffer.h:109
auto buffers() -> float **
Definition AudioBuffer.h:95
Definition AudioBuffer.h:80
ArrayVector< ChannelGroup, MaxGroupsPerAudioBuffer > m_groups
Divides channels into arbitrary groups.
Definition AudioBuffer.h:405
auto silenceTrackingEnabled() const -> bool
Definition AudioBuffer.h:317
auto allBuffers() const -> PlanarBufferView< const float >
Definition AudioBuffer.h:184
f_cnt_t m_frames
Frame count for every channel buffer.
Definition AudioBuffer.h:408
auto groupBuffers(group_cnt_t index) const -> PlanarBufferView< const float >
Definition AudioBuffer.h:196
std::pmr::vector< float > m_interleavedBuffer
Definition AudioBuffer.h:402
auto groupBuffers(group_cnt_t index) -> PlanarBufferView< float >
Definition AudioBuffer.h:204
auto group(group_cnt_t index) const -> const ChannelGroup &
Definition AudioBuffer.h:180
auto buffer(ch_cnt_t channel) const -> std::span< const float >
Definition AudioBuffer.h:212
auto allBuffers() -> PlanarBufferView< float >
Definition AudioBuffer.h:190
auto interleavedBuffer() const -> InterleavedBufferView< const float, 2 >
Definition AudioBuffer.h:230
auto hasInterleavedBuffer() const -> bool
Definition AudioBuffer.h:168
void assumeNonSilent(ch_cnt_t channel)
Forcibly pessimizes silence tracking for a specific channel.
Definition AudioBuffer.h:303
auto groupCount() const -> group_cnt_t
Definition AudioBuffer.h:178
auto group(group_cnt_t index) -> ChannelGroup &
Definition AudioBuffer.h:181
auto frames() const -> f_cnt_t
Definition AudioBuffer.h:227
bool m_silenceTrackingEnabled
Definition AudioBuffer.h:423
std::bitset< MaxChannelsPerAudioBuffer > ChannelFlags
Definition AudioBuffer.h:82
std::pmr::vector< float > m_sourceBuffer
Definition AudioBuffer.h:388
AudioBuffer(const AudioBuffer &)=delete
AudioBuffer(AudioBuffer &&) noexcept=default
ChannelFlags m_silenceFlags
Definition AudioBuffer.h:421
auto silenceFlags() const -> const ChannelFlags &
Definition AudioBuffer.h:300
void setGroups(group_cnt_t groups, F &&groupVisitor)
Changes the channel grouping without changing the channel count. Does not reallocate any buffers.
Definition AudioBuffer.h:265
auto interleavedBuffer() -> InterleavedBufferView< float, 2 >
Definition AudioBuffer.h:237
std::pmr::vector< float * > m_accessBuffer
Definition AudioBuffer.h:395
auto buffer(ch_cnt_t channel) -> std::span< float >
Definition AudioBuffer.h:218
AudioBuffer()=delete
auto totalChannels() const -> ch_cnt_t
Definition AudioBuffer.h:224
Definition AudioBufferView.h:291
Definition AudioBufferView.h:481
Definition RemotePluginBase.cpp:34
std::uint8_t group_cnt_t
Definition LmmsTypes.h:48
std::uint16_t ch_cnt_t
Definition LmmsTypes.h:44
constexpr auto DEFAULT_CHANNELS
Definition lmms_constants.h:38
constexpr auto MaxGroupsPerAudioBuffer
Definition lmms_constants.h:40
std::uint64_t f_cnt_t
Definition LmmsTypes.h:43
constexpr auto MaxChannelsPerAudioBuffer
Definition lmms_constants.h:39