MusIT

WebGL build with Maestro MPTK


So late!

Why now? The persistent and frustrating Unity issue with looping inside a sample from a web build has finally been resolved by Unity. https://issuetracker.unity3d.com/issues/webgl-a-looping-audio-sounds-different-on-webgl-than-in-the-editor-slash-native-desktop-player

MPTK webGL build has been tested with Unity  6 and Unity 2022.3.

Working mode and Historics

When webGL build is selected in the editor, some changes are applied to MPTK version 2.16:

  • Core mode is set to false: each couple note / instrument are played in a dedicated AudioSource.
  • Coroutines are used for MIDI sequencer (threads are not allowed with webGL, perhaps one day …).

The MPTK non core mode was the legacy mode for playing generated samples. The initial version released in 2018 achieved significant success, despite sound quality was suboptimal. So, I have decided to build a real synth process based on the OnAudioFilterRead callback.

With core mode enabled, one AudioSource is able to play all samples (so, for each note-on) with a very low latency and an excellent stable timing. Also, there is only one AudioSource for each MIDI Synth (MidiFilePlayer, MisiStreamPlayer, …).

The legacy mode was notified as deprecated and to be removed in a future major version. Unfortunately, callback OnAudioFilterRead with AudioSource is not implemented for web. Therefore, non core mode is mandatory for webGL build … and finally this mode will be retained with next MPTK versions and named Legacy Mode!

Limitations

  • Coroutine for MIDI sequencer is not perfect for very precise tempo.
  • Sound effects real-time modification by the MPTK API when playing is unavailable (but there is a solution, perhaps for a future version).
  • The sound quality is inferior to other platforms such as macOS, iOS Windows, and Android. Look above Working Mode chapter to understand why..
  • Audio Spatializer plugins like Steam Audio seems not implemented, so prefab MidiSpatializer will not work.
  • Real-time loading soundfont from the web is not working (not yet compliant with legacy mode) but switching on local soundfont do the job.
  • The plugins MidiKeyboard is not available for WebGL build. Some tests in progress with this Unity package

Good points

  • The interactivity between your gameplay and the music works well. This is one of Maestro MPTK’s strengths: influencing the music by script (or the inverse!) according to the scenarios you have imagined.
  • Spatialization based on distance between MPTK Prefab and Listener is working.
  • Real-time loading soundfont from local soundfont is working.
  • SoundFont effects by the MPTK API is working but not dynamic change.
  • Low latency on user action!
  • Compatible with the Free MPTK! If you publish a web app, I’d be happy if you could give credit to MPTK somewhere in your app.

Quick how-to from a newer project

  1. Create a fresh project with Unity version >= 2022.3.
  2. Import Maestro MPTK Pro or Free.
  3. Switch to WebGL build (Build settings) if not already done.
  4. From the menu Maestro / SoundFont setup,
    1. import a SoundFont,
    2. extract samples with default choices “Compressed In Memory” and “PCM”
  5. Select the project you want to build  (try with MPTK demos like TestMidiFilePlayerScript).
  6. Build and run your project. When the webpage pop up, your project will be loaded and run. A first user action is requested to activate the sound in the web page.

Tip: If you want to build for another platform, switch back to ‘Windows, Mac, Linux platform’. Unfortunately, all audio files will be converted by Unity to be compliant, and that takes time — but the conversion will run only once per platform.

Want to test?

The demo scene (Demo/Builder/ScenesDemonstration) has been build for webGL and deployed on itch.io
See here: Unity MIDI Player Tool Kit on WebGL

This demo contains all the scenes available with the Pro version

WebGL Demos

Next Step

  • Release 2.16.0 is mandatory for webGL build, soon available!
  • Improve Legacy mode with your input.
  • A full game with music as a key role. I have the ideas – therefore, now to be transformed in the real world, summer will be hot!

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.

Demo on itch!

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!