-
Notifications
You must be signed in to change notification settings - Fork 2
/
midimacs-pitch.el
37 lines (32 loc) · 1.33 KB
/
midimacs-pitch.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(eval-when-compile
(require 'cl))
(require 'midimacs-globals)
(defun midimacs-parse-pitch (ss)
(let ((s (cond ((symbolp ss) (symbol-name ss))
(t ss))))
(if (string-match "^\\([a-gA-G]\\)\\([sb]\\)?\\(-?[0-9]\\)$" s)
(let* ((base (downcase (match-string 1 s)))
(accidental (match-string 2 s))
(octave (string-to-number (match-string 3 s)))
(pitch (+ (* 12 (+ octave 1))
(+ (cdr (assoc base midimacs-pitch-numbers))
(cdr (assoc accidental midimacs-accidental-numbers))))))
(unless (and (>= pitch 0)
(<= pitch 127))
(error (format "Pitch \"%s\" (%d) is out of range 0 <= x <= 127" s pitch)))
pitch)
(if (equal s "-")
nil
(error (format "Couldn't parse pitch \"%s\"" s))))))
(defun midimacs-anything-to-pitch (pitch-raw)
(cond ((not pitch-raw) nil)
((symbolp pitch-raw) (midimacs-parse-pitch (symbol-name pitch-raw)))
((stringp pitch-raw) (midimacs-parse-pitch pitch-raw))
(t pitch-raw)))
(defun midimacs-pitch-to-string (pitch)
(if pitch
(let ((octave (- (floor (/ pitch 12)) 1))
(name (cdr (assoc (% pitch 12) midimacs-pitch-names))))
(format "%s%d" name octave))
"-"))
(provide 'midimacs-pitch)