Skip to content

feat(firmware): USB audio, the pedal as a class-compliant USB sound card - #25

Merged
isod89 merged 8 commits into
isod89:mainfrom
VinSpee:fm/upstream-usb-audio
Nov 10, 2026
Merged

isod89 merged 8 commits into
isod89:mainfrom
VinSpee:fm/upstream-usb-audio

Conversation

@VinSpee

@VinSpee VinSpee commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

This pull request lets the pedal work as a USB sound card over its USB-C cable. With the new USB AUDIO setting on, a
computer sees a class-compliant audio device named OpenPocket beside the MIDI port, records the pedal's sound and plays
through the pedal's output, with no driver. The setting is off by default, and with it off the pedal is the same USB
device as before, byte for byte, with the same sound and the same DSP load. It was verified in the fork on a BlackBox with macOS
26 only: Windows, iOS, iPadOS, Linux and older macOS are still to be tried, and nobody has listened to the result yet.

It is based on v41 (69561af) and works with the current web editor; it does not depend on any other pull request.
It changes some of the files that #24 (the pedal UI overhaul) also changes: firmware/ui.h, firmware/openpocket.c,
firmware/px_usb.h, tests/uisim.c, tests/edsim.c, docs/SOUNDS-AND-CONTROLS.md, docs/MIDI.md and
docs/MIDI-TEST.md. The two branches merge without a conflict today, but this one may need a rebase after #24 is merged.

What the study found

  • The chip can do it. The AC791N's USB controller has a fourth endpoint with isochronous transfers, and the stock
    firmware is a USB Audio Class 1 device (record asynchronous, playback adaptive, 44.1 kHz only) beside its MIDI port.
  • The USB code was already there. OpenPocket's USB device is felucca-bsp's chip/usb.c. It holds a record stream
    and a playback stream on endpoint 4 (16 bit, stereo, 44.1 kHz), with rings, pacing, descriptors and its notes on
    what macOS needs. OpenPocket built it with MIDI alone.
  • The clocks. The pedal's sample clock is its own (6 MHz / 136 = 44,117.6 Hz) and is not locked to USB. The record
    endpoint is asynchronous, so the computer follows the pedal: each 1 ms packet carries the frames the pedal's clock
    made, and the ring's fill steers the rest by one frame a block at most. The playback endpoint is adaptive: the pedal
    resamples the computer's packets to its own clock, steered by its ring's fill. Nothing drifts.

The design

The smallest design that works is felucca-bsp's streams switched on, a setting, and the pedal's side of the audio.
It is described in docs/USB-AUDIO.md.

  • Format: 2 channels, 16 bit, 44.1 kHz, both ways. One isochronous endpoint each way, beside MIDI.
  • Recorded: the chain's output before MASTER, through the output's soft knee, on both channels. The level is the
    output's with MASTER at 100, so the knob does not change a recording.
  • Played: the computer's stereo sound is added to the pedal's output at MASTER's gain and the sum is clipped. It is
    not in the recording.
  • Where it runs: once a block in the I2S interrupt on CPU0, at the point where the block the second core made is
    complete (firmware/px_uaudio.h). The packets move in the 2 kHz USB poll that MIDI already uses.
  • The setting: the fourth row of the pedal's MIDI page (hold FX), and a button in the web editor. It is one bit in
    the preset store's header, which every earlier firmware wrote as 0. A change takes the pedal off USB for one second
    and it comes back as the other device (bcdDevice 3.00 without audio, 3.D0 with it).
  • Editor protocol 4: one new command, USB_AUDIO (28), reads and sets the setting and gives the streams' state
    and counts. Nothing else changes: editors for protocol 1 to 3 work as before.
  • Boot order: the store is now read before the USB device starts, so the computer finds the right device at once.

Commits

  1. feat(usb): usb.c can switch the audio streams at run time; felucca-bsp's later packet pacing; the playback mix
    in integers; fewer controller reads a poll.
  2. feat(firmware): the audio glue, the setting in the store, the boot order, and the host test.
  3. feat(firmware): the MIDI page's row, USB_AUDIO, the editor's button, their tests.
  4. docs: docs/USB-AUDIO.md and the README.
  5. no-mistakes(document): two mentions in docs/SOUNDS-AND-CONTROLS.md.
  6. The port to v41: the work's own labels say v42 instead of v40.6.

