This commit is contained in:
2026-02-25 00:43:00 +01:00
parent 59a2062f6a
commit b14e6e3fca
3 changed files with 63 additions and 19 deletions

View File

@@ -1,9 +1,11 @@
#lang racket/base
(define libao-async-mode 'ffi) ; 'ffi or 'scheme
(require "libao-ffi.rkt"
"libao-async-ffi.rkt"
;"libao-async.rkt"
(prefix-in fin: finalizer)
(prefix-in ffi: "libao-async-ffi.rkt")
(prefix-in scm: "libao-async.rkt")
ffi/unsafe
data/queue
)
@@ -16,11 +18,21 @@
ao-at-second
ao-bufsize-async
ao-clear-async
ao-set-async-mode!
ao-async-mode
)
(define devices (make-hash))
(define device-number 1)
(define (ao-set-async-mode! mode)
(if (or (eq? mode 'ffi) (eq? mode 'scheme))
(set! libao-async-mode mode)
(error "mode must be 'ffi or 'scheme"))
mode)
(define (ao-async-mode)
libao-async-mode)
(define-struct ao-handle (handle-num
[bits #:auto #:mutable]
@@ -89,7 +101,9 @@
(set-ao-handle-byte-format! handle endianness)
(set-ao-handle-rate! handle rate)
(set-ao-handle-channels! handle channels)
(set-ao-handle-async-player! handle (ao_create_async ao-device ao_play_ptr))
(if (eq? libao-async-mode 'ffi)
(set-ao-handle-async-player! handle (ffi:ao_create_async ao-device ao_play_ptr))
(set-ao-handle-async-player! handle (scm:ao_create_async ao-device)))
(hash-set! devices handle-num ao-device)
(fin:register-finalizer handle
(lambda (handle)
@@ -117,11 +131,15 @@
'warning-ao-device-already-closed
(begin
(set-ao-handle-closed! handle #t)
;(displayln "clear-async")(sleep 1.0)
(ao_clear_async (ao-handle-async-player handle))
;(displayln "stop-async")(sleep 1.0)
(ao_stop_async (ao-handle-async-player handle))
;(displayln "closing")(sleep 1.0)
(if (eq? libao-async-mode 'ffi)
(begin
(ffi:ao_clear_async (ao-handle-async-player handle))
(ffi:ao_stop_async (ao-handle-async-player handle))
)
(begin
(scm:ao_clear_async (ao-handle-async-player handle))
(scm:ao_stop_async (ao-handle-async-player handle))
))
(if (eq? ao-device #f)
'error-ao-device-non-existent
(let ((r (ao_close ao-device)))
@@ -132,12 +150,10 @@
)
))
;(displayln handle)(sleep 1.0)
(if (number? handle)
(let ((ao-device (hash-ref devices handle #f)))
(close-device #f ao-device))
(let ((handle-num (ao-handle-handle-num handle)))
;(displayln (ao-handle-closed handle))(sleep 1.0)
(let ((ao-device (hash-ref devices handle-num #f)))
(close-device handle ao-device))
)
@@ -166,7 +182,9 @@
(endianess (ao-handle-byte-format handle))
(buf-len (vector-length (car buffer)))
(audio-buf-len (* channels bytes-per-sample buf-len))
(audio (malloc 'atomic audio-buf-len))
(audio (if (eq? libao-async-mode 'ffi)
(malloc 'atomic audio-buf-len)
(malloc 'raw audio-buf-len)))
(get-sample (lambda (k channel)
(let ((chan-buf (list-ref buffer channel)))
(vector-ref chan-buf k))))
@@ -190,9 +208,16 @@
'filled))
))
(fill 0 0)
(ao_play_async (ao-handle-async-player handle) (exact->inexact at-time-in-s)
audio-buf-len
audio)
(if (eq? libao-async-mode 'ffi)
(ffi:ao_play_async (ao-handle-async-player handle)
(exact->inexact at-time-in-s)
audio-buf-len
audio)
(scm:ao_play_async (ao-handle-async-player handle)
(exact->inexact at-time-in-s)
audio-buf-len
audio)
)
)
;(let* ((handle-num (ao-handle-handle-num handle))
; (ao-device (hash-ref devices handle-num #f)))
@@ -203,13 +228,20 @@
)
(define (ao-at-second handle)
(ao_is_at_second_async (ao-handle-async-player handle)))
(if (eq? libao-async-mode 'ffi)
(ffi:ao_is_at_second_async (ao-handle-async-player handle))
(scm:ao_is_at_second_async (ao-handle-async-player handle))))
(define (ao-bufsize-async handle)
(ao_bufsize_async (ao-handle-async-player handle)))
(if (eq? libao-async-mode 'ffi)
(ffi:ao_bufsize_async (ao-handle-async-player handle))
(scm:ao_bufsize_async (ao-handle-async-player handle))))
(define (ao-clear-async handle)
(ao_clear_async (ao-handle-async-player handle)))
(if (eq? libao-async-mode 'ffi)
(ffi:ao_clear_async (ao-handle-async-player handle))
(scm:ao_clear_async (ao-handle-async-player handle))))
;(let* ((handle-num (ao-handle-handle-num handle))
; (ao-device (hash-ref devices handle-num #f)))