lilypond-user
[Top][All Lists]
Advanced

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

Snippet 390


From: Alexander Kobel
Subject: Snippet 390
Date: Sat, 12 Apr 2008 00:45:08 +0200
User-agent: Thunderbird 2.0.0.12 (Macintosh/20080213)

Oh, and since I'm typing right now anyway: Here's a slight modification of LSR snippet 390 to also modify rest events, laying around here for some time...

        (if (or (eq? 'NoteEvent (ly:music-property chordElt 'name))
                (eq? 'RestEvent (ly:music-property chordElt 'name)))
instead of just
        (if (eq? 'EventChord (ly:music-property musicElt 'name))
quite at the beginning of the code.

Might be of use for some of you...


Alexander
%% from: http://lsr.dsi.unimi.it/LSR/Item?id=390

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Here begins the scheme functions definitions
%% You can put all this section in a separate file (rhythm.ly for example)

#(define rhythmVectorIndex 0)

#(define (transformEachNote chordElt rhythmVector)
(begin          
        (if (or (eq? 'NoteEvent (ly:music-property chordElt 'name))
                (eq? 'RestEvent (ly:music-property chordElt 'name)))
                (set! (ly:music-property chordElt 'duration) (vector-ref 
rhythmVector rhythmVectorIndex)) 
        )               
        chordElt
))

#(define (getChords musicElt rhythmVector)
(begin
        (if (eq? 'EventChord (ly:music-property musicElt 'name))
                (begin
                        (map                    
                                (lambda (x) (transformEachNote x rhythmVector)) 
                                (ly:music-property musicElt 'elements)
                        ) 
                        (set! rhythmVectorIndex (1+ rhythmVectorIndex))
                        (if (= rhythmVectorIndex (vector-length rhythmVector)) 
(set! rhythmVectorIndex 0))                      
                )       
        )               
        musicElt
)) 

         %%%%%%%%%%%%%%%% string functions %%%%%%%%%%%%%%%%%%%%%

#(define (string->duration strElt)
(
        let*(
                (ptindex (string-index strElt #\. ))    
                                                        ;; position of "." in 
"4." for exemple. #f if no ".".
                (ptnumber 0)                            
                (val (string->number (if ptindex (substring strElt 0 ptindex) 
strElt))) 
                                                        ;; val = 1 2 4 8 ... 
(without the ".")
                (dur (ly:intlog2 val))  
                                                        ;; dur = 0 1 2 3 ... 
(need for ly:make-duration)
        )
                                                        ;; find the number of 
"." in Duration
    (while ptindex (
                begin
                        (set! ptnumber (1+ ptnumber))
                        (set! ptindex (string-index strElt #\.  (1+ ptindex) ))
                        )
        )
        (ly:make-duration dur ptnumber 1 1)             
))



#(define (string->vectorDuration str)
(   let* (
                (i 0)
                (strList (string-split str #\space ))
                (len (length strList))
                (v (make-vector len))
                )
        (map   
                (lambda (x)
                        (begin
                                (vector-set! v i (string->duration x))
                                (set! i (1+ i))
                        )
                        x
                )       
                strList
        )
        v
))

         %%%%%%%%%%%%%%%%%% the main function %%%%%%%%%%%%%%%%%%%%

makeRhythm  = #(define-music-function (parser location m str) (ly:music? 
string?)
(       let* (
                        (prevWasSpace #t)
                        (trimmedStr 
                                (string-delete  
                                        (string-trim-right str) 
                                        (lambda (c)
                                                (       let* 
                                                        (
                                                                (currentIsSpace 
(char=? c #\space))
                                                                (res (and 
prevWasSpace currentIsSpace))
                                                        )
                                                        (if (not res) (set! 
prevWasSpace currentIsSpace))       
                                                        res
                                                )
                                        )
                                )
                        )
                )
        (set! rhythmVectorIndex 0)
        (music-map 
                (lambda (x) 
                        (getChords x (string->vectorDuration trimmedStr)))
                m
        )
)) 
%% End of the scheme functions definitions
%% ( end of rhythm.ly if you use this separate file for them)

reply via email to

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