Пример #1
0
 /**
  * Converts a Hertz value to relative cents. E.g. 440Hz is converted to 900 if the reference is a
  * C.
  *
  * @param hertzValue A value in hertz.
  * @return A value in relative cents.
  */
 public static double hertzToRelativeCent(final double hertzValue) {
   double absoluteCentValue = PitchUnit.hertzToAbsoluteCent(hertzValue);
   // make absoluteCentValue positive. E.g. -2410 => 1210
   if (absoluteCentValue < 0) {
     absoluteCentValue = Math.abs(1200 + absoluteCentValue);
   }
   // so it can be folded to one octave. E.g. 1210 => 10
   return absoluteCentValue % 1200.0;
 }
Пример #2
0
 /**
  * Calculates the frequency (Hz) for a MIDI key.
  *
  * @param midiKey The MIDI key. A MIDI key is an integer between 0 and 127, inclusive.
  * @return A frequency in Hz corresponding to the MIDI key.
  * @exception IllegalArgumentException If midiKey is not in the valid range between 0 and 127,
  *     inclusive.
  */
 public static double midiKeyToHertz(final int midiKey) {
   if (midiKey < 0 || midiKey > 127) {
     LOG.warning(
         "MIDI keys are defined between 0 and 127 or from "
             + midiKeyToHertz(0)
             + "Hz to "
             + midiKeyToHertz(127)
             + "Hz, MIDI KEY "
             + midiKey
             + " is out of this range.");
   }
   return PitchUnit.midiCentToHertz(midiKey);
 }
Пример #3
0
 /**
  * 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;
 }
Пример #4
0
 /**
  * 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;
 }
Пример #5
0
 private double convertToHertz(final double value, final PitchUnit valueUnit) {
   final double hertzValue;
   switch (valueUnit) {
     case ABSOLUTE_CENTS:
       hertzValue = PitchUnit.absoluteCentToHertz(value);
       break;
     case HERTZ:
       hertzValue = value;
       break;
     case MIDI_CENT:
       hertzValue = PitchUnit.midiCentToHertz(value);
       break;
     case MIDI_KEY:
       hertzValue = PitchUnit.midiKeyToHertz((int) value);
       break;
     case RELATIVE_CENTS:
       hertzValue = PitchUnit.relativeCentToHertz(value);
       break;
     default:
       throw new AssertionError("Unknown pitch unit: " + getHumanName());
   }
   return hertzValue;
 }