Skip to content

Commit 0f4c22e

Browse files
initial repo load
1 parent a35a69e commit 0f4c22e

File tree

5 files changed

+92
-66
lines changed

5 files changed

+92
-66
lines changed

README.rst

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Introduction
99
:alt: Discord
1010

1111

12-
.. image:: https://github.com/CedarGroveStudios/Cedargrove_CircuitPython_MIDI_Tools/workflows/Build%20CI/badge.svg
13-
:target: https://github.com/CedarGroveStudios/Cedargrove_CircuitPython_MIDI_Tools/actions
12+
.. image:: https://github.com/CedarGroveStudios/CircuitPython_MIDI_Tools/workflows/Build%20CI/badge.svg
13+
:target: https://github.com/CedarGroveStudios/CircuitPython_MIDI_Tools/actions
1414
:alt: Build Status
1515

1616

@@ -31,35 +31,7 @@ Please ensure all dependencies are available on the CircuitPython filesystem.
3131
This is easily achieved by downloading
3232
`the Adafruit library and driver bundle <https://circuitpython.org/libraries>`_
3333
or individual libraries can be installed using
34-
`circup <https://github.com/adafruit/circup>`_.Installing from PyPI
35-
=====================
36-
.. note:: This library is not available on PyPI yet. Install documentation is included
37-
as a standard element. Stay tuned for PyPI availability!
38-
39-
.. todo:: Remove the above note if PyPI version is/will be available at time of release.
40-
41-
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
42-
PyPI <https://pypi.org/project/Cedargrove-circuitpython-midi-tools/>`_.
43-
To install for current user:
44-
45-
.. code-block:: shell
46-
47-
pip3 install Cedargrove-circuitpython-midi-tools
48-
49-
To install system-wide (this may be required in some cases):
50-
51-
.. code-block:: shell
52-
53-
sudo pip3 install Cedargrove-circuitpython-midi-tools
54-
55-
To install in a virtual environment in your current project:
56-
57-
.. code-block:: shell
58-
59-
mkdir project-name && cd project-name
60-
python3 -m venv .venv
61-
source .env/bin/activate
62-
pip3 install Cedargrove-circuitpython-midi-tools
34+
`circup <https://github.com/adafruit/circup>`_.
6335

6436
Installing to a Connected CircuitPython Device with Circup
6537
==========================================================
@@ -76,7 +48,7 @@ following command to install:
7648

7749
.. code-block:: shell
7850
79-
circup install midi_tools
51+
circup install cedargrove_midi_tools
8052
8153
Or the following command to update an existing version:
8254

@@ -87,12 +59,69 @@ Or the following command to update an existing version:
8759
Usage Example
8860
=============
8961

