Skip to content

Instantly share code, notes, and snippets.

@uliska
Last active November 26, 2016 14:30
Show Gist options
  • Save uliska/6ae9f73efbaff0c7828c82a04c76e08b to your computer and use it in GitHub Desktop.
Save uliska/6ae9f73efbaff0c7828c82a04c76e08b to your computer and use it in GitHub Desktop.
Underlying calculations to convert a frequency ratio in a pair of chromatic scale steps and cent deviation
\version "2.19.50"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Basic Caculation Example for "Just Intonation with LilyPond" paper
%
% This file is self-contained to be compiled with recent versions
% of LilyPond's 2.19 development version or later. It will not work
% with LilyPond 2.18.
% But as it only contains function definitions there is no real use
% in compiling it yet.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Underlying mathematics
% Convert a ratio to a floating point step representation.
% The integer part is the number of semitones above the fundamental,
% the fractional part is the fraction of a semitone
#(define (ratio->step ratio)
(* 12 (/ (log ratio) (log 2))))
% Convert a ratio and return a pair with
% - the pitch in semitones
% - the cent deviation above or below (rounded)
% Rounds to the nearest semitone and gives the deviation
% in cents -50 < cent < 49.
#(define (ratio->step/cent ratio)
(let*
((step-cent (ratio->step ratio))
;; truncate the floating point number to the nearest integer (scale step)
(step (inexact->exact (round step-cent)))
;; determine the cent deviation by stripping off
;; the floating point part of step-ratio
(cent (* 100 (- step-cent step))))
;; construct and return the pair
(cons step cent)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment