]> git.donarmstrong.com Git - lilypond.git/blobdiff - scm/music-functions.scm
* input/test/scales-greek.ly: remove.
[lilypond.git] / scm / music-functions.scm
index 3ef7eccc666b52b0cdbccf449f82bfa1e8a2921e..479c28b79f2bc5b2dcba253426fd0db149115d07 100644 (file)
@@ -1,4 +1,11 @@
-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;; music-functions.scm --
+;;;;
+;;;;  source file of the GNU LilyPond music typesetter
+;;;; 
+;;;; (c)  1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
+;;;;                 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+
+;; (use-modules (ice-9 optargs)) 
 
 ;;; ly:music-property with setter
 ;;; (ly:music-property my-music 'elements)
@@ -51,7 +58,7 @@
   (set! music (inner-music-filter pred? music))
   (if (ly:music? music)
       music
-      (make-music-by-name 'Music)))      ;must return music.
+      (make-music 'Music)))      ;must return music.
 
 (define-public (remove-tag tag)
   (lambda (mus)
@@ -68,7 +75,7 @@
   (display ": { ")  
   (let ((es (ly:music-property music 'elements))
        (e (ly:music-property music 'element)))
-    (display (ly:get-mutable-properties music))
+    (display (ly:music-mutable-properties music))
     (if (pair? es)
        (begin (display "\nElements: {\n")
               (map display-music es)
 (define-public (note-to-cluster music)
   "Replace NoteEvents by ClusterNoteEvents."
   (if (eq? (ly:music-property music 'name) 'NoteEvent)
-      (let ((cn (make-music-by-name 'ClusterNoteEvent)))
-       (set! (ly:music-property cn 'pitch)
-             (ly:music-property music 'pitch))
-       (set! (ly:music-property cn 'duration)
-             (ly:music-property music 'duration))
-       cn)
+      (make-music 'ClusterNoteEvent
+                 'pitch (ly:music-property music 'pitch)
+                 'duration (ly:music-property music 'duration))
       music))
 
 (define-public (notes-to-clusters music)
 
 (define-public (unfold-repeats music)
   "
-This function replaces all repeats  with unfold repeats. It was 
-written by Rune Zedeler. "
+This function replaces all repeats  with unfold repeats. "
+  
   (let ((es (ly:music-property music 'elements))
        (e  (ly:music-property music 'element))
        (n  (ly:music-name music)))
     (if (equal? n "Repeated_music")
        (begin
+         
          (if (equal? (ly:music-property music 'iterator-ctor)
                      Chord_tremolo_iterator::constructor)
-             (shift-duration-log music (ly:intlog2 (ly:music-property music 'repeat-count)) 0))
+             (let*
+                 ((seq-arg? (memq 'sequential-music
+                                  (ly:music-property e 'types)))
+                  (count  (ly:music-property music 'repeat-count))
+                  (dot-shift (if (= 0 (remainder count 3))
+                                 -1 0))
+                  )
+
+               (if (= 0 -1)
+                   (set! count (* 2 (quotient count 3))))
+               
+               (shift-duration-log music (+ (if seq-arg? 1 0)
+                                            (ly:intlog2 count)) dot-shift)
+               
+               (if seq-arg?
+                   (ly:music-compress e (ly:make-moment (length (ly:music-property e 'elements)) 1)))
+               ))
+         
          (set! (ly:music-property music 'length)
                Repeated_music::unfolded_music_length)
          (set! (ly:music-property music 'start-moment-function)
                Repeated_music::first_start)
          (set! (ly:music-property music 'iterator-ctor)
                Unfolded_repeat_iterator::constructor)))
+    
     (if (pair? es)
        (set! (ly:music-property music 'elements)
              (map unfold-repeats es)))
@@ -157,31 +180,29 @@ written by Rune Zedeler. "
 (define-public (make-grob-property-set grob gprop val)
   "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
 i.e.  this is not an override"
-  (let ((m (make-music-by-name 'OverrideProperty)))
-    (set! (ly:music-property m 'symbol) grob)
-    (set! (ly:music-property m 'grob-property) gprop)
-    (set! (ly:music-property m 'grob-value) val)
-    (set! (ly:music-property m 'pop-first) #t)
-    m))
+  (make-music 'OverrideProperty
+             'symbol grob
+             'grob-property gprop
+             'grob-value val
+             'pop-first #t))
 
 (define-public (make-grob-property-override grob gprop val)
   "Make a Music expression that sets GPROP to VAL in GROB. Does a pop first,
 i.e.  this is not an override"
-  (let ((m (make-music-by-name 'OverrideProperty)))
-    (set! (ly:music-property m 'symbol) grob)
-    (set! (ly:music-property m 'grob-property) gprop)
-    (set! (ly:music-property m 'grob-value) val)
-    m))
+  (make-music 'OverrideProperty
+             'symbol grob
+             'grob-property gprop
+             'grob-value val))
 
 (define-public (make-grob-property-revert grob gprop)
   "Revert the grob property GPROP for GROB."
-   (let* ((m (make-music-by-name  'OverrideProperty)))
-     (set! (ly:music-property m 'symbol) grob)
-     (set! (ly:music-property m 'grob-property) gprop)
-     m))
+  (make-music 'OverrideProperty
+             'symbol grob
+             'grob-property gprop))
 
 (define direction-polyphonic-grobs
-  '(Tie Rest Slur Script TextScript Stem Dots DotColumn))
+  '(Stem Tie Rest Slur Script TextScript Dots DotColumn
+        ))
 
 (define-public (make-voice-props-set n)
   (make-sequential-music
@@ -189,83 +210,92 @@ i.e.  this is not an override"
     (map (lambda (x) (make-grob-property-set x 'direction
                                             (if (odd? n) -1 1)))
         direction-polyphonic-grobs)
-    (list (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
-         (make-grob-property-set 'MultiMeasureRest 'staff-position
-                                 (if (odd? n) -4 4))))))
+    (list
+     (make-grob-property-set 'NoteColumn 'horizontal-shift (quotient n 2))
+     (make-grob-property-set 'MultiMeasureRest 'staff-position (if (odd? n) -4 4))
+     
+     )))) 
+
 
 (define-public (make-voice-props-revert)
   (make-sequential-music
    (append
     (map (lambda (x) (make-grob-property-revert x 'direction))
         direction-polyphonic-grobs)
-    (list (make-grob-property-revert 'NoteColumn 'horizontal-shift)))))
+    (list (make-grob-property-revert 'NoteColumn 'horizontal-shift))
+    (list (make-grob-property-revert 'MultiMeasureRest 'staff-position)))))
+
+
+(define*-public (context-spec-music m context #:optional id)
+  "Add \\context CONTEXT = ID to M. "
+  (let ((cm (make-music 'ContextSpeccedMusic
+                       'element m
+                       'context-type context)))
+    (if (string? id)
+       (set! (ly:music-property cm 'context-id) id))
+    cm))
 
 
-(define-public (context-spec-music m context . rest)
-  "Add \\context CONTEXT = foo to M. "
-  (let ((cm (make-music-by-name 'ContextSpeccedMusic)))
-    (set! (ly:music-property cm 'element) m)
-    (set! (ly:music-property cm 'context-type) context)
-    (if (and  (pair? rest) (string? (car rest)))
-       (set! (ly:music-property cm 'context-id) (car rest)))
+(define-public (descend-to-context m context)
+  "Like context-spec-music, but only descending. "
+  (let ((cm (context-spec-music m context)))
+    (ly:music-set-property! cm 'descend-only #t)
     cm))
 
+(define-public (make-non-relative-music mus)
+  (make-music 'UnrelativableMusic
+             'element mus
+  ))
+
 (define-public (make-apply-context func)
-  (let ((m (make-music-by-name 'ApplyContext)))
-    (set! (ly:music-property m 'procedure) func)
-    m))
+  (make-music 'ApplyContext
+             'procedure func))
 
 (define-public (make-sequential-music elts)
-  (let ((m (make-music-by-name 'SequentialMusic)))
-    (set! (ly:music-property m 'elements) elts)
-    m))
+  (make-music 'SequentialMusic
+             'elements elts))
 
 (define-public (make-simultaneous-music elts)
-  (let ((m (make-music-by-name 'SimultaneousMusic)))
-    (set! (ly:music-property m 'elements) elts)
-    m))
+  (make-music 'SimultaneousMusic
+             'elements elts))
 
 (define-public (make-event-chord elts)
-  (let ((m (make-music-by-name 'EventChord)))
-    (set! (ly:music-property m 'elements) elts)
-    m))
+  (make-music 'EventChord
+             'elements elts))
 
 (define-public (make-skip-music dur)
-  (let ((m (make-music-by-name 'SkipMusic)))
-    (set! (ly:music-property m 'duration) dur)
-    m))
+  (make-music 'SkipMusic
+             'duration dur))
+
+(define-public (make-grace-music music)
+  (make-music 'GraceMusic
+             'element music))
 
 ;;;;;;;;;;;;;;;;
 
 ;; mmrest
 (define-public (make-multi-measure-rest duration location)
-  (let ((start (make-music-by-name 'MultiMeasureRestEvent))
-       (ch    (make-music-by-name 'BarCheck))
-       (ch2   (make-music-by-name 'BarCheck))
-       (seq   (make-music-by-name 'MultiMeasureRestMusicGroup)))
-    (map (lambda (x) (set! (ly:music-property x 'origin) location))
-        (list start ch ch2 seq))
-    (set! (ly:music-property start 'duration) duration)
-    (set! (ly:music-property seq 'elements)
-         (list ch
-               (make-event-chord (list start))
-               ch2))
-    seq))
+  (make-music 'MultiMeasureRestMusicGroup
+             'origin location
+             'elements (list (make-music 'BarCheck
+                                         'origin location)
+                             (make-event-chord (list (make-music 'MultiMeasureRestEvent
+                                                                 'origin location
+                                                                 'duration duration)))
+                             (make-music 'BarCheck
+                                         'origin location))))
 
 (define-public (glue-mm-rest-texts music)
   "Check if we have R1*4-\\markup { .. }, and if applicable convert to
 a property set for MultiMeasureRestNumber."
-  
   (define (script-to-mmrest-text script-music)
-    "Extract 'direction and 'text   from SCRIPT-MUSIC, and transform into property sets."
-    (let ((text (ly:music-property script-music 'text))
-         (dir  (ly:music-property script-music 'direction))
-         (p    (make-music-by-name 'MultiMeasureTextEvent)))
+    "Extract 'direction and 'text from SCRIPT-MUSIC, and transform into property sets."
+    (let ((dir (ly:music-property script-music 'direction))
+         (p   (make-music 'MultiMeasureTextEvent
+                          'text (ly:music-property script-music 'text))))
       (if (ly:dir? dir)
          (set! (ly:music-property p 'direction) dir))
-      (set! (ly:music-property p 'text) text)
       p))
-  
   (if (eq? (ly:music-property music 'name) 'MultiMeasureRestMusicGroup)
       (let* ((text? (lambda (x) (memq 'script-event (ly:music-property x 'types))))
             (es (ly:music-property  music 'elements))
@@ -278,34 +308,32 @@ a property set for MultiMeasureRestNumber."
 
 
 (define-public (make-property-set sym val)
-  (let ((m (make-music-by-name 'PropertySet)))
-    (set! (ly:music-property m 'symbol) sym)
-    (set! (ly:music-property m 'value) val)
-    m))
+  (make-music 'PropertySet
+             'symbol sym
+             'value val))
 
 (define-public (make-ottava-set octavation)
-  (let ((m (make-music-by-name 'ApplyContext)))
-    
+  (let ((m (make-music 'ApplyContext)))
     (define (ottava-modify context)
-      "Either reset centralCPosition to the stored original, or remember
-old centralCPosition, add OCTAVATION to centralCPosition, and set
+      "Either reset middleCPosition to the stored original, or remember
+old middleCPosition, add OCTAVATION to middleCPosition, and set
 OTTAVATION to `8va', or whatever appropriate."     
-      (if (number? (ly:context-property         context 'centralCPosition))
+      (if (number? (ly:context-property         context 'middleCPosition))
          (if (= octavation 0)
-             (let ((where (ly:context-property-where-defined context 'centralCPosition))
+             (let ((where (ly:context-property-where-defined context 'middleCPosition))
                    (oc0 (ly:context-property context 'originalCentralCPosition)))
-               (ly:context-set-property! context 'centralCPosition oc0)
-               (ly:unset-context-property where 'originalCentralCPosition)
-               (ly:unset-context-property where 'ottavation))
-             (let* ((where (ly:context-property-where-defined context 'centralCPosition))
-                    (c0 (ly:context-property context 'centralCPosition))
+               (ly:context-set-property! context 'middleCPosition oc0)
+               (ly:context-unset-property where 'originalCentralCPosition)
+               (ly:context-unset-property where 'ottavation))
+             (let* ((where (ly:context-property-where-defined context 'middleCPosition))
+                    (c0 (ly:context-property context 'middleCPosition))
                     (new-c0 (+ c0 (* -7 octavation)))
                     (string (cdr (assoc octavation '((2 . "15ma")
                                                      (1 . "8va")
                                                      (0 . #f)
                                                      (-1 . "8va bassa")
                                                      (-2 . "15ma bassa"))))))
-               (ly:context-set-property! context 'centralCPosition new-c0)
+               (ly:context-set-property! context 'middleCPosition new-c0)
                (ly:context-set-property! context 'originalCentralCPosition c0)
                (ly:context-set-property! context 'ottavation string)))))
     (set! (ly:music-property m 'procedure) ottava-modify)
@@ -315,10 +343,8 @@ OTTAVATION to `8va', or whatever appropriate."
   (ly:export (make-ottava-set ottavation)))
 
 (define-public (make-time-signature-set num den . rest)
-  " Set properties for time signature NUM/DEN.
-Rest can contain a list of beat groupings 
-
-"
+  "Set properties for time signature NUM/DEN.  Rest can contain a list
+of beat groupings "
   (let* ((set1 (make-property-set 'timeSignatureFraction (cons num den)))
         (beat (ly:make-moment 1 den))
         (len  (ly:make-moment num den))
@@ -328,16 +354,16 @@ Rest can contain a list of beat groupings
                                                    (car rest)
                                                    '())))
         (basic  (list set1 set2 set3 set4)))
-    (context-spec-music
+    (descend-to-context
      (context-spec-music (make-sequential-music basic) 'Timing) 'Score)))
 
 (define-public (make-mark-set label)
-  "make the music for the \\mark command."  
+  "Make the music for the \\mark command."  
   (let* ((set (if (integer? label)
                  (context-spec-music (make-property-set 'rehearsalMark label)
                                      'Score)
                  #f))
-        (ev (make-music-by-name 'MarkEvent))
+        (ev (make-music 'MarkEvent))
         (ch (make-event-chord (list ev))))
     (if set
        (make-sequential-music (list set ch))
@@ -348,26 +374,23 @@ Rest can contain a list of beat groupings
 (define-public (set-time-signature num den . rest)
   (ly:export (apply make-time-signature-set `(,num ,den . ,rest))))
 
-(define-public (make-penalty-music pen)
- (let ((m (make-music-by-name 'BreakEvent)))
-   (set! (ly:music-property m 'penalty) pen)
-   m))
+(define-public (make-penalty-music pen page-pen)
+  (make-music 'BreakEvent
+             'penalty pen
+             'page-penalty page-pen))
 
 (define-public (make-articulation name)
-  (let ((m (make-music-by-name 'ArticulationEvent)))
-    (set! (ly:music-property m 'articulation-type) name)
-    m))
+  (make-music 'ArticulationEvent
+             'articulation-type name))
 
 (define-public (make-lyric-event string duration)
-  (let ((m (make-music-by-name 'LyricEvent)))
-    (set! (ly:music-property m 'duration) duration)
-    (set! (ly:music-property m 'text) string)
-    m))
+  (make-music 'LyricEvent
+             'duration duration
+             'text string))
 
 (define-public (make-span-event type spandir)
-  (let ((m (make-music-by-name type)))
-    (set! (ly:music-property m 'span-direction) spandir)
-    m))
+  (make-music type
+             'span-direction spandir))
 
 (define-public (set-mus-properties! m alist)
   "Set all of ALIST as properties of M." 
@@ -424,7 +447,7 @@ Rest can contain a list of beat groupings
     m))
 
 (define-public (empty-music)
-  (ly:export (make-music-by-name 'Music)))
+  (ly:export (make-music 'Music)))
 ;;;
 
 ; Make a function that checks score element for being of a specific type. 
@@ -454,7 +477,7 @@ Rest can contain a list of beat groupings
 (define-public (smart-bar-check n)
   "Make         a bar check that checks for a specific bar number. 
 "
-  (let ((m (make-music-by-name 'ApplyContext)))
+  (let ((m (make-music 'ApplyContext)))
     (define (checker tr)
       (let* ((bn (ly:context-property tr 'currentBarNumber)))
        (if (= bn n)
@@ -508,7 +531,7 @@ without context specification. Called  from parser."
   "Make a new vector consisting of V, with X added to the end."
   (let*
       ((n (vector-length v))
-       gm(nv (make-vector (+ n 1) '())))
+       (nv (make-vector (+ n 1) '())))
     (vector-move-left! v 0 n nv 0)
     (vector-set! nv n x)
     nv))
@@ -539,7 +562,7 @@ without context specification. Called  from parser."
 
 (define-public (set-start-grace-properties context)
   (define (execute-1 x)
-    (let ((tr (ly:translator-find context (car x))))
+    (let ((tr (ly:context-find context (car x))))
       (if (ly:context? tr)
          (ly:context-pushpop-property tr (cadr x) (caddr x) (cadddr x)))))
   
@@ -549,7 +572,7 @@ without context specification. Called  from parser."
 
 (define-public (set-stop-grace-properties context)
   (define (execute-1 x)
-    (let ((tr (ly:translator-find context (car x))))
+    (let ((tr (ly:context-find context (car x))))
       (if (ly:context? tr)
          (ly:context-pushpop-property tr (cadr x) (caddr x)))))
   
@@ -557,13 +580,35 @@ without context specification. Called  from parser."
     (if (vector? props)
        (vector-reverse-map execute-1 props))))
 
+
+
+(defmacro-public def-grace-function (start stop)
+  `(def-music-function (location music) (ly:music?)
+     (make-music 'GraceMusic
+                'origin location
+                'element (make-music 'SequentialMusic
+                                     'elements (list (ly:music-deep-copy ,start)
+                                                     music
+                                                     (ly:music-deep-copy ,stop))))))
+
+(defmacro-public def-music-function (args signature . body)
+  "Helper macro for `ly:make-music-function'.
+Syntax:
+  (def-music-function (location arg1 arg2 ...) (arg1-type? arg2-type? ...)
+    ...function body...)
+"
+  `(ly:make-music-function (list ,@signature)
+                          (lambda (,@args)
+                            ,@body)))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; switch it on here, so parsing and init isn't checked (too slow!)
 ;;
 ;; automatic music transformations.
 
 (define (switch-on-debugging m)
-  (set-debug-cell-accesses! 15000)
+  (if (defined? 'set-debug-cell-accesses!)
+      (set-debug-cell-accesses! 15000))
   m)
 
 (define-public toplevel-music-functions
@@ -621,12 +666,12 @@ without context specification. Called  from parser."
 
 (define-public (set-accidental-style style . rest)
   "Set accidental style to STYLE. Optionally takes a context argument,
-eg. 'Staff or 'Voice. The context defaults to Voice, except for piano styles, which
-use PianoStaff as a context. "
+e.g. 'Staff or 'Voice. The context defaults to Voice, except for piano styles, which
+use GrandStaff as a context. "
   (let ((context (if (pair? rest)
                     (car rest) 'Staff))
        (pcontext (if (pair? rest)
-                     (car rest) 'PianoStaff)))
+                     (car rest) 'GrandStaff)))
     (ly:export
      (cond
       ;; accidentals as they were common in the 18th century.
@@ -670,14 +715,14 @@ use PianoStaff as a context. "
       ((equal? style 'piano)
        (set-accidentals-properties #f
                                   '( Staff (same-octave . 0) (any-octave . 0) (same-octave . 1)
-                                           PianoStaff (any-octave . 0) (same-octave . 1))
+                                           GrandStaff (any-octave . 0) (same-octave . 1))
                                   '()
                                   pcontext))
       ((equal? style 'piano-cautionary)
        (set-accidentals-properties #f
                                   '(Staff (same-octave . 0))
                                   '(Staff (any-octave . 0) (same-octave . 1)
-                                          PianoStaff (any-octave . 0) (same-octave . 1))
+                                          GrandStaff (any-octave . 0) (same-octave . 1))
                                   pcontext))
       ;; do not set localKeySignature when a note alterated differently from
       ;; localKeySignature is found.
@@ -697,7 +742,31 @@ use PianoStaff as a context. "
                                   '()
                                   context))
       (else
-       (ly:warn (string-append "Unknown accidental style: " (symbol->string style)))
+       (ly:warn "Unknown accidental style: ~S" (symbol->string style))
        (make-sequential-music '()))))))
 
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+(define-public (skip-of-length mus)
+  "Create a skip of exactly the same length as MUS."
+  (let*
+   ((skip
+     (make-music
+      'SkipEvent
+      'duration (ly:make-duration 0 0))))
+
+   (make-event-chord (list (ly:music-compress skip (ly:music-length mus))))
+))
+
+
+(define-public (mmrest-of-length mus)
+  "Create a mmrest of exactly the same length as MUS."
+  
+  (let*
+   ((skip
+     (make-multi-measure-rest
+      (ly:make-duration 0 0) '() )))
+   (ly:music-compress skip (ly:music-length mus))
+   skip
+))