[4913c3]: src / score.lisp  Maximize  Restore  History

Download this file

212 lines (197 with data), 8.7 kB

  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
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
;;; Copyright (c) 2013 Tito Latini
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
(in-package :incudine)
;;; A score file can contain time-tagged lisp functions, lisp statements
;;; and lisp tags.
(declaim (inline blank-char-p))
(defun blank-char-p (c)
(member c '(#\space #\tab)))
(declaim (inline line-parse-skip-string))
(defun line-parse-skip-string (str index end)
(declare (type string str) (type non-negative-fixnum index end))
(labels ((skip-p (i)
(if (>= i end)
i
(case (char str i)
(#\" i)
(#\\ (skip-p (+ i 2)))
(otherwise (skip-p (1+ i)))))))
(skip-p (1+ index))))
(defun %time-tagged-function-p (s)
(declare (type string s))
(let* ((slen (length s))
(last (1- slen)))
(declare (type non-negative-fixnum slen last))
(or (char/= (char s last) #\))
(labels ((stmt-p (i unmatched-parens)
(when (< i slen)
(if (zerop unmatched-parens)
(find-if-not #'blank-char-p s :start i)
(case (char s i)
(#\" (stmt-p (line-parse-skip-string s i slen)
unmatched-parens))
(#\) (stmt-p (1+ i) (1- unmatched-parens)))
(#\( (stmt-p (1+ i) (1+ unmatched-parens)))
(otherwise (stmt-p (1+ i) unmatched-parens)))))))
(stmt-p 1 1)))))
;;; The syntax of a time-tagged lisp function is:
;;;
;;; start-time-in-beats function-name [arg1] [arg2] ...
;;;
(declaim (inline time-tagged-function-p))
(defun time-tagged-function-p (string)
(declare (type string string))
(if (char= (char string 0) #\()
(%time-tagged-function-p string)
(let ((space-pos (position-if #'blank-char-p string)))
(declare (type (or non-negative-fixnum null)))
(when space-pos
(find-if-not #'blank-char-p string :start space-pos)))))
(declaim (inline score-skip-line-p))
(defun score-skip-line-p (line)
(declare (type string line))
(let ((non-blank (find-if-not #'blank-char-p line)))
(or (null non-blank)
(char= non-blank #\;))))
(defmacro %at-sample (at-var time-at-var offset env beats func-symbol &rest args)
`(,at-var (+ ,offset (* *sample-rate*
(,time-at-var ,env (setf ,at-var (sample ,beats)))))
#',func-symbol ,@args))
(declaim (inline score-line->sexp))
(defun score-line->sexp (line at-var time-at-var)
(declare (type string line))
(if (time-tagged-function-p line)
(macroexpand-1
(read-from-string
(format nil "(INCUDINE::%AT-SAMPLE ~A ~A TIME TEMPO-ENV ~A)"
at-var time-at-var line)))
;; Tag or lisp statement
(read-from-string (string-left-trim '(#\space #\tab) line))))
(defun find-score-local-bindings (stream at time-at)
(declare (type stream stream) (type symbol at))
(labels ((score-bindings-p (line)
(string-equal (subseq line 0 (min 5 (length line))) "with "))
(format-bindings (line)
(concatenate 'string "((" (subseq line 5) "))"))
(first-score-stmt (line)
(declare (type (or string null) line))
(when line
(cond ((score-skip-line-p line)
(first-score-stmt (read-score-line stream)))
((score-bindings-p line)
;; Local bindings at the beginning of the score
(read-from-string (format-bindings line)))
(t ;; There aren't local bindings
(list nil (score-line->sexp line at time-at)))))))
(first-score-stmt (read-score-line stream))))
(defun read-score-line (stream)
(declare (type stream stream))
(flet ((remove-comment (str)
;; A comment starts with `;'
(subseq str 0 (position #\; str)))
(line-break-p (str strlen)
;; Line continuation with `\' at the end
(and (> strlen 1)
(char= (char str (- strlen 1)) #\\)
(char/= (char str (- strlen 2)) #\\))))
(let ((line (read-line stream nil nil)))
(declare (type (or string null) line))
(when line
(let* ((s (remove-comment (string-trim '(#\space #\tab) line)))
(slen (length s)))
(declare (type non-negative-fixnum slen))
(if (line-break-p s slen)
(concatenate 'string
(subseq s 0 (- slen 2))
" "
(read-score-line stream))
s))))))
;;; Local variables usable inside the rego file:
;;;
;;; TIME initial time in samples
;;; TEMPO-ENV temporal envelope of the events
;;;
;;; It is possible to define other local variables by inserting
;;; the bindings after WITH, at the beginning of the score.
;;; For example:
;;;
;;; ;;; test.rego
;;; with (id 1) (last 4)
;;;
;;; ;; simple oscillators
;;; 0 simple 440 .2 :id id
;;; 1 simple 448 .2 :id (+ id 1)
;;; (1- last) simple 661 .2 :id (+ id 2)
;;; last free 0
;;;
;;; We can also add a DECLARE expression after the bindings.
;;;
;;; DUR is a local function to convert the duration from
;;; beats to seconds with respect to TEMPO-ENV.
;;;
;;; TEMPO is a local macro to change the tempo of the score.
;;; The syntax is
;;;
;;; (tempo bpm)
;;; (tempo bpms beats &key curve loop-node release-node
;;; restart-level real-time-p)
;;;
(defun regofile->sexp (path &optional fname)
(declare (type (or pathname string)))
(with-ensure-symbol (time dur tempo tempo-env)
(let ((at (ensure-symbol (symbol-name (gensym "%AT%"))))
(time-at (ensure-symbol (symbol-name (gensym "%TIME-AT%"))))
;; *PRINT-GENSYM* is NIL in REGOFILE->LISPFILE
(beats (gensym "%%%BEATS%%%")))
`(,@(if fname `(defun ,fname) '(lambda)) ()
(let ((,tempo-env (make-tempo-envelope '(60 60) '(0))))
(with-samples ((,time (now))
(,at +sample-zero+))
(flet ((,dur (,beats)
(time-at ,tempo-env ,beats ,at))
(,time-at (tenv beats)
(%time-at tenv beats))
(,at (time fn &rest args)
(incudine.edf::%%at time fn args)))
(declare (ignorable (function ,dur)))
(macrolet ((,tempo (&rest args)
`(set-tempo-envelope ,',tempo-env
,@(if (cdr args)
args
;; Constant tempo
`((list ,(car args) ,(car args)) '(0))))))
(prog*
,@(with-open-file (score path)
(append (find-score-local-bindings score at time-at)
(loop for line of-type (or string null)
= (read-score-line score)
while line
unless (score-skip-line-p line)
collect (score-line->sexp line at time-at)))))))))))))
(declaim (inline regofile->function))
(defun regofile->function (path &optional fname)
(eval (regofile->sexp path fname)))
(defun regofile->lispfile (rego-file &optional fname lisp-file)
(declare (type (or pathname string null) rego-file lisp-file))
(let ((lisp-file (or lisp-file
(make-pathname :defaults rego-file
:type "cudo"))))
(with-open-file (lfile lisp-file :direction :output
:if-exists :supersede)
(write (regofile->sexp rego-file fname) :stream lfile :gensym nil)
(terpri lfile)
(msg debug "convert ~A -> ~A" rego-file lisp-file)
lisp-file)))

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks