/** * 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. * * @param hertzValue The pitch in Hertz. * @return An integer representing the closest midi key. * @exception IllegalArgumentException if the hertzValue does not fall within the range of valid * MIDI key frequencies. */ public static int hertzToMidiKey(final Double hertzValue) { final int midiKey = (int) Math.round(PitchUnit.hertzToMidiCent(hertzValue)); if (midiKey < 0 || midiKey > 127) { LOG.warning( "MIDI keys are defined between 0 and 127 or from " + midiKeyToHertz(0) + "Hz to " + midiKeyToHertz(127) + "Hz, " + hertzValue + "does not map directly to a MIDI key."); } return midiKey; }
/** * Converts a Hertz value to pitch in this unit. * * @param hertzValue The value in Hertz. * @return The pitch in this unit. */ private double convertHertz(final double hertzValue) { final double convertedPitch; switch (this) { case ABSOLUTE_CENTS: convertedPitch = PitchUnit.hertzToAbsoluteCent(hertzValue); break; case HERTZ: convertedPitch = hertzValue; break; case MIDI_CENT: convertedPitch = PitchUnit.hertzToMidiCent(hertzValue); break; case MIDI_KEY: convertedPitch = PitchUnit.hertzToMidiKey(hertzValue); break; case RELATIVE_CENTS: convertedPitch = PitchUnit.hertzToRelativeCent(hertzValue); break; default: throw new AssertionError("Unknown pitch unit: " + getHumanName()); } return convertedPitch; }