Midi

Convert music into a midi file.

See this page for information on the standard midi file format.

Primary functions

music_to_midi_file(music, *, config, filename='jird_out.midi')[source]

Write midi file for music.

Parameters:
  • music (Piece) – Music to convert to midi.

  • config (Config) – Config controlling playback.

  • filename (str or Path) – Name for output midi file.

Returns:

First item is the midi channels used for each part in the midi file. Second item is the tuning data to be used with the midi file (if scala retuning is used).

Return type:

tuple of (list of list of int, optional ScalaData)

play_music(music, *, config, filename='jird_out.midi')[source]

Play music with chosen synth.

Parameters:
  • music (Piece) – Music to be played.

  • config (Config) – Config controlling playback.

  • filename (str or Path) – Filename to use for temporary midi file. Defaults to jird_out.midi.

Helper functions

chord_to_midi(notes, *, f0, channels, pitch_bend_range)[source]

Generate midi events to play the notes in a chord.

Each note in the chord is sent on a separate midi channel to allow pitch bending each note independently. First the pitch bends needed for each note are sent (to get their exact just frequencies). Then the note on events for all notes are sent. Finally the note off events for all notes are sent.

Parameters:
  • notes (tuple of Note or Note) – Notes in the chord to be represented as midi. A single note is treated as a one note chord.

  • f0 (float) – Basic frequency used to convert note frequency ratios into real frequencies.

  • channels (list of int) – Midi channels on which to send notes. Each note is sent on its own channel.

  • pitch_bend_range (int) – Number of semitones to assume max pitch bend corresponds to.

Returns:

Hex string of midi events to play notes.

Return type:

str

chord_to_scala_midi(notes, frequency_map, channel)[source]

Midi events to play a chord for use with scala files.

Only note-ons and note-offs are needed since no bends are used when using scala files. All notes are sent on the same channel.

Parameters:
  • notes (Note or tuple of Note) – Notes in the chord.

  • frequency_map (dict of {Fraction or float : int}) – Map from frequencies to midi note numbers.

  • channel (int) – Channel to send notes on.

Returns:

Hex for midi to send the chord.

Return type:

str

track_header(track_hex)[source]

Compute midi track header for track in track_hex.

The track header is MTrk followed by the number of bytes in the track.

Parameters:

track_hex (str) – Hex string representing the bytes in the track.

Returns:

Hex string of midi track header.

Return type:

str

midi_track(music, *, f0, channels, program, pitch_bend_range)[source]

Build midi track for music.

The midi track is made up of a track header, program changes, then midi events to play chord in music.

Parameters:
  • music (Part) – Music to build midi track for.

  • f0 (float) – Basic frequency used to convert note frequency ratios into real frequencies.

  • channels (list of int) – Midi channels to use to play music.

  • program (int, optional) – Midi program to use for playback.

  • pitch_bend_range (int) – Number of semitones to assume max pitch bend corresponds to.

Returns:

Hex for midi track representing music.

Return type:

str

tempo_track(t0)[source]

Generate tempo track corresponding to time unit t0.

Midi tempo is specified in microseconds per beat. The tempo is set assuming time unit t0 corresponds to one beat.

Parameters:

t0 (float) – Basic time unit used by Jird.

Returns:

Hex string of tempo track.

Return type:

str

set_program(program, channels)[source]

Generate midi events to set program.

One program change to program is sent for each channel in channels. The program change sets the instrument which will be used when playing the midi file.

Parameters:
  • program (int) – Midi program to change to. Using program 1 changes to the first midi program, which means sending program number byte 00.

  • channels (list of int) – Midi channels on which to send program changes.

Returns:

Hex string containing program change events.

Return type:

str

variable_length_quantity(n)[source]

Compute bytes encoding n as a midi variable length quantity.

A midi variable length quantity is the bits of n grouped into sevens. Each group is placed in a byte with the top bit set to 0 for the last byte and 1 for the others. For more information see this webpage Variable length quantities are used for example to represent the delta times between midi events in midi files.

Parameters:

n (int) – Number to encode.

Returns:

Hex string of variable length quantity encoding of n.

Return type:

str

fourteen_bit(n)[source]

Represent n as fourteen bits stored in two bytes.

The highest and lowest seven bits of n are stored in separate bytes, with the top bit zero in each. This representation is used for the size of the pitch bend for midi pitch wheel change events.

Parameters:

n (int) – Number to represent.

Returns:

Hex string of fourteen bit representation of n.

Return type:

str