90-
.. todo:: Add a quick, simple example. It and other examples should live in the
91-
examples folder and be included in docs/examples.rst.
62+
``note_or_name(note)``
63+
Bidirectionally translates a MIDI sequential note value to a note name or a note
64+
name to a MIDI sequential note value. Note values are of integer type in the
65+
range of 0 to 127 (inclusive). Note names are character strings expressed
66+
in the NoteOctave format, such as 'C4' or 'G#7'. Note names can range from
67+
'C-1' (note value 0) to 'F#9' (note value 127). If the input value is outside
68+
of the note value or name range, the value of ``None`` is returned.
69+
70+
.. code_block:: python
71+
72+
>>> from cedargrove_midi_tools import note_or_name >>> note_or_name('G5')
73+
79
74+
>>> note_or_name(79)
75+
'G5'
76+
77+
``note_to_name(note)`` and ``name_to_note(name)``
78+
Translates a MIDI sequential note value to a note name or note name to a note
79+
value. Note values are of integer type in the range of 0 to 127 (inclusive).
80+
Note names are strings expressed in the NoteOctave format, such as 'C4' or
81+
'G#7'. Note names can range from 'C-1' (note value 0) to 'F#9' (note value 127).
82+
If the input value is outside the range, the value of ``None`` is returned.
83+
84+
.. code_block:: python
85+
86+
>>> from cedargrove_midi_tools import note_to_name, name_to_note
87+
>>> note_to_name(70)
88+
'A#4'
89+
>>> name_to_note('A#4')
90+
70
91+
92+
93+
``note_to_frequency(note)`` and ``frequency_to_note(frequency)``
94+
Translates a MIDI sequential note value to its corresponding frequency in
95+
Hertz (Hz) or a frequency to its nearest note value. Note values are of integer
96+
type in the range of 0 to 127 (inclusive). Frequency values are floating point.
97+
If the input is outside of the range, the value ``None`` is returned.
98+
Ref: MIDI Tuning Standard formula: https://en.wikipedia.org/wiki/ MIDI_tuning_standard
99+
100+
.. code_block:: python
101+
102+
>>> from cedargrove_midi_tools import note_to_frequency, frequency_to_note
103+
>>> note_to_frequency(60)
104+
261.625
105+
>>> frequency_to_note(261.63)
106+
60
107+
108+
``cc_code_to_description(cc_code)``
109+
Provides a controller description decoded from a Control Change controller code
110+
value.
111+
Ref: https://www.midi.org/specifications-old/item/table-3-control-change-messages-data-bytes-2
112+
113+
.. code_block:: python
114+
115+
>>> from cedargrove_midi_tools import cc_code_to_description
116+
>>> cc_code_to_description(24)
117+
'Ctrl_24'
118+
>>> cc_code_to_description(1)
119+
'Modulation'
120+
92121

93122
Documentation
94123
=============
95-
API documentation for this library can be found on `Read the Docs <https://circuitpython-midi-tools.readthedocs.io/>`_.
124+
API documentation for this library can be found `here <https://circuitpython-midi-tools.readthedocs.io/>`_.
96125

97126
For information on building library documentation, please check out
98127
`this guide <https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/sharing-our-docs-on-readthedocs#sphinx-5-1>`_.
@@ -101,5 +130,5 @@ Contributing
101130
============
102131

103132
Contributions are welcome! Please read our `Code of Conduct
104-
<https://github.com/CedarGroveStudios/Cedargrove_CircuitPython_MIDI_Tools/blob/HEAD/CODE_OF_CONDUCT.md>`_
133+
<https://github.com/CedarGroveStudios/CircuitPython_MIDI_Tools/blob/HEAD/CODE_OF_CONDUCT.md>`_
105134
before contributing to help this project stay welcoming.

cedargrove_midi_tools.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
NOTE_BASE = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
3030

3131

32-
def note_or_name(input):
32+
def note_or_name(value):
3333
"""Bidirectionally translates a MIDI sequential note value to a note name
3434
or a note name to a MIDI sequential note value. Note values are
3535
of integer type in the range of 0 to 127 (inclusive). Note names are
@@ -38,17 +38,16 @@ def note_or_name(input):
3838
'F#9' (note value 127). If the input value is outside of the note
3939
value or name range, the value of None is returned.
4040
41-
:param union(int, str) input: The note name or note value input. Note value
41+
:param union(int, str) value: The note name or note value input. Note value
4242
is an integer; note name is a string. No default value.
4343
"""
44-
if type(input) == str:
44+
if isinstance(value, str):
4545
# Input is a string, so it's a note name
46-
return name_to_note(input)
47-
elif type(input) == int:
46+
return name_to_note(value)
47+
if isinstance(value, int):
4848
# Input is an integer, so it's a note value
49-
return note_to_name(input)
50-
else:
51-
return None # Invalid input parameter type
49+
return note_to_name(value)
50+
return None # Invalid input parameter type
5251

5352