On top of v41

  • v41's CORE 2 OFF line on the MIDI page is kept. With the fourth row (USB AUDIO) the rows end at y = 154, the USB and
    LAST lines follow at 162 and 186, and the CORE 2 OFF line stays at 204, above the footer at 222. The page redraws when
    either changes.
  • v41's limit on CPU0's waits for CPU1 (a_lp_post, dsp_lp_run) is untouched. USB audio runs in a_dec, after the
    block is complete, and its time is added to CPU0's share of the load as before.
  • The work was built and measured in the fork on v40 with the DSP work that became v41 (1226e4a), and was labelled
    v40.6 there. v41 shipped without it, so it now says v42, and editor protocol 4 is v42's.

Measured on the pedal

These measurements were made in the fork, on this work built on 1226e4a (v41's DSP work before the maintainer's
v41 commits). This rebased branch was not flashed.

BlackBox on USB-C, Apple silicon Mac, macOS 26.2. The release image measured was
1382d02b046749be78323573a86ac768b40fd30efdaf12c12c2fefda4a9b83e6 (BlackBox_940.fwsc); three builds gave the same file.

  • The computer sees it. macOS lists OpenPocket with 2 inputs and 2 outputs, 44,100 Hz, 2 ch 16-bit. The release
    image was installed over USB MIDI while the setting was on and started as the audio device.
  • No frame lost or repeated. A trial build sent a counter in place of the sound. In a 90 s capture every one of
    3,971,072 frames is the one before plus one.
  • No gap in 10 minutes of sound. 26,472,960 frames; the pedal counted 0 packets with its ring empty, 0 missing
    packets and 0 stalls. The Mac measured 44,121.3 Hz, the pedal's nominal rate plus 83 ppm, and the ring's fill stayed
    between 67 and 190 frames of 512.
  • Playback. A 440 Hz sine played by the Mac came back from the pedal's output stage as a steady sine (trial build,
    loopback), with the two channels kept apart; 0 packets dropped. On the release image 60,131 packets were played with
    the playback ring never empty and never full.
  • MIDI and updates beside it. Every load figure below was read over the editor protocol while the streams ran.

DSP load

Load of the busier core in points of an audio block, against the base firmware (1226e4a), from
node tools/op.mjs dsp and PING. The safety bypass engages above 93 % for 8 blocks in a row.

In the audio interrupt (CPU0) 11 worst-case chains 23 factory presets with an amp
Setting off one flag tested 0.0 to +0.6, mean +0.2 (the spread between two runs) -1 to +1, mean 0.0
The computer records 0.38 % of a block -0.5 to +1.0, mean +0.6 0 to +2, mean +0.4
The computer records and plays 1.13 % of a block -0.4 to +2.0, mean +1.3 0 to +2, mean +1.2

No chain engaged the bypass and no block was late in any run, including with a synthetic guitar at the input, which
costs more than a quiet one. The heaviest chain, W4, went from 82.4 % to 83.4 % with both streams (83.8 % with the
synthetic guitar, highest single block 92 %).

Chain Base Setting off Recording Recording and playing
F1 70.9 71.0 71.8 72.9
F2 77.2 77.2 78.0 79.2
F3 76.0 76.0 76.1 76.7
F4 75.0 75.2 75.9 77.0
F5 74.8 75.0 74.3 74.4
F6 70.8 71.4 71.8 72.8
W1 78.2 78.2 78.8 79.0
W2 78.5 78.9 78.9 80.0
W3 78.0 78.0 78.7 79.8
W4 82.4 82.4 83.0 83.4
W5 78.8 79.0 79.5 80.0

The engine is not touched: tests/equiv.sh origin/openpocket-v40-ram-dsp prints identical, 852 runs.

Memory, release image: main RAM 77,960 of 98,304 bytes (71,272 on the base, so 6.7 KB: the two rings and their
buffers); RAM code window 15,272 bytes free (15,704 on the base).

