Class RationalDuration

java.lang.Object
eu.bandm.music.entities.RationalDuration
All Implemented Interfaces:
DurationIndication, Comparable<RationalDuration>

public class RationalDuration extends Object implements DurationIndication, Comparable<RationalDuration>
Implements the fundamental duration domain in CWN, namely durations expressed by rational numbers. These rational numbers represent durations in the "abstract CWN time". The Rational "1/4" corresponds to the abstract duration notated as a "quarter note", "3/8" to a "dotted quarter note", and "1/6" to a "quarter note under a triplet bracket", etc.

This class also provides
  • Field Details

  • Constructor Details

    • RationalDuration

      public RationalDuration(int num, int den)
      Make a duration n/d for n>=0 and d>0.
    • RationalDuration

      public RationalDuration(Rational rat)
      Make a duration for rat>=0/1.
  • Method Details

    • primeFactors_mod_2

      public static PrimeFactors primeFactors_mod_2(int value)
      Delivers a PrimeFactors object after removing all powers of two(2), since these are ubiquituous in CWN notation and not subject to dedicated calculation.
    • primeFactors_mod_2

      public static PrimeFactors primeFactors_mod_2(BigInteger value)
      See Also:
    • primeFactors_mod_2

      public static PrimeFactors primeFactors_mod_2(Rational value)
      Delivers a PrimeFactors object after removing all powers of two(2), since these are ubiquituous in CWN notation and not subject to dedicated calculation.
    • highest_odd_factor

      public static int highest_odd_factor(int value)
      Divides repeatedly by 2 as long as the result is even.
    • dottedNotation

      @Opt public static @Opt RationalDuration.DottedBaseDuration dottedNotation(Rational rat)
      Return the representation as base duration plus zero or more DOTs, or null if no such exists. The denominator must be a power of two(2). The enumerator must be one less than a power of two(2). Then (e+1/d*2) is the base value, and ld(e+1)-1 is the dot count. Eg.
             rat      nextFullNote
             factors  nextFactors    
               7         8     1      1            8 => 2^3 => 2 is dot count
      (rat=)  --    =>  -- =   -  =>  - (=base) 
              16        16     2      4  
      
               3         4     1      1            4 => 2^2 => 1 is dot count
      (rat=)  --    => --- =   -  => -- 
              32        32     8     16 
      
               1         2     1      1            2 => 2^1 => 0 is dot count
      (rat=)  --    => --- =   -  => -- 
               8         8     4      8 
      
        
    • dottedNotation

      Return the duration's representation as base duration plus zero or more DOTs,
      See Also:
    • e_writable

      public static boolean e_writable(int enumerator, int maxdots)
      Quick test whether a numerator alone is writable as RationalDuration.DottedBaseDuration, with given maximal dot count. (This is called "e-writable". If dot-count==0, it is even "d-writable"). This test is sensible, since prime factors in the denominator must be eliminated anyhow (by some proportional bracket). So all durations with a numerator of (2^x)*[(2^n)-1] are writable in one single symbol, using (n-1) dots:
        1/2  -> 0 dots   1+1 = 2^1     = 00 0010
        3/4  -> 1 dot    3+1 = 2^2     = 00 0100
        7/8  -> 2 dots   7+1 = 2^3     = 00 1000
        
    • nearest_power_of_two

      public static int nearest_power_of_two(int i)
      Used to find the most sensible denominator for to introduce a proportion with the numerator i. Assumes i>0. The middle of the interval between two powers of two is mapped upward, but not 3, which is mapped downward:
        |  |  ,  |     ,      |             ,               |                ,         |
        1  2  3  4  5  6  7   8  9  10  11  12  13  14  15  16  17  18 .. 23 24  25 .. 32 
        1  2  2  4  4  8  8   8  8   8   8  16  16  16  16  16  16  16    16 32  32    32
        
      So you get proportions like 5/4, 7/8, 11/8, 17/16, 33/32.
    • x_nearest_power_of_two

      protected static int x_nearest_power_of_two(int i)
      Internal aux function. @see nearest_power_of_two(int).
    • isPowerOfTwo

      public static boolean isPowerOfTwo(int i)
      Whether an integer is a power of two. Only defined for positive integers (>=0). Zero returns false.
    • nextNotLowerPowerOfTwo

      public static int nextNotLowerPowerOfTwo(int i)
      Returns the next power of two which is not lower than the argument. Only defined for positive integers (>=0).
    • nextNotHigherPowerOfTwoExponent

      public static int nextNotHigherPowerOfTwoExponent(int i)
      Returns the next power of two which is not higher than the argument. Only defined for positive integers (>=0).
    • logTwo

      public static int logTwo(int i)
      Delivers the exponent of two. Only defined if the argument indeed is a power of two. Only defined for positive integers (>0).
    • isPowerOfTwo

      public static boolean isPowerOfTwo(Rational rat)
      Returns true for 1/n or n/1, with n power of two(2), including 0/1.
    • binarySectioning

      public static List<Integer> binarySectioning(int i)
      Returns the unique descending list of powers of two which sum up to the argument.
    • of

      public static RationalDuration of(int num, int den)
      Make a duration n/d for n>=0 and d>0.
    • of

      public static RationalDuration of(Rational rat)
      Make a duration for rat>=0/1.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • compareTo

      public int compareTo(RationalDuration o)
      Specified by:
      compareTo in interface Comparable<RationalDuration>
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toRational

      public Rational toRational()
      Description copied from interface: DurationIndication
      Returns the "Notated CWN Duration", which is a rational, standing for a note duration (1/4 for a quater note, 3/8 for a dotted quater note, etc.)
      Specified by:
      toRational in interface DurationIndication
    • fromRational

      public static RationalDuration fromRational(Rational rat)