+ (define (entry-count art-list)
+ "Count the number of entries in a list of articulations."
+ (length (filter (lambda (x) (not (null? x)))
+ art-list)))
+
+ (define (string-number event)
+ "Get the string-number from @var{event}. Return @var{#f}
+if no string-number is present."
+ (let ((num (ly:event-property event 'string-number)))
+ (and (integer? num) (positive? num) num)))
+
+ (define (determine-frets-and-strings
+ notes
+ defined-strings
+ defined-fingers
+ minimum-fret
+ maximum-stretch
+ tuning)
+ "Determine the frets and strings used to play the notes in
+@var{notes}, given @var{defined-strings} and @var{defined-fingers}
+along with @var{minimum-fret}, @var{maximum-stretch}, and
+@var{tuning}. Returns a list of @code{(string fret finger) lists."
+
+
+ (define restrain-open-strings (ly:context-property context
+ 'restrainOpenStrings
+ #f))
+ (define specified-frets '())
+ (define free-strings (iota (length tuning) 1))
+
+ (define (calc-fret pitch string tuning)
+ "Calculate the fret to play @var{pitch} on @var{string} with
+@var{tuning}."
+ (* 2 (- (ly:pitch-tones pitch) (ly:pitch-tones (list-ref tuning (1- string))))))
+
+ (define (note-pitch note)
+ "Get the pitch (in semitones) from @var{note}."
+ (ly:event-property note 'pitch))
+
+ (define (note-finger ev)
+ "Get the fingering from @var{ev}. Return @var{#f}
+if no fingering is present."
+ (let* ((articulations (ly:event-property ev 'articulations))
+ (finger-found #f))
+ (for-each (lambda (art)
+ (let* ((num (ly:event-property art 'digit)))
+
+ (if (and (ly:in-event-class? art 'fingering-event)
+ (number? num)
+ (> num 0))
+ (set! finger-found num))))
+ articulations)
+ finger-found))
+
+ (define (delete-free-string string)
+ (if (number? string)
+ (set! free-strings
+ (delete string free-strings))))
+
+ (define (close-enough fret)
+ "Decide if @var{fret} is acceptable, given the already used frets."
+ (every (lambda (specced-fret)
+ (or (zero? specced-fret)
+ (zero? fret)
+ (>= maximum-stretch (abs (- fret specced-fret)))))
+ specified-frets))
+
+ (define (string-qualifies string pitch)
+ "Can @var{pitch} be played on @var{string}, given already placed
+notes?"
+ (let* ((fret (calc-fret pitch string tuning)))
+ (and (or (and (not restrain-open-strings)
+ (zero? fret))
+ (>= fret minimum-fret))
+ (integer? fret)
+ (close-enough fret))))
+
+ (define (open-string string pitch)
+ "Is @var{pitch} and open-string note on @var{string}, given
+the current tuning?"
+ (let* ((fret (calc-fret pitch string tuning)))
+ (zero? fret)))
+
+ (define (set-fret! pitch-entry string finger)
+ (let ((this-fret (calc-fret (car pitch-entry)
+ string
+ tuning)))
+ (if (< this-fret 0)
+ (ly:warning (_ "Negative fret for pitch ~a on string ~a")
+ (car pitch-entry) string)
+ (if (not (integer? this-fret))
+ (ly:warning (_ "Missing fret for pitch ~a on string ~a")
+ (car pitch-entry) string)))
+ (delete-free-string string)
+ (set! specified-frets (cons this-fret specified-frets))
+ (list-set! string-fret-fingers
+ (cdr pitch-entry)
+ (list string this-fret finger))))
+
+ (define (kill-note! string-fret-fingers note-index)
+ (list-set! string-fret-fingers note-index (list #f #t)))
+
+ (define string-fret-fingers
+ (map (lambda (string finger)
+ (if (null? finger)
+ (list string #f)
+ (list string #f finger)))
+ defined-strings defined-fingers))
+
+ ;;; body of determine-frets-and-strings
+ (let* ((pitches (map note-pitch notes))
+ (pitch-alist (map cons pitches (iota (length pitches)))))
+
+ ;; handle notes with strings assigned and fingering of 0
+ (for-each
+ (lambda (pitch-entry string-fret-finger)
+ (let* ((string (list-ref string-fret-finger 0))
+ (finger (if (= (length string-fret-finger) 3)
+ (list-ref string-fret-finger 2)
+ '()))
+ (pitch (car pitch-entry))
+ (digit (if (null? finger)
+ #f
+ finger)))
+ (if (or (not (null? string))
+ (eqv? digit 0))
+ (if (eqv? digit 0)
+ ;; here we handle fingers of 0 -- open strings
+ (let ((fit-string
+ (find (lambda (string)
+ (open-string string pitch))
+ free-strings)))
+ (if fit-string
+ (set-fret! pitch-entry fit-string #f)
+ (ly:warning (_ "No open string for pitch ~a")
+ pitch)))
+ ;; here we handle assigned strings
+ (let ((this-fret
+ (calc-fret pitch string tuning))
+ (handle-negative
+ (ly:context-property context
+ 'handleNegativeFrets
+ 'recalculate)))
+ (cond ((or (and (>= this-fret 0) (integer? this-fret))
+ (eq? handle-negative 'include))
+ (set-fret! pitch-entry string finger))
+ ((eq? handle-negative 'recalculate)
+ (begin
+ (ly:warning
+ (_ "Requested string for pitch requires negative fret: string ~a pitch ~a")
+ string
+ pitch)
+ (ly:warning (_ "Ignoring string request and recalculating."))
+ (list-set! string-fret-fingers
+ (cdr pitch-entry)
+ (if (null? finger)
+ (list '() #f)
+ (list '() #f finger)))))
+ ((eq? handle-negative 'ignore)
+ (begin
+ (ly:warning
+ (_ "Requested string for pitch requires negative fret: string ~a pitch ~a")
+ string
+ pitch)
+ (ly:warning (_ "Ignoring note in tablature."))
+ (kill-note! string-fret-fingers
+ (cdr pitch-entry))))))))))
+ pitch-alist string-fret-fingers)
+ ;; handle notes without strings assigned -- sorted by pitch, so
+ ;; we need to use the alist to have the note number available
+ (for-each
+ (lambda (pitch-entry)
+ (let* ((string-fret-finger (list-ref string-fret-fingers
+ (cdr pitch-entry)))
+ (string (list-ref string-fret-finger 0))
+ (finger (if (= (length string-fret-finger) 3)
+ (list-ref string-fret-finger 2)
+ '()))
+ (pitch (car pitch-entry))
+ (fit-string
+ (find (lambda (string)
+ (string-qualifies string pitch))
+ free-strings)))
+ (if (not (list-ref string-fret-finger 1))
+ (if fit-string
+ (set-fret! pitch-entry fit-string finger)
+ (begin
+ (ly:event-warning
+ (list-ref notes (cdr pitch-entry))
+ (_ "No string for pitch ~a (given frets ~a)")
+ pitch
+ specified-frets)
+ (kill-note! string-fret-fingers
+ (cdr pitch-entry)))))))
+ (sort pitch-alist (lambda (pitch-entry-a pitch-entry-b)
+ (ly:pitch<? (car pitch-entry-b)
+ (car pitch-entry-a)))))
+ string-fret-fingers)) ;; end of determine-frets-and-strings