musicalscale.frink

Download or view musicalscale.frink in plain text format


// Optimize a musical scale to find the optimal number of notes in an octave.

// Key frequency ratios to evaluate at
// These numbers are the Farey series between 1 and 2, in order of generation.
points= [[3/2, 1],
         [4/3, 1/2], [5/3, 1/2],
         [5/4, 1/4], [7/5, 1/4], [8/5, 1/4], [7/4, 1/4]] /*
         [6/5, 1/8], [9/7, 1/8], [11/8, 1/8], [10/7, 1/8], [11/7, 1/8], [13/8, 1/8], [12/7, 1/8], [9/5, 1/8]] */


bestError = 1000

for root = 2 to million
{
//   print["$root\t"]
   ratio = 2^(1/root)

   sum = 0
   for [point, weight] = points
   {
      // Find optimal number of steps
      optSteps = log[point]/log[ratio]

      // Error between steps and closest note in this scale
      error = abs[(optSteps-round[optSteps])/optSteps]

      // We're going to take RMS error
      sum = sum + error^2 * weight^2

//      println["$point\t$error"]
   }

   e = sqrt[sum] * root
   
//   print[format[e,1,6]]

   // Best so far?
   if (e < bestError)
   {
//      print["\t*"]
      println["$root\t$e\t*"]
      bestError = e
   }
   
//   println[]
}


Download or view musicalscale.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 20145 days, 5 hours, 54 minutes ago.