libao backend no longer necessary, all playing done via ao-play-async

This commit is contained in:
2026-04-15 09:40:46 +02:00
parent fdba3ad8f8
commit 296e4bb687
3 changed files with 40 additions and 186 deletions

View File

@@ -57,8 +57,8 @@
;extern int ao_async_version() ;extern int ao_async_version()
(define-libao-async ao_async_version (_fun -> _int)) (define-libao-async ao_async_version (_fun -> _int))
;extern void *ao_create_async(void *ao_device, ); ;extern void *ao_create_async(int bits, int rate, int channel, int byte_format);
(define-libao-async ao_create_async(_fun _pointer _fpointer -> _libao-async-handle-pointer)) (define-libao-async ao_create_async(_fun _int _int _int _Endian_t -> _libao-async-handle-pointer))
;extern void ao_stop_async(void *handle); ;extern void ao_stop_async(void *handle);
(define-libao-async ao_stop_async(_fun _libao-async-handle-pointer -> _void)) (define-libao-async ao_stop_async(_fun _libao-async-handle-pointer -> _void))

219
libao.rkt
View File

@@ -1,7 +1,6 @@
#lang racket/base #lang racket/base
(require "libao-ffi.rkt" (require (prefix-in fin: finalizer)
(prefix-in fin: finalizer)
(prefix-in ffi: "libao-async-ffi.rkt") (prefix-in ffi: "libao-async-ffi.rkt")
ffi/unsafe ffi/unsafe
ffi/unsafe/custodian ffi/unsafe/custodian
@@ -11,9 +10,7 @@
(provide ao-open-live (provide ao-open-live
ao-play ao-play
ao-mk-format
ao-close ao-close
ao-default-driver-id
ao-at-second ao-at-second
ao-music-duration ao-music-duration
ao-bufsize-async ao-bufsize-async
@@ -21,7 +18,6 @@
ao-pause ao-pause
) )
(define devices (make-hash))
(define device-number 1) (define device-number 1)
(define-struct ao-handle (handle-num (define-struct ao-handle (handle-num
@@ -36,152 +32,44 @@
#:auto-value #f #:auto-value #f
) )
(ao_initialize)
(define (ao-finalizer devices) (define (bytes-for-bits bits)
(hash-for-each devices (/ bits 8))
(lambda (handle-num device)
(ao-close handle-num)))
(set! devices (make-hash))
(ao_shutdown)
)
(define custodian-finalizer (define (ao-open-live bits rate channels byte-format)
(register-custodian-shutdown devices (let ((handle (make-ao-handle device-number)))
(λ (devices)
(ao-finalizer devices))
#:at-exit? #t)
)
#| (fin:register-finalizer handle
(define libao-plumber-flush-handle (lambda (handle)
(plumber-add-flush! (current-plumber) (ao-close handle)))
(lambda (my-handle)
(hash-for-each devices
(lambda (handle-num device)
(dbg-sound "closing ao handle ~a" handle-num)
(ao-close handle-num)))
(set! devices (make-hash))
(dbg-sound "shutting down ao")
(ao_shutdown)
(plumber-flush-handle-remove! my-handle)
)))
|#
(set-ao-handle-bits! handle bits)
(set-ao-handle-bytes-per-sample! handle (bytes-for-bits bits))
(set-ao-handle-byte-format! handle byte-format)
(set-ao-handle-channels! handle channels)
(define (ao-mk-format bits rate channels byte-format . matrix) (set-ao-handle-rate! handle rate)
(let ((bf (if (eq? byte-format 'little-endian)
AO-FMT-LITTLE (let ((player (ffi:ao_create_async bits rate channels byte-format)))
(if (eq? byte-format 'big-endian) (set-ao-handle-async-player! handle player)
AO-FMT-BIG (if (eq? player #f)
AO-FMT-NATIVE)))) (begin
(let ((format (make-ao_sample_format bits rate channels bf #f))) (set-ao-handle-closed! handle #t)
format)))
(define (ao-endianness->symbol e)
(if (= e AO-FMT-LITTLE)
'little-endian
(if (= e AO-FMT-BIG)
'big-endian
'native)))
(define (ao-default-driver-id)
(ao_default_driver_id))
(define (ao-open-live driver-id sample-format . options)
(let ((id (if (eq? driver-id #f) (ao-default-driver-id) driver-id)))
(let ((ao-device (ao_open_live id sample-format #f)))
(if (eq? ao-device #f)
(let ((handle (ao-handle -1)))
handle) handle)
(let ((handle-num device-number)) (begin
(set! device-number (+ device-number 1)) (set-ao-handle-closed! handle #f)
(let ((handle (ao-handle handle-num))) handle
(let* ((bits (ao_sample_format-bits sample-format))
(bytes-per-sample (inexact->exact (round (/ bits 8))))
(channels (ao_sample_format-channels sample-format))
(endianness (ao-endianness->symbol
(ao_sample_format-byte_format sample-format)))
(rate (ao_sample_format-rate sample-format))
)
(set-ao-handle-bits! handle bits)
(set-ao-handle-bytes-per-sample! handle bytes-per-sample)
(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 (ffi:ao_create_async ao-device ao_play_ptr))
(hash-set! devices handle-num ao-device)
(fin:register-finalizer handle
(lambda (handle)
(ao-close handle)))
handle))
))
)))
(define (ao-close handle)
(define (close-device handle ao-device)
(if (eq? handle #f)
(begin
(if (eq? ao-device #f)
'error-ao-device-non-existent
(let ((r (ao_close ao-device)))
(if (= r 0)
'error-closing-ao-device
'ok
)
)
)
)
(if (ao-handle-closed handle)
'warning-ao-device-already-closed
(begin
(set-ao-handle-closed! handle #t)
(ffi:ao_clear_async (ao-handle-async-player handle))
(ffi:ao_stop_async (ao-handle-async-player handle))
(if (eq? ao-device #f)
'error-ao-device-non-existent
(let ((r (ao_close ao-device)))
(if (= r 0)
'error-closing-ao-device
'ok)))
)
) )
))
(if (number? handle)
(let ((ao-device (hash-ref devices handle #f)))
(unless (eq? ao-device #f)
(dbg-sound "Closing ao device ~a" ao-device)
(close-device #f ao-device)
(hash-remove! devices handle)))
(let ((handle-num (ao-handle-handle-num handle)))
(let ((ao-device (hash-ref devices handle-num #f)))
(unless (eq? ao-device #f)
(dbg-sound "ao-device = ~a" ao-device)
(close-device handle ao-device)
(hash-remove! devices handle-num)))
) )
) )
)
) )
(define (ao-close handle)
(define count 0) (unless (eq? (ao-handle-async-player handle) #f)
(define (abs x) (if (>= x 0) x (* x -1))) (ffi:ao_stop_async (ao-handle-async-player handle))
(set-ao-handle-async-player! handle #f)
(define (make-sample-bytes sample bytes-per-sample endianess) )
(letrec ((mk (lambda (i d) )
(if (< i bytes-per-sample)
(cons (bitwise-and d 255)
(mk (+ i 1) (arithmetic-shift d -8)))
'()))))
(let ((bytes (mk 0 sample)))
(if (eq? endianess 'big-endian)
(reverse bytes)
bytes))))
(define (ao-play handle at-time-in-s music-duration-s buffer buf-len buf-type) (define (ao-play handle at-time-in-s music-duration-s buffer buf-len buf-type)
(let* ((bytes-per-sample (ao-handle-bytes-per-sample handle)) (let* ((bytes-per-sample (ao-handle-bytes-per-sample handle))
@@ -189,39 +77,14 @@
(rate (ao-handle-rate handle)) (rate (ao-handle-rate handle))
(channels (ao-handle-channels handle)) (channels (ao-handle-channels handle))
(endianess (ao-handle-byte-format handle)) (endianess (ao-handle-byte-format handle))
;(audio-buf-len (* channels bytes-per-sample buf-len)) (buf-info (ffi:make-BufferInfo_t buf-type bits rate channels endianess))
;(audio (malloc 'atomic audio-buf-len))
;(get-sample (lambda (k channel)
; (let ((chan-buf (list-ref buffer channel)))
; (vector-ref chan-buf k))))
) )
;(letrec ((i 0) (ffi:ao_play_async (ao-handle-async-player handle)
; (fill (lambda (k channel) (exact->inexact at-time-in-s)
; (if (< k buf-len) (exact->inexact music-duration-s)
; (if (< channel channels) buf-len
; (let* ((sample (get-sample k channel)) buffer
; (bytes (make-sample-bytes sample bytes-per-sample endianess)) buf-info)
; )
; (for-each (lambda (byte)
; (ptr-set! audio _byte i byte)
; (set! i (+ i 1)))
; bytes)
; ;; process sample
; (fill k (+ channel 1)))
; (fill (+ k 1) 0))
; 'filled))
; ))
; (fill 0 0)
(let ((buf-info (ffi:make-BufferInfo_t buf-type bits rate channels endianess)))
;(displayln "Calling ao_play_async")
(ffi:ao_play_async (ao-handle-async-player handle)
(exact->inexact at-time-in-s)
(exact->inexact music-duration-s)
buf-len
buffer
buf-info)
)
) )
) )
@@ -245,12 +108,4 @@
(ffi:ao_clear_async (ao-handle-async-player handle)) (ffi:ao_clear_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)
; (error "No device for this handle")
; (ao_play ao-device audio audio-buf-len))))))

View File

@@ -35,8 +35,7 @@
(duration (hash-ref frame 'duration)) (duration (hash-ref frame 'duration))
) )
(when (eq? ao-h #f) (when (eq? ao-h #f)
(let ((fmt (ao-mk-format bits-per-sample rate channels 'big-endian))) (set! ao-h (ao-open-live bits-per-sample rate channels 'big-endian)))
(set! ao-h (ao-open-live #f fmt))))
;(displayln 'ao-play) ;(displayln 'ao-play)
(ao-play ao-h second duration buffer buf-len 'flac) (ao-play ao-h second duration buffer buf-len 'flac)
;(displayln 'done) ;(displayln 'done)