Sound Byte Libs 0.5.1-121-g3358a44
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
pan.hpp
Go to the documentation of this file.
1// sbl/dsp/comp/pan.hpp — Stereo pan composition with smoothing
2//
3// Places a mono signal in the stereo field using constant-power panning.
4// Smoothed per-sample to avoid clicks/zipper noise on position changes.
5//
6// Domain: Stereo placement — maps a mono signal to a position in the L/R field.
7// See docs/design-philosophy.md for the domain manipulation framework.
8//
9// Usage:
10// sbl::dsp::comp::Pan pan;
11// pan.set_position_su(0.75f); // 75% right
12// pan.process(mono_in, left_out, right_out, n); // mono → stereo
13//
14// // Or scale an existing stereo pair in-place:
15// pan.process(left_buf, right_buf, n);
16
17#ifndef SBL_DSP_COMP_PAN_HPP_
18#define SBL_DSP_COMP_PAN_HPP_
19
20#include <cstdint>
24
25namespace sbl::dsp::comp {
26
27class Pan {
28public:
29 /// @note All public methods are ISR-safe — bounded computation, no I/O.
30
31 Pan() {
33 smoother_.reset(0.5f);
34 }
35
36 /**
37 * @brief Set pan position. Port — accepts normalized [0,1] signal.
38 *
39 * 0.0 = hard left, 0.5 = center, 1.0 = hard right.
40 * Input is clamped to [0,1]. Smoothing is applied per-sample in process().
41 *
42 * @param su Pan position in normalized units
43 */
44 void set_position_su(float su) {
45 if (su < 0.0f) su = 0.0f;
46 if (su > 1.0f) su = 1.0f;
47 target_ = su;
48 }
49
50 /**
51 * @brief Apply panning to a mono input, writing to stereo output buffers.
52 *
53 * Output buffers are overwritten (= semantics, not +=).
54 * For accumulating multiple sources, use sbl::dsp::stereo::pan_mix().
55 *
56 * @param in Mono input buffer
57 * @param out_l Left output buffer (overwritten)
58 * @param out_r Right output buffer (overwritten)
59 * @param n Number of samples
60 */
61 void process(const float* in, float* out_l, float* out_r, uint16_t n) {
62 for (uint16_t i = 0; i < n; ++i) {
63 float pos = smoother_.process(target_);
64 auto gain = sbl::dsp::stereo::constant_power(pos);
65 out_l[i] = in[i] * gain.left;
66 out_r[i] = in[i] * gain.right;
67 }
68 }
69
70 /**
71 * @brief Apply panning in-place to a stereo pair (scales existing L/R).
72 *
73 * Multiplies existing left samples by the left pan gain and right samples
74 * by the right pan gain. Useful for adjusting stereo balance of an
75 * already-stereo signal.
76 *
77 * @param buf_l Left buffer (modified in-place)
78 * @param buf_r Right buffer (modified in-place)
79 * @param n Number of samples
80 */
81 void process(float* buf_l, float* buf_r, uint16_t n) {
82 for (uint16_t i = 0; i < n; ++i) {
83 float pos = smoother_.process(target_);
84 auto gain = sbl::dsp::stereo::constant_power(pos);
85 buf_l[i] *= gain.left;
86 buf_r[i] *= gain.right;
87 }
88 }
89
90 /**
91 * @brief Current smoothed position [0,1]
92 * @return The smoother's current value, not the target
93 */
94 float position() const { return smoother_.value(); }
95
96 /** @brief Reset smoother to center (0.5) */
97 void reset() {
98 target_ = 0.5f;
99 smoother_.reset(0.5f);
100 }
101
102 /** @brief Reset smoother to a specific position */
103 void reset(float position) {
104 if (position < 0.0f) position = 0.0f;
105 if (position > 1.0f) position = 1.0f;
106 target_ = position;
107 smoother_.reset(position);
108 }
109
110private:
111 float target_ = 0.5f;
113};
114
115} // namespace sbl::dsp::comp
116
117#endif // SBL_DSP_COMP_PAN_HPP_
void process(float *buf_l, float *buf_r, uint16_t n)
Apply panning in-place to a stereo pair (scales existing L/R).
Definition pan.hpp:81
void set_position_su(float su)
Set pan position. Port — accepts normalized [0,1] signal.
Definition pan.hpp:44
void process(const float *in, float *out_l, float *out_r, uint16_t n)
Apply panning to a mono input, writing to stereo output buffers.
Definition pan.hpp:61
void reset(float position)
Reset smoother to a specific position.
Definition pan.hpp:103
float position() const
Current smoothed position [0,1].
Definition pan.hpp:94
void reset()
Reset smoother to center (0.5)
Definition pan.hpp:97
void set_time_ms(float ms, float rate_hz)
Compute coefficient from settling time in milliseconds.
float process(float target)
Process one sample toward target.
float value() const
Current smoothed value.
Fixed-point constants and audio sample types.
Compositions: subcircuits of primitives.
PanGain constant_power(float position)
Compute constant-power pan gains (-3 dB at center)
Definition stereo.hpp:54
float SAMPLE_RATE_F
Definition fixed.hpp:17
One-pole parameter smoother.
Stereo panning and mixing (Audio Stack — Atoms)