Fluid MIDI

Requirements

Quick Start

1. Import SoundFont and MIDI files

Add all your SoundFont and MIDI files to the StreamingAssets folder. See the Unity documentation on streaming assets for more information.

Streaming Assets

2. Select host GameObjects

MIDI player components must be hosted in GameObjects. Create new ones or select existing ones. One possible setup could be to have one GameObject for the synthesizer and another GameObject for the songs. You can also just have them all in the same GameObject.

GameObjects

3. Create Synthesizer

The Synthesizer component loads a SoundFont, which is used by songs. Add a Synthesizer to your GameObject.

Synthesizer

If a SoundFont is detected in your streaming assets, the Synthesizer will automatically be configured to use it. Otherwise, select the SoundFont you want to use.

4. Create SongPlayer

The SongPlayer component controls MIDI file loading and playback. Add a SongPlayer to your GameObject.

SongPlayer

If a Synthesizer component is detected, the SongPlayer will be automatically configured to use it. Otherwise, select a Synthesizer for your SongPlayer. Next, select a song. This must be a MIDI file located in your streaming assets. If you want the song to start playing when the SongPlayer is started, select “Play On Start”.

5. Configure Audio Mixer

Song audio is output through an Audio Mixer effect. If you do not have an Audio Mixer, you can add one using the “Assets” menu by selecting “Create > Audio Mixer”

Mixer Asset

Next, select the Audio Mixer and uncheck “Auto Mixer Suspend” in the inspector.

Mixer Config

Next, open the “Audio Mixer” panel, select the mixer, click the “Add” button, and select the “FluidSynth” effect.

Mixer Panel

6. Configure AudioSource

An AudioSource must be configured to “prime” the Audio Mixer. Add an AudioSource to any GameObject and ensure that it is set to “Play On Awake” and outputs to the Audio Mixer that contains the “FluidSynth” effect.

Audio Source

7. Play the Scene

If everything is configured correctly, you should now hear music playing when you start the scene. If you do not hear anything, view the console to see if any errors have been logged.

Reference

Synthesizer

A Synthesizer does not play any music on its own. It simply hosts a SoundFont, which can be used by a SongPlayer. When a Synthesizer is enabled, it will load the SoundFont, so it will be immediately ready for use. Otherwise, the SoundFont will be loaded on demand for each SongPlayer.

Inspector

Synthesizer

Scripting

Synthesizer extends MonoBehaviour.

SongPlayer

A SongPlayer is the main component that controls song loading and playback. You should have a different SongPlayer for each song. When a SongPlayer is enabled, it will load the song, so it is ready to play. When a SongPlayer is disabled, no song is loaded, so there may be a delay before playback can start.

Inspector

SongPlayer

Scripting

SongPlayer extends MonoBehaviour.

Property Description
int Ticks Current playback ticks. Useful for synchronizing game events to music.
float Gain Get or set the song gain. Shound be a value between 0 and 100, with 0.2 as the default.
float Tempo Get or set the song tempo multiplier. Should be value between 0.001 and 1000, with 1 as the default.
bool IsReady True if the song has been loaded and playback can begin with minimal latency.
bool IsPlaying True if the song is playing. A paused song is still considered to be playing.
bool IsPaused True if the song is paused. A paused song is still considered to be playing.
bool IsDone True if the song is finished playing, either becaue it was stopped or reached the end.
Method Description
static void PauseAll() Pauses all playing songs.
static void ResumeAll() Resumes all paused songs.
static void StopAll() Stops all playing songs.
void Play() Plays the song. If the SongPlayer is not enabled, this method will enable it and load the song. Playback always begins from the start of the song.
void Stop() Stops the song. If it was paused, it can no longer be resumed.
void Pause() Pauses the song if it is playing.
void Resume() Resumes the song if it is paused.
bool Seek(int ticks) Moves the platyback position to the specified ticks. The song must be playing for this to work. Returns true if successful. A seek may fail if it is requested before a previous seek has completed.
bool IsChannelEnabled(int channel) Returns true if the specified MIDI channel is enabled. Channel should be between 1 and 16, inclusive.
void EnableChannels(params int[] channels) Enables the specified MIDI channels. Channels should be between 1 and 16, inclusive.
void DisableChannels(params int[] channels) Disables the specified MIDI channels. Disabled channels do not produce sounds. Channels should be between 1 and 16, inclusive.

Troubleshooting

I can’t select a SoundFont in the Synthesizer or a MIDI file in the SongPlayer

I don’t hear any music when I play my scene

I hear music in the editor but not in the player

Song does not play after pausing

Support

Please send questions or comments to support@thunkmonkey.com