- articulations)
-
- finger-found))
-
- (define (note-string ev)
- (let* ((articulations (ly:event-property ev 'articulations))
- (string-found #f))
-
- (map (lambda (art)
- (let*
- ((num (ly:event-property art 'string-number)))
-
- (if (number? num)
- (set! string-found num))))
- articulations)
-
- string-found))
-
- (define (del-string string)
- (if (number? string)
- (set! free-strings
- (delete string free-strings))))
- (define specified-frets '())
- (define free-strings '())
-
- (define (close-enough fret)
- (reduce
- (lambda (x y)
- (and x y))
- #t
- (map (lambda (specced-fret)
- (> max-stretch (abs (- fret specced-fret))))
- specified-frets)
- ))
-
- (define (string-qualifies string pitch)
- (let*
- ((fret (calc-fret pitch string tunings)))
-
- (and (>= fret minimum-fret)
- (close-enough fret))
-
- ))
-
- (define string-fret-fingering-tuples '())
- (define (set-fret note string)
- (set! string-fret-fingering-tuples
- (cons (list string
- (calc-fret (ly:event-property note 'pitch)
- string tunings)
- (note-finger note))
- string-fret-fingering-tuples))
- (del-string string))
-
-
- ;;; body.
- (set! specified-frets
- (filter identity (map
- (lambda (note)
- (if (note-string note)
- (calc-fret (note-pitch note)
- (note-string note) tunings)
- #f))
- notes)))
-
-
- (set! free-strings (map 1+ (iota (length tunings))))
-
- (for-each (lambda (note)
- (del-string (note-string note)))
- notes)
-
-
- (for-each
- (lambda (note)
- (if (note-string note)
- (set-fret note (note-string note))
- (let*
- ((fit-string (find (lambda (string)
- (string-qualifies string (note-pitch note)))
- free-strings)))
- (if fit-string
- (set-fret note fit-string)
- (ly:warning "No string for pitch ~a (given frets ~a)"
- (note-pitch note)
- specified-frets))
-
- )))
- (sort notes note-pitch>?))
-
- string-fret-fingering-tuples)
+ articulations)
+ finger-found))
+
+ (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)))
+ (if (number? num)
+ num
+ #f)))
+
+ (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."
+ (if (null? specified-frets)
+ #t
+ (reduce
+ (lambda (x y)
+ (and x y))
+ #t
+ (map (lambda (specced-fret)
+ (or (eq? 0 specced-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 (>= fret minimum-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)))
+ (eq? fret 0)))
+
+ (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))
+ (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* ((pitch-alist (apply (lambda (mylist)
+ (let ((index -1))
+ (map (lambda (note)
+ (begin
+ (set! index (1+ index))
+ (cons (note-pitch note)
+ index)))
+ mylist)))
+ notes '()))
+ (pitches (map note-pitch notes)))
+
+ ;; 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 (eq? (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))
+ (eq? digit 0))
+ (if (eq? 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 (>= this-fret 0)
+ (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 (eq? (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:warning (_ "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
+
+ (define (get-predefined-fretboard predefined-fret-table tuning pitches)
+ "Search through @var{predefined-fret-table} looking for a predefined
+fretboard with a key of @var{(tuning . pitches)}. The search will check
+both up and down an octave in order to accomodate transposition of the
+chords. Returns a placement-list."
+
+ (define (get-fretboard key)
+ (let ((hash-handle
+ (hash-get-handle predefined-fret-table key)))
+ (if hash-handle
+ (cdr hash-handle) ; return table entry
+ '())))
+
+ ;; body of get-predefined-fretboard
+ (let ((test-fretboard (get-fretboard (cons tuning pitches))))
+ (if (not (null? test-fretboard))
+ test-fretboard
+ (let ((test-fretboard
+ (get-fretboard
+ (cons tuning (map (lambda (x) (shift-octave x 1)) pitches)))))
+ (if (not (null? test-fretboard))
+ test-fretboard
+ (get-fretboard
+ (cons tuning (map (lambda (x) (shift-octave x -1))
+ pitches))))))))
+
+ ;; body of determine-frets
+ (let* ((predefined-fret-table
+ (ly:context-property context 'predefinedDiagramTable))
+ (tunings (ly:context-property context 'stringTunings))
+ (string-count (length tunings))
+ (grob (if (null? rest) '() (car rest)))
+ (pitches (map (lambda (x) (ly:event-property x 'pitch)) notes))
+ (defined-strings (map (lambda (x)
+ (if (null? x)
+ x
+ (ly:event-property x 'string-number)))
+ (car specified-info)))
+ (defined-fingers (map (lambda (x)
+ (if (null? x)
+ x
+ (ly:event-property x 'digit)))
+ (cadr specified-info)))
+ (default-strings (ly:context-property context 'defaultStrings '()))
+ (strings-used (if (and (zero? (entry-count defined-strings))
+ (not (zero? (entry-count default-strings))))
+ default-strings
+ defined-strings))
+ (predefined-fretboard
+ (if predefined-fret-table
+ (get-predefined-fretboard
+ predefined-fret-table
+ tunings
+ pitches)
+ '())))
+ (if (null? predefined-fretboard)
+ (let ((string-frets
+ (determine-frets-and-strings
+ notes
+ strings-used
+ defined-fingers
+ (ly:context-property context 'minimumFret 0)
+ (ly:context-property context 'maximumFretStretch 4)
+ tunings)))
+ (if (null? grob)
+ string-frets
+ (create-fretboard
+ context grob (string-frets->placement-list
+ (filter (lambda (entry)
+ (car entry))
+ string-frets)
+ string-count))))
+ (if (null? grob)
+ (placement-list->string-frets predefined-fretboard)
+ (create-fretboard context grob predefined-fretboard)))))
+