Pitch and frequency are two ways to communicate how high or low a note sounds. Pitch is how humans perceive change in how high or low a sound is (e.g., MIDI pitch 0 to 127, with A4 = 69). Frequency is a physical measurement of how fast a sound wave vibrates.
NOTE: MIDI pitch A4 (69) corresponds to 440 Hertz in frequency.
The relationship between pitch and frequency is logarithmic / exponential.
NOTE: One octave change in pitch (e.g., A4 to A5), corresponds to a doubling (or halving) of frequency. For example:
- A4 is 69 in pitch, and 440.0 Hz in frequency;
- A5 is 81 (69 + 12) in pitch, and 880.0 Hz (440.0 * 2) in frequency;
- A3 is 57 (69 – 12) in pitch, and 220.0 Hz (440.0 / 2) in frequency.
Conversion between Pitch and Frequency
JythonMusic provides two functions to convert between pitch (0-127) and frequency (in Hertz):
- pitchToFrequency()
- frequencyToPitch()
pitchToFrequency() converts MIDI pitch (0-127) to frequency (in Hertz):
>>> pitchToFrequency(A4) 440.0 >>> pitchToFrequency(C4) 261.6255653005986 >>> pitchToFrequency(E4) 329.6275569128699
frequencyToPitch() converts frequency (in Hertz) to MIDI pitch (0-127).
This returns the closest MIDI pitch (0-127), and the remaining pitch bend for finer (microtonal) control. Pitch bend corresponds to +/- 2 half tones, and ranges from -8191 to +8192 (0 means no pitch bend).
>>> frequencyToPitch(440.0) (69, 0) >>> frequencyToPitch(443.5) (69, 562) >>> frequencyToPitch(438.5) (69, -242) # capture values in variables >>> pitch, pitchBend = frequencyToPitch(438.5) >>> pitch 69 >>> pitchBend -242
NOTE: Note objects may be generated with either pitch or frequency values.