Skip to content

feat(midi)!: add pitch bend and sustain support - #56

Open
google-labs-stitch[bot] wants to merge 6 commits into
mainfrom
stitch/midi-pitch-bend-sustain-jm0-d922745c-01e0-4367-9bc6-d727815a97d2
Open

google-labs-stitch[bot] wants to merge 6 commits into
mainfrom
stitch/midi-pitch-bend-sustain-jm0-d922745c-01e0-4367-9bc6-d727815a97d2

Conversation

@google-labs-stitch

@google-labs-stitch google-labs-stitch Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This change adds 14-bit MIDI Pitch Bend (0xE0) and Sustain Pedal (CC 64) support across the Rust, WebAssembly and TypeScript APIs.

  • Mark MidiEvent as #[non_exhaustive] and add PitchBend { channel: u8, value: i16 } (-8192..=8191) and SustainPedal { channel: u8, held: bool } with camelCase tsify tags, byte decode in MidiEvent::from_bytes, range checks in MidiEvent::validate and Wasm event decode in decode_midi_event.
  • Add pitch_bend_semitones: f32 (0.0..=24.0, default 2.0, optional or undefined in JavaScript) to HarmonyConfig and CorrectionConfig, validate both fields before any state change and add harmony_pitch_bend_semitones to ControlActivity.
  • Narrow the setup_chord() guard in HarmonyState::apply_config to the chord voice fields so a change to pitch_bend_semitones alone does not calculate chords again.
  • Add zero-allocation sustain latches, 16-channel pedal flags and fixed-point Q14 pitch-bend ratios to HarmonyState and CorrectionTable.
  • Hold released notes while sustain is active, keep stolen sustained notes in pending_order for MidiOmni and ChordViaMidi, release unheld notes on pedal release, support keys 0..=127 with configured notes reload in CorrectionTable, preserve scale_initialized and humanize dwell on sustained NoteOff (resetting dwell only when correction MIDI reloads the note table) and clear sustain and pitch-bend state on AllNotesOff and reset.
  • Scale voice and correction ratios only when the Q14 bend ratio differs from Q14::ONE, so default PCM output stays identical.
  • Normalize the new MIDI and pitchBendSemitones rustdoc links in scripts/normalize-jsdoc.mjs, check undefined and omitted pitchBendSemitones plus processMono pitchBend and sustainPedal events in scripts/verify-pack.mjs, and document the new MIDI variants and pitchBendSemitones ranges in src/wasm.rs and README.md.

Verification

  • cargo rustc --locked --lib --release -- -D warnings
  • cargo fmt --check
  • cargo clippy --locked --lib -- -D warnings
  • RUSTDOCFLAGS="-D warnings" cargo doc --locked --no-deps
  • cargo package --locked
  • npm run check:wasm
  • npm run verify:pack
  • npm run verify:pack -- --ignore-scripts
  • Verified identical fixed-seed stereo PCM output against main (8880205) including humanize, flex_tune and ParameterControl (81,920 bytes, SHA-256 7c254a23a2d369d2ec207dcf1794342e6d4ec21374fd6f84c4b6f71c5a243c37), bit-for-bit humanize dwell preservation on sustained NoteOff, plus Rust and Node.js WebAssembly checks (Int16Array and Float32 in-place views) for #[non_exhaustive], ControlActivity, pitch bend, sustain latches, voice theft, key 127 and preflight validation.

Automated PR initiated by user @byteland-app via Stitch Loop.

- Add PitchBend and SustainPedal variants and byte decode to MIDI
- Validate MIDI channel and 14-bit pitch-bend value ranges
- Add pitch_bend_semitones to HarmonyConfig and CorrectionConfig
- Apply Q14 pitch-bend ratios and sustain latches to harmony
- Apply Q14 pitch-bend ratios and sustain latches to pitch correction
- Clear sustain and pitch-bend state on All Notes Off and on reset
- Document the new MIDI controls and bend ranges in the README
- Resolve the conflict in src/harmony.rs
- Iterate pending_order with enumerate in HarmonyState::resume_pending
@byteland-app

Copy link
Copy Markdown
Contributor

Review: Well-implemented 14-bit Pitch Bend and Sustain Pedal support across HarmonyState and CorrectionTable. Please update for the 0.4.0 breaking window:

  1. MidiEvent is not #[non_exhaustive], so adding PitchBend and SustainPedal is a Rust breaking change — please mark the commit as 'feat(midi)!:' and consider adding #[non_exhaustive] to MidiEvent.
  2. In HarmonyState::apply_config, narrow the setup_chord() guard so changing pitch_bend_semitones alone does not recompute chords.
  3. Rebase onto current main and squash the merge commits.

- Mark MidiEvent as non-exhaustive for future MIDI controls.
- Narrow the chord setup guard in HarmonyState::apply_config.
- Decode pitchBend and sustainPedal in the Wasm event decoder.
- Merge the upstream main branch at 0.3.3.
@google-labs-stitch google-labs-stitch Bot changed the title feat(midi): add pitch bend and sustain support feat(midi)!: add pitch bend and sustain support Nov 25, 2026
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Addressed in 3d7b873:

  1. MidiEvent breaking change & #[non_exhaustive]: Added #[non_exhaustive] to MidiEvent in src/midi.rs and updated the commit header and PR title to feat(midi)!: add pitch bend and sustain support.
  2. HarmonyState::apply_config chord guard: Narrowed the self.setup_chord() condition in src/harmony.rs to check only the chord-voicing fields (source, key, scale, chord, degree, inversion, register_percent and voicing_spread_percent), so updating pitch_bend_semitones (or interval/portamento controls) alone does not recompute chords.
  3. Sync with current main (0.3.3): Merged current origin/main (451ea1e), integrating the unified MIDI helpers in src/harmony.rs, CorrectionTable::new in src/resynthesis.rs, and pitchBend/sustainPedal decoding in the reusable Wasm event decoder in src/wasm.rs. Force-push is disabled in this automated sandbox environment, so branch history could not be rewritten via rebase/squash directly; with the PR title updated to feat(midi)!: add pitch bend and sustain support, GitHub Squash and merge will land a single clean feat(midi)!: commit on main.

- Merge the upstream main branch at 0.3.4.
- Decode undefined pitchBendSemitones values to 2 in Wasm.
- Add harmony_pitch_bend_semitones to ControlActivity.
- Align correction sustain and reset with keys 0..=127 and notes.
- Return true on sustain release for channels 0..=3 in MidiChannels.
@byteland-app

Copy link
Copy Markdown
Contributor

Round 3 review: Approved for 0.4.0 — 14-bit Pitch Bend, Sustain Pedal, MidiEvent #[non_exhaustive], and ControlActivity alignment are verified. Please rebase onto updated main after #45 and #107.

- Merge the humanize, flex-tune, Float32 and JSDoc changes from main.
- Keep humanize and flex_tune with pitch_bend_semitones in correction.
- Reset correction dwell only when correction MIDI reloads the table.
- Normalize the MIDI and pitch-bend rustdoc links for TypeScript JSDoc.
- Check undefined and omitted pitchBendSemitones in verify-pack.mjs.
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Synced with updated main (0850e25) in 8cf0e6c:

  • #45 (humanize and flex_tune): Integrated pitch_bend_semitones alongside humanize and flex_tune in CorrectionConfig (including optional/undefined Wasm deserialization and range checks), and updated Resynthesis::apply_correction_midi so PitchBend and SustainPedal events that do not reload CorrectionTable preserve self.dwell and self.scale_initialized.
  • #107 (JSDoc normalization & package checks): Added JSDoc link and prose replacements in scripts/normalize-jsdoc.mjs for HarmonyConfig.pitchBendSemitones, MidiEvent::PitchBend, Self::NoteOff, and the HarmonySource variants, and extended scripts/verify-pack.mjs to check undefined and omitted pitchBendSemitones values.
  • Verification: Confirmed bit-exact stereo PCM output against main (0850e25) across all modes (including humanize, flex_tune, and Float32 Wasm views from #117), and all CI checks (verify) are green. (Force-push is disabled in this automated environment, so main was merged rather than rebased; GitHub Squash and merge will land a single clean feat(midi)!: add pitch bend and sustain support commit.)

@byteland-app

Copy link
Copy Markdown
Contributor

Review: 14-bit Pitch Bend, Sustain Pedal, MidiEvent #[non_exhaustive], and ControlActivity updates look solid. Two items before merge:

  1. Preserve humanize dwell on sustained NoteOff (src/scale.rs): In CorrectionTable::apply_validated_midi, when NoteOff moves a held key into sustained while sustain is active, the active pitch-class mask is unchanged; please return false so scale_initialized and dwell are not reset. Also add a processMono runtime smoke check for pitchBend and sustainPedal events in scripts/verify-pack.mjs.
  2. Sync and coordination with feat(midi)!: add MIDI keybed, channel map and latch #69: Please sync onto updated main (build(cargo): bump the cargo group with 2 updates #139, ci: deny compiler and rustdoc warnings in scripts and docs #131, feat!: add pitch fields to Telemetry and trim Wasm voice slices #104, feat(events)!: add scalar parameter control events #61). Note that we plan to merge feat(midi)!: add MIDI keybed, channel map and latch #69 ahead of feat(midi)!: add pitch bend and sustain support #56 because feat(midi)!: add MIDI keybed, channel map and latch #69 replaces HarmonyState's pending_* arrays with MidiKeybed and adds MidiChannelMap (BaseWindow / WrapFour), which supersedes the channel < 4 channel-to-voice assumption. Once feat(midi)!: add MIDI keybed, channel map and latch #69 lands, feat(midi)!: add pitch bend and sustain support #56 will rebase cleanly by storing sustained: [u128; 16] and pitch bend in MidiKeybed and routing via HarmonyConfig::map_channel.

- Merge main (8880205) with ParameterControl and Telemetry updates.
- Return false on sustained NoteOff in CorrectionTable to keep dwell.
- Check pitchBend and sustainPedal in processMono in verify-pack.mjs.
@google-labs-stitch

Copy link
Copy Markdown
Contributor Author

Addressed in a7d0d85:

  1. Preserve humanize dwell on sustained NoteOff & processMono smoke check: Updated CorrectionTable::apply_validated_midi in src/scale.rs to return false when NoteOff latches a held key into sustained (or arrives on an already-sustained key) while sustain is active, preserving scale_initialized and dwell bit-for-bit on vibrato input. Added runtime and strict TypeScript processMono checks for pitchBend and sustainPedal events on midi and correctionMidi in scripts/verify-pack.mjs.
  2. Synced onto main (8880205): Merged #139, #131, #104 and #61, integrating ParameterControl, stage diff-guards, Telemetry pitch fields and Float32Array voice slices, and -D warnings doc/release checks. Ready to sync onto #69 (MidiKeybed / MidiChannelMap) once #69 lands on main.

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.

1 participant