Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
buffer_ops.hpp
Go to the documentation of this file.
1#pragma once
2
23
24#include <pulp/audio/buffer.hpp>
25
26#include <cstddef>
27#include <span>
28#include <type_traits>
29
31
45template <typename Fn>
46inline void map_samples(const float* in, float* out, std::size_t n, Fn&& fn) {
47 if (in == out) {
48 for (std::size_t i = 0; i < n; ++i)
49 out[i] = fn(out[i]);
50 } else {
51 for (std::size_t i = 0; i < n; ++i)
52 out[i] = fn(in[i]);
53 }
54}
55
75template <std::size_t... Counts, typename Fn>
76inline void with_channel_count(std::size_t channels, Fn&& fn) {
77 const bool matched =
78 ((channels == Counts
79 ? (static_cast<void>(fn(std::integral_constant<std::size_t, Counts>{})), true)
80 : false) ||
81 ...);
82 if (!matched)
83 fn(channels);
84}
85
87void apply_gain(std::span<float> samples, float gain);
88
90void apply_gain(BufferView<float> buffer, float gain);
91
97void apply_gain_ramp(std::span<float> samples, float start_gain, float end_gain);
98
100void apply_gain_ramp(BufferView<float> buffer, float start_gain, float end_gain);
101
105void clip(std::span<float> samples, float lo, float hi);
106
108void clip(BufferView<float> buffer, float lo, float hi);
109
113struct MinMax {
114 float min = 0.0f;
115 float max = 0.0f;
116};
117
120MinMax find_min_max(std::span<const float> samples);
121
123MinMax find_min_max(const BufferView<float>& buffer, std::size_t channel);
124MinMax find_min_max(const BufferView<const float>& buffer, std::size_t channel);
125
133float find_magnitude(std::span<const float> samples);
134
137float find_magnitude(const BufferView<float>& buffer, std::size_t channel);
138float find_magnitude(const BufferView<const float>& buffer, std::size_t channel);
139
143struct PeakRms {
144 float peak = 0.0f;
145 float rms = 0.0f;
146};
147
157inline constexpr std::size_t kPeakRmsLanes = 16;
158
170PeakRms measure_peak_rms(std::span<const float> samples) noexcept;
171
173PeakRms measure_peak_rms(const BufferView<float>& buffer, std::size_t channel) noexcept;
174PeakRms measure_peak_rms(const BufferView<const float>& buffer, std::size_t channel) noexcept;
175
176} // namespace pulp::audio::buffer_ops
Non-owning view over multi-channel audio sample data.
Definition buffer.hpp:30
Definition buffer_ops.hpp:30
void apply_gain(std::span< float > samples, float gain)
Apply a constant gain to a single channel of samples in place.
void with_channel_count(std::size_t channels, Fn &&fn)
Call fn once with the channel count, as a compile-time constant when it is one of Counts....
Definition buffer_ops.hpp:76
float find_magnitude(std::span< const float > samples)
Find the peak absolute value (magnitude) in a span.
MinMax find_min_max(std::span< const float > samples)
Find the min and max sample values in a span.
void clip(std::span< float > samples, float lo, float hi)
Hard-clip every sample to [lo, hi] in place.
constexpr std::size_t kPeakRmsLanes
Number of independent partial sums measure_peak_rms keeps.
Definition buffer_ops.hpp:157
PeakRms measure_peak_rms(std::span< const float > samples) noexcept
Measure the peak absolute value and RMS of a block in one pass.
void apply_gain_ramp(std::span< float > samples, float start_gain, float end_gain)
Apply a linear gain ramp from start_gain to end_gain across the span.
void map_samples(const float *in, float *out, std::size_t n, Fn &&fn)
Write out[i] = fn(in[i]) for every sample, in index order.
Definition buffer_ops.hpp:46
Pair returned by find_min_max.
Definition buffer_ops.hpp:113
float max
Definition buffer_ops.hpp:115
float min
Definition buffer_ops.hpp:114
Block level reading returned by measure_peak_rms: the peak absolute sample value and the root-mean-sq...
Definition buffer_ops.hpp:143
float peak
Definition buffer_ops.hpp:144
float rms
Definition buffer_ops.hpp:145