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 00:44:52 +0100
User-agent: KMail/4.14.5 (Linux/4.1.13-desktop-2.mga5; KDE/4.14.5; x86_64; ; )

> #(define (semitones->pitch semitone)
>    (let ((index (modulo semitone 12))
>          (octave (floor (/ semitone 12))))
>      (list
>       octave
>       (list-ref
>        '((0 0)   ; c
>           (0 1/2) ; cis
>           (1 0)   ; d
>           (1 1/2) ; dis
>           (2 0)   ; e
>           (3 0)   ; f
>           (3 1/2) ; fis %  \jiPitch 2 1
> 
>           (4 0)   ; g
>           (4 1/2) ; gis
>           (5 0)   ; a
>           (5 1/2) ; ais
>           (6 0))   ; b
>        index))))
> 
> but would like to find a solution that is more straightforward.
Sorry to answer so late.
I made this function some times ago, but don't know if is enough   
straightforward :-)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\version "2.18.2"

#(define (semitones->pitch n)
(let loop ((a 0)
           (b 0)
           (c n))
  (cond 
   ((> c 11)(loop (1+ a) b (- n 12)))
   ((< c 0)(loop (1- a) b (+ n 12)))
   ((< 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)))))))

%%% 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]