Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
usb_audio_pcm.h
1/*
2 * Copyright © 2026 Synthstrom Audible Limited
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 <algorithm>
21#include <argon.hpp>
22#include <array>
23#include <cstddef>
24#include <cstdint>
25
26// Converting between the Deluge's samples and multitrack USB audio's (see r_usb_paudio_config.h), which are 16-bit and
27// little-endian.
28//
29// To the host, a frame is 8 channels: four stereo pairs, each from its own buffer of interleaved stereo q31 samples
30// (StereoSamples). A pair narrowed to 16-bit is 32 bits - one lane - so a 4-lane interleaving store (vst4.32) puts
31// four sources' pairs side by side, in channel order: one instruction for every 4 frames.
32//
33// From the host, a frame is 4 channels: two stereo pairs, each going to its own buffer of interleaved stereo q31. A
34// 2-lane deinterleaving load (vld2.32) splits the pairs apart, already interleaved L/R as the Deluge's are, and then
35// each sample only needs widening.
36namespace deluge::usb_audio::pcm {
37
38constexpr size_t kToHostSources = 4;
39constexpr size_t kToHostBytesPerFrame = kToHostSources * 2 * sizeof(int16_t);
40constexpr size_t kFromHostDestinations = 2;
41constexpr size_t kFromHostBytesPerFrame = kFromHostDestinations * 2 * sizeof(int16_t);
42constexpr size_t kFramesPerVector = 4; // A q register of interleaved stereo q31 holds 2 frames; we take two at a time
43
47constexpr int kMainShift = 16;
48constexpr int kTrackBusShift = 16 - 5;
49
51template <int shift>
52[[gnu::always_inline]] inline int16_t narrowSample(int32_t sample) {
53 int64_t const rounded = (static_cast<int64_t>(sample) + (int64_t{1} << (shift - 1))) >> shift;
54 return static_cast<int16_t>(std::clamp<int64_t>(rounded, INT16_MIN, INT16_MAX));
55}
56
58template <int shift>
59[[gnu::always_inline]] inline Argon<uint32_t> narrowFrames(int32_t const* source) {
60 auto const first = Argon<int32_t>::Load(source).ShiftRightRoundSaturateNarrow<shift>();
61 auto const second = Argon<int32_t>::Load(source + 4).ShiftRightRoundSaturateNarrow<shift>();
62 return first.CombineWith(second).template As<uint32_t>();
63}
64
68[[gnu::always_inline]] inline void packToHost(uint32_t* __restrict__ out, int32_t const* __restrict__ main,
69 std::array<int32_t const*, kToHostSources - 1> buses, size_t numFrames) {
70 // A silent bus reads the same 4 frames of zeros over and over, rather than branching in the loop. Named variables
71 // rather than arrays, which GCC keeps on the stack
72 alignas(16) static constexpr int32_t kSilence[kFramesPerVector * 2] = {};
73 int32_t const* bus0 = (buses[0] != nullptr) ? buses[0] : kSilence;
74 int32_t const* bus1 = (buses[1] != nullptr) ? buses[1] : kSilence;
75 int32_t const* bus2 = (buses[2] != nullptr) ? buses[2] : kSilence;
76 size_t const step0 = (buses[0] != nullptr) ? kFramesPerVector * 2 : 0;
77 size_t const step1 = (buses[1] != nullptr) ? kFramesPerVector * 2 : 0;
78 size_t const step2 = (buses[2] != nullptr) ? kFramesPerVector * 2 : 0;
79
80 for (; numFrames >= kFramesPerVector; numFrames -= kFramesPerVector) {
81 argon::store_interleaved<kToHostSources>(
82 out, std::array<Argon<uint32_t>, kToHostSources>{
83 narrowFrames<kMainShift>(main), narrowFrames<kTrackBusShift>(bus0),
84 narrowFrames<kTrackBusShift>(bus1), narrowFrames<kTrackBusShift>(bus2)});
85 out += kFramesPerVector * kToHostSources;
86 main += kFramesPerVector * 2;
87 bus0 += step0;
88 bus1 += step1;
89 bus2 += step2;
90 }
91
92 // The last few frames
93 auto* outSamples = reinterpret_cast<int16_t*>(out);
94 std::array<int32_t const*, kToHostSources - 1> const source{bus0, bus1, bus2};
95 std::array<size_t, kToHostSources - 1> const step{step0, step1, step2};
96 for (size_t frame = 0; frame < numFrames; frame++) {
97 *outSamples++ = narrowSample<kMainShift>(main[frame * 2]);
98 *outSamples++ = narrowSample<kMainShift>(main[frame * 2 + 1]);
99 for (size_t i = 0; i < source.size(); i++) {
100 size_t const offset = step[i] ? frame * 2 : 0;
101 *outSamples++ = narrowSample<kTrackBusShift>(source[i][offset]);
102 *outSamples++ = narrowSample<kTrackBusShift>(source[i][offset + 1]);
103 }
104 }
105}
106
108[[gnu::always_inline]] inline void widenFrames(int32_t* out, Argon<int16_t> samples) {
109 samples.GetLow().ShiftLeftLong<16>().StoreTo(out);
110 samples.GetHigh().ShiftLeftLong<16>().StoreTo(out + 4);
111}
112
113[[gnu::always_inline]] inline int32_t widenSample(int16_t sample) {
114 return static_cast<int32_t>(static_cast<uint32_t>(static_cast<uint16_t>(sample)) << 16);
115}
116
119[[gnu::always_inline]] inline void unpackFromHost(int32_t* __restrict__ first, int32_t* __restrict__ second,
120 int16_t const* __restrict__ in, size_t numFrames) {
121 for (; numFrames >= kFramesPerVector; numFrames -= kFramesPerVector) {
122 // Each frame's pairs are a 32-bit lane apiece, so deinterleaving by lane separates them
123 auto const pairs =
124 Argon<uint32_t>::LoadInterleaved<kFromHostDestinations>(reinterpret_cast<uint32_t const*>(in));
125 widenFrames(first, pairs[0].As<int16_t>());
126 widenFrames(second, pairs[1].As<int16_t>());
127 in += kFramesPerVector * kFromHostDestinations * 2;
128 first += kFramesPerVector * 2;
129 second += kFramesPerVector * 2;
130 }
131 for (size_t frame = 0; frame < numFrames; frame++) {
132 first[frame * 2] = widenSample(in[frame * 4]);
133 first[frame * 2 + 1] = widenSample(in[frame * 4 + 1]);
134 second[frame * 2] = widenSample(in[frame * 4 + 2]);
135 second[frame * 2 + 1] = widenSample(in[frame * 4 + 3]);
136 }
137}
138
139} // namespace deluge::usb_audio::pcm