# Function for note string to fraction?

 Classic List Threaded
14 messages
Reply | Threaded
Open this post in threaded view
|

## Function for note string to fraction?

 Hi all, is there already a function that converts a string "4." to the fraction '3/8', or the other-way round? Maybe it's helpful to know that the string is intended for use with \note, e g \note #"4." #UP and the fraction is intended for use with \set Timing.baseMoment, e g \set Timing.baseMoment = #(ly:make-moment 3/8) Best Peter _______________________________________________ lilypond-user mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

## Re: Function for note string to fraction?

 On 2014-05-15 15:17, Peter Bjuhr wrote: is there already a function that converts a string "4." to the fraction '3/8', or the other-way round? Maybe it's helpful to know that the string is intended for use with \note, e g \note #"4." #UP and the fraction is intended for use with \set Timing.baseMoment, e g \set Timing.baseMoment = #(ly:make-moment 3/8) After some research I think I've found a way forward with this: In ' scm/define-markup-commands.scm' there's a function/procedure called 'parse-simple-duration', which is used to translate from '\note' to '\note-by-number'. In a similar way I could use that to create the arguments to 'ly:make-duration'. And from there it would be easy to get the moment with ' ly:duration->string'. This all leads to an additional question: Is it possible to include and use 'parse-simple-duration' in a local script (without copy and pasting it)? If it's not possible I will settle with using \note-by-number instead! For those of you which find it easier to read code than my ramblings, here is what I've got so far: #(define (note-to-moment notestr) (let ((parsed (parse-simple-duration notestr))) (ly:duration-length (ly:make-duration (car parsed) (cadr parsed))))) Best Peter   _______________________________________________ lilypond-user mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

## Re: Function for note string to fraction?

 In reply to this post by Peter Bjuhr Peter Bjuhr wrote:  > is there already a function that converts a string "4." to > the fraction '3/8', or the other-way round?           I don't think you need ly:duration->string; I think you can just skip that step.  And as far as I know, you do need to copy out the definition of parse-simple-duration, and above it add:             #(use-modules (ice-9 regex)) The whole thing will look like this: ____________________________________________________________   #(use-modules (ice-9 regex)) #(define (parse-simple-duration duration-string)   "Parse the `duration-string', e.g. ''4..'' or ''breve.'', and return a (log dots) list."   (let ((match (regexp-exec (make-regexp "(breve|longa|maxima|[0-9]+)(\\.*)")                             duration-string)))     (if (and match (string=? duration-string (match:substring match 0)))         (let ((len (match:substring match 1))               (dots (match:substring match 2)))           (list (cond ((string=? len "breve") -1)                       ((string=? len "longa") -2)                       ((string=? len "maxima") -3)                       (else (log2 (string->number len))))                 (if dots (string-length dots) 0)))         (ly:error (_ "not a valid duration string: ~a") duration-string)))) #(define (note-to-moment notestr)    (let ((parsed (parse-simple-duration notestr)))      (ly:duration-length        (ly:make-duration (car parsed) (cadr parsed))))) ____________________________________________________________ Then you would use it like this: \set Timing.baseMoment = #(note-to-moment "4.") Hope that helps. - Mark _______________________________________________ lilypond-user mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

## Re: Function for note string to fraction?

 In reply to this post by Peter Bjuhr 2014-05-19 22:50 GMT+02:00 Peter Bjuhr <[hidden email]>: > > On 2014-05-15 15:17, Peter Bjuhr wrote: > > is there already a function that converts a string "4." to the fraction > '3/8', or the other-way round? > > Maybe it's helpful to know that the string is intended for use with \note, e > g > > \note #"4." #UP > > and the fraction is intended for use with \set Timing.baseMoment, e g > > \set Timing.baseMoment = #(ly:make-moment 3/8) > > > After some research I think I've found a way forward with this: > > In ' scm/define-markup-commands.scm' there's a function/procedure called > 'parse-simple-duration', which is used to translate from '\note' to > '\note-by-number'. > > In a similar way I could use that to create the arguments to > 'ly:make-duration'. And from there it would be easy to get the moment with ' > ly:duration->string'. > > This all leads to an additional question: Is it possible to include and use > 'parse-simple-duration' in a local script (without copy and pasting it)? Well, I would have no problem to c/p 'parse-simple-duration', though yes, it is possible: #(define (note-to-moment notestr)    (let ((parsed ((@@ (lily) parse-simple-duration ) notestr)))      (ly:duration-length         (ly:make-duration (car parsed) (cadr parsed))))) #(write (note-to-moment "4.")) --> # Though, quoting guile-manual: — syntax: @@ module-name binding-name Refer to the binding named binding-name in module module-name. The binding must not have been exported by the module. This syntax is only intended for debugging purposes or as a last resort. !! Cheers,   Harm _______________________________________________ lilypond-user mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

## Re: Function for note string to fraction?

 In reply to this post by Mark Polesky Thanks a lot! On 2014-05-20 00:49, Mark Polesky wrote: > Peter Bjuhr wrote: >> And from there it would be easy to get the >>      moment with 'ly:duration->string'. >             > I don't think you need ly:duration->string; I think you can > just skip that step. Yes, that was indeed a rambling. I meant I could get the moment with 'ly:duration-length'. > And as far as I know, you do need to > copy out the definition of parse-simple-duration, and above > it add: >   >    #(use-modules (ice-9 regex)) > > > Hope that helps. > - Mark > It works great! Best Peter _______________________________________________ lilypond-user mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

## Re: Function for note string to fraction?

 In reply to this post by Thomas Morley-2 Thanks a lot! On 2014-05-20 00:50, Thomas Morley wrote: >> Is it possible to include and use >> >'parse-simple-duration' in a local script (without copy and pasting it)? > > Well, I would have no problem to c/p 'parse-simple-duration', though > yes, it is possible: > > #(define (note-to-moment notestr) >     (let ((parsed ((@@ (lily) parse-simple-duration ) notestr))) >       (ly:duration-length >          (ly:make-duration (car parsed) (cadr parsed))))) > > #(write (note-to-moment "4.")) > > --> # > > > Though, quoting guile-manual: > — syntax: @@ module-name binding-name > Refer to the binding named binding-name in module module-name. The > binding must not have been exported by the module. This syntax is only > intended for debugging purposes or as a last resort. > > !! > > Cheers, >    Harm Part of the problem with copy and pasting was that it didn't work. I got the final piece of the puzzle though by Mark. I would still prefer this solution, but what you're saying is that it isn't really suitable to use this kind of syntax and that it is better to copy-and-paste!? Best Peter _______________________________________________ lilypond-user mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

## Re: Function for note string to fraction?

 In reply to this post by Mark Polesky Mark Polesky <[hidden email]> writes: > Then you would use it like this: > > \set Timing.baseMoment = #(note-to-moment "4.") Anything wrong with {   \set Timing.baseMoment = #(ly:duration-length #{ 4. #})   \unset Timing.beamExceptions   c8 c8 c8 c8 c8 c8 c8 c8 } here?  Ok, you need 2.19 for it, but it seems like one of the cheaper ways to ask the parser for input. -- 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: Function for note string to fraction?

 On 2014-05-20 10:26, David Kastrup wrote: ```Mark Polesky [hidden email] writes: ``` ```Then you would use it like this: \set Timing.baseMoment = #(note-to-moment "4.") ``` ```Anything wrong with ``` \set Timing.baseMoment = #(ly:duration-length #{ 4. #}) ```Ok, you need 2.19 for it, but it seems like one of the cheaper ways to ask the parser for input. ``` Thanks, indeed it is cheaper. But I'm not sure if I can use it. This is part of a larger whole. Basically the user is intended to input a string with the duration, and the string will be used both for \set Timing.baseMoment and for markup via \note I'll present the script shortly... Best Peter _______________________________________________ lilypond-user mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

