PitchConverter

class pitchtools.PitchConverter(a4=442.0, eightnote_symbol=True)[source]

Bases: object

Convert between midinote, frequency and notename.

Parameters:
  • a4 – the reference frequency

  • eightnote_symbol – if True, a special symbol is used (“>”, “<”) when a note is exactly 25 cents higher or lower (for example, “4C>”). Otherwise, a notename would be, for example, “4C+25”

Example

>>> cnv = PitchConverter(a4=435)
>>> cnv.m2f(69)
435.0
>>> cnv.f2n(440)
'4A+20'

Attributes Summary

a4

eighthnote_symbol

Methods Summary

as_midinotes(x)

Tries to interpret x as a list of pitches, returns these as midinotes

asmidi(x)

Convert x to a midinote

default()

Get the default PitchConverter Returns:

f2m(freq)

Convert a frequency in Hz to a midi-note

f2n(freq)

Return the notename corresponding to the given freq

freq_round(freq[, semitone_divisions])

Round the freq.

get_reference_freq()

Get the reference frequency for this converter

m2f(midinote)

Convert a midi-note to a frequency

m2n(midinote)

Convert midinote to notename

midi_to_note_parts(midinote)

Convert a midinote into its parts as a note

n2f(note)

Convert a notename to its corresponding frequency

n2m(note)

Convert a notename to a midinote

normalize_notename(notename)

Convert notename to its canonical form

pianofreqs([start, stop])

Generate an array of the frequencies for all the piano keys

set_reference_freq(a4)

Set the reference freq.

str2midi(s)

Accepts all that n2m accepts but with the addition of frequencies

Attributes Documentation

a4
eighthnote_symbol

Methods Documentation

as_midinotes(x)[source]

Tries to interpret x as a list of pitches, returns these as midinotes

Parameters:

x – either list of midinotes (floats/ints), a list of notenames (str), one str with notenames (divided by spaces), or a single notename or midinote

Return type:

list[float]

Returns:

the corresponding list of midinotes.

Example

>>> as_midinotes(["4G", "4C"])
[67., 60.]
>>> as_midinotes((67, 60))
[67., 60.]
>>> as_midinotes("4G 4C 4C+10hz")
[67., 60., 60.65]
asmidi(x)[source]

Convert x to a midinote

Parameters:

x (int | float | str) – an object which can be converted to a midinote (a freq., a notename)

Return type:

float

Returns:

The corresponding midinote.

Example

>>> from pitchtools import *
>>> cnv = PitchConverter()
>>> cnv.asmidi("4C+10Hz")
272.8
classmethod default()[source]

Get the default PitchConverter Returns:

Return type:

PitchConverter

f2m(freq)[source]

Convert a frequency in Hz to a midi-note

Parameters:

freq (float) – the frequency to convert, in Hz

Return type:

float

Returns:

the midi note corresponding to freq

f2n(freq)[source]

Return the notename corresponding to the given freq

Parameters:

freq (float) – the freq. to convert

Return type:

str

Returns:

the corresponding notename

freq_round(freq, semitone_divisions=1)[source]

Round the freq. to the nearest semitone or fraction thereof

Parameters:
  • freq (float) – the freq. to round

  • semitone_divisions – the number of divisions per semitone

Return type:

float

Returns:

the rounded frequency

get_reference_freq()[source]

Get the reference frequency for this converter

Return type:

float

Returns:

the freq. of A4

See also

set_reference_frequency(), set_reference_freq()

m2f(midinote)[source]

Convert a midi-note to a frequency

Parameters:

midinote (float) – the midinote to convert to frequency

Return type:

float

Returns:

the freq. corresponding to midinote

m2n(midinote)[source]

Convert midinote to notename

Parameters:

midinote (float) – a midinote (60=C4)

Return type:

str

Returns:

the notename corresponding to midinote.

midi_to_note_parts(midinote)[source]

Convert a midinote into its parts as a note

Parameters:

midinote (float) – the midinote to analyze

Return type:

tuple[int, str, str, int]

Returns:

a tuple (octaveint, chromatic_note: str, microtonal_alternation: str, cents_deviation: int), where octave is the octave number; chromatic_note is the pitch class

Example

>>> import pitchtools as pt
>>> pt.midi_to_note_parts(60.5)
(4, 'C', '+', 0)
>>> pt.midi_to_note_parts(61.2)
(4, 'C#', '', 20)
n2f(note)[source]

Convert a notename to its corresponding frequency

Return type:

float

n2m(note)[source]

Convert a notename to a midinote

Parameters:

note (str) – the notename

Return type:

float

Returns:

the midinote corresponding to note

See also

m2n()

normalize_notename(notename)[source]

Convert notename to its canonical form

The canonical form follows the scheme octave:pitchclass:microtone

Parameters:

notename (str) – the note to normalize

Return type:

str

Returns:

the normalized notename

Example

>>> normalize_notename("a4+24")
4A+24
pianofreqs(start='A0', stop='C8')[source]

Generate an array of the frequencies for all the piano keys

Parameters:
  • start – the starting note

  • stop – the ending note

Return type:

list[float]

Returns:

a list of frequencies

set_reference_freq(a4)[source]

Set the reference freq. (the freq. of A4) for this converter

Parameters:

a4 (float) – the freq. of A4 in Hz

Return type:

None

See also

get_reference_frequency(), PitchConverter.get_reference_freq()

str2midi(s)[source]

Accepts all that n2m accepts but with the addition of frequencies

Note

The hz part must be at the end

Parameters:

s (str) – pitch describes as a string. Possible values: “100hz”, “4F+20hz”, “8C-4hz”

Return type:

float

Returns:

the corresponding midinote