Tests

On this branch, rebased on v41:

  • tests/test_usb_audio.c (new, in tests/run_tests.sh): usb.c and px_uaudio.h on a PC against a model of the USB
    controller and of a computer. With the setting off, the descriptors read through the control-transfer code equal
    those of a build without audio, byte for byte. With it on: the descriptors' structure, the requests, the switch, the
    levels, and 10 minutes of each stream with the clocks 300 ppm apart each way and with late blocks. USB audio: ok (0 failures).
  • tests/test_store.c, tests/midiio.c, tests/uisim.c, tests/test_editor.mjs: the setting in the store and through a
    power cycle, the MIDI page's row (drawn with v41's CORE 2 OFF line), USB_AUDIO.
  • tests/test_editor_page.mjs in headless Chromium: the editor's USB audio button, off by default, the question first,
    the reconnection, on and off again (43 checks, editor page test passed).
  • node tools/op.mjs test passes in the container, node tools/op.mjs package builds, and the installer, pedal skin and
    audio page tests pass.

Not checked on the real pedal

  • Any computer other than this Mac (macOS 26.2): Windows, iOS, iPadOS, Linux, macOS 13 to 15.
  • The sound by ear. Nobody listened to a recording, and the output jack and headphones were not measured with playback.
  • The pedal's own controls. Nobody was at the pedal: the MIDI page's USB AUDIO row on the real screen, the MASTER knob
    during playback (it stayed at about 23 %), the tuner's mute in a recording, and a played guitar at the input.
  • A power cycle by hand. The start with the setting on was seen after an install, which restarts the pedal.
  • Unplugging the cable, and the computer going to sleep, while a stream runs.
  • A save on the pedal during a recording. Preset changes over the editor protocol were made during the streams: the
    "ring empty" count rose as described below and nothing stalled.
  • A Pocket X. Only a BlackBox was connected.
  • The Bluetooth test image. It has no USB audio, and it does not link on v41 either (node tools/op.mjs ble: its XIP
    region is 1,200 bytes over), so it was not flashed.
  • Sessions longer than 10 minutes.

Known limits

  1. 16 bit, 44.1 kHz only. The stock firmware sends 24 bit. felucca-bsp's streams were kept as they are.
  2. Mono on both channels. A dry guitar on the second channel and playing the computer's sound into the effect
    chain (reamping inside the pedal) are not done.
  3. No volume or mute control is offered to the computer.
  4. A full change, a preset change and a save leave a short silence in the recording, as at the output, and the
    pedal counts those packets as "ring empty".
  5. Two of the measurements used development builds that are not in this branch: the counter and the loopback
    (an early state of this branch with test signals), and the synthetic guitar (OP_TESTSIG 1).
  6. The other systems' needs are taken from felucca-bsp, not from a test here: the interface order it found macOS
    13 to 15 to need, and the packet pacing it adopted for iOS.

How to reproduce

  • Host tests: node tools/op.mjs test; the USB part alone is the "USB audio" section of tests/run_tests.sh.
  • Build: node tools/op.mjs package.
  • On a pedal: install, open the web editor, USB audio, Turn on. The computer then lists OpenPocket as an audio
    input and output at 44.1 kHz. USB_AUDIO's counts (see docs/EDITOR-PROTOCOL.md) show the streams' health.

The USB device code (firmware/bsp/chip/usb.c, felucca-bsp, MPL-2.0) already holds a USB Audio Class 1 record stream
and a playback stream on endpoint 4. OpenPocket builds it with MIDI alone. This prepares it for a USB AUDIO setting:

- FELUCCA_UAC_SWITCH: the audio streams are presented only while usb_uac_on is set. usb_uac_switch changes it and
  takes the device off the bus so that the computer enumerates it afresh, as usb_cdc_switch does for the console.
  With it off the descriptors, the requests and endpoint 4 are those of a build without audio.
- The record stream's packets follow the I2S clock (44,117.647 Hz) as a steady pattern, and the ring's fill steers
  the rest by one frame a block at most. This is felucca-bsp's own later pacing (its commit e79a1f8): steering every
  packet gave dropouts with hosts that follow the device's rate closely.