## Re: Function for note string to fraction?

 Peter Bjuhr <[hidden email]> writes: > On 2014-05-20 10:26, David Kastrup wrote: >> Mark Polesky <[hidden email]> writes: >> >>> Then you would use it like this: >>> >>> \set Timing.baseMoment = #(note-to-moment "4.") >> Anything wrong with >> >> \set Timing.baseMoment = #(ly:duration-length #{ 4. #}) >> >> >> Ok, you need 2.19 for it, but it seems like one of the cheaper >> ways to ask the parser for input. >> > > Thanks, indeed it is cheaper. But I'm not sure if I can use it. This > is part of a larger whole. Basically the user is intended to input a > string with the duration, and the string will be used both for > > \set Timing.baseMoment > > and for markup via > > \note We probably should let \note accept a duration, but it's not actually hard to pick apart and use \note-by-number on the parts. -- 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: Function for note string to fraction?

 On 2014-05-20 12:02, David Kastrup wrote: >> Thanks, indeed it is cheaper. But I'm not sure if I can use it. This >> >is part of a larger whole. Basically the user is intended to input a >> >string with the duration, and the string will be used both for >> > >> >\set Timing.baseMoment >> > >> >and for markup via >> > >> >\note > We probably should let \note accept a duration, but it's not actually > hard to pick apart and use \note-by-number on the parts. One should always be careful with the word 'part' in musical circumstances... Maybe you misunderstood me!? I meant that the functionality was intended for a larger script... Best Peter _______________________________________________ lilypond-user mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

## Re: Function for note string to fraction?

 Peter Bjuhr <[hidden email]> writes: > On 2014-05-20 12:02, David Kastrup wrote: >>> Thanks, indeed it is cheaper. But I'm not sure if I can use it. This >>> >is part of a larger whole. Basically the user is intended to input a >>> >string with the duration, and the string will be used both for >>> > >>> >\set Timing.baseMoment >>> > >>> >and for markup via >>> > >>> >\note >> We probably should let \note accept a duration, but it's not actually >> hard to pick apart and use \note-by-number on the parts. > > One should always be careful with the word 'part' in musical > circumstances... Maybe you misunderstood me!? I meant that the > functionality was intended for a larger script... So? -- 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: Function for note string to fraction?

 On 2014-05-20 12:23, David Kastrup wrote: ```We probably should let \note accept a duration, but it's not actually >> hard to pick apart and use \note-by-number on the parts. ``` ```> Maybe you misunderstood me!? I meant that the > functionality was intended for a larger script... ``` `So?` Maybe it was me that misunderstood you instead! Did you mean to use \note like in your previous example: \markup { \note { 4. } } ? But why should \note-by-number be used for the parts, I didn't follow that? Best Peter _______________________________________________ lilypond-user mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/lilypond-user
Reply | Threaded
Open this post in threaded view
|

## Re: Function for note string to fraction?

 Peter Bjuhr <[hidden email]> writes: > On 2014-05-20 12:23, David Kastrup wrote: >>>> We probably should let \note accept a duration, but it's not actually >>>> >>hard to pick apart and use \note-by-number on the parts. >>> > >>> Maybe you misunderstood me!? I meant that the >>> >functionality was intended for a larger script... >> So? > > Maybe it was me that misunderstood you instead! Did you mean to use > \note like in your previous example: > > \markup { \note { 4. } } > > ? > > But why should \note-by-number be used for the parts, I didn't follow that? You want to enter a duration in a simple manner and use it in several contexts.  One as markup, one as a duration or length. The suggestion was to let LilyPond see it not as a string but as a duration and go from there instead of writing your own duration parser. I don't understand the problem you have with that. -- 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: Function for note string to fraction?

 On 2014-05-20 12:51, David Kastrup wrote: > You want to enter a duration in a simple manner and use it in several > contexts.  One as markup, one as a duration or length. > > The suggestion was to let LilyPond see it not as a string but as a > duration and go from there instead of writing your own duration parser. > > I don't understand the problem you have with that. I think the suggestion is great, at least for the purpose of the script I'm trying to write! Best Peter _______________________________________________ lilypond-user mailing list [hidden email] https://lists.gnu.org/mailman/listinfo/lilypond-user