Skip to content

Converting Between Formats

spessasus edited this page Oct 15, 2024 · 2 revisions

Converting between various file formats

This guide demonstrates how to convert various file formats using spessasynth_lib. Whether you're working with SoundFont files (SF2, SF3), Downloadable Sounds (DLS), or MIDI-related formats (RMI, MIDI), these examples will help you perform conversions quickly and efficiently.

Important

The input file binary is named input and the output binary file is named output in the examples.

Table of contents

SF2 To SF3

Note

This example uses soundfont3 compression. Make sure you've read this

const sfont = loadSoundFont(input);
const output = sfont.write({
    compress: true,
    compressionQuality: 0.5, // adjust this to your liking
    compressionFunction: EncodeVorbisFunction // make sure to obtain the function for compression
});

DLS to SF2

const sfont = loadSoundFont(input);
const output = sfont.write();

RMI To MIDI

const RMID = new MIDI(input);
const output = writeMIDIFile(RMID);

RMI To SF2/SF3

const RMID = new MIDI(input);
const sfont = loadSoundFont(RMID.embeddedSoundFont);
const output = sfont.write();

SF2/DLS + MIDI To RMI

This uses two inputs, input1 for MIDI and input2 for SoundFont.

const mid = new MIDI(input1);
const sfont = loadSoundFont(input2);
// compress this if you want
const sfontBinary = sfont.write();
const output = writeRMIDI(
    sfontBinary,
    mid,
    sfont,
    0, // bank offset: adjust this if necessary
    "utf-8", // encoding: utf-8 recommended
    {
        // all the values below are examples, showing how to copy MIDI data to the RMI file
        name: mid.midiName,
        copyright: mid.copyright,
        engineer: sfont.soundFontInfo["IENG"],
    },
    true // adjust program changes: recommended for self-contained files
);

DLS RMI To SF2 RMI

const dlsRMID = new MIDI(input);
const sfont = loadSoundFont(dlsRMID.embeddedSoundFont);
const sfontBinary = sfont.write();
const output = writeRMIDI(
    sfontBinary,
    dlsRMID,
    sfont,
    dlsRMID.bankOffset, // bank offset gets detected for DLS rmi
    "utf-8", // encoding: utf-8 recommended
    {
        // here we try to extract the metadata from the file, then fall back to embedded MIDI
        name: dlsRMID.RMIDInfo["INAM"] || dlsRMID.midiName,
        copyright: dlsRMID.RMIDInfo["ICOP"] || dlsRMID.copyright,
        engineer: sfont.soundFontInfo["IENG"],
        artist: dlsRMID.RMIDInfo["IART"],
        // both IPRD and IALB represent album name
        album: dlsRMID.RMIDInfo["IPRD"] || dlsRMID.RMIDInfo["IALB"],
        genre: dlsRMID.RMIDInfo["IGNR"],
        comment: dlsRMID.RMIDInfo["ICMT"],
        // either use the embedded one or today                     
        creationDate: dlsRMID.RMIDInfo["ICRD"] || new Date().toDateString()
    },
    false // adjust program changes: recommend false for that one
);