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

List<MidiMessage>

Lyrics

An array containing the lyrics of the sequence.

public readonly List<MidiMessage> Lyrics

Field Value

List<MidiMessage>

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

List<int>

RmidiInfo

public readonly Dictionary<RMidi.Info.Key, ArraySegment<byte>> RmidiInfo

Field Value

Dictionary<RMidi.Info.Key, ArraySegment<byte>>

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

List<Midi.TempoChange>

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

List<Midi.TimeSignatureChange>

Tracks

The tracks in the sequence.

public readonly List<Track> Tracks

Field Value

List<Track>

Properties

BankOffset

The bank offset used for RMID files.

public int BankOffset { get; }

Property Value

int

Duration

The duration of the sequence.

public TimeSpan Duration { get; }

Property Value

TimeSpan

EmbeddedSoundBank

The embedded sound bank in the MIDI file, represented as an ArrayBuffer, if available.

public ArraySegment<byte>? EmbeddedSoundBank { get; set; }

Property Value

ArraySegment<byte>?

FileName

The file name of the MIDI sequence, if provided during parsing.

public string? FileName { get; set; }

Property Value

string

FirstNoteOn

The Tick position of the first note-on event in the MIDI sequence.

public int FirstNoteOn { get; }

Property Value

int

Format

public MidiFormat Format { get; }

Property Value

MidiFormat

IsDLSRMIDI

If the MIDI file is a DLS RMIDI file.

public bool IsDLSRMIDI { get; }

Property Value

bool

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

bool

IsMultiPort

Indicates if this file is a Multi-Port MIDI file.

public bool IsMultiPort { get; }

Property Value

bool

this[TimelineEvent]

public MidiMessage this[Midi.TimelineEvent ev] { get; }

Parameters

ev Midi.TimelineEvent

Property Value

MidiMessage

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

(int Min, int Max)

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

int

Loop

The loop points (in ticks) of the sequence, including both start and end points.

public Midi.MidiLoop Loop { get; set; }

Property Value

Midi.MidiLoop

MaxBeat

public int MaxBeat { get; }

Property Value

int

TimeDivision

The time division of the sequence, representing the number of MIDI ticks per beat.

public int TimeDivision { get; }

Property Value

int

Timeline

public ReadOnlySpan<Midi.TimelineEvent> Timeline { get; }

Property Value

ReadOnlySpan<Midi.TimelineEvent>

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

snapshot SynthesizerSnapshot

The snapshot to apply.

BeatsToMidiTicks(int)

Converts beats to time in MIDI ticks.

public int BeatsToMidiTicks(int beats)

Parameters

beats int

The time in beats.

Returns

int

The time in MIDI ticks.

Copy(Midi)

Copies a MIDI.

public static Midi Copy(Midi mid)

Parameters

mid Midi

The MIDI to copy.

Returns

Midi

The copied MIDI.

Flush(bool)

Updates all internal values of the MIDI.

public void Flush(bool sortEvents = true)

Parameters

sortEvents bool

If 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

bytes ArraySegment<byte>

The ArraySegment containing the binary file data.

fileName string

The optional name of the file, will be used if the MIDI file does not have a name.

Returns

Midi

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

file FileInfo

The file containing the MIDI data

Returns

Midi

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

encoding string

The encoding to use if the MIDI uses an extended code page.

Returns

IEnumerable<string>

Remarks

RMIDI encoding overrides the provided encoding.

GetInfoEncoding()

The encoding of the RMIDI info in file, if specified.

public ArraySegment<byte>? GetInfoEncoding()

Returns

ArraySegment<byte>?

GetName(string)

Gets the MIDI's decoded name.

public string? GetName(string encoding = "Shift_JIS")

Parameters

encoding string

The encoding to use if the MIDI uses an extended code page.

Returns

string

Remarks

RMIDI encoding overrides the provided encoding.

GetNoteTimes(int)

Calculates all note times in seconds.

public List<Midi.NoteTime>[] GetNoteTimes(int minDrumLength = 0)

Parameters

minDrumLength int

The 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

RMidi.Info

GetRMidiInfo(Key)

Gets a given chunk from the RMIDI information, undefined if it does not exist.

public object? GetRMidiInfo(RMidi.Info.Key infoType)

Parameters

infoType RMidi.Info.Key

The metadata type.

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

getPreset IPresetGetter

The 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

tl Midi.TimelineEvent
msg MidiMessage

InsertBefore(TimelineEvent, MidiMessage)

public void InsertBefore(Midi.TimelineEvent tl, MidiMessage msg)

Parameters

tl Midi.TimelineEvent
msg MidiMessage

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

Midi.Enumerable

MidiTicksToBeats(int)

Converts MIDI ticks to time in beats.

public int MidiTicksToBeats(int ticks)

Parameters

ticks int

The 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

ticks int

The 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

tempoChanges ReadOnlySpan<Midi.TempoChange>

The tempo changes in the sequence, ordered from the last change to the first.

timeDivision int

The time division of the sequence, representing the number of MIDI ticks per beat.

ticks int

The 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. This modifies the MIDI sequence in-place.

public void Modify(MidiEditor.Options opts)

Parameters

opts MidiEditor.Options

Options 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

synth SpessaSharpProcessor

synth

SecondsToMidiTicks(double)

Converts seconds to time in MIDI ticks.

public int SecondsToMidiTicks(double seconds)

Parameters

seconds double

The 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

infoType RMidi.Info.Key

The type to set.

infoData object

The value to set it to.

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

Exception

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

soundBank ArraySegment<byte>

The binary sound bank to embed into the file.

options WriterRMidi.Options?

Extra options for writing the file.

Returns

ArraySegment<byte>

The binary file data.