1 %% Do not edit this file; it is auto-generated from input/new
2 %% This file is in the public domain.
5 doctitle = "Transposing music with minimum accidentals"
7 texidoc = "There is a way to enforce enharmonic modifications for
8 notes in order to have the minimum number of accidentals. In this
9 case, the following rules apply:
11 \"Double accidentals should be removed, as well as E sharp (-> F),
12 C flat (-> B), F flat (-> E) and B sharp (-> C)\".
14 In this manner, the most natural enharmonic notes are chosen.
17 #(define (naturalize-pitch p)
18 (let* ((o (ly:pitch-octave p))
19 (a (* 4 (ly:pitch-alteration p)))
20 ; alteration, a, in quarter tone steps, for historical reasons
21 (n (ly:pitch-notename p)))
23 ((and (> a 1) (or (eq? n 6) (eq? n 2)))
26 ((and (< a -1) (or (eq? n 0) (eq? n 3)))
30 ((> a 2) (set! a (- a 4)) (set! n (+ n 1)))
31 ((< a -2) (set! a (+ a 4)) (set! n (- n 1))))
32 (if (< n 0) (begin (set! o (- o 1)) (set! n (+ n 7))))
33 (if (> n 6) (begin (set! o (+ o 1)) (set! n (- n 7))))
34 (ly:make-pitch o n (/ a 4))))
36 #(define (naturalize music)
37 (let* ((es (ly:music-property music 'elements))
38 (e (ly:music-property music 'element))
39 (p (ly:music-property music 'pitch)))
41 (ly:music-set-property!
43 (map (lambda (x) (naturalize x)) es)))
45 (ly:music-set-property!
50 (set! p (naturalize-pitch p))
51 (ly:music-set-property! music 'pitch p)))
55 #(define-music-function (parser location m)
59 music = \relative c' { c4 d e g }
63 \transpose c ais \music
64 \naturalizeMusic \transpose c ais \music
65 \transpose c deses \music
66 \naturalizeMusic \transpose c deses \music
68 \layout { ragged-right = ##t }