Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
stereo_pan.h
1/*
2 * Copyright © 2026 Mark Adams
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include "util/fixedpoint.h"
21#include <argon.hpp>
22#include <array>
23#include <cstdint>
24
25namespace deluge::dsp {
26
27// shouldDoPanning()'s full-level amplitude. Every pan has it on at least one side (both, when centred).
28constexpr int32_t kPanUnityAmplitude = 1073741823;
29
30namespace detail {
31// One channel of the pan, (sample * amplitude) >> 30, or the sample itself on a unity side.
32// MultiplyFixedQMax (vqdmulh) gives floor(2xa / 2^32), which only saturates when x == a == INT32_MIN, and halving that
33// again rounds down to floor(xa / 2^32), so with amplitude >= 0 (pan amplitudes are in [0, 2^30]) this is exactly
34// (multiply_32x32_rshift32(x, amplitude) << 2). Adding the sample directly on a unity side differs from that by
35// -1..+5 LSB (about -172 dBFS), and saves three of the four vector operations for the channel.
36template <bool unity>
37[[gnu::always_inline]] inline Argon<int32_t> panChannel(Argon<int32_t> x, Argon<int32_t> amplitude) {
38 if constexpr (unity) {
39 return x;
40 }
41 else {
42 return x.MultiplyFixedQMax(amplitude).ShiftRight<1>().ShiftLeft<2>();
43 }
44}
45template <bool unity>
46[[gnu::always_inline]] inline int32_t panChannel(int32_t x, int32_t amplitude) {
47 if constexpr (unity) {
48 return x;
49 }
50 else {
51 return multiply_32x32_rshift32(x, amplitude) << 2;
52 }
53}
54
55template <bool unityL, bool unityR>
56[[gnu::always_inline]] inline void addPannedMonoToStereo(int32_t* __restrict__ stereo, const int32_t* __restrict__ mono,
57 int32_t numSamples, int32_t amplitudeL, int32_t amplitudeR) {
58 constexpr int32_t kLanes = Argon<int32_t>::lanes;
59 // Broadcast once: with a scalar operand GCC re-inserts the amplitude into a lane on every iteration
60 Argon<int32_t> const ampL{amplitudeL};
61 Argon<int32_t> const ampR{amplitudeR};
62 int32_t i = 0;
63 for (; i + kLanes <= numSamples; i += kLanes) {
64 auto in = Argon<int32_t>::Load(mono + i);
65 auto [l, r] = Argon<int32_t>::LoadInterleaved<2>(stereo + 2 * i);
66 argon::store_interleaved<2>(stereo + 2 * i, std::array<Argon<int32_t>, 2>{
67 l + panChannel<unityL>(in, ampL),
68 r + panChannel<unityR>(in, ampR),
69 });
70 }
71 for (; i < numSamples; i++) {
72 stereo[2 * i] += panChannel<unityL>(mono[i], amplitudeL);
73 stereo[2 * i + 1] += panChannel<unityR>(mono[i], amplitudeR);
74 }
75}
76
77template <bool unityL, bool unityR>
78[[gnu::always_inline]] inline void addPannedStereoToStereo(int32_t* __restrict__ stereo, const int32_t* __restrict__ in,
79 int32_t numSamples, int32_t amplitudeL, int32_t amplitudeR) {
80 constexpr int32_t kLanes = Argon<int32_t>::lanes;
81 // Broadcast once: with a scalar operand GCC re-inserts the amplitude into a lane on every iteration
82 Argon<int32_t> const ampL{amplitudeL};
83 Argon<int32_t> const ampR{amplitudeR};
84 int32_t i = 0;
85 for (; i + kLanes <= numSamples; i += kLanes) {
86 auto [inL, inR] = Argon<int32_t>::LoadInterleaved<2>(in + 2 * i);
87 auto [l, r] = Argon<int32_t>::LoadInterleaved<2>(stereo + 2 * i);
88 argon::store_interleaved<2>(stereo + 2 * i, std::array<Argon<int32_t>, 2>{
89 l + panChannel<unityL>(inL, ampL),
90 r + panChannel<unityR>(inR, ampR),
91 });
92 }
93 for (; i < numSamples; i++) {
94 stereo[2 * i] += panChannel<unityL>(in[2 * i], amplitudeL);
95 stereo[2 * i + 1] += panChannel<unityR>(in[2 * i + 1], amplitudeR);
96 }
97}
98} // namespace detail
99
100// stereo[2i] += mono[i] * amplitudeL, stereo[2i + 1] += mono[i] * amplitudeR (amplitudes are Q2.30), for i <
101// numSamples. StereoSample::addPannedMono() per sample, except that a kPanUnityAmplitude side adds the sample directly
102// (see panChannel()). Reads and writes exactly numSamples frames. Out of line: it runs once per unison part, and
103// inlining its call sites cost more in I-cache misses than the call.
104[[gnu::noinline]] inline void addPannedMonoToStereo(int32_t* __restrict__ stereo, const int32_t* __restrict__ mono,
105 int32_t numSamples, int32_t amplitudeL, int32_t amplitudeR) {
106 bool unityL = amplitudeL == kPanUnityAmplitude;
107 bool unityR = amplitudeR == kPanUnityAmplitude;
108 if (unityL && unityR) {
109 detail::addPannedMonoToStereo<true, true>(stereo, mono, numSamples, amplitudeL, amplitudeR);
110 }
111 else if (unityL) {
112 detail::addPannedMonoToStereo<true, false>(stereo, mono, numSamples, amplitudeL, amplitudeR);
113 }
114 else if (unityR) {
115 detail::addPannedMonoToStereo<false, true>(stereo, mono, numSamples, amplitudeL, amplitudeR);
116 }
117 else {
118 detail::addPannedMonoToStereo<false, false>(stereo, mono, numSamples, amplitudeL, amplitudeR);
119 }
120}
121
122// stereo[2i] += in[2i] * amplitudeL, stereo[2i + 1] += in[2i + 1] * amplitudeR (amplitudes are Q2.30), for i <
123// numSamples. StereoSample::addPannedStereo() per frame, except that a kPanUnityAmplitude side adds the sample
124// directly. Reads and writes exactly numSamples frames.
125[[gnu::noinline]] inline void addPannedStereoToStereo(int32_t* __restrict__ stereo, const int32_t* __restrict__ in,
126 int32_t numSamples, int32_t amplitudeL, int32_t amplitudeR) {
127 bool unityL = amplitudeL == kPanUnityAmplitude;
128 bool unityR = amplitudeR == kPanUnityAmplitude;
129 if (unityL && unityR) {
130 detail::addPannedStereoToStereo<true, true>(stereo, in, numSamples, amplitudeL, amplitudeR);
131 }
132 else if (unityL) {
133 detail::addPannedStereoToStereo<true, false>(stereo, in, numSamples, amplitudeL, amplitudeR);
134 }
135 else if (unityR) {
136 detail::addPannedStereoToStereo<false, true>(stereo, in, numSamples, amplitudeL, amplitudeR);
137 }
138 else {
139 detail::addPannedStereoToStereo<false, false>(stereo, in, numSamples, amplitudeL, amplitudeR);
140 }
141}
142
143} // namespace deluge::dsp