public static IEnumerable <RhythmicDuration> Parse(params int[] durations)
 {
     return(durations.Select(i =>
     {
         var denominatorAsPowerOfTwo = UsefulMath.Log2(i);
         return new RhythmicDuration(denominatorAsPowerOfTwo);
     }).ToArray());
 }
예제 #2
0
        /// <summary>
        /// Tunes this Pitch to TunedPitch using a specific standard pitch and a specific tuning system.
        /// </summary>
        /// <param name="standardPitch">Standard pitch (i.e. A4 = 440 Hz, etc.)</param>
        /// <param name="tuningSystem"></param>
        /// <returns>Pitch with assigned frequency</returns>
        public TunedPitch Tune(TunedPitch standardPitch, TuningSystem tuningSystem)
        {
            var logarythmicProportion = tuningSystem.AllIntervalRatios[new BoundInterval(standardPitch, this)];
            var freq = standardPitch.Frequency * UsefulMath.CentsToLinear(logarythmicProportion);

            freq *= (this.Octave - standardPitch.Octave) + 1;
            return(new TunedPitch(this, freq));
        }
        public static bool TryParse(string s, out RhythmicDuration duration)
        {
            int val;

            if (!int.TryParse(s, out val))
            {
                duration = default(RhythmicDuration);
                return(false);
            }
            var denominatorAsPowerOfTwo = UsefulMath.Log2(val);

            duration = new RhythmicDuration(denominatorAsPowerOfTwo);
            return(true);
        }
        /// <summary>
        /// Parses RhythmicDuration from a string
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static RhythmicDuration Parse(string s)
        {
            var numberOfDots = s.ToCharArray().Count(c => c == '.');
            int denominator;

            if (!int.TryParse(s.Replace(".", ""), out denominator))
            {
                throw new Exception("Could not parse string. Unrecognized duration.");
            }

            var denominatorAsPowerOfTwo = UsefulMath.Log2(denominator);

            return(new RhythmicDuration(denominatorAsPowerOfTwo, numberOfDots));
        }
예제 #5
0
        /// <summary>
        /// Converts cents value to approximated proportion.
        /// </summary>
        /// <param name="cents"></param>
        /// <param name="precision"></param>
        /// <returns></returns>
        public static Proportion GetApproximatedProportionFromCents(double cents, int precision = 6)
        {
            if (precision < 1 || precision > 20)
            {
                throw new ArgumentException("Precision must be between 1 and 20.", "precision");
            }

            var decimalValue    = UsefulMath.CentsToLinear(cents);
            var normalizedValue = decimalValue;
            var denominator     = 1;
            var maxDenominator  = Math.Pow(10, precision);

            while (normalizedValue % 10 != 0 && denominator <= maxDenominator)
            {
                denominator     *= 10;
                normalizedValue *= 10;
            }
            return(new Proportion((int)normalizedValue, denominator).Normalize());
        }