MeltySynth
MeltySynth is a SoundFont synthesizer written in C#. The purpose of this project is to provide a MIDI music playback functionality for any .NET applications without suffering from complicated dependencies. The codebase is lightweight and can be applied to any audio drivers which support streaming audio, such as SFML.Net, Silk.NET, OpenTK, and NAudio.
The entire code is heavily inspired by the following projects:
- C# Synth by Alex Veltsistas
- TinySoundFont by Bernhard Schelling
An example code to synthesize a simple chord:
// Create the synthesizer.
var sampleRate = 44100;
var synthesizer = new Synthesizer("TimGM6mb.sf2", sampleRate);
// Play some notes (middle C, E, G).
synthesizer.NoteOn(0, 60, 100);
synthesizer.NoteOn(0, 64, 100);
synthesizer.NoteOn(0, 67, 100);
// The output buffer (3 seconds).
var left = new float[3 * sampleRate];
var right = new float[3 * sampleRate];
// Render the waveform.
synthesizer.Render(left, right);
Another example code to synthesize a MIDI file:
// Create the synthesizer.
var sampleRate = 44100;
var synthesizer = new Synthesizer("TimGM6mb.sf2", sampleRate);
// Read the MIDI file.
var midiFile = new MidiFile("flourish.mid");
var sequencer = new MidiFileSequencer(synthesizer);
sequencer.Play(midiFile, false);
// The output buffer.
var left = new float[(int)(sampleRate * midiFile.Length.TotalSeconds)];
var right = new float[(int)(sampleRate * midiFile.Length.TotalSeconds)];
// Render the waveform.
sequencer.Render(left, right);
Features
- No dependencies other than .NET Core 3.1.
- No memory allocation in the rendering process.
- No unsafe code.
Installation
The NuGet package is available:
Install-Package MeltySynth
If you don't like DLLs, copy all the .cs files to your project.
Demo
Here is a demo song generated with Arachno SoundFont.
https://www.youtube.com/watch?v=xNgsIJKxPkI
Examples
- MIDI file player for SFML.Net
- MIDI file player for Silk.NET (OpenAL)
- MIDI file player for Silk.NET (SDL)
- MIDI file player for OpenTK
- MIDI file player for SDL2#
- MIDI file player for MonoGame
- MIDI file player for Raylib-cs
- MIDI file player for NAudio
- MIDI file player for TinyAudio
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
- Loop extension support
- Performace optimization
License
MeltySynth is available under the MIT license.
References
-
SoundFont® Technical Specification
http://www.synthfont.com/SFSPEC21.PDF -
Polyphone Soundfont Editor
Some of the test cases were generated with Polyphone.
https://www.polyphone-soundfonts.com/ -
Freeverb by Jezar at Dreampoint
The implementation of the reverb effect is based on Freeverb.
https://music.columbia.edu/pipermail/music-dsp/2001-October/045433.html