\version "2.14.2"

\markup \column { \bold \fill-line { "EXAMPLE" } \vspace #2 }

#(define ((grow-dir-var top y-offset corr-y slope-1 slope-2 slope-3) grob)
 (cond ((= top 1) (and (display "\n ______________No Top wanted?________________________") #f))
       ((< top 1) (and (display "\n ______________No reachable Top!_____________________") #f))
       ((> top 1)
  (if (ly:stencil? (ly:beam::print grob))
     (let* ((beam (ly:beam::print grob))
            (dir (ly:beam::calc-direction grob))
            (beam-extent-X (ly:stencil-extent beam X))
            (beam-length-x (interval-length beam-extent-X))
            (beam-extent-Y (ly:stencil-extent beam Y))
            (beam-length-y (interval-length beam-extent-Y))
            (line-thickness (ly:output-def-lookup (ly:grob-layout grob) 'line-thickness))
            (beam-thickness (* 0.48 (ly:output-def-lookup (ly:grob-layout grob) 'staff-space)))
            (half-stem-thickness (/ (* 1.3 line-thickness) 2))	
            (space-helper (/ beam-length-y (/ beam-length-x 1.5)))
            
;; markup-a is the longest beam
            
            (markup-a (markup #:beam (- beam-length-x (* half-stem-thickness 1.25))	;; length
            		     (if (or (> 0 (cdr beam-extent-Y)) (equal? dir 1)) 	;; slope
            		           	(+ (car slope-1) 0)
            		           	(+ (car slope-1) (cdr beam-extent-Y)))
            		           	beam-thickness))		;; thickness  
            (long-beam (grob-interpret-markup grob markup-a))
            (long-beam-ext-y (ly:stencil-extent long-beam Y))
            (long-beam-ext-x (ly:stencil-extent long-beam X))
            	
;; markup-b is begin of the first additional beam

            (part-beam-1 (markup #:beam (- (/ beam-length-x top) (* half-stem-thickness 0.8)) 	;; length
            		     (if (> 0 (cdr beam-extent-Y))		;; slope
            		     	(+ (car slope-2) (* dir (/ space-helper  -1 )))	
            		     	(+ (car slope-2) (* dir (/ space-helper -0.8))))
            		     	beam-thickness))		;; thickness  
            (markup-b (markup part-beam-1))
            (first-part-beam-1 (grob-interpret-markup grob markup-b))
            (first-part-beam-1-ext-y (ly:stencil-extent first-part-beam-1 Y))
            (length-first-part-beam-1-y (interval-length first-part-beam-1-ext-y))
            (first-part-beam-1-ext-x (ly:stencil-extent first-part-beam-1 X))

;; markup-bb is the end of the first additional beam
            (part-beam-11 (markup #:beam (- (- beam-length-x (/ beam-length-x top)) (* half-stem-thickness 0.8)) ;; length
            		      (if (> 0 dir)				 ;; slope
            		        	(+ (cdr slope-2) (* dir (/
            		        	(- (cdr first-part-beam-1-ext-y) (cdr long-beam-ext-y))
            		        	(- (cdr long-beam-ext-x) (cdr first-part-beam-1-ext-x))
            		        	)))
            		        	(+ (cdr slope-2) (* dir (/
            		        	(- (cdr long-beam-ext-y)(cdr first-part-beam-1-ext-y))
            		        	(- (cdr long-beam-ext-x) (cdr first-part-beam-1-ext-x))
            		        	))))
            		        	beam-thickness))			 ;; thickness
            (markup-bb (markup part-beam-11))
            
;; markup-c is the begin of the second additional beam
            (part-beam-2 
              (if (not (equal? (car slope-3) #f))
            	(markup #:beam (- (/ beam-length-x top) (* half-stem-thickness 0.8)) 		;; length		
            		      (if (> 0 (cdr beam-extent-Y))			;; slope
            		       	(+ (car slope-3) (* dir (/ space-helper -2)))	
            		       	(+ (car slope-3) (* dir (/ space-helper -1.2))))
            		       	beam-thickness)				;; thickness
            	(markup #:null)))
            (markup-c (markup part-beam-2))
            
            (first-part-beam-2 (grob-interpret-markup grob markup-c))
            (first-part-beam-2-ext-y (ly:stencil-extent first-part-beam-2 Y))
            (length-first-part-beam-2-y (interval-length first-part-beam-2-ext-y))
            (first-part-beam-2-ext-x (ly:stencil-extent first-part-beam-2 X))
            
;; markup-cc is the end of the second additional beam
            (part-beam-22 
              (if (not (equal? (cdr slope-3) #f))
              	(markup #:beam (- (- beam-length-x (/ beam-length-x top)) (* half-stem-thickness 0.8)) 		;;length
            		      (if (> 0 dir)				 ;;slope
            		      	(+ (cdr slope-3)
            		      	(* dir (/
            		      	(- (cdr first-part-beam-2-ext-y) (cdr long-beam-ext-y))
            		      	(- (cdr long-beam-ext-x) (cdr first-part-beam-2-ext-x))
            		      	)))
            		      	(+ (cdr slope-3)
            		      	(* dir (/
            		      	(- (cdr long-beam-ext-y) (cdr first-part-beam-2-ext-y))
            		      	(- (cdr long-beam-ext-x) (cdr first-part-beam-2-ext-x))
            		      	))))
            		      	beam-thickness)				;; thickness
              	(markup #:null)))			 
            (markup-cc (markup part-beam-22))
            )   ;; end of defs in let*
            
 (ly:stencil-translate-axis
   (ly:stencil-add
   ;; first (long beam)
     (ly:stencil-translate-axis
       (grob-interpret-markup grob markup-a)
         (/ half-stem-thickness -2.7) 
         	X)
   ;; parts of second beam 
     (ly:stencil-combine-at-edge
       (grob-interpret-markup grob markup-b)
       X RIGHT
       (ly:stencil-translate-axis
         (grob-interpret-markup grob markup-bb)
      	(+ (car corr-y) 
      	  (if (> 0 dir)
      	      (- (cdr first-part-beam-1-ext-y) (* 0.5 beam-thickness))
      	      (* -1 (- length-first-part-beam-1-y (* 1 beam-thickness)))))
      	      	Y)
      0) 
   ;; parts of third beam 
     (ly:stencil-combine-at-edge 
      (grob-interpret-markup grob markup-c)
      X RIGHT
      (ly:stencil-translate-axis
        (grob-interpret-markup grob markup-cc)
        	(+ (cdr corr-y) 
        	  (if (equal? dir -1)
        	    (- (cdr first-part-beam-2-ext-y) (* 0.5 beam-thickness))
        	    (* -1 (- length-first-part-beam-2-y beam-thickness))))
        	    	Y)
      0)
      ) ;; end of stencil-add
    (if (equal? dir 1)
      (+ y-offset 0)
        (if (> 0 (cdr beam-extent-Y))
          (+ y-offset -3.3)
          (+ y-offset -1.8)))
   Y)
       ) ;; end of let*
       #f))))
       
%--------------------- Test ----------------------------------------------------
% '(#f . #f)) %

\relative c' {
  \once \override Beam #'stencil = #(grow-dir-var 2 		; where to print the turn (2 is half-beam)
         			         0.7 		; Y-offset of the whole construct
         			         '(0 . 0) 	; additional Y-offset 
         			         		;    '(<second part of the first added beam>
         			         		;      .
         			         		;      <second part of the second added beam>)
         			         '(0 . 0) 	; slope of the long beam (without) top
         			         '(0 . .221) 	; slope '(<first part of the first added beam>
         			         		;         .
         			         		;         <second part of the first added beam>)
         			         '(.04 . .108)) 	% slope '(<first part of the second added beam>
         			         		%         .
         			         		%         <second part of the second added beam>)
         			         		% to delete the second additional 
         			         		%     beam use '(#f . #f)
  c32[ c c c c c c c]
} 


\relative c' {
  \once \override Beam #'stencil = #(grow-dir-var 3 		; where to print the turn (2 is half-beam)
         			         0.7 		; Y-offset of the whole construct
         			         '(0 . 0) 	; additional Y-offset 
         			         		;    '(<second part of the first added beam>
         			         		;      .
         			         		;      <second part of the second added beam>)
         			         '(0 . 0) 	; slope of the long beam (without) top
         			         '(-.09 . .14) 	; slope '(<first part of the first added beam>
         			         		;         .
         			         		;         <second part of the first added beam>)
         			         '(-.02 . .08)) 	% slope '(<first part of the second added beam>
         			         		%         .
         			         		%         <second part of the second added beam>)
         			         		% to delete the second additional 
         			         		%     beam use '(#f . #f)
  c32[ c c c c c c c]
} 


\relative c' {
  \once \override Beam #'stencil = #(grow-dir-var 2 		; where to print the turn (2 is half-beam)
         			         2 		; Y-offset of the whole construct
         			         '(0 . 0) 	; additional Y-offset 
         			         		;    '(<second part of the first added beam>
         			         		;      .
         			         		;      <second part of the second added beam>)
         			         '(0.1 . 0) 	; slope of the long beam (without) top
         			         '(0.3 . 0.1) 	; slope '(<first part of the first added beam>
         			         		;         .
         			         		;         <second part of the first added beam>)
         			         '(0.3 . .02)) 	% slope '(<first part of the second added beam>
         			         		%         .
         			         		%         <second part of the second added beam>)
         			         		% to delete the second additional 
         			         		%     beam use '(#f . #f)
  c32[ d e f g a b c]
  
  \once \override Beam #'stencil = #(grow-dir-var 2 		; where to print the turn (2 is half-beam)
         			         2 		; Y-offset of the whole construct
         			         '(0 . 0) 	; additional Y-offset 
         			         		;    '(<second part of the first added beam>
         			         		;      .
         			         		;      <second part of the second added beam>)
         			         '(0.1 . 0) 	; slope of the long beam (without) top
         			         '(0.3 . 0.1) 	; slope '(<first part of the first added beam>
         			         		;         .
         			         		;         <second part of the first added beam>)
         			         '(0.28 . .01)) 	% slope '(<first part of the second added beam>
         			         		%         .
         			         		%         <second part of the second added beam>)
         			         		% to delete the second additional 
         			         		%     beam use '(#f . #f)
  c,32[ d e f g a b c]
  
  \once \override Beam #'stencil = #(grow-dir-var 1.4 		; where to print the turn (2 is half-beam)
         			         2 		; Y-offset of the whole construct
         			         '(0 . 0) 	; additional Y-offset 
         			         		;    '(<second part of the first added beam>
         			         		;      .
         			         		;      <second part of the second added beam>)
         			         '(0.1 . 0) 	; slope of the long beam (without) top
         			         '(0.35 . 0.2) 	; slope '(<first part of the first added beam>
         			         		;         .
         			         		;         <second part of the first added beam>)
         			         '(0.29 . .01)) 	% slope '(<first part of the second added beam>
         			         		%         .
         			         		%         <second part of the second added beam>)
         			         		% to delete the second additional 
         			         		%     beam use '(#f . #f)
  c,32[ d e f g a b c]
} 