Skip to content

soundmodule: prepare() makes channel 1's and the drums' instruments - #183

Merged
bdbarnett merged 2 commits into
mainfrom
soundmodule-ready-parts
Nov 17, 2026
Merged

bdbarnett merged 2 commits into
mainfrom
soundmodule-ready-parts

Conversation

@bdbarnett

Copy link
Copy Markdown
Contributor

The first key played into midiEngine on a board was late when the SoundFont is a large one: with a 32 MB General MIDI bank on an ESP32-P4, the first note on channel 1 took 0.9 to 1.3 s to start and the first drum 0.45 to 0.63 s. A part's instrument is made by its first program change or, when nothing selects a program first, by its first note. A keyboard plays channel 1 and the drums exactly that way.

prepare() now makes those two parts' instruments once the bank is open, through a new ready() that takes other channels too. A part that can't be made is left for its first note, and error stays as it was.

This is half of the fix. The other half is PyDevices/audioinstruments#123, which works out a SoundFont preset when it is selected rather than in its first note; until a firmware carries it, the instruments made here still leave that work to the first note, so this change alone moves about 30 ms. Measured on the P4 with both changes put over the frozen modules at run time:

before after
channel 1, first note 899 and 1295 ms 50 ms (43 of it the sample read from the card)
drums, first note 454 and 630 ms 12 ms
prepare() 556 ms 1268 ms
heap free after the same notes 11,826,480 11,825,888
slowest of 400 notes over Bluetooth MIDI, write to voice 200, 197 and 236 ms 49 and 56 ms

So opening the app takes 0.7 s longer with a bank that size, once, while it already shows that it is loading. With a 315 KB bank the first note was never late.

tests/test_engine.py passes on CPython, MicroPython and CircuitPython, and the two new plants (ready-skipped, ready-keeps-error) each fail the contract. tools/check_stubs.py passes. Not done: no firmware was built with this, and midiEngine's file player was not timed.

A part's instrument was made by its first note when nothing selected a
program first, which is how a keyboard plays channel 1 and the drums. With
a large SoundFont on a board, making it and working out its preset is most
of a second, so the first key sounded late. prepare() now makes those two
parts once the bank is open (ready(), which takes other channels too).

Checked with tests/test_engine.py on CPython, MicroPython and
CircuitPython, with a planted fault for each new check, and on an ESP32-P4
with a 32 MB General MIDI bank.
@bdbarnett
bdbarnett merged commit 74da034 into main Nov 17, 2026
14 checks passed
@bdbarnett
bdbarnett deleted the soundmodule-ready-parts branch November 17, 2026 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant