Skip to content

Instantly share code, notes, and snippets.

@uliska
Created November 21, 2016 15:00
Show Gist options
  • Save uliska/69bed53d5bc3658336619161aecb3dd8 to your computer and use it in GitHub Desktop.
Save uliska/69bed53d5bc3658336619161aecb3dd8 to your computer and use it in GitHub Desktop.
Displaying basic Just Intonation pitches
\version "2.19.50"
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Basic Notation 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.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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)))
% Maintain a current duration to be used when no duration is given,
% initialize to quarter notes (like with regular pitches without duration)
#(define ji-duration (ly:make-duration 2))
% Map the semitone returned by ratio->step/cent
% to a LilyPond pitch definition.
% This is based on the middle c
#(define (semitones->pitch semitone)
(let
;; two lists defining the 12 steps within the octave
;; c cis d dis e f fis g as a bes b
((steps '(0 0 1 1 2 3 3 4 4 5 6 6))
(semis '(0 1/2 0 1/2 0 0 1/2 0 1/2 0 -1/2 0))
;; strip semitones of octave
(index (modulo semitone 12)))
;; generate LilyPond pitch object to be returned
(ly:make-pitch
(floor (/ semitone 12))
(list-ref steps index)
(list-ref semis index))))
% Produce a note in Just Intonation.
% if dur(ation) is given the active duration is changed.
jiNote =
#(define-music-function (dur ratio)
((ly:duration?) rational?)
(if dur
;; optional argument is present: set duration
(set! ji-duration dur))
(let*
;; note as pair of semitone-interval and cent deviation
((step/cent (ratio->step/cent ratio))
;; LilyPond pitch as defined by the ratio
(pitch-from-ratio (semitones->pitch (car step/cent)))
(cent (cdr step/cent)))
;; finally create the note with generated pitch and markup addition
(make-music
'NoteEvent
;; add text element as articulation
'articulations
(list (make-music
'TextScriptEvent
;; round the cent deviation number for display
'text (format "(~@f)" (round cent))))
'pitch pitch-from-ratio
'duration ji-duration)))
%%%%%%%%%%%
% Example %
%%%%%%%%%%%
% Style the display of the cent deviation
% (note that this is completely independent from the
% calculation and encoding)
\layout {
\context {
\Voice
\override TextScript.direction = #UP
\override TextScript.font-size = -2.5
\override TextScript.self-alignment-X = -0.25
}
}
\markup "Different ratios over the middle c"
{
\jiNote 1 #7/4
\jiNote #3/2
\jiNote #4/3
\jiNote #5/4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment