From aae8f9ee8f62e8a57f4f60a60e639a040571f37b Mon Sep 17 00:00:00 2001 From: Dan Eble Date: Fri, 17 Apr 2015 23:01:55 -0400 Subject: [PATCH] Issue 4356: Part combiner: generate mark events in Scheme (not C++) --- lily/part-combine-iterator.cc | 49 +----------------------- ly/music-functions-init.ly | 72 +++++++++++++++-------------------- scm/part-combiner.scm | 67 ++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 90 deletions(-) diff --git a/lily/part-combine-iterator.cc b/lily/part-combine-iterator.cc index 4d1baec756..f84e70cc9a 100644 --- a/lily/part-combine-iterator.cc +++ b/lily/part-combine-iterator.cc @@ -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; } } diff --git a/ly/music-functions-init.ly b/ly/music-functions-init.ly index e039c1cf7c..beb8bd10e4 100644 --- a/ly/music-functions-init.ly +++ b/ly/music-functions-init.ly @@ -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?) diff --git a/scm/part-combiner.scm b/scm/part-combiner.scm index 8fe293e947..7278b29e3c 100644 --- a/scm/part-combiner.scm +++ b/scm/part-combiner.scm @@ -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)))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; -- 2.39.2