Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
r_usb_paudio_config.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// USB audio (UAC1) as a peripheral: one stream each way, alongside USB MIDI - stereo both ways, or multitrack (below).
21// "Out" and "in" are from the host's point of view, as in the USB spec - so the out stream is audio coming into the
22// Deluge from the computer, and the in stream is audio the Deluge sends to the computer.
23
24// Only pipes 1 and 2 can do isochronous transfers
25#define USB_CFG_PAUDIO_ISO_OUT (USB_PIPE1)
26#define USB_CFG_PAUDIO_ISO_IN (USB_PIPE2)
27
28#define USB_PAUDIO_EP_OUT 3 // Endpoint numbers. MIDI has 1 (in) and 2 (out)
29#define USB_PAUDIO_EP_IN 4
30
31#define USB_PAUDIO_INTERFACE_CONTROL 0
32#define USB_PAUDIO_INTERFACE_STREAM_OUT 1
33#define USB_PAUDIO_INTERFACE_STREAM_IN 2
34#define USB_PMIDI_INTERFACE 3
35#define USB_PAUDIO_NUM_INTERFACES 4
36
37// Unit ID of the feature unit (volume and mute) on the stream from the host. Terminal IDs 1-4 are taken
38#define USB_PAUDIO_FEATURE_UNIT_FROM_HOST 5
39
40#define USB_PAUDIO_SAMPLE_RATE 44100
41#define USB_PAUDIO_NUM_CHANNELS 2
42// 24-bit samples, each in a 4-byte subframe and MSB-aligned - which makes them the same as the Deluge's q31 samples,
43// and keeps every packet a whole number of 32-bit words for the FIFO DMA
44#define USB_PAUDIO_SUBFRAME_BYTES 4
45#define USB_PAUDIO_BIT_RESOLUTION 24
46#define USB_PAUDIO_BYTES_PER_FRAME (USB_PAUDIO_NUM_CHANNELS * USB_PAUDIO_SUBFRAME_BYTES)
47// 44.1 frames per 1ms frame on average. We never send more than 46 (see framesForNextPacket()), and allow the host up
48// to 48
49#define USB_PAUDIO_MAX_FRAMES_PER_PACKET 48
50#define USB_PAUDIO_MAX_PACKET_BYTES (USB_PAUDIO_MAX_FRAMES_PER_PACKET * USB_PAUDIO_BYTES_PER_FRAME)
51// The out stream's adaptive alternate setting (for hosts without implicit feedback, like Windows) gets a slightly
52// smaller max packet size - still plenty for a host sending 44 or 45 frames at its own rate. Hosts choosing between
53// alternate settings of the same format by bandwidth then prefer it, if they can't use the asynchronous one
54#define USB_PAUDIO_ADAPTIVE_MAX_FRAMES_PER_PACKET 46
55#define USB_PAUDIO_ADAPTIVE_MAX_PACKET_BYTES (USB_PAUDIO_ADAPTIVE_MAX_FRAMES_PER_PACKET * USB_PAUDIO_BYTES_PER_FRAME)
56
57/* Multitrack: an optional configuration (SETTINGS > USB AUDIO > MODE), in place of the stereo one. The Deluge sends the
58 * host 8 channels - its main mix, and three stereo buses that tracks can be routed to - and the host sends it 4: two
59 * stereo inputs, the first working just as the stereo configuration's does. Same endpoints, pipes and interfaces, so
60 * all the code is shared.
61 *
62 * Its size is set by full speed's isochronous bandwidth. The host reserves time for each isochronous endpoint at its
63 * max packet size, including worst-case bit stuffing (USB 2.0 5.11.3: 7268ns + 83.54ns x floor(3.167 + 7 x 8 x bytes /
64 * 6), plus about 1us of host delay), and only 900us of each 1ms frame can be reserved like that. Nor can a full speed
65 * isochronous packet be over 1023 bytes. 8 channels of 45 frames in 4-byte subframes, like the stereo configuration's,
66 * would be 1440 bytes, and packed 24-bit 1080 - neither's possible. So samples are 16-bit:
67 * - To the host, 8 channels x 2 bytes x at most 45 frames: 720 bytes, 570us. 44.1kHz needs packets of 44 or 45, so
68 * framesForNextPacket() never sends more than 45, only speeding up on what would have been 44-frame packets
69 * - From the host, 4 channels x 2 bytes x 46 frames: 368 bytes, 294us (360 bytes, 288us, for the adaptive setting).
70 * A host following our clock through implicit feedback sends what we sent it, so 45 at most, and one sending at its
71 * own rate 44 or 45 - the 46th frame is only slack
72 * - 864us together, inside the 900us but with only enough left over for something small, like a keyboard's interrupt
73 * endpoint, sharing the bus (or a hub's transaction translator). 6 channels to the host would leave 165us
74 * 8 channels is also the most Windows' audio stack handles in shared mode. Every frame is still a whole number of
75 * 32-bit words (16 and 8 bytes), as the FIFO DMA needs */
76#define USB_PAUDIO_MT_CHANNELS_TO_HOST 8
77#define USB_PAUDIO_MT_CHANNELS_FROM_HOST 4
78#define USB_PAUDIO_MT_SUBFRAME_BYTES 2
79#define USB_PAUDIO_MT_BIT_RESOLUTION 16
80#define USB_PAUDIO_MT_BYTES_PER_FRAME_TO_HOST (USB_PAUDIO_MT_CHANNELS_TO_HOST * USB_PAUDIO_MT_SUBFRAME_BYTES)
81#define USB_PAUDIO_MT_BYTES_PER_FRAME_FROM_HOST (USB_PAUDIO_MT_CHANNELS_FROM_HOST * USB_PAUDIO_MT_SUBFRAME_BYTES)
82#define USB_PAUDIO_MT_MAX_FRAMES_TO_HOST 45
83#define USB_PAUDIO_MT_MAX_FRAMES_FROM_HOST 46
84// Smaller, so that hosts choosing between alternate settings of the same format by bandwidth prefer it if they can't
85// use the asynchronous one - as with the stereo configuration's
86#define USB_PAUDIO_MT_ADAPTIVE_MAX_FRAMES_FROM_HOST 45
87#define USB_PAUDIO_MT_MAX_PACKET_BYTES_TO_HOST \
88 (USB_PAUDIO_MT_MAX_FRAMES_TO_HOST * USB_PAUDIO_MT_BYTES_PER_FRAME_TO_HOST)
89#define USB_PAUDIO_MT_MAX_PACKET_BYTES_FROM_HOST \
90 (USB_PAUDIO_MT_MAX_FRAMES_FROM_HOST * USB_PAUDIO_MT_BYTES_PER_FRAME_FROM_HOST)
91#define USB_PAUDIO_MT_ADAPTIVE_MAX_PACKET_BYTES_FROM_HOST \
92 (USB_PAUDIO_MT_ADAPTIVE_MAX_FRAMES_FROM_HOST * USB_PAUDIO_MT_BYTES_PER_FRAME_FROM_HOST)
93
94/* The pipes' buffers in the USB controller (r_usb_peptable.c) are sized for whichever configuration needs more, in
95 * whole 64-byte blocks, each double buffered */
96#define USB_PAUDIO_MAX_OF(a, b) ((a) > (b) ? (a) : (b))
97#define USB_PAUDIO_PIPE_BUF_BYTES(bytes) (((bytes) + 63) / 64 * 64)
98#define USB_PAUDIO_FROM_HOST_PIPE_BUF_BYTES \
99 USB_PAUDIO_PIPE_BUF_BYTES(USB_PAUDIO_MAX_OF(USB_PAUDIO_MAX_PACKET_BYTES, USB_PAUDIO_MT_MAX_PACKET_BYTES_FROM_HOST))
100#define USB_PAUDIO_TO_HOST_PIPE_BUF_BYTES \
101 USB_PAUDIO_PIPE_BUF_BYTES(USB_PAUDIO_MAX_OF(USB_PAUDIO_MAX_PACKET_BYTES, USB_PAUDIO_MT_MAX_PACKET_BYTES_TO_HOST))
102
103/* Which configuration descriptor the host gets - usbAudioStartPeripheral() picks it */
104#define USB_PAUDIO_CONFIG_MIDI_ONLY 0
105#define USB_PAUDIO_CONFIG_STEREO 1
106#define USB_PAUDIO_CONFIG_MULTITRACK 2