#lang racket
(require (planet gcr/libvorbisfile))

;; Sorry! Replace this paragraph of junk with
;;(require (planet gcr/openal))
 (dynamic-require '(planet gcr/openal)
(define open-device
 (dynamic-require '(planet gcr/openal)
(define create-context
 (dynamic-require '(planet gcr/openal)
(define gen-sources
 (dynamic-require '(planet gcr/openal)
(define stream-port-to-source
 (dynamic-require '(planet gcr/openal)
(define play-source
 (dynamic-require '(planet gcr/openal)
(define set-current-context
 (dynamic-require '(planet gcr/openal)
(define destroy-context!
 (dynamic-require '(planet gcr/openal)
(define close-device!
 (dynamic-require '(planet gcr/openal)

;; Initialize OpenAL (see the OpenAL guide)
(define device (open-device #f))
(define context (create-context device))
(set-current-context context)

(define filename (vector-ref (current-command-line-arguments) 0))
(printf "Playing file ~a\n" filename)

;; Open the file!
(define m (open-vorbis-file filename))

(printf "Rate: ~a Channels: ~a Length: ~a sec\n"
        (vorbis-frequency m)
        (vorbis-channels m)
        (vorbis-length-time m))

;; To read the binary PCM data, we make a port that supplies us with
;; the binary decompressed data.
(define vorbis-binary (make-vorbis-input-port m 0 2 1))

;; Make our OpenAL source
(define source (car (gen-sources 1)))

;; Start streaming
(define stream-thread
  (stream-port-to-source vorbis-binary
                         (vorbis-frequency m)))

;; Start playing
(play-source source)

;; Wait until we're fisished playing
(thread-wait stream-thread)

;; Clean up
(set-current-context #f)
(destroy-context! context)
(close-device! device)
(close-vorbis-file! m)