lilypond-user
[Top][All Lists]

## Re: Function for note string to fraction?

 From: Mark Polesky Subject: Re: Function for note string to fraction? Date: Mon, 19 May 2014 15:49:34 -0700 (PDT)

```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

#(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

```

reply via email to