#midi #synthesizer #music #soundfont

rustysynth

A SoundFont MIDI synthesizer written in pure Rust

12 releases (stable)

1.3.1 Sep 8, 2023
1.2.1 Jul 30, 2023
1.2.0 May 22, 2023
1.1.0 Apr 30, 2023
0.1.0 Jan 15, 2023

#34 in Audio

Download history 93/week @ 2024-01-14 161/week @ 2024-01-21 76/week @ 2024-01-28 140/week @ 2024-02-04 92/week @ 2024-02-11 163/week @ 2024-02-18 89/week @ 2024-02-25 227/week @ 2024-03-03 72/week @ 2024-03-10 87/week @ 2024-03-17 71/week @ 2024-03-24 72/week @ 2024-03-31 31/week @ 2024-04-07 87/week @ 2024-04-14 124/week @ 2024-04-21 173/week @ 2024-04-28

420 downloads per month
Used in 5 crates (2 directly)

MIT license

195KB
4.5K SLoC

RustySynth

RustySynth is a SoundFont MIDI synthesizer written in pure Rust, ported from MeltySynth.

Features

  • Suitable for both real-time and offline synthesis.
  • Supports standard MIDI files with additional features including dynamic tempo changing.
  • No dependencies other than the standard library.

Examples

An example code to synthesize a simple chord:

// Load the SoundFont.
let mut sf2 = File::open("TimGM6mb.sf2").unwrap();
let sound_font = Arc::new(SoundFont::new(&mut sf2).unwrap());

// Create the synthesizer.
let settings = SynthesizerSettings::new(44100);
let mut synthesizer = Synthesizer::new(&sound_font, &settings).unwrap();

// Play some notes (middle C, E, G).
synthesizer.note_on(0, 60, 100);
synthesizer.note_on(0, 64, 100);
synthesizer.note_on(0, 67, 100);

// The output buffer (3 seconds).
let sample_count = (3 * settings.sample_rate) as usize;
let mut left: Vec<f32> = vec![0_f32; sample_count];
let mut right: Vec<f32> = vec![0_f32; sample_count];

// Render the waveform.
synthesizer.render(&mut left[..], &mut right[..]);

Another example code to synthesize a MIDI file:

// Load the SoundFont.
let mut sf2 = File::open("TimGM6mb.sf2").unwrap();
let sound_font = Arc::new(SoundFont::new(&mut sf2).unwrap());

// Load the MIDI file.
let mut mid = File::open("flourish.mid").unwrap();
let midi_file = Arc::new(MidiFile::new(&mut mid).unwrap());

// Create the MIDI file sequencer.
let settings = SynthesizerSettings::new(44100);
let synthesizer = Synthesizer::new(&sound_font, &settings).unwrap();
let mut sequencer = MidiFileSequencer::new(synthesizer);

// Play the MIDI file.
sequencer.play(&midi_file, false);

// The output buffer.
let sample_count = (settings.sample_rate as f64 * midi_file.get_length()) as usize;
let mut left: Vec<f32> = vec![0_f32; sample_count];
let mut right: Vec<f32> = vec![0_f32; sample_count];

// Render the waveform.
sequencer.render(&mut left[..], &mut right[..]);

Todo

  • Wave synthesis
    • SoundFont reader
    • Waveform generator
    • Envelope generator
    • Low-pass filter
    • Vibrato LFO
    • Modulation LFO
  • MIDI message processing
    • Note on/off
    • Bank selection
    • Modulation
    • Volume control
    • Pan
    • Expression
    • Hold pedal
    • Program change
    • Pitch bend
    • Tuning
  • Effects
    • Reverb
    • Chorus
  • Other things
    • Standard MIDI file support
    • MIDI file loop extension support
    • Performace optimization

License

RustySynth is available under the MIT license.

No runtime deps