MidiPlayer¶
Inherits: AudioStreamPlayer < Node < Object
A MIDI and SoundFont player class.
Description¶
The MidiPlayer class can play MIDI files. It does this using a collection of sounds called SoundFont which was originally invented in the early 1990s by Creative Labs for their SoundBlaser AWE32 sound card. The MIDI specification is even older from 1983.
Although these technologies are old, they are still being used today in music industry because they are incredibly handy. While SoundFont has become less relevant with the development of sampling, MIDI specifically has seen continuous use very with minimal changes to the standard since inception.
In order to use this class, the node should be added to the scene tree. Create a new AudioStreamGenerator via inspector in AudioStreamPlayer.stream and then load a MidiFile containing a SoundFont should be set to soundfont and a MidiFile midi song set to midi. For instance:
func _ready():
$MidiPlayer.midi = preload("res://moonlight.mid")
$MidiPlayer.soundfont = preload("res://piano.sf2")
$MidiPlayer.play()
You can find MIDI songs and General MIDI SoundFonts online. Here are just a few completely free ones that you might want to look for: ChoriumRevA.sf2
, WeedsGM3.sf2
, SGM-V2.01FluidR3_GM.sf2
, TimGM6mb.sf2
(close in sound with the original Sound Blaster soundfont), Timbres Of Heaven GM_GS_XG_SFX V 3.0 Final.sf2
(very high quality).
It should be possible to play notes via InputEventMIDI as well.
List of keywords:
channel:
And audio channels (or tracks) is the audio route the sound takes and allows one instrument to play. Multiple channels allow for different instruments to play on top of each other. Channel 10 is always set to Drums and is handled differently. The number of channels depends on implementation most commonly 16. Tiny Sound Font (which MidiPlayer uses) allocates channels dynamically so you can have as many as you want.
voice:
A voice is related to a channel except that this is the actual audio you hear. Each channel has a voice called a Channel Voice. Some instruments or sound effects may also use voices. The number of voices depends on the implementation. On a typical instrument can be anywhere from 1 (monophonic) to 64 (polyphonic). Similar to channels, voices are allocated dynamically so you may have as many as you wish. There is a way to hard limit the max voices but hasn’t been implemented.
program:
A program is the tone quality. Drums has a unique program for playing individual samples, regular notes have a program, many effects have a program. Note that reverb and chorus have not been implemented.
preset/instrument:
This is the instrument for example Piano, Harpsichord, etc. It uses various PCM samples mapped to regions of the keyboard. Drums (usually set to channel 10) are special in that each key has a unique sample and the entire instrument is handled by a drum program.
bank:
All samples inside a SoundFont are organized in groups called banks. A bank is commonly 128 samples. In practice most SoundFonts define their own internal sample organization.
note/message:
Sometimes called message as it represents a note ON/OFF event. This is the note (or key) to play. It must be a value between 0 and 127. A value of 60 is middle C (C4).
volume/velocity:
Commonly called velocity. A value of 0.0 means lowest volume (mute) while a value of 1.0 is loudest volume.
Methods¶
Signals¶
- loop_finished ( )
Emitted when the MIDI song finishes playing one loop. This signal is emitted only if looping is true
.
Note: When not looping, a finished
signal is emitted instead and the MidiPlayer will automatically stop itself when reaching the end of the song.
Property Descriptions¶
- bool looping
Default | true |
Setter | set_looping(value) |
Getter | is_looping() |
When set to true
, midi file will loop forever.
- MidiFile midi
Setter | set_midi(value) |
Getter | get_midi() |
This is the MidiFile resource that contains the SoundFont data.
- float midi_speed
Default | 1.0 |
Setter | set_midi_speed(value) |
Getter | get_midi_speed() |
Controls the midi file playback speed.
- MidiFile soundfont
Setter | set_soundfont(value) |
Getter | get_soundfont() |
This is the MidiFile resource that contains the idi song data.
Method Descriptions¶
Returns the left-right panning of the specified channel.
Get the pitch range of a channel.
Get the pitch wheel value for the specified channel.
Get the preset sample bank for the specified channel.
Get the preset index for the specified channel.
Get the preset number for the specified channel.
Get the tuning of a channel.
Get the volume of a channel.
MIDI controls are used to produce specialized effects.
Stop playing a specific note on a specific channel.
- void channel_note_off_all ( int channel )
Stop all notes on a channel.
Start playing a note on a specific channel.
Defines the bank number for the specified channel. Bank is a collection of sound samples.
Defines the bank and preset for a channel.
Defines left and right panning of a channel.
The the pitch range for a channel.
Set the pitch wheel value for a channel.
Set the preset index for a channel.
Set the preset number of a channel. Drums is usually preset 10.
Set the tuning of a channel. Tuning is a float value between -1.0 and 1.0.
Set volume of a channel.
- PoolStringArray get_preset_names ( ) const
Returns a list of preset names stored in the SoundFont.
Stop playing a specific note.
- void note_off_all ( )
Stop playing all notes.
Start playing a single note.