vorbisfile-openal-test.rkt
#lang racket
(require (planet gcr/libvorbisfile))

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


;; 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
                         source
                         AL_FORMAT_STEREO16
                         (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)