[racket] sound + universe report: success.

From: John Clements (clements at brinckerhoff.org)
Date: Thu Oct 20 01:20:55 EDT 2011

I'm pleased to report that the sound library works pretty transparently with the universe teachpack.

Here's a program, written in the advanced language (minus the test cases). Naturally, the first time you run it you'll have to wait for the planet package to download and install. Two minutes? Three?

(require (planet "rsound.rkt" ("clements" "rsound.plt" 2 6)))

;; the "pop" sound
(define pop (rs-read "/tmp/popp.wav"))

;; a ball is (make-ball number number)
(define-struct ball (posn vel))

(define scene-height 200)

;; given a ball, produce a scene containing the 
;; ball at the given height
(define (draw-world b)
  (place-image (circle 5 'solid 'red)
               50 (- scene-height (ball-posn b))
               (empty-scene 100 scene-height)))

;; given a ball, produce a new ball different 
;; according to physics
;; EFFECT: plays a sound if the ball is on 
;; or below the ground.
(define (accelerate b)
  (cond [(and (<= (ball-posn b) 6)
              (<= (ball-vel b) 0)) 
         (begin (play pop)
                (make-ball 6 (- (ball-vel b))))]
        [else
         (make-ball (+ (ball-posn b) (ball-vel b))
                    (- (ball-vel b) 1))]))

;; start the world.
(big-bang (make-ball 100 0)
          (on-tick accelerate)
          (on-draw draw-world))

In order to make this work, you'll need the attached "pop" sound, and to adjust the path given above accordingly.

On my machine, the latency associated with the play is pretty good; I would guess at about 50-150 milliseconds. It would probably be acceptable for most situations involving universe programming. It could potentially be improved, if it turns out to be important.

The larger question could potentially be whether the sound-playing should be less imperative. If you wanted, you could add a "to-play" along with "to-draw", and just make the sound-playing part of the world state. This would avoid the "begin".

If people like this, it would be pretty easy to fold it in with the main tree.

John

-------------- next part --------------
A non-text attachment was scrubbed...
Name: popp.wav
Type: audio/wav
Size: 19096 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20111019/c4b3ca65/attachment.bin>
-------------- next part --------------



-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4624 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20111019/c4b3ca65/attachment.p7s>

Posted on the users mailing list.