Repository navigation
SoundFont instruments: soundfont and soundfont_drums - #176
Merged
Merged
Conversation
audioinstruments._sf2 opens an .sf2, reads the hydra, and turns a preset, key and velocity into flat regions: the sample's absolute frames with the fine and coarse address offsets applied, root key and tuning in cents, loop mode, the volume envelope in seconds and linear levels, attenuation, pan, exclusive class and the stereo link. Instrument global zones supply defaults, local zones override, preset generators add, ranges intersect, and the generators a preset may not carry are ignored there. Samples stay on disk and read_sample() fetches frames on demand; load_samples=True keeps the chunk in memory on the desktop. Modulators are only noticed; sm24 is ignored. Runs on CPython and MicroPython.
tests/support/sf2_builder.py writes a tiny .sf2 from the specification: three presets including a bank-128 kit, a global zone at both levels, split key ranges, a velocity split, every loop mode, an exclusive-class pair and a stereo pair. tests/test_cpython_sf2.py checks presets() and regions() exactly; tests/support/sf2_smoke.py builds and checks the same bank on any interpreter, MicroPython unix included.
audioinstruments.soundfont plays a bank's melodic presets under one program change, with Bank Select for the other banks, and audioinstruments.soundfont_drums plays bank 128 with the GM percussion map as its pads. The bank comes from the bank= option, $SOUNDFONT or ~/soundfonts; the hydra is read at create and samples the first time a note needs them. initialAttenuation is scaled by 0.4, the convention the banks were voiced for. Exclusive classes choke, and a note with every voice busy steals the oldest releasing one.
A stereo pair in one exclusive class stopped its own first half when the second region's choke ran: the hi-hats and guiros of a GM kit came out right-channel only.
…DI gate Eleven tests on the bank sf2_builder writes: finding the bank, patches and pads, one voice per region, the velocity split, release, the choke, Bank Select, voice stealing and the macros. Six more against FluidR3_GM.sf2 when it is at ~/soundfonts (skipped otherwise): all 128 programs sound and fall silent within their release, six pitched presets within 1% of middle C, every key of the Standard kit, the closed hi-hat choking the open one, and 16 held notes measured against the block.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Play a SoundFont 2 (
.sf2) bank as instruments.The bank is the
bank=option, or$SOUNDFONT, or the first.sf2in~/soundfonts. No bank ships with the library; any General MIDI bank works. Samples stay on disk and are read the first time a note needs them.audioinstruments._sf2reads.sf2files, written from the SoundFont 2.04 specification. It lists presets, and turns a key and velocity into regions with every generator applied: loop points, tuning, envelope, attenuation, pan, exclusive class and stereo links. Modulators are recorded but not applied; 24-bit samples are read as 16-bit.audiosampler.Sampler, with 32 voices. Bank Select reaches the other banks, exclusive class chokes (a closed hi-hat stops an open one), and voices are stolen oldest-releasing first. Initial attenuation is scaled by 0.4, the convention banks are voiced for.AUDIODSP_PINmoves to 6db15e3 (audiodsp#272), which addsaudiosampler. The pin file's entry lists the other changes since v0.6.6, all of them v0.6.7's.Tests:
test_cpython_sf2(15) andtest_cpython_soundfont(11) run against a bank the tests write from scratch.test_cpython_soundfont_gmruns against a real General MIDI bank when one is present and skips otherwise. Against FluidR3_GM, all 128 programs sound and stop on release, and six pitched programs land within 1% at C4. All 47 Standard-kit keys sound, and the choke works.Not yet: the sustain pedal (CC 64), and a short fade on a choke instead of a cut.