%%%% Copyright (C) 2016 Knut Petersen
%%%%
%%%% This file contains code derived from Lilypond's ly/event-listener.ly.
%%%% There the following copyright notice was included:
%%%%
%%%% Copyright (C) 2011--2015 Graham Percival
%%%%
%%%% Other parts of this file are based on code provide by
%%%% Thomas Morley , see
%%%% http://lilypond.1069038.n5.nabble.com/intercepting-implicit-explicit-page-breaks-td194196.html
%%%%
%%%% Needs at least lilypond version 2.19.23!
%%%%
%%%% This is free software: you can redistribute it and/or modify
%%%% it under the terms of the GNU General Public License as published by
%%%% the Free Software Foundation, either version 3 of the License, or
%%%% (at your option) any later version.
%%%%
%%%% This file is distributed in the hope that it will be useful,
%%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%%%% GNU General Public License for more details.
%%%%
%%%% You should have received a copy of the GNU General Public License
%%%% along with LilyPond. If not, see .
#(define out (open-output-file "videohelper.notes"))
% #(define lastmoment 0.000)
#(define (format-moment moment)
(exact->inexact
(/ (ly:moment-main-numerator moment)
(ly:moment-main-denominator moment))))
#(define (moment-grace->string moment)
"Prints a moment without grace note(s) as a float such as
0.25000. Grace notes are written with the grace duration as a
separate \"dashed\" number, i.e. 0.25000-0.12500. This allows any
program using the output of this function to interpret grace notes
however they want (half duration, quarter duration? before beat,
after beat? etc.)."
(if
(zero? (ly:moment-grace-numerator moment))
(ly:format "~a" (format-moment moment))
;; grace notes have a negative numerator, so no "-" necessary
(ly:format
"~a~a"
(format-moment moment)
(format-moment
(ly:make-moment
(ly:moment-grace-numerator moment)
(ly:moment-grace-denominator moment))))))
#(define (make-output-string-line context values)
"Constructs a tab-separated string beginning with the
score time (derived from the context) and then adding all the
values. The string ends with a newline."
(let* ((moment (ly:context-current-moment context)))
(string-append
(string-join
(append
(list (moment-grace->string moment))
(map
(lambda (x) (ly:format "~a" x))
values))
"\t")
"\n")))
#(define (print-line context . values)
"Prints the list of values (plus the score time) to a file, and
optionally outputs to the console as well. context may be specified
as an engraver for convenience."
(if (ly:translator? context)
(set! context (ly:translator-context context)))
(display (make-output-string-line context values) out)
)
#(define (format-tempo engraver event)
(if (ly:event-property event 'metronome-count)
(print-line engraver "tempo"
( / 60
(* (ly:event-property event 'metronome-count)
(format-moment (ly:duration-length (ly:event-property event 'tempo-unit))))))))
#(define (format-time engraver event)
(print-line engraver "time"))
#(define (format-rest engraver event)
(print-line engraver
"rest"
(format-moment (ly:duration-length
(ly:event-property event 'duration)))
))
#(define (format-note engraver event)
(print-line engraver
"note"
(format-moment (ly:duration-length
(ly:event-property event 'duration)))
))
\layout {
\context {
\Voice
\consists #(make-engraver
(listeners
(tempo-change-event . format-tempo)
(time-signature-event . format-time)
(rest-event . format-rest)
(note-event . format-note)))
}
}
\paper {
#(define (page-post-process layout pages) (print-pages-first-bar-numbers layout pages #t))
}
#(define* (print-pages-first-bar-numbers layout pages #:optional print-to-file)
(let* ((lines (map (lambda (page) (ly:prob-property page 'lines)) pages))
;; list of systems of each pages
(sys
(map
(lambda (line)
(append-map
(lambda (l)
(let ((system-grob (ly:prob-property l 'system-grob)))
(if (not (null? system-grob))
(list system-grob)
system-grob))
)
line))
lines))
(sys-moment-location
(map
(lambda (m)
(if (and (not (null? m)) (ly:grob? (car m)))
(grob::when (car m))
#f))
sys))
(formatted-output
(map
(lambda (page m)
(if m
(format #f "~a page ~a\n" (format-moment m) page)
(format #f "page ~a contains no music\n" page))
)
(iota (length pages) 1 1)
sys-moment-location))
)
(if (not (null? sys-moment-location))
(begin
(for-each (lambda (i) (display i out)) formatted-output))
(for-each display formatted-output)))
; (format out "lastmoment is ~a\n" lastmoment)
)
#(format out "~a~a~a" "LILYSOURCE=" (ly:parser-output-name) ".ly\n")
pdfforvideo = #(define-void-function () () (format out "~a~a~a" "VIDEOSOURCE=" current-outfile-name ".pdf\n"))
midiforvideo = #(define-void-function () () (format out "~a~a~a" "MIDISOURCE=" current-outfile-name ".midi\n"))