User:Don Guinn/PianoTuningScript

From J Wiki
Jump to: navigation, search
Note 'Verbs to build tables in Tuning'

This script builds the tables in the Jwiki on piano tuning.

Accessed from Essays/PianoTuning

f1 beats f2
  Calculates the number of beats per second heard when
  frequencies f1 and f2 are played together

f1 cent f2
  Gives the difference between frequencies f1 and f2 in cents

  The first table of fundamental frequencies and harmonics

  The second table showing beats for 1 hz frequency

  The third table showing the temperament tuning procedure

Bottom of this script
  Four statements which build the beats for the check for A#

These definitions are for the ideal string or pipe where the
overtones follow exact multiples of the fundamental. In
reality strings have stiffness, thickness and flaws which
shift the overtones away from exact multiples. The stiffness
shifts the overtones sharper, slightly more than exact

The thickness of pipes shifts the overtones flatter, less
than exact multiples.

Chimes and drums have overtones which are nowhere near
multiples For that reason two chimes are never played
harmonically but melodically as the overtones clash so much
that they sound awful together.

Otherwise these shifts are neglegable in the mid-ranges of
various instruments but become significant at the extremes.


NB.*beats v Find the smallest beat between two frequencies
NB. Example:
NB.   220 beats 329.628 NB. The beats between A and E
NB. Arguments:
NB.   x y - The frequencies
NB. Return:
NB.   Three number list -
NB.     0 - The smallest number of beats
NB.     1 - The multiple or the frequency giving the beat for y
NB.     2 - The multiple giving the beat for x
NB. Logic:
NB.   The fundemental and the next eight harmonics are calculated
NB.   and the all combinations of differences are calculated. Then
NB.   the difference nearest zero is found. That number, along with
NB.   the frequency ratios are returned. If the number of beats is
NB.   negative thn the second note is flat of perfect. If positive
NB.   it is sharp of perfect.
beats=:4 : 0"0
i=.($b)#:a i.<./a=.,|b

NB.*cent v Convert note pair frequencies to cents
NB.   from J-ottings 46: Musical J-ers by Norman Thomson
NB. Arguments:
NB.   [ ] - The note frequencies
NB. Return:
NB.   The interval from the left note to the right note in cents.

NB. 12%:2 to 1000 places just for fun, formatted for display

NB. Names of notes in the temperament
notes=:<;._2 'F F# G G# A A# B C C# D D# E '

NB. Names of Intervals
i=.'unison';'semi-tone';'second';'minor third';'major third'
i=.i,'fourth';'tritone';'fifth';'minor sixth';'major sixth'
intervals=:i,'minor seventh';'major seventh';'octave'

NB. Indices to names and intervals used in the temperament
temperament_intervals=:3 4 5 7 8 9

NB. When laying the temperament I do use the tritone as well, but
NB. it sounds very rough. If it doesn`t sound rough then I know I
NB. made a mistake. I just don`t include it as a temperament
NB. interval as it`s not very useful.

NB. simitone or half-step

NB. Frequency of the first note of the temperament

NB. Frequencies of the notes in the temperament

NB. The Temperament Octave
Temperament_Octave=:notes (4 : 0) ,;._2 (0 : 0)
                   First     Second      Third     Fourth      Fifth
  Fundamental   Harmonic   Harmonic   Harmonic   Harmonic   Harmonic

NB. Beats for various note intervals made to look pretty
NB. Beats are proportional to the frequency. So, to build this
NB. list, the frequency is set to one. multiply the beats for a
NB. given interval by the frequency to get the the beats for
NB. between a note and the note above in an interval.
b=.1 beats st^temperament_intervals
t=.21{."1]12j4":,.{."1 b
t=.t,.([:;[:":&.>{.;'r';}.)"1|."1}."1 b
Beats=:(;'Interval      ';'Beat Factor      ';'Ratio'),'-',t

NB. The temperament tuning list
NB. Each line is a step.
NB. The tuning and checks of the notes used in the temperament.
NB. The first token in the line is t for tune and c for check.
NB. The second and third token are the notes in the interval with
NB. the first being the note already tuned and the second the one
NB. being tuned or checked.
temperament=:cut;._2 toupper 0 : 0
t a  d
t a  e
t e  b
c d  b
t b  f#
c a  f#
c d  f#
t f# c#
c a  c#
c e  c#
t c# g#
c b  g#
c d  g#
c c# g#
t g# d#
c a  d#
c f# d#
t d# a#
c d  a#
c f# a#
c c# a#
t a# f
c g# f
c a  f
c d  f
c c# f
t f  c
c g# c
c e  c
c a  c
c d# c
t c  g
c g  b
c a# g
c e  g
c c# g
c d  g

NB.*Display_Temperament v Display the temperament with beats
NB. Builds each line in the temperament procedure table
Temperament_Step=:3 : 0
'c n'=.({.;}.)notes i. y
select. c
  case. 7  do. m=.<'Check'
  case. 12 do. m=.<'Tune '
  case.    do. wd 'mb "Error" "Unidentified case"'
t=.(m,2{.&.>}.y),(12&{.&.>intervals{~|--/n),(0j3":&.><"0 f)
NB. t=.t,(<([:;[:":&.>{.;'r';}.)"1|."1}."1 b),<7j3":{.b
;:^:_1]0 3 2 5 1 4 6{t

NB.*Temperament_Procedure n The temperament procedure table
Temperament_Procedure=: 3 : 0 ''
t=.Temperament_Step"1 temperament
t=.'Tune  octave       A  220.000 tuning fork  0.000',t
   '  Action           Note       Using        Beats','-',t

NB. Definitions for last calculations

NB. Calculate  A# beats F# D C#
{."1] 233.082 beats 184.997 293.665 277.183

NB. Raising A# by 0.5 hz
{."1] 233.582 beats 184.997 293.665 277.183

NB. Lowering A# by 0.5 hz
{."1] 232.582 beats 184.997 293.665 277.183

NB. Determine the error in cents
232.582 233.582 cent 233.082