Class Note

java.lang.Object
org.jjazz.harmony.api.Note
All Implemented Interfaces:
Cloneable, Comparable<Note>
Direct Known Subclasses:
NoteEvent

public class Note extends Object implements Comparable<Note>, Cloneable
A note has a pitch, a duration (symbolic and in beats), and a velocity.

This is an immutable class.

  • Field Details

  • Constructor Details

    • Note

      public Note()
      Use MidiConst.PITCH_STD, Quarter duration, Alteration.Flat and standard velocity.
    • Note

      public Note(int p)
      Create a note with a default beatDuration=1 and standard velocity. Use FLAT symbols if relevant
      Parameters:
      p - The pitch of the note.
    • Note

      public Note(int p, float bd)
      Create a Note with a pitch and a beat duration and standard velocity. Use FLAT symbol by default.
      Parameters:
      p - The pitch of the note.
      bd - The beat duration of the note.
    • Note

      public Note(int p, float bd, int v)
      Create a Note with a pitch, a duration in beat, standard velocity and an alteration if any.
      Parameters:
      p -
      bd -
      v -
    • Note

      public Note(int p, float bd, int v, Note.Alteration alt)
      Create a Note with a pitch, a duration in beat, a velocity and an alteration if any.
      Parameters:
      p -
      bd - Must be > 0
      v - velocity
      alt -
    • Note

      public Note(Note n, int newPitch)
      A new note based on n but with pitch=newPitch
      Parameters:
      n -
      newPitch -
    • Note

      public Note(Note n, Note.Alteration alt)
      A new note based on n but with the specified alteration.
      Parameters:
      n -
      alt -
    • Note

      public Note(String s) throws ParseException
      Build a note from a string as specified below.

      Ex: "G", "Cb", "A#m6", "E7" will generate notes G, B, A#, E with Const.OCTAVE_STD for the octave id.
      Ex: "C!3", "Db!6", "A#dim!2" will generate notes C octave 3, Db octave 6, A# octave 2.
      Important: note that the octave range is [0-10], unlike for toPianoOctaveString().

      Parameters:
      s - A string
      Throws:
      ParseException - If syntax error in string specification.
  • Method Details

    • clone

      public Note clone()
    • getPitch

      public final int getPitch()
    • getRelativePitch

      public final int getRelativePitch()
      Returns:
      An integer between 0 and 11.
    • getDurationInBeats

      public final float getDurationInBeats()
    • getSymbolicDuration

      public final SymbolicDuration getSymbolicDuration()
    • getOctave

      public final int getOctave()
      The octave starting at 0 for pitch=0, so for example the "Midi middle C=60" octave is 5.
      Returns:
    • getAlterationDisplay

      public final Note.Alteration getAlterationDisplay()
    • getRelativeAscInterval

      public int getRelativeAscInterval(Note n)
      Parameters:
      n - A Note.
      Returns:
      The relative ascendant interval in semitons towards n.
    • getRelativeDescInterval

      public int getRelativeDescInterval(Note n)
      Parameters:
      n - A Note.
      Returns:
      The ascendant interval in semitons towards n.
    • getRelativePitchDelta

      public int getRelativePitchDelta(int relPitch)
      Calculate the shortest pitch delta from this note's relative pitch to relPitch.

      Ex: if this=B3 and relPitch=0(C) then return 1.
      Ex: if this=E2 and relPitch=1(Db) then return -3.

      Parameters:
      relPitch -
      Returns:
      A value between -5 and +6.
    • getTransposed

      public Note getTransposed(int t)
      Transpose the note from t semi-tons.

      Parameters:
      t - Transposition value in positive/negative semi-tons.
      Returns:
      A new note transposed with the same alteration display.
    • getCentered

      public Note getCentered(int lowPitch, int highPitch)
      Change the octave of this note so that pitch is within the pitch limits (included).
      Parameters:
      lowPitch - Must be < (highPitch-12)
      highPitch -
      Returns:
      The new note with corrected pitch and same alteration display
    • getTransposed

      public Note getTransposed(int pitchShift, int pitchLimit)
      Get a new transposed note.

      If the new note is beyond pitchLimit, the note's octave is changed to remain below (pitchShift > 0) or above (pitchSshift < 0) pitchLimit.

      Parameters:
      pitchShift - A negative or positive value i semi-tons.
      pitchLimit - Authorized values are [13, 119]
      Returns:
      The new transposed Note with same alteration display
    • getTransposedWithinOctave

      public Note getTransposedWithinOctave(int t)
      Get a transposed note which is guaranteed to be in the same C-based octave.

      Parameters:
      t - Transposition value in positive/negative semi-tons.
      Returns:
      A new note transposed from t semi-tons but within the same octave and same alteration display.
    • getLowerPitch

      public int getLowerPitch(int relPitch, boolean acceptEquals)
      Return the absolute pitch corresponding to relPitch below this note (or possibly equals if acceptEquals is true).

      If this note's relative pitch is less than relativePitch, return this note's absolute pitch-12.
      Ex: this=G3. If relativePitch=F return value=F3, if relativePitch=A return value=A2

      If resulting pitch < 0, return resulting pitch+12

      Parameters:
      relPitch -
      acceptEquals -
      Returns:
      An absolute note pitch
      Throws:
      IllegalArgumentException - If there is no lower pitch possible.
    • getUpperPitch

      public int getUpperPitch(int relPitch, boolean inclusive)
      Return the absolute pitch corresponding to relPitch above this note (or possibly equals if inclusive is true).

      Ex: this=G3. If relativePitch=F return value=F4, if relativePitch=A return value=A3

      If resulting pitch >127, return resulting pitch-12.

      Parameters:
      relPitch -
      inclusive -
      Returns:
      Throws:
      IllegalArgumentException - If there is no lower pitch possible.
    • getClosestPitch

      public int getClosestPitch(int relPitch)
      Return the absolute pitch of the closest note from this note.

      Ex: if this=Ab3 and relPitch=0(C) then return C4.

      Parameters:
      relPitch - The relative pitch of the closest note.
      Returns:
    • getVelocity

      public int getVelocity()
    • equalsRelativePitch

      public boolean equalsRelativePitch(Note n)
      Compare the relative pitch of 2 notes.
      Parameters:
      n - Note The note to compare with this note.
      Returns:
      boolean Return true if Note n has the same relative pitch than this note.
    • equals

      public boolean equals(Object obj)
      Compare 2 objects.

      Overrides:
      equals in class Object
      Returns:
      True if 2 notes have same pitch, beatDuration and velocity. AlterationDisplay is ignored.
    • hashCode

      public int hashCode()
      Uses pitch, beatDuration and velocity, alterationDisplay is ignored.
      Overrides:
      hashCode in class Object
      Returns:
    • compareTo

      public int compareTo(Note n)
      Compare 2 notes.

      Uses pitch, then beatDuration, then velocity. AlterationDisplay is ignored.

      Specified by:
      compareTo in interface Comparable<Note>
      Parameters:
      n -
      Returns:
    • toString

      public String toString()
      Same as toPianoOctaveString().
      Overrides:
      toString in class Object
      Returns:
      See Also:
    • toPianoOctaveBeatString

      public String toPianoOctaveBeatString()
      Returns:
      String E.g. "C4:2.5" = Midi "Middle C" (pitch=60), beatDuration=2.5 beats
    • toRelativeNoteString

      public String toRelativeNoteString()
      Returns:
      E.g. "Db" or "E" (octave independent).
    • toRelativeNoteString

      public String toRelativeNoteString(Note.Alteration alt)
      Parameters:
      alt - Use the specified alteration.
      Returns:
      E.g. "Db" if alt=FLAT, "C#" if alt=SHARP.
    • toPianoOctaveString

      public String toPianoOctaveString()
      The note using the "piano octave", e.g. "D-1" for pitch=2 or "C4" for pitch=60 (Midi Middle C).

      "A0" is the lowest 88-note piano note.

      Returns:
      See Also:
    • parsePianoOctaveString

      public static Note parsePianoOctaveString(String s) throws ParseException
      Convert a string generated by toPianoOctaveString() (like "C4") to a note.
      Parameters:
      s -
      Returns:
      Throws:
      ParseException
      See Also:
    • checkPitch

      public static boolean checkPitch(int p)
    • checkVelocity

      public static boolean checkVelocity(int v)
    • checkOctave

      public static boolean checkOctave(int o)
      This is the "natural" octave, from 0 to 10.
      Parameters:
      o -
      Returns:
    • isFlat

      public boolean isFlat()
    • getGStaffLineNumber

      public int getGStaffLineNumber()
      Get the G-clef staff line for this note.
      Returns:
      0 for the line of middle C (Midi 60) or C#, 1 for D or Db or D#, 2 for E or Eb, 3 for F or F#, etc.
    • loadAsString

      public static Note loadAsString(String s) throws ParseException
      Create a Note from a String created with saveAsString().
      Parameters:
      s -
      Returns:
      Throws:
      ParseException - If s is not valid
      See Also:
      • saveAsString(Note)
    • saveAsString

      public static String saveAsString(Note n, boolean skipAlteration)
      Save a Note as a String object.

      Example "60,FLAT,102,2.5" means pitch=60, AlterationDisplay=FLAT, velocity=102, duration=2.5 beats

      Parameters:
      n -
      skipAlteration - Don't save the alteration
      Returns:
      See Also:
    • getNormalizedRelPitch

      public static int getNormalizedRelPitch(int absPitch)
      Convert a positive or relative absolute pitch to a relative pitch between 0 and 11.

      Ex: absPitch=13, result=1
      Ex: absPitch=-15, result=9

      Parameters:
      absPitch - Can be negative or positive, provided that -1 means 11(=B), -2 means 10(=Bb) etc.
      Returns:
    • isWhiteKey

      public static boolean isWhiteKey(int pitch)
      Return Check if a pitch corresponds to keyboard white key (C major scale).
      Parameters:
      pitch -
      Returns:
    • limitPitch

      public static int limitPitch(int pitch, int lowPitch, int highPitch)
      Return a pitch which is guaranteed to be between lowPitch and highPitch. If pitch is out of bounds, go up/down 1 octave until we're within the limits.
      Parameters:
      pitch -
      lowPitch - Constraint: highPitch-lowPitch must be > 11.
      highPitch - Constraint: highPitch-lowPitch must be > 11.
      Returns:
      A pitch between lowPitch and highPitch
    • getChromaticNotesArray

      public static Note[] getChromaticNotesArray(int pitchFrom, int pitchTo)
      Return an array of notes whose pitch start from pitchFrom up to pitchTo (included).
      E.g., if pitchFrom=0 and pitchTo=12, return the array ["C0", "C#0", "D0"...."C1"]
      Parameters:
      pitchFrom - A positive integer.
      pitchTo - A positive integer.
      Returns:
      An array of Note objects.