utilities

Utilty functions for converting between MIDI data and human-readable/usable values

pretty_midi.utilities.key_number_to_key_name(key_number)[source]

Convert a key number to a key string.

Parameters:
key_numberint

Uses pitch classes to represent major and minor keys. For minor keys, adds a 12 offset. For example, C major is 0 and C minor is 12.

Returns:
key_namestr

Key name in the format '(root) (mode)', e.g. 'Gb minor'. Gives preference for keys with flats, with the exception of F#, G# and C# minor.

pretty_midi.utilities.key_name_to_key_number(key_string)[source]

Convert a key name string to key number.

Parameters:
key_stringstr
Format is '(root) (mode)', where:
  • (root) is one of ABCDEFG or abcdefg. A lowercase root indicates a minor key when no mode string is specified. Optionally a # for sharp or b for flat can be specified.

  • (mode) is optionally specified either as one of ‘M’, ‘Maj’, ‘Major’, ‘maj’, or ‘major’ for major or ‘m’, ‘Min’, ‘Minor’, ‘min’, ‘minor’ for minor. If no mode is specified and the root is uppercase, the mode is assumed to be major; if the root is lowercase, the mode is assumed to be minor.

Returns:
key_numberint

Integer representing the key and its mode. Integers from 0 to 11 represent major keys from C to B; 12 to 23 represent minor keys from C to B.

pretty_midi.utilities.mode_accidentals_to_key_number(mode, num_accidentals)[source]

Convert a given number of accidentals and mode to a key number.

Parameters:
modeint

0 is major, 1 is minor.

num_accidentalsint

Positive number is used for sharps, negative number is used for flats.

Returns:
key_numberint

Integer representing the key and its mode.

pretty_midi.utilities.key_number_to_mode_accidentals(key_number)[source]

Converts a key number to number of accidentals and mode.

Parameters:
key_numberint

Key number as used in pretty_midi.

Returns:
modeint

0 for major, 1 for minor.

num_accidentalsint

Number of accidentals. Positive is for sharps and negative is for flats.

pretty_midi.utilities.qpm_to_bpm(quarter_note_tempo, numerator, denominator)[source]

Converts from quarter notes per minute to beats per minute.

Parameters:
quarter_note_tempofloat

Quarter note tempo.

numeratorint

Numerator of time signature.

denominatorint

Denominator of time signature.

Returns:
bpmfloat

Tempo in beats per minute.

pretty_midi.utilities.note_number_to_hz(note_number)[source]

Convert a (fractional) MIDI note number to its frequency in Hz.

Parameters:
note_numberfloat

MIDI note number, can be fractional.

Returns:
note_frequencyfloat

Frequency of the note in Hz.

pretty_midi.utilities.hz_to_note_number(frequency)[source]

Convert a frequency in Hz to a (fractional) note number.

Parameters:
frequencyfloat

Frequency of the note in Hz.

Returns:
note_numberfloat

MIDI note number, can be fractional.

pretty_midi.utilities.note_name_to_number(note_name)[source]

Converts a note name in the format '(note)(accidental)(octave number)' (e.g. 'C#4') to MIDI note number.

'(note)' is required, and is case-insensitive.

'(accidental)' should be '' for natural, '#' for sharp and '!' or 'b' for flat.

If '(octave)' is '', octave 0 is assumed.

Parameters:
note_namestr

A note name, as described above.

Returns:
note_numberint

MIDI note number corresponding to the provided note name.

Notes

Thanks to Brian McFee.

pretty_midi.utilities.note_number_to_name(note_number)[source]

Convert a MIDI note number to its name, in the format '(note)(accidental)(octave number)' (e.g. 'C#4').

Parameters:
note_numberint

MIDI note number. If not an int, it will be rounded.

Returns:
note_namestr

Name of the supplied MIDI note number.

Notes

Thanks to Brian McFee.

pretty_midi.utilities.note_number_to_drum_name(note_number)[source]

Converts a MIDI note number in a percussion instrument to the corresponding drum name, according to the General MIDI standard.

Any MIDI note number outside of the valid range (note 35-81, zero-indexed) will result in an empty string.

Parameters:
note_numberint

MIDI note number. If not an int, it will be rounded.

Returns:
drum_namestr

Name of the drum for this note for a percussion instrument.

Notes

See http://www.midi.org/techspecs/gm1sound.php

pretty_midi.utilities.drum_name_to_note_number(drum_name)[source]

Converts a drum name to the corresponding MIDI note number for a percussion instrument. Conversion is case, whitespace, and non-alphanumeric character insensitive.

Parameters:
drum_namestr

Name of a drum which exists in the general MIDI standard. If the drum is not found, a ValueError is raised.

Returns:
note_numberint

The MIDI note number corresponding to this drum.

Notes

See http://www.midi.org/techspecs/gm1sound.php

pretty_midi.utilities.program_to_instrument_name(program_number)[source]

Converts a MIDI program number to the corresponding General MIDI instrument name.

Parameters:
program_numberint

MIDI program number, between 0 and 127.

Returns:
instrument_namestr

Name of the instrument corresponding to this program number.

Notes

See http://www.midi.org/techspecs/gm1sound.php

pretty_midi.utilities.instrument_name_to_program(instrument_name)[source]

Converts an instrument name to the corresponding General MIDI program number. Conversion is case, whitespace, and non-alphanumeric character insensitive.

Parameters:
instrument_namestr

Name of an instrument which exists in the general MIDI standard. If the instrument is not found, a ValueError is raised.

Returns:
program_numberint

The MIDI program number corresponding to this instrument.

Notes

See http://www.midi.org/techspecs/gm1sound.php

pretty_midi.utilities.program_to_instrument_class(program_number)[source]

Converts a MIDI program number to the corresponding General MIDI instrument class.

Parameters:
program_numberint

MIDI program number, between 0 and 127.

Returns:
instrument_classstr

Name of the instrument class corresponding to this program number.

Notes

See http://www.midi.org/techspecs/gm1sound.php

pretty_midi.utilities.pitch_bend_to_semitones(pitch_bend, semitone_range=2.0)[source]

Convert a MIDI pitch bend value (in the range [-8192, 8191]) to the bend amount in semitones.

Parameters:
pitch_bendint

MIDI pitch bend amount, in [-8192, 8191].

semitone_rangefloat

Convert to +/- this semitone range. Default is 2., which is the General MIDI standard +/-2 semitone range.

Returns:
semitonesfloat

Number of semitones corresponding to this pitch bend amount.

pretty_midi.utilities.semitones_to_pitch_bend(semitones, semitone_range=2.0)[source]

Convert a semitone value to the corresponding MIDI pitch bend integer.

Parameters:
semitonesfloat

Number of semitones for the pitch bend.

semitone_rangefloat

Convert to +/- this semitone range. Default is 2., which is the General MIDI standard +/-2 semitone range.

Returns:
pitch_bendint

MIDI pitch bend amount, in [-8192, 8191].