problems trying to write a conditional \transpose

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

problems trying to write a conditional \transpose

Mark Polesky
Hi.

I'm trying to write a conditional version of the \transpose
function, that would work something like this:

input = {
  c c
  \conditionalTransposition c c' { c c }
}

\input
=> { c c c c }

\processConditionalTransposition \input
=> { c c c' c' }

********************

And I've hit another brick wall.  I experimented with some
of the functions in music-functions.scm (map-some-music,
fold-some-music, etc.), but those hard-to-follow function
descriptions slowed me down again.  I've included some vain
attempts below, which are clearly wrong...

Any advice?

Thanks
- Mark

********************

conditionalTranspose =
#(define-music-function
   (parser location from to music)
   (ly:pitch? ly:pitch? ly:music?)
   (set! music
     (make-sequential-music
       (list (make-music
               'Music
               'type 'conditional-transposition
               'from from
               'to   to)
             music)))
   music)

#(define conditional-transposition?
   (lambda (music)
     (eq? (ly:music-property music 'type) 'conditional-transposition)))

#(define sequential-music?
   (lambda (music)
     (music-is-of-type? music 'sequential-music)))

processConditionalTransposition =
#(define-music-function
   (parser location music)
   (ly:music?)
   (let ((music-list (extract-typed-music music '(general-music))))
     (if (list? music-list)
       (for-each (lambda (a b)
                   (if (and (conditional-transposition? a)
                            (sequential-music? b))
                     (ly:music-transpose b ...)))
                 music-list
                 (cdr music-list)))
     music))

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: problems trying to write a conditional \transpose

David Kastrup
Mark Polesky <[hidden email]> writes:

> I'm trying to write a conditional version of the \transpose
> function, that would work something like this:
>
> input = {
>   c c
>   \conditionalTransposition c c' { c c }
> }
>
> \input
> => { c c c c }
>
> \processConditionalTransposition \input
> => { c c c' c' }

conditionalTransposition =
#(define-music-function (parser location from to music)
  (ly:pitch? ly:pitch? ly:music?)
  (set! music #{ \transpose c' c' #music #})
  (set! (ly:music-property 'from-to music) (cons from to))
  music)

processConditionalTransposition =
#(define-music-function (parser location music) (ly:music?)
  (map-some-music
    (lambda (m)
      (and (music-is-of-type? m 'transposed-music)
           (pair? (ly:music-property m 'from-to music))
           #{ \transpose #(car (ly:music-property m 'from-to-music))
                         #(cdr (ly:music-property m 'from-to-music))
                         #(ly:music-property m 'element)
           #}))
     music))

This is assuming that you don't nest processConditionalTransposition, if
you do, you need to write
\processConditionalTransposition #(ly:music-property m 'element) at the
appropriate place.

Untested.

--
David Kastrup


_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: problems trying to write a conditional \transpose

Mark Polesky
In reply to this post by Mark Polesky
David Kastrup wrote:

>> I'm trying to write a conditional version of the \transpose
>> function, that would work something like this...
>>
>> input = {
>>   c c
>>   \conditionalTransposition c c' { c c }
>> }
>>
>> \input
>> => { c c c c }
>>
>> \processConditionalTransposition \input
>> => { c c c' c' }
>
> conditionalTransposition =
> #(define-music-function (parser location from to music)
>   (ly:pitch? ly:pitch? ly:music?)
>   (set! music #{ \transpose c' c' #music #})
>   (set! (ly:music-property 'from-to music) (cons from to))
>   music)
>
> processConditionalTransposition =
> #(define-music-function (parser location music) (ly:music?)
>   (map-some-music
>     (lambda (m)
>       (and (music-is-of-type? m 'transposed-music)
>            (pair? (ly:music-property m 'from-to music))
>            #{ \transpose #(car (ly:music-property m 'from-to-music))
>                          #(cdr (ly:music-property m 'from-to-music))
>                          #(ly:music-property m 'element)
>            #}))
>      music))


David,

sorry for the delayed response.  Your solution is excellent!
I'm learning so much from you.

Thanks again.
- Mark

_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

Re: problems trying to write a conditional \transpose

David Kastrup
Mark Polesky <[hidden email]> writes:

> David Kastrup wrote:
>>> I'm trying to write a conditional version of the \transpose
>>> function, that would work something like this...
>>>
>>> input = {
>>>   c c
>>>   \conditionalTransposition c c' { c c }
>>> }
>>>
>>> \input
>>> => { c c c c }
>>>
>>> \processConditionalTransposition \input
>>> => { c c c' c' }
>>
>> conditionalTransposition =
>> #(define-music-function (parser location from to music)
>>   (ly:pitch? ly:pitch? ly:music?)
>>   (set! music #{ \transpose c' c' #music #})
>>   (set! (ly:music-property 'from-to music) (cons from to))
>>   music)
>>
>> processConditionalTransposition =
>> #(define-music-function (parser location music) (ly:music?)
>>   (map-some-music
>>     (lambda (m)
>>       (and (music-is-of-type? m 'transposed-music)
>>            (pair? (ly:music-property m 'from-to music))
>>            #{ \transpose #(car (ly:music-property m 'from-to-music))
>>                          #(cdr (ly:music-property m 'from-to-music))
>>                          #(ly:music-property m 'element)
>>            #}))
>>      music))
>
> David,
>
> sorry for the delayed response.  Your solution is excellent!
> I'm learning so much from you.

The code is written in a manner that does not make it obvious, but I
should have mentioned why I stored a pitch pair rather than a single
pitch difference: this was in order not to have an outer \transpose
affect the pitch difference information: music properties containing a
single pitch are converted by \transpose.  A pitch pair is left alone.

--
David Kastrup


_______________________________________________
lilypond-user mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/lilypond-user