Hi,

On Sun, Nov 23, 2014 at 3:31 PM, Mike Solomon <m...@mikesolomon.org> wrote:

>
> > On Nov 23, 2014, at 10:21 PM, Jay Vara <j...@diljun.com> wrote:
> >
> > Given that NoteNames are really a type of lyrics, is it possible to have
> a function that just takes music and returns a text string that is a lyric.
> >
> > For example:
> >
> > music  = \relative c' { c4 d e f g a b c }
> >
> > notenameLyric = \NoteNameToLyric \music
> >
> > would make
> >
> > notenameLyric = "c d e f g a b c"
> >
> > so that we can write
> >
> > \newLyric \lyricmode \notenameLyric
> >
> > I understand that such a function would probably map the music and
> extract the 'text from it and concatenate it to get the result. Not yet
> successful in getting it to work.
>
> Cool idea!  You’re thinking is exactly how I’d do it.
> I'd copy and paste the naturalizeMusic function from the doc and adapt it
> to return a list of pitch classes from the music.
> Then, you can flatten the returned list (if need be) and do a map on the
> list with a lambda function that takes a numeric pitch class and returns a
> markup with a note name.
>
> Cheers,
> MS
>

Here is a file I cobbled together which attempts something like this.

There's a problem which I don't know how to fix, though.

The following part of the example given in the file works, with text items
associated with the Staff:

<<
  \new Staff \music
  \noteNameToLyric #'(c d e f g a b c)
>>

The following part of the example creates text out of the pitch integers
(could be converted to letter names easily enough).  The lyrics, however,
are clearly not associated with the Staff.

<<
  \new Staff \music
  \noteNameToLyric \extractPitches \music
>>

Does anybody have an idea why this is happening, and what to do to fix it?

Anyway, hopefully this provides a start of a solution.

--David
\version "2.19.15"

#(define (get-pitch elt)
   (ly:music-property elt 'pitch))

#(define (extract-pitches lst result) 
   (cond
    ((null? lst) result)
    ((ly:pitch? (get-pitch (car lst)))
     (set! result (append result (list (get-pitch (car lst)))))
     (extract-pitches (cdr lst) result))
    ((ly:music-property (car lst) 'elements)
     (append
      (extract-pitches (ly:music-property (car lst) 'elements) result)
      (extract-pitches (cdr lst) '())))
    (else (extract-pitches (cdr lst) result))))

extractPitches =
#(define-scheme-function
  (parser location lst)
  (ly:music?)
  (extract-pitches
   (extract-named-music lst '(EventChord NoteEvent))
   '()) )

#(define (t lst)
   (let loop ((t lst) (result ""))
     (if (null? t)
         result
         (loop (cdr t) 
           (string-append
            result
            "\""
            (if (symbol? (car t)) ;; this mess is just for testing
                (symbol->string (car t))
                (number->string (ly:pitch-notename (car t))))
            "\""
            " ")))))

noteNameToLyric =
#(define-void-function (parser location lst) (list?)
   (let* ((str (t lst))
          (str (string-append "{ " str "}")))
     (display str) (newline)
     (ly:parser-include-string
      parser
      (string-append "\\new Lyrics \\lyricmode " str))))

music  = \relative c' { c4 d e f g a b c }

%% This works
<<
  \new Staff \music
  \noteNameToLyric #'(c d e f g a b c)
>>

%% Tis doesn't
<<
  \new Staff \music
  \noteNameToLyric \extractPitches \music
>>



_______________________________________________
lilypond-user mailing list
lilypond-user@gnu.org
https://lists.gnu.org/mailman/listinfo/lilypond-user

Reply via email to