Repository navigation
feat(firmware): USB audio, the pedal as a class-compliant USB sound card - #25
Conversation
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).
4601f16 to
8e1f819
Compare
|
after some extensive testing, I noticed some clicking. I've got a branch incoming to fix that. |
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).
|
Two commits added to this PR ( What was wrongThe clicks were not in the USB streams. Each audio interrupt has two callbacks of the I2S driver: 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
Before and afterMeasured 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.
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
Seen and not changedmacOS, 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. |
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
…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
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.mdanddocs/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
firmware is a USB Audio Class 1 device (record asynchronous, playback adaptive, 44.1 kHz only) beside its MIDI port.
chip/usb.c. It holds a record streamand 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.
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.output's with MASTER at 100, so the knob does not change a recording.
not in the recording.
complete (
firmware/px_uaudio.h). The packets move in the 2 kHz USB poll that MIDI already uses.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 (
bcdDevice3.00 without audio, 3.D0 with it).USB_AUDIO(28), reads and sets the setting and gives the streams' stateand counts. Nothing else changes: editors for protocol 1 to 3 work as before.
Commits
feat(usb):usb.ccan switch the audio streams at run time; felucca-bsp's later packet pacing; the playback mixin integers; fewer controller reads a poll.
feat(firmware): the audio glue, the setting in the store, the boot order, and the host test.feat(firmware): the MIDI page's row,USB_AUDIO, the editor's button, their tests.docs:docs/USB-AUDIO.mdand the README.no-mistakes(document): two mentions indocs/SOUNDS-AND-CONTROLS.md.On top of v41
CORE 2 OFFline on the MIDI page is kept. With the fourth row (USB AUDIO) the rows end at y = 154, the USB andLAST lines follow at 162 and 186, and the
CORE 2 OFFline stays at 204, above the footer at 222. The page redraws wheneither changes.
a_lp_post,dsp_lp_run) is untouched. USB audio runs ina_dec, after theblock is complete, and its time is added to CPU0's share of the load as before.
1226e4a), and was labelledv40.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'sv41 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.image was installed over USB MIDI while the setting was on and started as the audio device.
3,971,072 frames is the one before plus one.
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.
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.
DSP load
Load of the busier core in points of an audio block, against the base firmware (1226e4a), from
node tools/op.mjs dspand PING. The safety bypass engages above 93 % for 8 blocks in a row.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 %).
The engine is not touched:
tests/equiv.sh origin/openpocket-v40-ram-dspprintsidentical, 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, intests/run_tests.sh):usb.candpx_uaudio.hon a PC against a model of the USBcontroller 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 apower cycle, the MIDI page's row (drawn with v41's
CORE 2 OFFline),USB_AUDIO.tests/test_editor_page.mjsin 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 testpasses in the container,node tools/op.mjs packagebuilds, and the installer, pedal skin andaudio page tests pass.
Not checked on the real pedal
during playback (it stayed at about 23 %), the tuner's mute in a recording, and a played guitar at the input.
"ring empty" count rose as described below and nothing stalled.
node tools/op.mjs ble: its XIPregion is 1,200 bytes over), so it was not flashed.
Known limits
chain (reamping inside the pedal) are not done.
pedal counts those packets as "ring empty".
(an early state of this branch with test signals), and the synthetic guitar (
OP_TESTSIG 1).13 to 15 to need, and the packet pacing it adopted for iOS.
How to reproduce
node tools/op.mjs test; the USB part alone is the "USB audio" section oftests/run_tests.sh.node tools/op.mjs package.input and output at 44.1 kHz.
USB_AUDIO's counts (seedocs/EDITOR-PROTOCOL.md) show the streams' health.