SonixMidiSynthesizer

MIDI synthesizer for converting MIDI to audio using SoundFont files.

What is MIDI Synthesis?

MIDI is a note description format - it says "play note C4 for 500ms" but doesn't contain actual audio. A synthesizer converts MIDI to audio using instrument samples from a SoundFont file.

Use it for:

  • Generating reference tracks from note sequences

  • Creating practice audio from lesson data

  • Converting MIDI files to audio

When to Use

ScenarioUse This?Why
Generate reference audioYesFrom lesson notes
Convert MIDI to audioYesCore use case
Play audio fileNoUse SonixPlayer
Real-time MIDI playbackNoNot supported yet

Quick Start

Kotlin

val synth = SonixMidiSynthesizer.create(soundFontPath = "/path/to/soundfont.sf2")

// Synthesize from MIDI file
synth.synthesize(midiPath = "input.mid", outputPath = "output.wav")

// Or synthesize from notes (timing in milliseconds)
val notes = listOf(
MidiNote(note = 60, startTime = 0f, endTime = 500f), // C4 at 0-500ms
MidiNote(note = 62, startTime = 500f, endTime = 1000f) // D4 at 500-1000ms
)
synth.synthesizeFromNotes(notes = notes, outputPath = "output.wav")

synth.release() // Optional - synth is stateless

Swift

let synth = SonixMidiSynthesizer.create(soundFontPath: "/path/to/soundfont.sf2")

// Synthesize from MIDI file
synth.synthesize(midiPath: "input.mid", outputPath: "output.wav")

// Or synthesize from notes
let notes = [
MidiNote(note: 60, startTime: 0, endTime: 500), // C4 at 0-500ms
MidiNote(note: 62, startTime: 500, endTime: 1000) // D4 at 500-1000ms
]
synth.synthesizeFromNotes(notes: notes, outputPath: "output.wav")

Builder Pattern (Advanced)

Kotlin

val synth = SonixMidiSynthesizer.Builder()
.soundFontPath("/path/to/soundfont.sf2")
.sampleRate(48000)
.onError { error -> println("Error: $error") }
.build()

Swift

let synth = SonixMidiSynthesizer.Builder()
.soundFontPath(path: "/path/to/soundfont.sf2")
.sampleRate(rate: 48000)
.build()

SoundFont Files

SoundFont (.sf2 or .sf3) files contain instrument samples:

  • Use high-quality SoundFonts for better audio

  • File size affects memory usage

  • SF3 format is compressed (smaller files)

Platform Notes

iOS/Android

  • Uses FluidSynth library for synthesis

  • Runs synchronously (consider background thread for large files)

  • Output is always WAV format

Common Pitfalls

  1. Missing SoundFont: Must provide valid .sf2 or .sf3 file

  2. Large MIDI files: Synthesis time scales with MIDI duration

  3. Note timing: MidiNote times are in milliseconds (not seconds)

  4. Output format: Always outputs WAV; use SonixEncoder to convert

See also

For lesson-specific synthesis

For converting WAV to MP3/M4A

For note specification format

Types

Link copied to clipboard
class Builder

Builder for advanced SonixMidiSynthesizer configuration.

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Sample rate for output audio.

Link copied to clipboard

Path to the SoundFont file being used.

Link copied to clipboard

FluidSynth version string for debugging.

Functions

Link copied to clipboard
fun release()

Release resources. Currently a no-op as MidiSynthesizer is stateless, but included for API consistency.

Link copied to clipboard
fun synthesize(midiPath: String, outputPath: String): Boolean

Synthesize a MIDI file to audio.

Link copied to clipboard
fun synthesizeFromNotes(notes: List<MidiNote>, outputPath: String): Boolean

Synthesize audio from a list of MIDI notes.

Link copied to clipboard
fun synthesizeFromPitchFile(pitchFilePath: String, outputPath: String, lessonTonicHz: Float, parentTonicHz: Float = lessonTonicHz): Boolean

Synthesize audio from a pitch contour file.

Link copied to clipboard
fun synthesizeMidi(midiData: ByteArray, outputPath: String): Boolean

Synthesize audio from raw MIDI data bytes.