sinshu / py-meltysynth

A SoundFont MIDI synthesizer written in pure Python
Other
17 stars 1 forks source link
audio meltysynth midi python soundfont synthesizer

Py-MeltySynth

Py-MeltySynth is a SoundFont MIDI synthesizer written in pure Python, ported from MeltySynth for C#. I believe this is the first fully functional SoundFont synthesizer made with Python without external libraries.

Features

Demo

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

Demo video

Development status

It works, but it's slow. Without external libraries, sample-by-sample audio processing seems tough for Python... But Python is said to be becoming more performance oriented, so it should be useful in the future.

Below is a comparison of the time it took to render a MIDI file in several languages. The MIDI file is flourish.mid (90 seconds) and the SoundFont used is TimGM6mb.sf2. Python is still slow, but it is certainly improving in speed as the versions go up.

The bar graph compares the processing speeds of several programming languages. Python is notably slower, taking dozens of times longer than the other languages in comparison.

Examples

An example code to synthesize a simple chord:

import meltysynth as ms

# Load the SoundFont.
sound_font = ms.SoundFont.from_file("TimGM6mb.sf2")

# Create the synthesizer.
settings = ms.SynthesizerSettings(44100)
synthesizer = ms.Synthesizer(sound_font, settings)

# 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).
left = ms.create_buffer(3 * settings.sample_rate)
right = ms.create_buffer(3 * settings.sample_rate)

# Render the waveform.
synthesizer.render(left, right)

Another example code to synthesize a MIDI file. It will take a long time 😉

import meltysynth as ms

# Load the SoundFont.
sound_font = ms.SoundFont.from_file("TimGM6mb.sf2")

# Create the synthesizer.
settings = ms.SynthesizerSettings(44100)
synthesizer = ms.Synthesizer(sound_font, settings)

# Load the MIDI file.
midi_file = ms.MidiFile.from_file("flourish.mid")

# Create the MIDI sequencer.
sequencer = ms.MidiFileSequencer(synthesizer)
sequencer.play(midi_file, False)

# The output buffer.
left = ms.create_buffer(int(settings.sample_rate * midi_file.length))
right = ms.create_buffer(int(settings.sample_rate * midi_file.length))

# Render the waveform.
sequencer.render(left, right)

Todo

License

Py-MeltySynth is available under the MIT license.