Class Midi
- Namespace
- SpessaSharp.MIDI
- Assembly
- SpessaSharp.dll
Midi is the base of a complete MIDI file.
public sealed class Midi
- Inheritance
-
Midi
- Inherited Members
Fields
ExtraMetadata
Any extra metadata found in the file. These messages were deemed "interesting" by the parsing algorithm
public readonly List<MidiMessage> ExtraMetadata
Field Value
Lyrics
An array containing the lyrics of the sequence.
public readonly List<MidiMessage> Lyrics
Field Value
PortChannelOffsetMap
An array of channel offsets for each MIDI port, using the SpessaSynth method. The index is the port number and the value is the channel offset.
public readonly List<int> PortChannelOffsetMap
Field Value
RmidiInfo
public readonly Dictionary<RMidi.Info.Key, ArraySegment<byte>> RmidiInfo
Field Value
TempoChanges
The tempo changes in the sequence, ordered from the last change to the first. Each change is represented by an object with a MIDI tick position and a tempo value in beats per minute.
public readonly List<Midi.TempoChange> TempoChanges
Field Value
TimeSignatureChanges
The time signature changes in the sequence. Each change is represented by an object with a MIDI tick position and a time signature.
public readonly List<Midi.TimeSignatureChange> TimeSignatureChanges
Field Value
Tracks
The tracks in the sequence.
public readonly List<Track> Tracks
Field Value
Properties
BankOffset
The bank offset used for RMID files.
public int BankOffset { get; }
Property Value
Duration
The duration of the sequence.
public TimeSpan Duration { get; }
Property Value
EmbeddedSoundBank
The embedded sound bank in the MIDI file, represented as an ArrayBuffer, if available.
public ArraySegment<byte>? EmbeddedSoundBank { get; set; }
Property Value
FileName
The file name of the MIDI sequence, if provided during parsing.
public string? FileName { get; set; }
Property Value
FirstNoteOn
The Tick position of the first note-on event in the MIDI sequence.
public int FirstNoteOn { get; }
Property Value
Format
public MidiFormat Format { get; }
Property Value
IsDLSRMIDI
If the MIDI file is a DLS RMIDI file.
public bool IsDLSRMIDI { get; }
Property Value
IsKaraokeFile
If the MIDI file is a Soft Karaoke file (.kar), this is set to true. https://www.mixagesoftware.com/en/midikit/help/HTML/karaoke_formats.html
public bool IsKaraokeFile { get; }
Property Value
IsMultiPort
Indicates if this file is a Multi-Port MIDI file.
public bool IsMultiPort { get; }
Property Value
this[TimelineEvent]
public MidiMessage this[Midi.TimelineEvent ev] { get; }
Parameters
Property Value
KeyRange
The MIDI key range used in the sequence, represented by a minimum and maximum note value.
public (int Min, int Max) KeyRange { get; }
Property Value
LastVoiceEventTick
The tick position of the last voice event (such as note-on, note-off, or control change) in the sequence.
public int LastVoiceEventTick { get; }
Property Value
Loop
The loop points (in ticks) of the sequence, including both start and end points.
public Midi.MidiLoop Loop { get; set; }
Property Value
MaxBeat
public int MaxBeat { get; }
Property Value
TimeDivision
The time division of the sequence, representing the number of MIDI ticks per beat.
public int TimeDivision { get; }
Property Value
Timeline
public ReadOnlySpan<Midi.TimelineEvent> Timeline { get; }
Property Value
Methods
Apply(SynthesizerSnapshot)
Modifies the sequence in-place according to the locked presets and controllers in the given snapshot. Note that System Parameters fineTune and keyShift are passed to the relative tuning parameters of the channels. Only locked MIDI parameters and controllers are applied.
public void Apply(SynthesizerSnapshot snapshot)
Parameters
snapshotSynthesizerSnapshotThe snapshot to apply.
BeatsToMidiTicks(int)
Converts beats to time in MIDI ticks.
public int BeatsToMidiTicks(int beats)
Parameters
beatsintThe time in beats.
Returns
- int
The time in MIDI ticks.
Copy(Midi)
Copies a MIDI.
public static Midi Copy(Midi mid)
Parameters
midMidiThe MIDI to copy.
Returns
- Midi
The copied MIDI.
Flush(bool)
Updates all internal values of the MIDI.
public void Flush(bool sortEvents = true)
Parameters
sortEventsboolIf the events should be sorted by ticks. Recommended to be true.
From(ArraySegment<byte>, string?)
Loads a MIDI file (SMF, RMIDI, XMF) from a given ArraySegment.
public static Midi From(ArraySegment<byte> bytes, string? fileName = null)
Parameters
bytesArraySegment<byte>The ArraySegment containing the binary file data.
fileNamestringThe optional name of the file, will be used if the MIDI file does not have a name.
Returns
Remarks
This function reads the MIDI file format, extracts the header and track chunks,
and populates the MIDI instance with the parsed data.
It supports Standard MIDI Files (SMF), RIFF MIDI (RMIDI), and Extensible Music Format (XMF).
It also handles embedded soundbanks in RMIDI files.
If the file is an RMIDI file, it will extract the embedded soundbank and store
it in the embeddedSoundFont property of the MIDI instance.
If the file is an XMF file, it will parse the XMF structure and extract the MIDI data.
From(FileInfo)
Loads a MIDI file (SMF, RMIDI, XMF) from a given File.
public static Midi From(FileInfo file)
Parameters
fileFileInfoThe file containing the MIDI data
Returns
GetExtraMetadata(string)
Gets the decoded extra metadata as text and removes any unneeded characters (such as "@T" for karaoke files)
public IEnumerable<string> GetExtraMetadata(string encoding = "Shift_JIS")
Parameters
encodingstringThe encoding to use if the MIDI uses an extended code page.
Returns
Remarks
RMIDI encoding overrides the provided encoding.
GetInfoEncoding()
The encoding of the RMIDI info in file, if specified.
public ArraySegment<byte>? GetInfoEncoding()
Returns
GetName(string)
Gets the MIDI's decoded name.
public string? GetName(string encoding = "Shift_JIS")
Parameters
encodingstringThe encoding to use if the MIDI uses an extended code page.
Returns
Remarks
RMIDI encoding overrides the provided encoding.
GetNoteTimes(int)
Calculates all note times in seconds.
public List<Midi.NoteTime>[] GetNoteTimes(int minDrumLength = 0)
Parameters
minDrumLengthintThe shortest a drum note (channel 10) can be, in seconds.
Returns
- List<NoteTime>[]
An array of 16 channels, each channel containing its notes, with their key number, velocity, absolute start time and length in seconds.
GetRMidiInfo()
public RMidi.Info? GetRMidiInfo()
Returns
GetRMidiInfo(Key)
Gets a given chunk from the RMIDI information, undefined if it does not exist.
public object? GetRMidiInfo(RMidi.Info.Key infoType)
Parameters
Returns
- object
String, Date, ArraySegment or null.
GetUsedProgramsAndKeys(IPresetGetter)
Gets the used programs and keys for this MIDI file with a given sound bank.
public PresetsWithKeyCombinations GetUsedProgramsAndKeys(IPresetGetter getPreset)
Parameters
getPresetIPresetGetterThe Preset provider.
Returns
- PresetsWithKeyCombinations
The output data is a key-value pair: Preset -> (Key-Velocity)
InsertAfter(TimelineEvent, MidiMessage)
public void InsertAfter(Midi.TimelineEvent tl, MidiMessage msg)
Parameters
tlMidi.TimelineEventmsgMidiMessage
InsertBefore(TimelineEvent, MidiMessage)
public void InsertBefore(Midi.TimelineEvent tl, MidiMessage msg)
Parameters
tlMidi.TimelineEventmsgMidiMessage
Iterate()
Iterates over the MIDI file, ordered by the time the events happen.
You probably should use the timeline property
if you're not mutating the MIDI in the iteration loop.
public Midi.Enumerable Iterate()
Returns
MidiTicksToBeats(int)
Converts MIDI ticks to time in beats.
public int MidiTicksToBeats(int ticks)
Parameters
ticksintThe time in MIDI ticks.
Returns
- int
The time in beats.
MidiTicksToSeconds(int)
Converts MIDI ticks to time in seconds.
public double MidiTicksToSeconds(int ticks)
Parameters
ticksintThe time in MIDI ticks.
Returns
- double
The time in seconds.
Exceptions
- ArgumentOutOfRangeException
Zero tempo changes
- InvalidOperationException
Last tempo change is not at tick 0
MidiTicksToSeconds(ReadOnlySpan<TempoChange>, int, int)
Converts MIDI ticks to time in seconds.
public static double MidiTicksToSeconds(ReadOnlySpan<Midi.TempoChange> tempoChanges, int timeDivision, int ticks)
Parameters
tempoChangesReadOnlySpan<Midi.TempoChange>The tempo changes in the sequence, ordered from the last change to the first.
timeDivisionintThe time division of the sequence, representing the number of MIDI ticks per beat.
ticksintThe time in MIDI ticks.
Returns
- double
The time in seconds.
Exceptions
- ArgumentOutOfRangeException
Zero tempo changes
- InvalidOperationException
Last tempo change is not at tick 0
Modify(Options)
Allows easily modifying the sequence's programs and controllers.
This is a very sophisticated method that supports various MIDI systems and inserts/deletes messages appropriately.
public void Modify(MidiEditor.Options opts)
Parameters
optsMidiEditor.OptionsOptions to modify the midi
Preload(SpessaSharpProcessor)
Preloads all voices for this sequence in a given synth. This caches all the needed voices for playing back this sequencer, resulting in a smooth playback. The sequencer calls this function by default when loading the songs.
public void Preload(SpessaSharpProcessor synth)
Parameters
synthSpessaSharpProcessorsynth
SecondsToMidiTicks(double)
Converts seconds to time in MIDI ticks.
public int SecondsToMidiTicks(double seconds)
Parameters
secondsdoubleThe time in seconds.
Returns
- int
The time in MIDI ticks.
Exceptions
- ArgumentOutOfRangeException
Zero tempo changes
- InvalidOperationException
Last tempo change is not at tick 0
SetRMidiInfo(Key, object)
Sets a given RMIDI info value.
public void SetRMidiInfo(RMidi.Info.Key infoType, object infoData)
Parameters
Remarks
This sets the Info encoding to utf-8.
Write()
Exports the midi as a standard MIDI file.
public ArraySegment<byte> Write()
Returns
- ArraySegment<byte>
The binary file data.
Exceptions
WriteRMIDI(ArraySegment<byte>, Options?)
Writes an RMIDI file. Note that this method modifies the MIDI file in-place.
public ArraySegment<byte> WriteRMIDI(ArraySegment<byte> soundBank, WriterRMidi.Options? options = null)
Parameters
soundBankArraySegment<byte>The binary sound bank to embed into the file.
optionsWriterRMidi.Options?Extra options for writing the file.
Returns
- ArraySegment<byte>
The binary file data.