Skip to content

A collection of CircuitPython methods for processing MIDI notes and Control Change codes.

License

Notifications You must be signed in to change notification settings

CedarGroveStudios/CircuitPython_MIDI_Tools

Repository files navigation

Introduction

Discord Build Status Code Style: Black

A CircuitPython method collection for processing MIDI notes and Control Change codes. It currently consists of seven helpers for converting MIDI note values to and from frequency values and note name representations, and to provide descriptions of MIDI Control Change (CC) controller codes.

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle or individual libraries can be installed using circup.

Installing to a Connected CircuitPython Device with Circup

Make sure that you have circup installed in your Python environment. Install it with the following command if necessary:

pip3 install circup

With circup installed and your CircuitPython device connected use the following command to install:

circup install cedargrove_midi_tools

Or the following command to update an existing version:

circup update

Usage Example

note_or_name(note)

Bidirectionally translates a MIDI sequential note value to a note name or a note name to a MIDI sequential note value. Note values are integers in the range of 0 to 127 (inclusive). Note names are character strings expressed in the format NoteOctave such as 'C4' or 'G#7'. Note names range from 'C-1' (note value 0) to 'F#9' (note value 127). If the input value is outside the note value or name range, the value of None is returned.

>>> from cedargrove_midi_tools import note_or_name
>>> note_or_name('G5')
79
>>> note_or_name(79)
'G5'

note_to_name(note) and name_to_note(name)

Translates a MIDI sequential note value to a note name or note name to a note value. Note values are integers in the range of 0 to 127 (inclusive). Note names are strings expressed in the NoteOctave format, such as 'C4' or 'G#7'. Note names can range from 'C-1' (note value 0) to 'G9' (note value 127). If the input value is outside the range, the value of None is returned.

>>> from cedargrove_midi_tools import note_to_name, name_to_note
>>> note_to_name(70)
'A#4'
>>> name_to_note('A#4')
70

note_to_frequency(note) and frequency_to_note(frequency)

Translates a MIDI sequential note value to its corresponding frequency in Hertz (Hz) or a frequency to a MIDI note value. Note values integers in the range of 0 to 127 (inclusive). Frequency values are floating point. If the input is outside of the range, the value None is returned. Ref: MIDI Tuning Standard formula: https://en.wikipedia.org/wiki/MIDI_tuning_standard

>>> from cedargrove_midi_tools import note_to_frequency, frequency_to_note
>>> note_to_frequency(60)
261.625
>>> frequency_to_note(261.63)
60

frequency_to_note_cents(frequency)

Translates a frequency in Hertz (Hz) to a MIDI sequential note value and positive offset in cents. Frequency values are floating point. Note values are integers in the range of 0 to 127 (inclusive). Cent values range from 0 to +100 cents. If the input frequency is less than the corresponding frequency for note 0 or greater than note 127, the note value cannot be determined and None is returned. Ref: MIDI Tuning Standard and cent formulae:

https://en.wikipedia.org/wiki/MIDI_tuning_standard

https://en.wikipedia.org/wiki/Cent_(music)

cc_code_to_description(cc_code)

Provides a controller description decoded from a Control Change controller code value. Ref: https://www.midi.org/specifications-old/item/table-3-control-change-messages-data-bytes-2

>>> from cedargrove_midi_tools import cc_code_to_description
>>> cc_code_to_description(24)
'Ctrl_24'
>>> cc_code_to_description(1)
'Modulation'

Documentation

API documentation for this library can be found here.

For information on building library documentation, please check out this guide.

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.