scheme-score

Check-in [4f8b76dc13]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fix type confusion in compilation routines
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:4f8b76dc13d2ec831ec3f94b7c9794243579a2d9711819cd26ae2e1a6b0a1b20
User & Date: wcm 2019-05-11 16:49:58
Context
2019-05-11
16:54
If a context is provided to music->events, merge it with default-context. check-in: 617f0ebc79 user: wcm tags: trunk
16:49
Fix type confusion in compilation routines check-in: 4f8b76dc13 user: wcm tags: trunk
2019-05-09
18:31
Filter out #f values from final event list check-in: bb652435be user: wcm tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to score.scm.

172
173
174
175
176
177
178

179
180
181
182
183
184
185
...
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
...
206
207
208
209
210
211
212

213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
...
232
233
234
235
236
237
238
239
240
241
242
243

244
245
246

247
248

249
250
251
252
253
254
255
256
(define (context-pfields ctx) (context-ref 'pfields ctx))
(define (context-amplitude ctx) (context-ref 'amplitude ctx))


;;; Events
;;;
;;; These are ilists which map directly to Csound score statements.


(define (event? x) (ipair? x))

;; These positions are fixed by Csound syntax.
(define (event-statement e) (icar e))
(define (event-rest e) (icdr e))         ; non-instrument pfields
(define (event-instrument e) (isecond e))
................................................................................
(define (event-duration e) (ifourth e))

(define (time-offset? x)
  (and (number? x) (>= x 0)))

;;; Compilation routines

;;; FIXME: Some type confusion.  compile-note and -rest both construct
;;; an event, while compile-sequence constructs a list of events.

;; State monad syntax helper.
(define-syntax state-letM*
  (syntax-rules ()
    ((state-letM* () exp) exp)
    ((state-letM* ((n1 s1) (n2 s2) ...) exp)
     (state-bind s1
                 (lambda (n1)
................................................................................

;; pfields p1 (instrument), p2 (start time), and p3 (duration) are
;; fixed by Csound.  p4 (frequency) and p5 (amplitude) are conventions
;; of this library.  pfields from the context are appended after p5.
(define (note->event note ctx start)
  (check-arg context? ctx note->event)
  (check-arg time-offset? start note->event)

  (iappend (ilist 'i
                  (context-instrument ctx)
                  start
                  (duration->seconds (note-duration note)
                                     (context-tempo ctx))
                  (pitch->frequency (note-pitch note)
                                    (context-base-freq ctx))
                  (context-amplitude ctx))
           (or (context-pfields ctx) '())))

;; Main compilation entrypoint.  All compile- procedures take a
;; music and return a state transformer.
(define (compile-mval m ctx)
  (let ((proc (cond ((note? m) compile-note)
                    ((rest? m) compile-rest)
                    ((sequence? m) compile-sequence)
................................................................................
    (proc m ctx)))

(define (compile-note m ctx)
  (state-letM* ((t state-get))
    (let ((e (note->event m ctx t)))
      (state-and-then
       (state-put (+ t (event-duration e)))
       (state-pure e)))))

;; Rests "disappear" and are not compiled to events.  Rather, they
;; adjust the time offset of the next event.
(define (compile-rest m ctx)

  (state-modify
   (lambda (t)
     (+ t (duration->seconds (rest-duration m) (context-tempo ctx))))))


(define (compile-sequence ms ctx)

  (state-traverse (lambda (m) (compile-mval m ctx)) ms))

(define (compile-modifier m ctx)
  (compile-mval (modifier-music m)
                (context-merge ctx (modifier-context m))))

(define (compile-simul m ctx)
  (state-letM* ((t state-get)







>







 







<
<
<







 







>
|
|
|
|
<
|
<
|
|







 







|

<
|

>
|
|
|
>


>
|







172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
...
188
189
190
191
192
193
194



195
196
197
198
199
200
201
...
204
205
206
207
208
209
210
211
212
213
214
215

216

217
218
219
220
221
222
223
224
225
...
229
230
231
232
233
234
235
236
237

238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
(define (context-pfields ctx) (context-ref 'pfields ctx))
(define (context-amplitude ctx) (context-ref 'amplitude ctx))


;;; Events
;;;
;;; These are ilists which map directly to Csound score statements.
;;; A music value is compiled to a list of one or more events.

(define (event? x) (ipair? x))

;; These positions are fixed by Csound syntax.
(define (event-statement e) (icar e))
(define (event-rest e) (icdr e))         ; non-instrument pfields
(define (event-instrument e) (isecond e))
................................................................................
(define (event-duration e) (ifourth e))

(define (time-offset? x)
  (and (number? x) (>= x 0)))

;;; Compilation routines




;; State monad syntax helper.
(define-syntax state-letM*
  (syntax-rules ()
    ((state-letM* () exp) exp)
    ((state-letM* ((n1 s1) (n2 s2) ...) exp)
     (state-bind s1
                 (lambda (n1)
................................................................................

;; pfields p1 (instrument), p2 (start time), and p3 (duration) are
;; fixed by Csound.  p4 (frequency) and p5 (amplitude) are conventions
;; of this library.  pfields from the context are appended after p5.
(define (note->event note ctx start)
  (check-arg context? ctx note->event)
  (check-arg time-offset? start note->event)
  (iappend
   (ilist 'i
          (context-instrument ctx)
          start
          (duration->seconds (note-duration note) (context-tempo ctx))

          (pitch->frequency (note-pitch note) (context-base-freq ctx))

          (context-amplitude ctx))
   (or (context-pfields ctx) '())))

;; Main compilation entrypoint.  All compile- procedures take a
;; music and return a state transformer.
(define (compile-mval m ctx)
  (let ((proc (cond ((note? m) compile-note)
                    ((rest? m) compile-rest)
                    ((sequence? m) compile-sequence)
................................................................................
    (proc m ctx)))

(define (compile-note m ctx)
  (state-letM* ((t state-get))
    (let ((e (note->event m ctx t)))
      (state-and-then
       (state-put (+ t (event-duration e)))
       (state-pure (ilist e))))))


;; Rests only adjust the time offset of the next event.
(define (compile-rest m ctx)
  (state-and-then
   (state-modify
    (lambda (t)
      (+ t (duration->seconds (rest-duration m) (context-tempo ctx)))))
   (state-pure '())))

(define (compile-sequence ms ctx)
  (state-fmap iconcatenate
              (state-traverse (lambda (m) (compile-mval m ctx)) ms)))

(define (compile-modifier m ctx)
  (compile-mval (modifier-music m)
                (context-merge ctx (modifier-context m))))

(define (compile-simul m ctx)
  (state-letM* ((t state-get)