Skip to content

Instantly share code, notes, and snippets.

@jbclements
Created October 21, 2013 18:20
Show Gist options
  • Save jbclements/7088455 to your computer and use it in GitHub Desktop.
Save jbclements/7088455 to your computer and use it in GitHub Desktop.
code from class 2011-10-21 morning section
(require rsound)
(require 2htdp/universe)
(require 2htdp/image)
(define SR 44100)
(define (s sec) (* sec SR))
(define beat-frames (round (/ (* SR 60) 128)))
(define (beat b) (* beat-frames b))
(define LEAD-FRAMES (beat 0.5))
(define TICK-INTERVAL 1/28)
(define TICK-FRAMES (s TICK-INTERVAL))
(define samp (rs-read/clip "/tmp/weasel-heart.wav" (s 0) (s 20)))
;(play samp)
;; a loop is (make-loop sound frame frame)
(define-struct loop (sound start end))
(define LOOP1 (make-loop samp (s 7) (s 8)))
(define LOOP2 (make-loop samp (beat 4) (beat 8)))
(define TEST-PSTREAM (make-pstream))
(check-expect (loop-start LOOP1) (s 7))
;; plays a loop
;; loop -> "played sound"
(define (play-loop-times n loop)
(play
(times
n
(loop->sound loop))))
;; loop -> sound
;; extract the looped portion of the sound
(define (loop->sound loop)
(clip
(loop-sound loop)
(loop-start loop)
(loop-end loop)))
;; loop -> number
;; return the length of the loop in frames
(define (loop-len l)
(- (loop-end l) (loop-start l)))
(check-expect (loop-len (make-loop samp 340 410)) 70)
;; should play sound, as well:
#;(check-expect (play-loop-times
4
(make-loop samp
(beat 4)
(beat 8)))
"played sound")
(define WIDTH 400)
(define HEIGHT 300)
(define LEFT-PAD 40)
(define RIGHT-PAD 40)
(define LINE-HEIGHT 50)
(define START-IMG (star 40 "solid" "red"))
(define STOP-IMG (triangle 40 "solid" "tan"))
;; a world is (make-world pct pct)
(define-struct world (start end))
;; world -> scene
;; draw a slider world
(define (draw-world w)
(place-image
STOP-IMG
(+ LEFT-PAD
(* (/ (world-end w) 100)
(- WIDTH LEFT-PAD RIGHT-PAD)))
LINE-HEIGHT
(place-image
START-IMG
(+ LEFT-PAD
(* (/ (world-start w) 100)
(- WIDTH LEFT-PAD RIGHT-PAD)))
LINE-HEIGHT
(scene+line
(empty-scene WIDTH HEIGHT)
LEFT-PAD LINE-HEIGHT (- WIDTH RIGHT-PAD) LINE-HEIGHT
(make-pen "lightblue" 15 "solid" "round" "round"))))
)
(check-expect
(draw-world (make-world 50 75))
(place-image
STOP-IMG
(+ LEFT-PAD
(* 0.75 (- WIDTH LEFT-PAD RIGHT-PAD)))
LINE-HEIGHT
(place-image
START-IMG
200
LINE-HEIGHT
(scene+line
(empty-scene WIDTH HEIGHT)
LEFT-PAD LINE-HEIGHT (- WIDTH RIGHT-PAD) LINE-HEIGHT
(make-pen "lightblue" 15 "solid" "round" "round")))))
(draw-world (make-world 25 50))
;; world number number string -> world
(define (mouse-handler w x y evt)
(make-world (* 100 (/ x WIDTH))
(world-end w))
)
(big-bang (make-world 25 50)
[to-draw draw-world]
[on-mouse mouse-handler])
(check-expect (mouse-handler (make-world 25 50)
200 30 "mouse-down")
(make-world 50 50))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment