[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
functions in include file
From: |
Anders Eriksson |
Subject: |
functions in include file |
Date: |
Fri, 22 Jun 2012 14:38:58 +0200 |
User-agent: |
Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20120614 Thunderbird/13.0.1 |
Hello,
I have just started using Lilypond and I have found some functions on
the net
which I would like to put in an include file. But I can't figure out how!?
One of these function is Mr. John Mandereaus "Diatonic/modal transposition
function"
I have put the code in a file (diatonic-transpose.lyi) and is calling it
like
this
%% Start of file
\version "2.14.2"
\include "diatonic-transpose.lyi"
notes = \relative c' {
c d e f g a b c
}
\score {
<<
\new Staff { \clef "G"
\notes
\diatonicTranspose c \major #2 \notes
}
>>
\layout {}
\midi {}
}
%% EOF
When I compile it I get:
Starting lilypond-windows.exe 2.14.2 [test-dia-transpose.ly]...
Processing `C:/Users/ame/Documents/Lilipond/Tests/test-dia-transpose.ly'
Parsing...
C:/Users/ame/Documents/Lilipond/Tests/test-dia-transpose.ly:13:15: error:
unknown escaped string: `\diatonicTranspose'
\diatonicTranspose c \major #2 \notes
C:/Users/ame/Documents/Lilipond/Tests/test-dia-transpose.ly:13:15:
error: syntax
error, unexpected STRING
\diatonicTranspose c \major #2 \notes
C:/Users/ame/Documents/Lilipond/Tests/test-dia-transpose.ly:10:2: error:
errors
found, ignoring music expression
<<
error: failed files: "C:/Users/ame/Documents/Lilipond/Tests/test-dia-
transpose.ly"
Exited with return code 1.
So WHY can't it find \diatonicTranspose ???
// Anders
Here is diatonic-transpose.lyi
%% Start of file diatonic-transpose.lyi
#(define (true-quotient n d)
"Return the one true quotient in integer division of n by d, i.e.
return the integer q so that there is a unique integer r that
satisfies n = qd + r and 0 <= r < |d|"
(if (>= n 0)
(quotient n d)
(- (quotient n d) 1)))
#(define (ly-pitch->modal-pitch ly-pitch scale tonic-c-diff)
"Convert ly-pitch to a list (octave degree depresentation) which
represents
the modal pitch in scale, with tonic-c-diff as the pitch diff from tonic to
middle C. scale should be a notename->alteration alist of length 7 which
represents the alteration of each note with C as first pitch of the scale."
(let* ((normalized-pitch (ly:pitch-transpose ly-pitch tonic-c-diff))
(notename (ly:pitch-notename normalized-pitch)))
(list
(ly:pitch-octave normalized-pitch) ;; octave
notename ;; degree
;; alteration
(- (ly:pitch-alteration normalized-pitch) (ly:assoc-get notename
scale)))))
#(define (degree-transpose modal-pitch degree-delta scale-length)
"Transpose modal-pitch (octave degree alteration) by degree-delta assuming
scale-length."
(let* ((octave (car modal-pitch))
(degree (cadr modal-pitch))
(alteration (caddr modal-pitch))
(relative-new-degree (+ degree degree-delta)))
(list
(+ octave (true-quotient relative-new-degree scale-length))
(modulo relative-new-degree scale-length)
alteration)))
#(define (modal-pitch->ly-pitch modal-pitch scale c-tonic-diff)
"Convert modal-pitch -- a list (octave degree alteration) -- to a
standard pitch using scale and pitch diff from middle C to tonic.
scale should be a notename->alteration alist of length 7 which represents
the alteration of each note with C as first pitch of the scale."
(let* ((octave (car modal-pitch))
(degree (min 6 (cadr modal-pitch)))
(alteration (caddr modal-pitch))
(abs-alteration (+ alteration (ly:assoc-get degree scale))))
(ly:pitch-transpose
(ly:make-pitch octave degree abs-alteration)
c-tonic-diff)))
#(define (lookup-music-property event type)
"Return the first music property of the given type found in event,
or #f is no such property is found. event should be music or a list
of music."
(if (null? event)
#f
(if (list? event)
(or
(lookup-music-property (car event) type)
(lookup-music-property (cdr event) type))
(let ((p (ly:music-property event 'pitch)))
(if (not (null? p))
p
(let* ((e (ly:music-property event 'element))
(es (ly:music-property event 'elements))
(p2 (if (null? e)
'()
(lookup-music-property e type))))
(if (not (null? p2))
p2
(lookup-music-property es type))))))))
#(define (mode-transpose
pitch-note1 scale1 degree-delta pitch-note2 scale2 music)
(let ((tonic-diff1 (ly:pitch-diff
(ly:make-pitch 0 0 0)
(lookup-music-property pitch-note1 'pitch)))
(tonic-diff2 (ly:pitch-diff
(lookup-music-property pitch-note2 'pitch)
(ly:make-pitch 0 0 0))))
(music-map
(lambda (event)
(let ((p (ly:music-property event 'pitch)))
(if (ly:pitch? p)
(ly:music-set-property!
event
'pitch
(modal-pitch->ly-pitch
(degree-transpose
(ly-pitch->modal-pitch p scale1 tonic-diff1)
degree-delta
(length scale1))
scale2
tonic-diff2)))
event))
music)))
modeTranspose =
#(define-music-function
(parser location pitch-note1 scale1 degree-delta pitch-note2 scale2
music)
(ly:music? list? number? ly:music? list? ly:music?)
"Transpose music from scale1 on tonic pitch-note1
by degree-delta to scale2 on tonic pitch-note2."
(mode-transpose
pitch-note1 scale1 degree-delta pitch-note2 scale2 music))
diatonicTranspose =
#(define-music-function
(parser location pitch-note scale degree-delta music)
(ly:music? list? number? ly:music?)
"Transpose music by degree-delta in scale on tonic pitch-note."
(mode-transpose
pitch-note scale degree-delta pitch-note scale music))
%% EOF
--
Computer says No!
- functions in include file,
Anders Eriksson <=