Skip to content

Instantly share code, notes, and snippets.

@jbclements
Created November 20, 2013 19:11
Show Gist options
  • Save jbclements/7569131 to your computer and use it in GitHub Desktop.
Save jbclements/7569131 to your computer and use it in GitHub Desktop.
code from class, 2013-11-20
(require rsound)
(require 2htdp/image)
(require 2htdp/universe)
(define (s sec) (* sec 44100))
(define samp (rs-read "/tmp/waaoow-chorus.wav"))
;; a world is (make-world nat frames)
(define-struct world (ctr next-play-time))
;; the pstream for playing
(define ps (make-pstream))
;; queue on ps:
(define (psq sound t)
(pstream-queue ps sound t))
;; do two things
(define (both a b)
b)
;; play a quarter-second
;; world -> world
(define (play-chunk w)
(local
[(define ctr (world-ctr w))]
(cond [(time-to-play? w (pstream-current-frame ps))
(both (psq (clip samp
(s (/ ctr 4))
(s (/ (add1 ctr) 4)))
(world-next-play-time w))
(world-bump-timer w))]
[else w])))
(define LEAD-TIME (round (s 1/10)))
;; is it time to play, yet?
;; world frame -> boolean
(define (time-to-play? w current-time)
(< (- (world-next-play-time w) LEAD-TIME) current-time))
(check-expect (time-to-play? (make-world 234 11025) 11020)
true)
(check-expect (time-to-play? (make-world 234 11025) 3020)
false)
(define SUBTIMER-INCREMENT (round (s 1/4)))
;; increase the next-time-to-play by 1/4 second
(define (world-bump-timer w)
(make-world (add1 (world-ctr w))
(+ SUBTIMER-INCREMENT
(world-next-play-time w))))
(check-expect (world-bump-timer (make-world 234 1234))
(make-world 235 (+ SUBTIMER-INCREMENT 1234)))
;; should play fifth 1/4-sec of song
;(play-chunk (make-world 5))
;; draw the world
;; world -> image
(define (draw-world w)
(rectangle 100 100 "solid" "lightgray"))
(big-bang (make-world 0 0)
[on-tick play-chunk]
[to-draw draw-world])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment