lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: transpose pitch by semitones


From: Gilles THIBAULT
Subject: Re: transpose pitch by semitones
Date: Tue, 01 Dec 2015 15:21:54 +0100
User-agent: KMail/4.14.5 (Linux/4.1.13-desktop-2.mga5; KDE/4.14.5; x86_64; ; )

> I made this function some times ago, but don't know if is enough
> straightforward :-)
OK, this one is more straightforward... 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% sharp
#(define (semitones->pitch n)
(let ((a (quotient n 12))
      (b 0)
      (c (modulo n 12)))
  (cond ;; 2 semitones in a tone but not between E and F (c=5)
   ((< c 5) (ly:make-pitch a (quotient c 2) (* 1/2 (modulo c 2))))
   (else (ly:make-pitch a (quotient (1+ c) 2) (* 1/2 (modulo (1+ c) 2)))))))

%% flat
%{ %comment this line if you want notes with flats instead sharps
#(define (semitones->pitch n)
(let ((a (quotient n 12))
      (b 0)
      (c (modulo n 12)))
  (cond 
   ((< c 5) (ly:make-pitch a (+ (quotient c 2) (modulo c 2))
                             (* -1/2 (modulo c 2))))
   (else (ly:make-pitch a (+ (quotient (1+ c) 2) (modulo (1+ c) 2))
                          (* -1/2 (modulo (1+ c) 2)))))))
%}

%%test

#(for-each 
   (lambda(n)(format #t "~2,' d - ~a\n" n (semitones->pitch n)))
   '(-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-- 
Gilles



reply via email to

[Prev in Thread] Current Thread [Next in Thread]