This commit is contained in:
2026-02-22 17:37:15 +01:00
parent 869fa8739f
commit 2cc96f30ae
6 changed files with 138 additions and 56 deletions

View File

@@ -1,6 +1,7 @@
#lang racket/base
(require "libao-ffi.rkt"
"libao-async-ffi.rkt"
(prefix-in fin: finalizer)
ffi/unsafe
data/queue
@@ -11,6 +12,7 @@
ao-mk-format
ao-close
ao-default-driver-id
ao-at-second
)
(define devices (make-hash))
@@ -23,8 +25,7 @@
[byte-format #:auto #:mutable]
[channels #:auto #:mutable]
[rate #:auto #:mutable]
[buffer #:auto #:mutable]
[player #:auto #:mutable]
[async-player #:auto #:mutable]
)
#:auto-value #f
)
@@ -84,36 +85,7 @@
(set-ao-handle-byte-format! handle endianness)
(set-ao-handle-rate! handle rate)
(set-ao-handle-channels! handle channels)
(let ((buf (make-queue)))
(set-ao-handle-buffer! handle buf)
(set-ao-handle-player! handle
(thread (λ ()
(let ((samples-per-time 10)
(sample 0)
)
(define (play)
(displayln (format "Queue-length: ~a" (queue-length buf)))
(if (queue-empty? buf)
(begin
(sleep 0.1)
(play))
(let ((front (dequeue! buf)))
(if (eq? (car front) 'close)
'closed
(let ((audio (cadr front))
(audio-buf-len (caddr front)))
(ao_play ao-device audio audio-buf-len)
(play))
)
)
)
)
(play))
)
#:pool 'own
)
)
)
(set-ao-handle-async-player! handle (ao_create_async ao-device))
(hash-set! devices handle-num ao-device)
(fin:register-finalizer handle
(lambda (handle)
@@ -131,15 +103,7 @@
(printf "Unexpected: cannot close ao-device"))))
'internally-closed)
(let ((handle-num (ao-handle-handle-num handle)))
(let ((q (ao-handle-buffer handle)))
(letrec ((emptier (λ () (if (queue-empty? q)
'done
(begin
(dequeue! q)
(emptier))))))
(emptier)
(enqueue! q (list 'close #f #f))
))
(ao_stop_async (ao-handle-async-player handle))
(let ((ao-device (hash-ref devices handle-num #f)))
(if (eq? ao-device #f)
'error-ao-device-non-existent
@@ -164,7 +128,7 @@
(reverse bytes)
bytes))))
(define (ao-play handle buffer)
(define (ao-play handle at-time-in-s buffer)
(let* ((bytes-per-sample (ao-handle-bytes-per-sample handle))
(bits (ao-handle-bits handle))
(channels (ao-handle-channels handle))
@@ -195,11 +159,14 @@
'filled))
))
(fill 0 0)
(enqueue! (ao-handle-buffer handle) (list 'play-buf audio audio-buf-len))
(ao_play_async (ao-handle-async-player handle) (exact->inexact at-time-in-s) audio-buf-len audio)
)
)
)
(define (ao-at-second handle)
(ao_is_at_second_async (ao-handle-async-player handle)))
;(let* ((handle-num (ao-handle-handle-num handle))
; (ao-device (hash-ref devices handle-num #f)))
; (if (eq? ao-device #f)