Add all your SoundFont and MIDI files to the StreamingAssets
folder. See the Unity documentation on streaming assets for more information.
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.
The Synthesizer
component loads a SoundFont, which is used by songs. Add a Synthesizer
to your GameObject.
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.
The SongPlayer
component controls MIDI file loading and playback. Add a SongPlayer
to your GameObject.
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”.
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”
Next, select the Audio Mixer and uncheck “Auto Mixer Suspend” in the inspector.
Next, open the “Audio Mixer” panel, select the mixer, click the “Add” button, and select the “FluidSynth” effect.
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.
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.
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
.
Synthesizer
. Must be a streaming asset.Synthesizer
extends MonoBehaviour.
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.
Synthesizer
in the scene. The SongPlayer
will use the SoundFont from this Synthesizer
.SongPlayer
. Must be a streaming asset.SongPlayer
is startedSongPlayer
when the song finishes playing or is stopped. This effectively unloads the song from memory.
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. |
Synthesizer
or a MIDI file in the SongPlayer
StreamingAssets
folder. See the Unity documentation for more information on streaming assets.SongPlayer
SongPlayer
is activeAudioSource
configured with “Play on Awake” and output set to the Audio Mixer that has the “FluidSynth” effectResume()
to resume playing a paused song.Please send questions or comments to support@thunkmonkey.com