Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
usb_audio.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 <stdbool.h>
21#include <stdint.h>
22
23// USB audio (UAC1) as a peripheral: a stereo stream from the host, and a stereo stream to it, each 44.1kHz with 24-bit
24// samples. The packets go between memory and the USB FIFOs by DMA (channels 0 and 1); the CPU only kicks off each
25// transfer, from the USB and DMA interrupts.
26//
27// Both endpoints are "asynchronous" - our sample clock sets the rate. To the host, we send 44 or 45 frames per USB
28// frame to match how fast we're producing them. Our in endpoint is marked as implicit feedback for the out one, so the
29// host sends us as many frames as we sent it, and they're copied straight through with no resampling. Hosts that don't
30// do implicit feedback send at their own clock's rate, which we cope with by dropping or repeating the odd frame.
31// Audio from the host is laid out in a ring buffer exactly like the SSI RX buffer, so it can be used anywhere the
32// line/mic input is. The host gets a volume and mute control for it (a feature unit), which we apply as it goes into
33// that ring - at full volume, the samples are untouched.
34//
35// Multitrack is an optional configuration in place of stereo, chosen at power-on: 8 channels of 16-bit to the host (the
36// usual stereo output, then three stereo track buses), and 4 of 16-bit from it (two stereo inputs). Its rings are
37// allocated then, and only if it's chosen, as 16-bit frames in exactly the layout of the USB packets - so the DMA moves
38// packets straight between them and the FIFOs, with no copying in the interrupts, and NEON converts to and from the
39// Deluge's q31 samples in the audio routine. With it off, it costs nothing beyond a few never-taken branches.
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
49uint8_t usbAudioStartPeripheral(void);
51bool usbAudioActive(void);
52
53// Hooks for the Renesas USB peripheral driver. These may be called from the USB interrupt or the USB task.
54
58void usbAudioSetInterface(uint16_t interfaceNumber, uint16_t alternateSetting);
60void usbAudioStopAll(void);
63void usbAudioBusReset(void);
66uint16_t usbAudioHandleBrdy(uint16_t pipeBits);
69void usbAudioServiceTask(void);
72bool usbAudioClassRequest(uint16_t type, uint16_t value, uint16_t index, uint16_t length);
74void usbAudioClassRequestWriteDone(uint16_t type, uint16_t value, uint16_t index);
75
76#ifdef __cplusplus
77}
78
79#include "dsp/stereo_sample.h"
80#include <cstddef>
81#include <span>
82
83namespace deluge::usb_audio {
84
86bool receivingFromHost();
88bool sendingToHost();
89
94void fillInputRing(uint32_t rxPosition, size_t numSamples, size_t txQueued);
95
97constexpr uint8_t kMaxInputs = 2;
99uint8_t numInputs();
102uint8_t resolveInput(uint8_t input);
103
106int32_t* getInputRingStart(uint8_t input = 0);
107int32_t* getInputRingEnd(uint8_t input = 0);
109int32_t* toInputRing(int32_t const* rxPosition, uint8_t input = 0);
110
114void sendToHost(std::span<const StereoSample> frames, int32_t const* txPosition, size_t renderOffset);
115
118extern bool directMonitoring;
119
121enum class OutputSource : uint8_t {
122 MAIN_MIX, // The Deluge's final output
123 LINE_IN, // The codec's line/mic input, as it arrives
124};
125constexpr uint8_t kNumOutputSources = 2;
126extern OutputSource outputSource;
127
129enum class Mode : uint8_t {
130 STEREO = 0, // Stereo both ways, 24-bit
131 OFF = 1, // USB MIDI only
132 MULTITRACK = 2,
133};
134constexpr uint8_t kNumModes = 3;
135
137bool multitrackActive();
138
139/*
140 * Multitrack's track buses. Channels 1/2 to the host are the usual output (outputSource); 3/4, 5/6 and 7/8 are buses 1
141 * to 3, which tracks can be routed to (Output::getUSBBus()). A routed track is rendered into its bus instead of the
142 * main mix, at the same level - before the song's FX and volume. Its reverb send still goes to the Deluge's reverb, in
143 * the main mix.
144 */
145constexpr uint8_t kNumTrackBuses = 3;
146
149extern bool routingToTrackBuses;
150
152void startRender(bool routeTracks);
153
156std::span<StereoSample> trackBus(uint8_t bus, size_t numSamples);
157
158} // namespace deluge::usb_audio
159#endif