- The playback mix interpolates and adds in integers and clips the sum to 24 bits. In float a frame took 88 CPU cycles
  on the pedal.
- A poll reads the controller less often: RXCSR2 only when a packet waits, INDEX written once for both directions.
- UO_N can be set by the including firmware.

Nothing changes in the firmware yet: FELUCCA_UAC stays 0 until the next commit.
With the new USB AUDIO setting on, the pedal is a USB Audio Class 1 device beside its MIDI port: 16-bit stereo at
44.1 kHz, no driver. The computer records the pedal's sound, and what it plays is added to the pedal's output. The
setting is off by default, and with it off the USB device is the one before, byte for byte.

- firmware/px_uaudio.h, the pedal's side. Once a block, in the I2S interrupt on CPU0, where the block the second core
  made is complete, px_ua_block writes the chain's output into felucca-bsp's record ring (before MASTER, through the
  output's soft knee, on both channels) and lets usb.c add the computer's playback to the output at MASTER's gain.
  px_ua_service presents the USB device anew when the setting changes.
- The record endpoint is asynchronous: the computer follows the pedal's clock (44,117.6 Hz). The playback endpoint is
  adaptive: the pedal resamples to its own clock. Nothing drifts.
- The setting is bit 0 of the preset store header's second spare byte (0, off, in every earlier store). The store is
  now read before the USB device starts, so the computer finds the right device from its first question.
- The time the block takes on CPU0 is counted in CPU0's load and in what the layer split balances. It is 0 with the
  setting off.
- Not in the Bluetooth test image (OP_UAUDIO 0): its RAM is full.

tests/test_usb_audio.c runs usb.c and px_uaudio.h on a PC against a model of the USB controller and of a computer:
the descriptors with the setting off against a build without audio, the requests, the switch, the levels, and 10
minutes of each stream with the clocks 300 ppm apart. tests/test_store.c covers the setting in the store.
- The MIDI page (hold FX) has a fourth row, USB AUDIO. The setting follows the row when the page is left, because a
  change takes the pedal off USB for a second. The rows are 30 px apart instead of 36.
- Editor protocol 4: USB_AUDIO (command 28) reads the setting, the streams' state and 17 counts, and sets the setting.
  It is an addition only: editors written for protocol 1 to 3 work unchanged.
- The web editor has a USB audio section with one button. After a change it waits for the pedal to come back on USB
  and connects again by itself.
- tests/edsim.c boots the store through px_store_boot, as the pedal does, so the settings are read there too.

Tests: the MIDI page's row and the save on leaving (tests/uisim.c, tests/midiio.c), USB_AUDIO on the simulated pedal
through a power cycle (tests/test_editor.mjs), the editor's button in headless Chromium (tests/test_editor_page.mjs).

The MIDI page keeps v41's CORE 2 OFF line below its four rows.
How to use the pedal as a USB sound card, what is recorded and played, the limits, and the design: what the chip and
felucca-bsp's USB code already had, the smallest design on top of it, how the pedal's clock and USB's are kept from
drifting, what stays the same with the setting off, and the cost measured on a BlackBox (0.38 % of an audio block in
the audio interrupt while the computer records, 1.13 % while it also plays; the worst-case chains' load rises by at
most 1.0 and 2.0 points and none engages the safety bypass).
v41 shipped without USB audio, so the comments, docs and test messages that called it v40.6 now say v42 (editor
protocol 4 is v42's).
@VinSpee
VinSpee force-pushed the fm/upstream-usb-audio branch from 4601f16 to 8e1f819 Compare November 10, 2026 17:44
@VinSpee

VinSpee commented Nov 10, 2026

Copy link
Copy Markdown
Contributor Author

after some extensive testing, I noticed some clicking. I've got a branch incoming to fix that.

@VinSpee
VinSpee marked this pull request as draft November 10, 2026 20:08
The I2S driver (sdk/iis.bc) serves the output's channel before the input's. Normally the audio interrupt enters with the
input alone pending, so a_enc (the guitar's block) runs before a_dec (the output block to fill). When the interrupt enters
late, both are pending and a_dec ran first: it sent the block before once more, and the block a_enc made after it was
never sent. One block of 2.9 ms twice and one lost, a click at the pedal's output and in a USB recording.

The interrupt enters late when TIMER5's USB poll runs at that moment. With a USB audio stream open the poll builds or
takes a packet every millisecond, and the order turned 1.7 times a second with one stream and 3.4 times with both; MIDI
and editor traffic do it too, rarely.

firmware/px_aorder.h: an a_dec that comes before its a_enc only notes its block, and the a_enc that follows within
microseconds fills it once the sound is started. tests/test_aorder.c runs 100,000 interrupts with the order turned at
random; it fails on the old order.

Measured on a BlackBox, a computer recording the pedal's sound, a synthetic guitar playing: 212 blocks sent twice in
120 s on AC15 CHIME and 154 on a worst-case NAM chain before, 0 in 300 s on both after.

px_audio.h also gains OP_UADEV, a measurement option that is never a release (used by the next commit).
…s, starts a stream clean

With the callbacks' order kept (px_aorder.h), the moment a block is written to the record ring and read from the
playback ring is steady within a preset (within 0.06 ms over the 52 presets, 0.2 ms on ORGAN). The rings no longer need
felucca-bsp's allowance for a render that starts late:

- the record ring's band 52 to 108 frames (was 80 to 128), primed at 80;
- the playback ring's target 208 frames (was 256).

The round trip computer, pedal, computer, by the computer's time stamps: 12.7 ms before, 11.1 ms after, on a light and
on a worst-case preset. The margin left is 0.6 ms and more with the clocks 300 ppm apart either way
(tests/test_usb_audio.c). The figures, and the streams' options that px_usb.h and the host test each had a copy of, are
in firmware/px_uaconf.h.

A playback stream opened again no longer starts with what the ring still held of the stream before.

USB_AUDIO has three more counts (20 in all; the reply says how many): record packets the computer took late, playback
packets that came late, and the output blocks asked for before their input. docs/EDITOR-PROTOCOL.md, docs/pxedit.js.

OP_UADEV 1 (measurements only): the callbacks' timing and the rings' fills in USB_AUDIO's reply, a counter or the
computer's playback in the record stream, a steady tone as the input, the old order and the ring figures switched by
USB_AUDIO's argument. docs/USB-AUDIO.md has the measurements.

With USB AUDIO off the USB device is unchanged, byte for byte (tests/test_usb_audio.c). Loads over the 24 factory
presets are unchanged (mean difference 0.0 to +0.1 points).
@VinSpee

VinSpee commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Two commits added to this PR (cadcf9e, 6c1c752): the clicks heard with a USB audio stream open are gone, and the streams hold 1.6 ms less.

What was wrong

The clicks were not in the USB streams. Each audio interrupt has two callbacks of the I2S driver: a_enc hands over the guitar's block and a_dec asks for the output block, which must be the sound made from that guitar block. The driver (sdk/iis.bc, iis_irq_handler) serves the output's channel before the input's. Normally the interrupt enters with the input alone pending, so a_enc runs first. When the interrupt enters late, both are pending and a_dec ran first: it sent the block before once more, and the block a_enc made after it was never sent. One block of 2.9 ms twice and one lost, at the pedal's output as well as in the recording.

The interrupt enters late when TIMER5's USB poll runs at that moment. With a USB audio stream open the poll builds or takes a packet every millisecond (up to 40 us), and the order turned 1.7 times a second with one stream and 3.4 times with both. MIDI and editor traffic lengthen the poll too, so the same click could come without USB audio, far less often (also on v41).

What changed

  • firmware/px_aorder.h: an a_dec that comes before its a_enc only notes its block; the a_enc that follows within microseconds fills it once the sound is started. tests/test_aorder.c runs 100,000 interrupts with the order turned at random and fails on the old order.
  • With the order kept, the moment a block is written and read is steady within a preset (within 0.06 ms over the 52 presets, 0.2 ms on ORGAN), so the rings hold less: the record ring's band is 52 to 108 frames (was 80 to 128), the playback ring's target 208 frames (was 256). The figures are in firmware/px_uaconf.h, shared with the host test. The margin left is 0.6 ms and more with the clocks 300 ppm apart either way.
  • A playback stream opened again starts empty, not with the last 5 ms of the stream before.
  • USB_AUDIO has three more counts (20 in all): record packets the computer took late, playback packets that came late, and output blocks asked for before their input.
  • OP_UADEV 1 (measurements only, never a release) adds the callbacks' timing and the rings' fills to USB_AUDIO's reply and can put a counter or the computer's playback in the record stream.

Before and after

Measured on a BlackBox with this branch (v41 base), macOS 26, a synthetic guitar playing, the Mac recording the pedal's sound. A block sent twice is 128 frames of the recording equal to the 128 before, sample for sample. "Before" is the same image with the old order and the old ring figures switched back at run time.

Before After
Blocks sent twice, AC15 CHIME (load 68 %) 199 in 120 s (99 a minute) 0 in 300 s (494 turned orders put right)
Blocks sent twice, worst-case chain (NAM amp, 12 blocks on, load 83 %) 147 in 120 s (74 a minute) 0 in 300 s (511 put right)
Round trip computer, pedal, computer (the pedal hands the playback to the record stream; by the computer's time stamps, 119 bursts), AC15 CHIME 12.71 ms 11.07 ms
Round trip, worst-case chain 12.71 ms 11.07 ms
1 kHz sine played by the Mac, back through the pedal not run on this build (0 breaks in 120 s on the v40 build) 0 breaks in 120 s, each preset
Ring empty or full, packets late, the Mac's sample time jumps 0 0

On the v40 build of the same change (the fork's main): 212 and 154 blocks sent twice in 120 s before, 0 in 300 s after; a counter in the record stream without a break over 7.9 million frames on each preset; loads over the 24 factory presets unchanged (mean difference 0.0 to +0.1 points); USB audio's time in the audio interrupt 0.38 % of a block before and 0.42 % after while recording, 1.14 % and 1.16 % with both streams.

Checks

  • node tools/op.mjs test passes (it includes the new tests/test_aorder.c and the USB audio test: with USB AUDIO off the descriptors are still byte for byte the earlier device's).
  • The installer and editor pages pass in headless Chromium.
  • Not tested: Windows, iOS, iPadOS, Linux. Sound was measured, not listened to.

Seen and not changed

macOS, about a second after a program has opened both streams, sometimes (2 of 15 starts) leaves one USB frame without asking for a record packet. For about half a second its recording then holds pieces of up to 65 frames that are exact copies of the recording 11,025 frames earlier: they are made in the computer's own buffer, not in the pedal's packets. docs/USB-AUDIO.md has it under the limits.

@VinSpee
VinSpee marked this pull request as ready for review November 10, 2026 22:04
@isod89
isod89 merged commit dfb1dcb into isod89:main Nov 10, 2026
1 check passed
isod89 pushed a commit that referenced this pull request Nov 10, 2026
feat(docs): redesign the in-browser editor into Presets, Sound, Models and MIDI sections. By VinSpee.

Merged after #25 (USB audio), whose button lived in the old one-screen editor.html: it moves to the new MIDI section's
Connection card (edmidi.js: the state, the button, a confirmation dialog), and editor.js lets go of the pedal and looks
again on its own when USB AUDIO changes ("reconnect": the pedal leaves USB for a second and comes back as another USB
device). The editor page test drives it there. The test also selects a MODEL option by its text with spaces counted
loosely (pickOpt): an option's label collapses runs of spaces, and Playwright versions differ in comparing it so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LFaxqW3CpFrLCvqoteTUv9
isod89 pushed a commit that referenced this pull request Nov 10, 2026
…al controls, #25 USB audio, #26 V21 factory sounds)

OP_VERSION_NUM 42. docs/WHATS-NEW-v42.md (with #24's text for the release notes), README, NOTICE (contributors: Vince
Speelman for #23-#25, Ivan Shevchuk for #26).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LFaxqW3CpFrLCvqoteTUv9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants