sinshu / rustysynth

A SoundFont MIDI synthesizer written in pure Rust
Other
108 stars 20 forks source link

Event callbacks #16

Open Tails opened 4 months ago

Tails commented 4 months ago

For the integration in applications, a client will need to know the offset of notes being played so they can update a UI. Can one register a listener/callback somewhere to listen for MIDI events when playing?

sinshu commented 4 months ago

If you want to hook MIDI events during the playback of a MIDI file, it's possible, but you'll need a custom MIDI sequencer. You can either create a customized sequencer by copying and modifying the default MidiFileSequencer, or fork RustySynth and alter the MidiFileSequencer itself.

To hook events in MidiFileSequencer, create a function to register callbacks to MidiFileSequencer and call these callbacks when sending messages to the Synthesizer in the process_events function.

In the C# version, MidiFileSequencer exposes a delegate called OnSendMessage, which allows registering callbacks to achieve the above. This may be helpful as a reference when implementing the functionality above. https://github.com/sinshu/meltysynth/blob/103dd9c3928411da57cb97fd3d632e3b568f3b55/MeltySynth/src/MidiFileSequencer.cs#L130

Tails commented 4 months ago

Thanks for the effort describing this! Will get on it.