MusIT

Loading SoundFont at run-time


This feature allow you to create an MPTK runtime without including a SoundFont at start. An enhanced API will facilitate downloading, caching, and loading a SoundFont while your application is running.

The first benefit is providing a smaller application to your store. Additionally, you’ll be able to offer different sets of instruments to your users, which is perfect for implementing your in-app purchasing feature.

As usual, a demo is provided. So, in the MidiPlayer.Demo assembly, look at the scene TestLoadingExternalSoundFont and the script TestLoadSF.cs from the folder Assets\MidiPlayer\Demo\ProDemos\Script.

In this demo, the SF https://mptkapi.paxstellar.com/Piano.sf2 is loaded when the app start. This SoundFont contains only one preset (piano, of course) but it’s possible to load a SF with multiple presets (piano, sax, guitar) like in this demo and with the quality level that you want.

Demo SoundFont loading at run-time

While it may seem obvious, it’s important to clarify that MusIT (the company offering Maestro Unity packages) will not provide any SoundFonts or hosting sites. As an editor, it will be your responsibility to create or reuse SoundFonts (taking care with any potential copyright issues) and host them on your website.

For detailed instructions on where to find SoundFonts and how to create them, you can refer to the guide I’ve provided here: Setting Up MPTK: Adding SoundFonts.

Look here for detailed information about the API.

Scripts example, just one line for loading a SF!

void Start()
{

    // Set OnEventPresetLoaded Unity Start event.
    // Not in Awake because MidiPlayerGlobal could be not yet available.
    MidiPlayerGlobal.OnEventPresetLoaded.AddListener(EndLoadingSF);

    // Show values on the UI
    TextInitialSoundFont.text = URLSoundFontAtStart;
    InputURLSoundFontAtRun.text = URLSoundFontAtRun;

    // Start using the SF https://mptkapi.paxstellar.com/Piano.sf2
    // defined in the inspector. 
    // If not yet available in the cache, the SF is downloaded. 
    if (!string.IsNullOrEmpty(URLSoundFontAtStart))
    {
        Debug.Log($"Demo - The SoundFont {URLSoundFontAtStart} is defined in the inspector for LoadingSoundFontAtRuntime, load it at start.");
        MidiPlayerGlobal.MPTK_LoadLiveSF(pPathSF: URLSoundFontAtStart, useCache: ToggleSoundFontCache.isOn, log: true);
    }
}

 /// <summary>@brief
 /// Run when SF is loaded.
 /// </summary>
 public void EndLoadingSF()
 {
     // when SoundFont is dynamically loaded, MidiPlayerGlobal.ImSFCurrent.SoundFontName not contains name - TBD
     
     Debug.LogFormat($"End loading: '{MidiPlayerGlobal.ImSFCurrent?.SoundFontName}' Status: {MidiPlayerGlobal.MPTK_StatusLastSoundFontLoaded}");
     Debug.Log("Load statistique");
     Debug.Log($"   Time To Download SF:     {Math.Round(MidiPlayerGlobal.MPTK_TimeToDownloadSoundFont.TotalSeconds, 3)} second");
     Debug.Log($"   Time To Load SoundFont:  {Math.Round(MidiPlayerGlobal.MPTK_TimeToLoadSoundFont.TotalSeconds, 3)} second");
     Debug.Log($"   Time To Load Samples:    {Math.Round(MidiPlayerGlobal.MPTK_TimeToLoadWave.TotalSeconds, 3).ToString()} second");
     Debug.Log($"   Presets Loaded: {MidiPlayerGlobal.MPTK_CountPresetLoaded}");
     Debug.Log($"   Samples Loaded: {MidiPlayerGlobal.MPTK_CountWaveLoaded}");
 }

  void Update()
  {
      // MPTK_SoundFontIsReady: loading status for SoundFont from resources folder.
      //      False at start.
      //      True when a SF defined in the Maestro setup is ready.
      ToggleSoundFontReady.isOn = MidiPlayerGlobal.MPTK_SoundFontIsReady;

      // MPTK_SoundFontLoaded: loading status for SoundFont from external file or URL.
      //      False at start.
      //      True when a SF downloaded or loaded from the cache is ready.
      ToggleSoundFontLoaded.isOn = MidiPlayerGlobal.MPTK_SoundFontLoaded;
  }

SoundFont(R) is a registered trademark of E-mu Systems, Inc.

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.

Get MPTK from the Unity store

If you like Midi Player Tool Kit, please leave a review on the Asset Store. It’s very appreciated!!!

Maestro MPTK on ChatGPT!

From various MPTK documentation sources, @DarkSky42 has created a custom LLM based on ChatGPT. You can now ask all the questions you want and get a good level of answer: request code example, verify your source code, explain the MPTK demo …

Contact

If you have questions, please don’t hesitate to contact us via the dedicated Unity forum or our Discord  channel.

Reach the Discord archive by topic.

We are always happy to discuss your projects!