]> git.donarmstrong.com Git - lilypond.git/commitdiff
Issue 4356: Part combiner: generate mark events in Scheme (not C++)
authorDan Eble <nine.fierce.ballads@gmail.com>
Sat, 18 Apr 2015 03:01:55 +0000 (23:01 -0400)
committerDan Eble <nine.fierce.ballads@gmail.com>
Sat, 9 May 2015 13:59:49 +0000 (09:59 -0400)
lily/part-combine-iterator.cc
ly/music-functions-init.ly
scm/part-combiner.scm

index 4d1baec7568b3d24939dac9c7bdac59b5467ad00..f84e70cc9ac00bcf903ccf42cb9797fb64e6d8ac 100644 (file)
@@ -66,9 +66,6 @@ private:
 
   SCM split_list_;
 
-  Stream_event *unisono_event_;
-  Stream_event *solo_one_event_;
-  Stream_event *solo_two_event_;
   Stream_event *mmrest_event_;
 
   enum Status
@@ -134,9 +131,6 @@ Part_combine_iterator::do_quit ()
 Part_combine_iterator::Part_combine_iterator ()
 {
   mmrest_event_ = 0;
-  unisono_event_ = 0;
-  solo_two_event_ = 0;
-  solo_one_event_ = 0;
 
   first_iter_ = 0;
   second_iter_ = 0;
@@ -157,14 +151,8 @@ Part_combine_iterator::derived_mark () const
     scm_gc_mark (first_iter_->self_scm ());
   if (second_iter_)
     scm_gc_mark (second_iter_->self_scm ());
-  if (unisono_event_)
-    scm_gc_mark (unisono_event_->self_scm ());
   if (mmrest_event_)
     scm_gc_mark (mmrest_event_->self_scm ());
-  if (solo_one_event_)
-    scm_gc_mark (solo_one_event_->self_scm ());
-  if (solo_two_event_)
-    scm_gc_mark (solo_two_event_->self_scm ());
 }
 
 void
@@ -250,17 +238,6 @@ Part_combine_iterator::unisono (bool silent, int newpart)
       if (playing_state_ != PLAYING_UNISONO
           && newstate == UNISONO)
         {
-          if (!unisono_event_)
-            {
-              unisono_event_ = new Stream_event
-                (scm_call_1 (ly_lily_module_constant ("ly:make-event-class"),
-                             ly_symbol2scm ("unisono-event")));
-              unisono_event_->unprotect ();
-            }
-
-          Context *out = (newpart == 2 ? second_iter_ : first_iter_)
-                         ->get_outlet ();
-          out->event_source ()->broadcast (unisono_event_);
           playing_state_ = PLAYING_UNISONO;
         }
       state_ = newstate;
@@ -282,18 +259,6 @@ Part_combine_iterator::solo1 ()
       kill_mmrest (CONTEXT_TWO);
       kill_mmrest (CONTEXT_SHARED);
 
-      if (playing_state_ != PLAYING_SOLO1)
-        {
-          if (!solo_one_event_)
-            {
-              solo_one_event_ = new Stream_event
-                (scm_call_1 (ly_lily_module_constant ("ly:make-event-class"),
-                             ly_symbol2scm ("solo-one-event")));
-              solo_one_event_->unprotect ();
-            }
-
-          first_iter_->get_outlet ()->event_source ()->broadcast (solo_one_event_);
-        }
       playing_state_ = PLAYING_SOLO1;
     }
 }
@@ -309,19 +274,7 @@ Part_combine_iterator::solo2 ()
       chosen_part_ = 2;
       substitute_both (CONTEXT_NULL, CONTEXT_SOLO);
 
-      if (playing_state_ != PLAYING_SOLO2)
-        {
-          if (!solo_two_event_)
-            {
-              solo_two_event_ = new Stream_event
-                (scm_call_1 (ly_lily_module_constant ("ly:make-event-class"),
-                             ly_symbol2scm ("solo-two-event")));
-              solo_two_event_->unprotect ();
-            }
-
-          second_iter_->get_outlet ()->event_source ()->broadcast (solo_two_event_);
-          playing_state_ = PLAYING_SOLO2;
-        }
+      playing_state_ = PLAYING_SOLO2;
     }
 }
 
index e039c1cf7c00d82f4c68c106ac9498ee98b1a8f0..beb8bd10e497a20937066c8372f691a264928487 100644 (file)
@@ -1134,6 +1134,24 @@ parenthesize =
        (set! (ly:music-property arg 'parenthesize) #t))
    arg)
 
+#(define (make-directed-part-combine-music
+          parser direction chord-range part1 part2
+          one-context-settings
+          two-context-settings
+          shared-context-settings)
+
+   (let ((pc-music (make-part-combine-music
+                     parser (list part1 part2) direction chord-range)))
+     #{ \context Staff <<
+          \context Voice = "one" \with #one-context-settings {}
+          \context Voice = "two" \with #two-context-settings {}
+          \context Voice = "shared" \with #shared-context-settings {}
+          #pc-music
+          #(make-part-combine-marks
+            default-part-combine-mark-state-machine
+            (ly:music-property pc-music 'split-list))
+        >> #} ))
+
 partcombine =
 #(define-music-function (parser location chord-range part1 part2)
    ((number-pair? '(0 . 8)) ly:music? ly:music?)
@@ -1142,60 +1160,30 @@ a music expression containing simultaneous voices, where @var{part1}
 and @var{part2} are combined into one voice where appropriate.
 Optional @var{chord-range} sets the distance in steps between notes
 that may be combined into a chord or unison.")
-   #{ \context Staff <<
-        \context Voice = "one" \with {
-          \voiceOne
-          \override DynamicLineSpanner.direction = #UP
-        } {}
-        \context Voice = "two" \with {
-          \voiceTwo
-          \override DynamicLineSpanner.direction = #DOWN
-        } {}
-        \context Voice = "shared" {}
-        #(make-part-combine-music parser (list part1 part2) #f chord-range)
-      >> #} )
+   (make-directed-part-combine-music parser #f chord-range part1 part2
+    #{ \with { \voiceOne \override DynamicLineSpanner.direction = #UP } #}
+    #{ \with { \voiceTwo \override DynamicLineSpanner.direction = #DOWN } #}
+    #{ #} ))
 
 partcombineUp =
 #(define-music-function (parser location chord-range part1 part2)
    ((number-pair? '(0 . 8)) ly:music? ly:music?)
    (_i "Take the music in @var{part1} and @var{part2} and typeset so
 that they share a staff with stems directed upward.")
-   #{ \context Staff <<
-        \context Voice = "one" \with {
-          \voiceOne
-          \override DynamicLineSpanner.direction = #UP
-        } {}
-        \context Voice = "two" \with {
-          \voiceThree
-          \override DynamicLineSpanner.direction = #UP
-        } {}
-        \context Voice = "shared" \with {
-          \voiceOne
-          \override DynamicLineSpanner.direction = #UP
-        } {}
-        #(make-part-combine-music parser (list part1 part2) UP chord-range)
-      >> #} )
+   (make-directed-part-combine-music parser UP chord-range part1 part2
+    #{ \with { \voiceOne \override DynamicLineSpanner.direction = #UP } #}
+    #{ \with { \voiceThree \override DynamicLineSpanner.direction = #UP } #}
+    #{ \with { \voiceOne \override DynamicLineSpanner.direction = #UP } #} ))
 
 partcombineDown =
 #(define-music-function (parser location chord-range part1 part2)
    ((number-pair? '(0 . 8)) ly:music? ly:music?)
    (_i "Take the music in @var{part1} and @var{part2} and typeset so
 that they share a staff with stems directed downward.")
-   #{ \context Staff <<
-        \context Voice = "one" \with {
-          \voiceFour
-          \override DynamicLineSpanner.direction = #DOWN
-        } {}
-        \context Voice = "two" \with {
-          \voiceTwo
-          \override DynamicLineSpanner.direction = #DOWN
-        } {}
-        \context Voice = "shared" \with {
-          \voiceTwo
-          \override DynamicLineSpanner.direction = #DOWN
-        } {}
-        #(make-part-combine-music parser (list part1 part2) DOWN chord-range)
-      >> #} )
+   (make-directed-part-combine-music parser DOWN chord-range part1 part2
+    #{ \with { \voiceFour \override DynamicLineSpanner.direction = #DOWN } #}
+    #{ \with { \voiceTwo \override DynamicLineSpanner.direction = #DOWN } #}
+    #{ \with { \voiceTwo \override DynamicLineSpanner.direction = #DOWN } #} ))
 
 partcombineForce =
 #(define-music-function (location parser type once) (boolean-or-symbol? boolean?)
index 8fe293e94743fde86f30b5025813b0b130e845e6..7278b29e3cb5273ab730f5ebfb22d635a9fa2d2d 100644 (file)
@@ -732,6 +732,73 @@ the mark when there are no spanners active.
         (display result))
     result))
 
+(define-public default-part-combine-mark-state-machine
+  ;; (current-state . ((split-state-event .
+  ;;                      (output-voice output-event next-state)) ...))
+  '((Initial . ((solo1   . (solo   SoloOneEvent Solo1))
+                (solo2   . (solo   SoloTwoEvent Solo2))
+                (unisono . (shared UnisonoEvent Unisono))))
+    (Solo1   . ((apart   . (#f     #f           Initial))
+                (chords  . (#f     #f           Initial))
+                (solo2   . (solo   SoloTwoEvent Solo2))
+                (unisono . (shared UnisonoEvent Unisono))))
+    (Solo2   . ((apart   . (#f     #f           Initial))
+                (chords  . (#f     #f           Initial))
+                (solo1   . (solo   SoloOneEvent Solo1))
+                (unisono . (shared UnisonoEvent Unisono))))
+    (Unisono . ((apart   . (#f     #f           Initial))
+                (chords  . (#f     #f           Initial))
+                (solo1   . (solo   SoloOneEvent Solo1))
+                (solo2   . (solo   SoloTwoEvent Solo2))))))
+
+(define-public (make-part-combine-marks state-machine split-list)
+  "Generate a sequence of part combiner events from a split list"
+
+  (define (get-state state-name)
+    (assq-ref state-machine state-name))
+
+  (let ((full-seq '()) ; sequence of { \context Voice = "x" {} ... }
+        (segment '()) ; sequence within \context Voice = "x" {...}
+        (prev-moment ZERO-MOMENT)
+        (prev-voice #f)
+        (state (get-state 'Initial)))
+
+    (define (commit-segment)
+      "Add the current segment to the full sequence and begin another."
+      (if (pair? segment)
+          (set! full-seq
+                (cons (make-music 'ContextSpeccedMusic
+                                  'context-id (symbol->string prev-voice)
+                                  'context-type 'Voice
+                                  'element (make-sequential-music (reverse! segment)))
+                      full-seq)))
+      (set! segment '()))
+
+    (define (handle-split split)
+      (let* ((moment (car split))
+             (action (assq-ref state (cdr split))))
+        (if action
+            (let ((voice (car action))
+                  (part-combine-event (cadr action))
+                  (next-state-name (caddr action)))
+              (if part-combine-event
+                  (let ((dur (ly:moment-sub moment prev-moment)))
+                    ;; start a new segment when the voice changes
+                    (if (not (eq? voice prev-voice))
+                        (begin
+                          (commit-segment)
+                          (set! prev-voice voice)))
+                    (if (not (equal? dur ZERO-MOMENT))
+                        (set! segment (cons (make-music 'SkipEvent
+                                                          'duration (make-duration-of-length dur)) segment)))
+                    (set! segment (cons (make-music part-combine-event) segment))
+
+                    (set! prev-moment moment)))
+              (set! state (get-state next-state-name))))))
+
+    (for-each handle-split split-list)
+    (commit-segment)
+    (make-sequential-music (reverse! full-seq))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;