lilypond-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Scheme music function attempt


From: Carl D. Sorensen
Subject: Scheme music function attempt
Date: Sun, 25 Jan 2009 17:37:38 -0700



On 1/25/09 6:08 AM, "Tao Cumplido" <address@hidden> wrote:


> Now I've got a question which is slightly off-topic.
> This thread got me inspired to give scheme another try (everytime I tried to
> do
> something with scheme before it'd never work).
> I am trying to create a music function that makes it possible to transpose
> chord
> symbols by my means. My initial idea was a function that takes two arguments,
> ly:music and string, i.e. the transposable root and the fixed suffix, which
> returns then a converted string of both arguments.
> For example:
> \c c1 #"m7" -> "Cm7"
> \transpose c d { \c c1 #"m7" } -> "Dm7"
> 
> I am trying to go there step by step, but now I got already an error and don't
> know why.
> 
> #(define (root-name music)
>    (let ((p (ly:music-property music 'pitch)))
>      (if (ly:pitch? p)
>          (let ((n (ly:pitch-notename p)))

First error is a syntax error -- no parenthesis before the if.  Always in
Scheme you must have a parenthesis before a function

>            if (number? n)
>               (number->string n)))))
>
> c = #(define-music-function (parser location pitch) (ly:music?)
>        (let ((rn (root-name pitch)))
>          #{ \mark \markup { $rn } #}))
> 
> \new Staff { c'1 \c c'1 c'1 }
> 

Second error is a mistake in defining a variable c.  c is a notename, so it
can't be a variable name.

After I fixed the (if error above, I ran lilypond on the file and got the
following:

sorensen2:lilypond-working Carl$ lilypond test-tao.ly
GNU LilyPond 2.12.2
Processing `test-tao.ly'
Parsing...
test-tao.ly:8:0: error: syntax error, unexpected NOTENAME_PITCH

c = #(define-music-function (parser location pitch) (ly:music?)
test-tao.ly:12:17: error: unknown escaped string: `\c'
\new Staff { c'1 
                 \c c'1 c'1 }
test-tao.ly:12:17: error: syntax error, unexpected STRING
\new Staff { c'1 
                 \c c'1 c'1 }
test-tao.ly:12:0: error: errors found, ignoring music expression

\new Staff { c'1 \c c'1 c'1 }
test-tao.ly:0: warning: : no \version statement found, please add

\version "2.12.2"

for future compatibility
error: failed files: "test-tao.ly"


Note that the first error says I've got an unexpected NOTENAME_PITCH, which
is shown on the next line to be c.

The next error message says that \c is an unknown escaped string (or what we
would call a variable).  So \c is never executed.

The third error message says that we can't use a string after a note, i.e.
{c "4"} isn't valid LilyPond input.

So the natural thing to do is to change the name of the function.  I changed
the name to cc, and then the program still didn't work.  Being not sure why,
I put a lot of display statements in to see what was going on:

                
#(define (root-name music)
   (let ((p (ly:music-property music 'pitch)))
     (if (ly:pitch? p)
         (let ((n (ly:pitch-notename p)))
          (display "\n in root-name \n")
          (display "music ")(display-scheme-music music)(newline)
          (display "pitch ")(display-scheme-music pitch)(newline)
          (display "n ")(display n)(newline)
           (if (number? n)
               (number->string n))))))
                
cc = #(define-music-function (parser location pitch) (ly:music?)
       (let ((rn (root-name pitch)))
        (display "\n in cc \n")
        (display "pitch ")(display-scheme-music pitch)(newline)
        (display "rn ")(display rn)(newline)
         #{ \mark \markup { $rn } #}))

The output I get then is:
sorensen2:lilypond-working Carl$ lilypond test-tao.ly
GNU LilyPond 2.12.2
Processing `test-tao.ly'
Parsing...
 in cc 
pitch (make-music
  'EventChord
  'elements
  (list (make-music
          'NoteEvent
          'duration
          (ly:make-duration 0 0 1 1)
          'pitch
          (ly:make-pitch 0 0 0))))


rn #<unspecified>

<string>:1:49: error: syntax error, unexpected SCM_IDENTIFIER
parseStringResult = \notemode {  \mark \markup {
                                                 \lilyvartmpb }
}test-tao.ly:19:0: error: errors found, ignoring music expression

\new Staff { c'1 \cc c'1 c'1 }
test-tao.ly:0: warning: : no \version statement found, please add

\version "2.12.2"

for future compatibility


Two things are of interest:

1) None of the display statements from root-name was executed.
2) pitch is not a NoteEvent, but an EventChord,  which is something we
weren't expecting.

So this means that p in root-name isn't a ly:pitch.  In fact, it's probably
null, because EventChord doesn't have a pitch.  It has elements, the first
of which has a pitch.

This means that root-name returns nothing, so rn is #<unspecified>.

So you need to do a little bit of checking on your music before you try to
get the pitch.

Hope this helps,

Carl





reply via email to

[Prev in Thread] Current Thread [Next in Thread]