Package be.panako.util
Enum PitchUnit
java.lang.Object
java.lang.Enum<PitchUnit>
be.panako.util.PitchUnit
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<PitchUnit>
,java.lang.constant.Constable
public enum PitchUnit extends java.lang.Enum<PitchUnit>
Defines the unit of the pitch value.
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
java.lang.Enum.EnumDesc<E extends java.lang.Enum<E>>
-
Enum Constant Summary
Enum Constants Enum Constant Description ABSOLUTE_CENTS
Number of cents compared to the "absolute zero" a configured, low frequency.HERTZ
Oscillations per second.MIDI_CENT
An double from 0 to 127 that represents the closest MIDI key.MIDI_KEY
An integer from 0 to 127 that represents the closest MIDI key.RELATIVE_CENTS
Number of cents (between 0 and 1200) relative to the start of the octave. -
Method Summary
Modifier and Type Method Description static double
absoluteCentToHertz(double absoluteCent)
Returns the frequency (Hz) of an absolute cent value.static double
centToRatio(double cent)
Converts cent values to ratios.double
convert(double value, PitchUnit valueUnit)
Convert the given pitch in the given unit to this unit.java.lang.String
getHumanName()
static double
hertzToAbsoluteCent(double hertzValue)
The reference frequency is configured.static double
hertzToMidiCent(double hertzValue)
Converts a frequency in Hz to a MIDI CENT value using(12 ^ log2 (f / 440)) + 69
E.g.
69.168 MIDI CENTS = MIDI NOTE 69 + 16,8 cents
69.168 MIDI CENTS = 440Hz + x Hz
static int
hertzToMidiKey(java.lang.Double hertzValue)
A MIDI key is an integer between 0 and 127, inclusive.static double
hertzToRelativeCent(double hertzValue)
Converts a Hertz value to relative cents.static double
midiCentToHertz(double midiCent)
Converts a MIDI CENT frequency to a frequency in Hz.static double
midiKeyToHertz(int midiKey)
Calculates the frequency (Hz) for a MIDI key.static double
ratioToCent(double ratio)
Converts a ratio to cents.static double
relativeCentToHertz(double relativeCent)
Converts a relative cent value to an absolute Hertz value by using PitchUnit.REF_FREQ.static PitchUnit
valueOf(java.lang.String name)
Returns the enum constant of this type with the specified name.static PitchUnit[]
values()
Returns an array containing the constants of this enum type, in the order they are declared.Methods inherited from class java.lang.Enum
compareTo, describeConstable, equals, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
-
Enum Constant Details
-
HERTZ
Oscillations per second. -
ABSOLUTE_CENTS
Number of cents compared to the "absolute zero" a configured, low frequency. By default C0 (16Hz) is used. -
RELATIVE_CENTS
Number of cents (between 0 and 1200) relative to the start of the octave. The first octave starts at "absolute zero" a configured, low frequency. -
MIDI_KEY
An integer from 0 to 127 that represents the closest MIDI key. All Hz values above 13289.7300 Hz are mapped to 127, all values below a certain value are mapped to 0. -
MIDI_CENT
An double from 0 to 127 that represents the closest MIDI key. All values above (13289.7300 Hz + 99,99... cents) are mapped to 127,99999... All values below a certain value are mapped to 0.00.
-
-
Method Details
-
values
Returns an array containing the constants of this enum type, in the order they are declared.- Returns:
- an array containing the constants of this enum type, in the order they are declared
-
valueOf
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
java.lang.IllegalArgumentException
- if this enum type has no constant with the specified namejava.lang.NullPointerException
- if the argument is null
-
getHumanName
public java.lang.String getHumanName()- Returns:
- A nicer description of the name of the unit.
-
convert
Convert the given pitch in the given unit to this unit. E.g. following statements should evaluate to true with the default reference frequency and the default offset octave (for RELATIVE_CENTS).PitchUnit.HERZ.convert(6900,PitchUnit.ABSOLUTE_CENTS) == 440 PitchUnit.HERZ.convert(69,PitchUnit.MIDI_KEY) == 440 PitchUnit.HERZ.convert(69.00,PitchUnit.MIDI_CENTS) == 440 PitchUnit.HERZ.convert(700,PitchUnit.RELATIVE_CENTS) == 440 PitchUnit.ABSOLUTE_CENTS.convert(440,PitchUnit.HERTZ) == 6900
- Parameters:
value
- The value of the given pitch.valueUnit
- The unit of the given pitch.- Returns:
- The given pitch converted to this unit.
-
midiCentToHertz
public static double midiCentToHertz(double midiCent)Converts a MIDI CENT frequency to a frequency in Hz.- Parameters:
midiCent
- The pitch in MIDI CENT.- Returns:
- The pitch in Hertz.
-
hertzToMidiCent
public static double hertzToMidiCent(double hertzValue)Converts a frequency in Hz to a MIDI CENT value using(12 ^ log2 (f / 440)) + 69
E.g.
69.168 MIDI CENTS = MIDI NOTE 69 + 16,8 cents
69.168 MIDI CENTS = 440Hz + x Hz
- Parameters:
hertzValue
- The pitch in Hertz.- Returns:
- The pitch in MIDI cent.
-
absoluteCentToHertz
public static double absoluteCentToHertz(double absoluteCent)Returns the frequency (Hz) of an absolute cent value. This calculation uses a configured reference frequency.- Parameters:
absoluteCent
- The pitch in absolute cent.- Returns:
- A pitch in Hz.
-
hertzToAbsoluteCent
public static double hertzToAbsoluteCent(double hertzValue)The reference frequency is configured. The default reference frequency is 16.35Hz. This is C0 on a piano keyboard with A4 tuned to 440 Hz. This means that 0 cents is C0; 1200 is C1; 2400 is C2; ... also -1200 cents is C-1- Parameters:
hertzValue
- The pitch in Hertz.- Returns:
- The value in absolute cents using the configured reference frequency
-
hertzToRelativeCent
public static double hertzToRelativeCent(double hertzValue)Converts a Hertz value to relative cents. E.g. 440Hz is converted to 900 if the reference is a C.- Parameters:
hertzValue
- A value in hertz.- Returns:
- A value in relative cents.
-
relativeCentToHertz
public static double relativeCentToHertz(double relativeCent)Converts a relative cent value to an absolute Hertz value by using PitchUnit.REF_FREQ.- Parameters:
relativeCent
- a value in relative cents.- Returns:
- A pitch in Hertz.
-
midiKeyToHertz
public static double midiKeyToHertz(int midiKey)Calculates the frequency (Hz) for a MIDI key.- Parameters:
midiKey
- The MIDI key. A MIDI key is an integer between 0 and 127, inclusive.- Returns:
- A frequency in Hz corresponding to the MIDI key.
- Throws:
java.lang.IllegalArgumentException
- If midiKey is not in the valid range between 0 and 127, inclusive.
-
hertzToMidiKey
public static int hertzToMidiKey(java.lang.Double hertzValue)A MIDI key is an integer between 0 and 127, inclusive. Within a certain range every pitch is mapped to a MIDI key. If a value outside the range is given an IllegalArugmentException is thrown.- Parameters:
hertzValue
- The pitch in Hertz.- Returns:
- An integer representing the closest midi key.
- Throws:
java.lang.IllegalArgumentException
- if the hertzValue does not fall within the range of valid MIDI key frequencies.
-
ratioToCent
public static double ratioToCent(double ratio)Converts a ratio to cents. "Ratios Make Cents: Conversions from ratios to cents and back again" in the book "Tuning Timbre Spectrum Scale" William A. Sethares- Parameters:
ratio
- A cent value- Returns:
- A ratio containing the same information.
-
centToRatio
public static double centToRatio(double cent)Converts cent values to ratios. See "Ratios Make Cents: Conversions from ratios to cents and back again" in the book "Tuning Timbre Spectrum Scale" William A. Sethares.- Parameters:
cent
- A cent value- Returns:
- A ratio containing the same information.
-