3 ; -> Counts number of notes between last | and point. Adds durations of
\r
4 ; each note up, and returns result.
\r
6 ; -> Works well on notes and chords.
\r
8 ; -> Ignores most keywords, like \override
\r
10 ; -> Is aware of certain keywords which often contain parameters that
\r
11 ; look like notes, but should not be counted.
\r
12 ; | a \key b \minor c % b is not counted, but a and c are.
\r
14 ; -> Ignores Scheme expressions, which start with #
\r
16 ; -> Doesn't ignore the \times keyword. Intelligently handles triplets.
\r
21 ; -> Doesn't work on regions that aren't preceded by a |. This is because such
\r
22 ; notes are only delimited by a {, and what-beat can't distinguish a { that
\r
23 ; opens a set of notes from an internal { (say from a triplet)
\r
25 ; -> Doesn't work with << >> expressions or nested {} expressions (unless
\r
26 ; {} is part of a keyword like \times)
\r
28 ; -> Keywords abutted against a note are not visible to what-beat, and
\r
29 ; can therefore surreptitiosly sneak fake notes into what-beat.
\r
30 ; | c\glissando f <- BAD: the f gets counted, but shouldn't
\r
31 ; | c \glissando f <- GOOD: the f gets ignored
\r
33 ; -> Does not look outside notes context. Derivation rules don't work:
\r
34 ; str = \notes { a8 b c d }
\r
35 ; \score { \notes { | e4 %{ gets counted }% \str %{gets ignored}%
\r
37 ; -> Does not handle repeats.
\r
39 ; -> Ignores \bar commands (and does not get confused by a | inside a \bar)
\r
42 ; Recognizes pitch & octave
\r
43 (setq pitch-regex "\\([a-z]+[,']*\\|<[^>]*>\\)\\(=[,']*\\)?")
\r
44 ; Recognizes duration
\r
45 (setq duration-regex "[ \t\n]*\\(\\(\\(128\\|6?4\\|3?2\\|16?\\|8\\)\\([.]*\\)\\)\\([ \t]*[*][ \t]*\\([0-9]+\\)\\(/\\([1-9][0-9]*\\)\\)?\\)?\\)")
\r
47 ; These keywords precede notes that should not be counted during beats
\r
48 (setq Parm-Keywords '("key" "clef" "appoggiatura" "acciaccatura" "grace"
\r
49 "override" "revert" "glissando"))
\r
52 (defun extract-match (string match-num)
\r
53 (if (null (match-beginning match-num))
\r
55 (substring string (match-beginning match-num) (match-end match-num))))
\r
58 (defun add-fractions (f1 f2)
\r
59 "Adds two fractions, both are (numerator denominator)"
\r
60 (set 'result (list (+ (* (car f1) (cadr f2)) (* (car f2) (cadr f1)))
\r
61 (* (cadr f1) (cadr f2))))
\r
62 (set 'result (reduce-fraction result 2))
\r
63 (set 'result (reduce-fraction result 3))
\r
64 (set 'result (reduce-fraction result 5))
\r
65 (set 'result (reduce-fraction result 7))
\r
69 (defun reduce-fraction (f divisor)
\r
70 "Eliminates divisor from fraction if present"
\r
71 (while (and (= 0 (% (car result) divisor))
\r
72 (= 0 (% (cadr result) divisor))
\r
75 (set 'result (list (/ (car result) divisor) (/ (cadr result) divisor))))
\r
80 (defun parse-duration (duration)
\r
81 "Returns a duration string parsed as '(numerator denominator)"
\r
82 (string-match duration-regex duration)
\r
83 (let ((result (list 1 (string-to-int (extract-match duration 2))))
\r
84 (dots (extract-match duration 4))
\r
85 (numerator (or (extract-match duration 6) "1"))
\r
86 (denominator (or (extract-match duration 8) "1")))
\r
87 (if (and (not (null dots)) (< 0 (string-width dots)))
\r
88 (dotimes (dummy (string-width dots))
\r
89 (set 'result (list (1+ (* 2 (car result))) (* 2 (cadr result))))))
\r
90 (list (* (string-to-int numerator) (car result))
\r
91 (* (string-to-int denominator) (cadr result)))
\r
94 (defun walk-note-duration ()
\r
95 "Returns duration of next note, moving point past note.
\r
96 If point is not before a note, returns nil
\r
97 If next note has no duration, returns t"
\r
98 (if (not (looking-at pitch-regex))
\r
101 (goto-char (match-end 0))
\r
102 (if (not (looking-at duration-regex))
\r
105 (goto-char (match-end 0))
\r
106 (parse-duration (match-string 0)))))))
\r
108 ; returns nil if not at a comment
\r
109 (defun skip-comment ()
\r
110 (if (not (char-equal ?\% (following-char)))
\r
114 (if (char-equal ?\{ (following-char))
\r
115 (re-search-forward "}%" nil t)
\r
117 (skip-chars-forward "^\n")
\r
122 ; returns nil if not at a quotation
\r
123 (defun skip-quotation ()
\r
124 (if (not (char-equal ?\" (following-char)))
\r
128 (skip-chars-forward "^\"")
\r
133 ; returns nil if not at a sexp
\r
134 (defun skip-sexp ()
\r
136 (if (not (char-equal ?\# (following-char)))
\r
140 (if (char-equal ?\' (following-char))
\r
142 (if (not (char-equal ?\( (following-char)))
\r
143 (skip-chars-forward "^ \t\n")
\r
148 (cond ((char-equal ?\( (following-char))
\r
149 (setq paren (1+ paren)))
\r
150 ((char-equal ?\) (following-char))
\r
151 (setq paren (1- paren)))))
\r
156 (defun goto-note-begin ()
\r
158 ; skip anything that is not ws. And skip any comments or quotations
\r
159 (while (or (< 0 (skip-chars-forward "^ \t\n~%#\""))
\r
163 ; Now skip anything that isn't alphanum or \. And skip comments or quotations
\r
164 (while (or (< 0 (skip-chars-forward "^A-Za-z<%}#=\""))
\r
168 ; (skip-chars-forward "^\\") Why doesn't this work?!!
\r
169 (if (char-equal ?\\ (preceding-char))
\r
174 (defun skip-good-keywords ()
\r
175 (if (looking-at "\\\\\\([a-z]*\\)")
\r
177 (goto-char (match-end 0))
\r
178 (if (member (match-string 1) Parm-Keywords)
\r
180 (if (looking-at "[ \t\n]*?\\([a-z0-9_]+\\|{[^}]*}\\|\"[^\"]*\"\\)")
\r
181 (goto-char (match-end 0))
\r
182 (error "Improper regex match:")
\r
183 (error "Unknown text: %s")
\r
186 (defun find-measure-start ()
\r
187 (let ((start (re-search-backward "\|" 0 t)))
\r
190 (if (looking-at "[^ \n\t]*\"")
\r
191 (find-measure-start)
\r
198 (let* ((end (point))
\r
199 (measure-start (find-measure-start))
\r
200 (last-dur (or (re-search-backward duration-regex 0 t) -1))
\r
201 (duration (if (= -1 last-dur) 0 (parse-duration (match-string 0))))
\r
202 (result '(0 1))) ; 0 in fraction form
\r
203 (if (= measure-start -1)
\r
204 (error "No | before point")
\r
206 (goto-char (1+ measure-start))
\r
208 (while (< (point) end)
\r
209 (set 'new-duration (walk-note-duration))
\r
210 (if (null new-duration)
\r
211 (if (not (looking-at "\\\\times[ \t]*\\([1-9]*\\)/\\([1-9]*\\)[ \t\n]*{"))
\r
212 (skip-good-keywords)
\r
214 ; handle \times specially
\r
215 (let ((numerator (string-to-int (match-string 1)))
\r
216 (denominator (string-to-int (match-string 2))))
\r
217 (goto-char (match-end 0))
\r
219 (while (and (not (looking-at "}"))
\r
221 (set 'new-duration (walk-note-duration))
\r
222 (if (null new-duration)
\r
223 (if (looking-at "\\\\[a-z]*[ \t]*[a-z]*")
\r
224 (goto-char (match-end 0))
\r
225 (error "Unknown text: %S %s" result(buffer-substring (point) end))))
\r
226 (if (not (eq new-duration t))
\r
227 (set 'duration new-duration))
\r
228 (set 'result (add-fractions result
\r
229 (list (* numerator (car duration))
\r
230 (* denominator (cadr duration)))))
\r
232 (if (< (point) end)
\r
233 (forward-char 1)))) ; skip }
\r
235 (if (not (eq new-duration t))
\r
236 (set 'duration new-duration))
\r
237 (set 'result (add-fractions result duration)))
\r
243 (defun LilyPond-what-beat ()
\r
244 "Returns how much of a measure lies between last measaure '|' and point.
\r
245 Recognizes chords, and triples."
\r
247 (let ((beat (get-beat)))
\r
248 (message "Beat: %d/%d" (car beat) (cadr beat)))
\r
251 (defun LilyPond-electric-bar ()
\r
252 "Indicate the number of beats in last measure when a | is inserted"
\r
254 (self-insert-command 1)
\r
258 (LilyPond-what-beat)
\r