MeltySynth 2.2.2

.NET Standard 2.1
Install-Package MeltySynth -Version 2.2.2
dotnet add package MeltySynth --version 2.2.2
<PackageReference Include="MeltySynth" Version="2.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MeltySynth --version 2.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MeltySynth, 2.2.2"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install MeltySynth as a Cake Addin
#addin nuget:?package=MeltySynth&version=2.2.2

// Install MeltySynth as a Cake Tool
#tool nuget:?package=MeltySynth&version=2.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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 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:

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 Standard 2.1.
  • No memory allocation in the rendering process.
  • No unsafe code.

Installation

.NET Standard 2.1 or higher

The NuGet package is available:

Install-Package MeltySynth

All the classes are in the MeltySynth namespace:

using MeltySynth;

If you don't like DLLs, copy all the .cs files to your project.

.NET Framework 4.6.1 or higher

.NET Framework is not recommended, but you can use MeltySynth by following the steps below.

  1. Install the System.Memory package to your project.
  2. Copy NetFrameworkSupport.cs to your project.
  3. Copy all the .cs files of MeltySynth to your project.

Demo

A demo song generated with Arachno SoundFont

https://www.youtube.com/watch?v=xNgsIJKxPkI

A Doom port written in C# with MIDI music playback

https://www.youtube.com/watch?v=_j1izHgIT4U

A virtual keyboard made with Raylib-CsLo

https://www.youtube.com/watch?v=a8vuIq4JKhs

Examples

MIDI file player for various audio drivers

Handling SoundFont

To enumerate samples in the SoundFont:

var soundFont = new SoundFont("TimGM6mb.sf2");

foreach (var sample in soundFont.SampleHeaders)
{
    Console.WriteLine(sample.Name);
}

To enumerate instruments in the SoundFont:

var soundFont = new SoundFont("TimGM6mb.sf2");

foreach (var instrument in soundFont.Instruments)
{
    Console.WriteLine(instrument.Name);
}

To enumerate presets in the SoundFont:

var soundFont = new SoundFont("TimGM6mb.sf2");

foreach (var preset in soundFont.Presets)
{
    var bankNumber = preset.BankNumber.ToString("000");
    var patchNumber = preset.PatchNumber.ToString("000");

    Console.WriteLine($"{bankNumber}:{patchNumber} {preset.Name}");
}

Handling synthesizer

To change the instrument to play, send a program change command (0xC0) to the synthesizer:

// Create the synthesizer.
var sampleRate = 44100;
var synthesizer = new Synthesizer("TimGM6mb.sf2", sampleRate);

// Change the instrument to electric guitar (#30).
synthesizer.ProcessMidiMessage(0, 0xC0, 30, 0);

// 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);

To play a melody, render the sound as a sequence of short blocks:

// Create the synthesizer.
var sampleRate = 44100;
var synthesizer = new Synthesizer("TimGM6mb.sf2", sampleRate);

// The length of a block is 0.1 sec.
var blockSize = sampleRate / 10;

// The entire output is 3 sec.
var blockCount = 30;

// Define the melody.
// A single row indicates the start timing, end timing, and pitch.
var data = new int[][]
{
    new int[] {  5, 10, 60 },
    new int[] { 10, 15, 64 },
    new int[] { 15, 25, 67 }
};

// The output buffer.
var left = new float[blockSize * blockCount];
var right = new float[blockSize * blockCount];

for (var t = 0; t < blockCount; t++)
{
    // Process the melody.
    foreach (var row in data)
    {
        if (t == row[0]) synthesizer.NoteOn(0, row[2], 100);
        if (t == row[1]) synthesizer.NoteOff(0, row[2]);
    }

    // Render the block.
    var blockLeft = left.AsSpan(blockSize * t, blockSize);
    var blockRight = right.AsSpan(blockSize * t, blockSize);
    synthesizer.Render(blockLeft, blockRight);
}

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

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on MeltySynth:

Repository Stars
sinshu/managed-doom
A Doom port written in C Sharp
Version Downloads Last updated
2.2.2 69 5/6/2022
2.2.1 102 4/15/2022
2.2.0 301 1/7/2022
2.1.0 276 9/30/2021
2.0.0 198 8/4/2021
2.0.0-alpha 138 8/4/2021
1.1.3 173 6/18/2021
1.1.2 213 6/5/2021
1.1.1 161 6/4/2021
1.1.0 217 5/16/2021
1.0.3 153 5/3/2021
1.0.2 138 4/29/2021
1.0.1 122 4/29/2021
1.0.0 153 4/29/2021
0.9.1 129 4/25/2021
0.9.0 141 4/24/2021