Skip to main content

SonixMidiSynthesizer

Convert MIDI notes or files to audio using SoundFont instruments.

Quick Start​

Kotlin​

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

// Synthesize from a list of notes
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
MidiNote(note = 64, startTime = 1000f, endTime = 1500f) // E4 at 1000-1500ms
)
synth.synthesizeFromNotes(notes = notes, outputPath = "/path/to/output.wav")

synth.release()

Swift​

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

let notes = [
MidiNote(note: 60, startTime: 0, endTime: 500),
MidiNote(note: 62, startTime: 500, endTime: 1000),
MidiNote(note: 64, startTime: 1000, endTime: 1500)
]
synth.synthesizeFromNotes(notes: notes, outputPath: "/path/to/output.wav")

synth.release()

Configuration​

Factory Method​

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

Builder​

For custom sample rate or error handling:

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()

Builder Parameters​

MethodTypeDefaultDescription
soundFontPathString—Path to .sf2 or .sf3 file (required)
sampleRateInt44100Output sample rate in Hz
onError(String) -> Unit—Error callback

Synthesis Methods​

From MIDI File​

val success: Boolean = synth.synthesize(
midiPath = "/path/to/input.mid",
outputPath = "/path/to/output.wav"
)

From MIDI Data Bytes​

val midiData: ByteArray = ...  // Standard MIDI File bytes
val success: Boolean = synth.synthesizeMidi(
midiData = midiData,
outputPath = "/path/to/output.wav"
)

From Note List​

Note timing is in absolute milliseconds:

val notes = listOf(
MidiNote(note = 60, startTime = 0f, endTime = 500f),
MidiNote(note = 62, startTime = 500f, endTime = 1000f)
)
val success: Boolean = synth.synthesizeFromNotes(
notes = notes,
outputPath = "/path/to/output.wav"
)

From Pitch File​

Generate audio from a pitch contour file (used in music education):

val success: Boolean = synth.synthesizeFromPitchFile(
pitchFilePath = "/path/to/melody.pitchPP",
outputPath = "/path/to/output.wav",
lessonTonicHz = 261.63f, // C4 tonic
parentTonicHz = 261.63f // defaults to lessonTonicHz
)

Method Summary​

MethodInputOutputDescription
synthesizeMIDI file pathWAV fileConvert MIDI file to audio
synthesizeMidiMIDI bytesWAV fileConvert raw MIDI data to audio
synthesizeFromNotesList<MidiNote>WAV fileConvert note list to audio
synthesizeFromPitchFilePitch file pathWAV fileConvert pitch contour to audio
release——Release resources

Properties​

PropertyTypeDescription
soundFontPathStringPath to the SoundFont file
sampleRateIntOutput sample rate
versionStringFluidSynth version string

MidiNote​

PropertyTypeDescription
noteIntMIDI note number (60 = C4, 72 = C5)
startTimeFloatStart time in milliseconds
endTimeFloatEnd time in milliseconds
durationFloatNote duration in milliseconds (computed: endTime - startTime)

Common Patterns​

Generate and Play Reference Audio​

val synth = SonixMidiSynthesizer.create(soundFontPath = sfPath)

val notes = listOf(
MidiNote(note = 60, startTime = 0f, endTime = 500f),
MidiNote(note = 64, startTime = 500f, endTime = 1000f),
MidiNote(note = 67, startTime = 1000f, endTime = 1500f)
)

val outputPath = "${cacheDir}/reference.wav"
if (synth.synthesizeFromNotes(notes, outputPath)) {
val player = SonixPlayer.create(outputPath)
player.play()
}

Convert to Compressed Format​

// Synthesize to WAV
synth.synthesizeFromNotes(notes, outputPath = "output.wav")

// Convert to MP3 for sharing
val wavData = SonixDecoder.decode("output.wav", targetSampleRate = null)
if (wavData != null) {
SonixEncoder.encode(data = wavData, outputPath = "output.mp3", format = "mp3")
}

Next Steps​