5453
def note_to_name(note):
@@ -60,23 +59,22 @@ def note_to_name(note):
6059
the value of None is returned.
6160
6261
:param int note: The note value input in the range of 0 to 127 (inclusive).
63-
No default.
62+
No default value.
6463
"""
6564
if 0 <= note <= 127:
6665
return NOTE_BASE[note % 12] + str((note // 12) - 1)
67-
else:
68-
return None # Note value outside valid range
66+
return None # Note value outside valid range
6967

7068

7169
def name_to_note(name):
7270
"""Translates a note name to a MIDI sequential note value. Note names are
73-
character strings expressed in the format NoteOctave, such as
71+
character strings expressed in the NoteOctave format, such as
7472
'C4' or 'G#7'. Note names can range from 'C-1' (note value 0) to
7573
'F#9' (note value 127). Note values are of integer type in the range
7674
of 0 to 127 (inclusive). If the input value is outside of that range,
7775
the value of None is returned.
7876
79-
:param str name: The note name input in NoteOctave format. No default.
77+
:param str name: The note name input in NoteOctave format. No default value.
8078
"""
8179
name = name.upper() # Convert lower to uppercase
8280
if (name[:1] or name[:2]) in NOTE_BASE:
@@ -90,8 +88,7 @@ def name_to_note(name):
9088
note = NOTE_BASE.index(name[0])
9189
octave = int(name[1:])
9290
return note + (12 * (octave + 1)) # Calculated note value
93-
else:
94-
return None # Input string is not in NOTE_BASE
91+
return None # Input string is not in NOTE_BASE
9592

9693

9794
def note_to_frequency(note):
@@ -100,18 +97,17 @@ def note_to_frequency(note):
10097
127 (inclusive). Frequency values are floating point. If the input
10198
note value is less than 0 or greater than 127, the input is
10299
invalid and the value of None is returned. Ref: MIDI Tuning Standard
103-
formula.
100+
formula: https://en.wikipedia.org/wiki/MIDI_tuning_standard
104101
105-
:param int note: The note value input in the range of 0 to 127 (inclusive).
106-
No default.
102+
:param int note: The MIDI note value input in the range of 0 to 127
103+
(inclusive). No default.
107104
"""
108105
if 0 <= note <= 127:
109106
return pow(2, (note - 69) / 12) * 440
110-
else:
111-
return None # note value outside valid range
107+
return None # note value outside valid range
112108

113109

114-
def frequency_to_note(freq):
110+
def frequency_to_note(frequency):
115111
"""Translates a frequency in Hertz (Hz) to the closest MIDI sequential
116112
note value. Frequency values are floating point. Note values are of
117113
integer type in the range of 0 to 127 (inclusive). If the input
@@ -120,12 +116,11 @@ def frequency_to_note(freq):
120116
is returned. Ref: MIDI Tuning Standard formula:
121117
https://en.wikipedia.org/wiki/MIDI_tuning_standard
122118
123-
:param float freq: The frequency value input. No default.
119+
:param float frequency: The frequency value input in Hz. No default.
124120
"""
125-
if (pow(2, (0 - 69) / 12) * 440) <= freq <= (pow(2, (127 - 69) / 12) * 440):
126-
return int(69 + (12 * log(freq / 440, 2)))
127-
else:
128-
return None # Frequency outside valid range
121+
if (pow(2, (0 - 69) / 12) * 440) <= frequency <= (pow(2, (127 - 69) / 12) * 440):
122+
return int(69 + (12 * log(frequency / 440, 2)))
123+
return None # Frequency outside valid range
129124

130125

131126
# Controller descriptions -- no list offset
@@ -265,5 +260,8 @@ def frequency_to_note(freq):
265260
def cc_code_to_description(cc_code):
266261
"""Provides a controller description decoded from a Control Change code value.
267262
https://www.midi.org/specifications-old/item/table-3-control-change-messages-data-bytes-2
263+
264+
:param int cc_code: The Control Change code value in the range of 0 to 127.
265+
No default value.
268266
"""
269267
return CONTROLLERS[cc_code]

examples/midi_tools_simpletest.py

Lines changed: 0 additions & 4 deletions
This file was deleted.
89.4 KB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SPDX-FileCopyrightText: CedarGroveStudios
2+
3+
SPDX-License-Identifier: Unlicense

0 commit comments

Comments
 (0)