Files
racket-sound/libao.rkt
T

112 lines
3.1 KiB
Racket
Raw Normal View History

2025-08-10 22:09:59 +02:00
#lang racket/base
(require (prefix-in fin: finalizer)
2026-02-25 00:43:00 +01:00
(prefix-in ffi: "libao-async-ffi.rkt")
2026-02-22 16:08:02 +01:00
ffi/unsafe
ffi/unsafe/custodian
2026-02-22 16:08:02 +01:00
data/queue
2026-04-09 13:04:42 +02:00
"private/utils.rkt"
2026-02-22 16:08:02 +01:00
)
2025-08-10 22:09:59 +02:00
(provide ao-open-live
ao-play
ao-close
2026-02-22 17:37:15 +01:00
ao-at-second
2026-02-25 10:15:51 +01:00
ao-music-duration
2026-02-22 23:15:22 +01:00
ao-bufsize-async
2026-02-23 01:23:49 +01:00
ao-clear-async
2026-02-25 13:50:22 +01:00
ao-pause
2025-08-10 22:09:59 +02:00
)
2025-08-13 14:13:35 +02:00
(define device-number 1)
(define-struct ao-handle (handle-num
[bits #:auto #:mutable]
[bytes-per-sample #:auto #:mutable]
[byte-format #:auto #:mutable]
[channels #:auto #:mutable]
[rate #:auto #:mutable]
2026-02-22 17:37:15 +01:00
[async-player #:auto #:mutable]
2026-02-24 14:05:46 +01:00
[closed #:auto #:mutable]
2025-08-13 14:13:35 +02:00
)
#:auto-value #f
)
(define (bytes-for-bits bits)
(/ bits 8))
2025-08-10 22:09:59 +02:00
(define (ao-open-live bits rate channels byte-format)
(let ((handle (make-ao-handle device-number)))
2025-08-10 22:09:59 +02:00
(fin:register-finalizer handle
(lambda (handle)
(ao-close handle)))
2025-08-13 14:13:35 +02:00
(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)
(set-ao-handle-rate! handle rate)
(let ((player (ffi:ao_create_async bits rate channels byte-format)))
(set-ao-handle-async-player! handle player)
(if (eq? player #f)
(begin
(set-ao-handle-closed! handle #t)
2025-08-13 14:13:35 +02:00
handle)
(begin
(set-ao-handle-closed! handle #f)
handle
2026-02-24 14:57:29 +01:00
)
)
)
)
2026-02-24 14:57:29 +01:00
)
2025-08-13 14:13:35 +02:00
(define (ao-close handle)
(unless (eq? (ao-handle-async-player handle) #f)
(ffi:ao_stop_async (ao-handle-async-player handle))
(set-ao-handle-async-player! handle #f)
)
)
2025-08-13 14:13:35 +02:00
2026-04-10 08:32:55 +02:00
(define (ao-play handle at-time-in-s music-duration-s buffer buf-len buf-type)
2025-08-13 14:13:35 +02:00
(let* ((bytes-per-sample (ao-handle-bytes-per-sample handle))
(bits (ao-handle-bits handle))
2026-04-10 08:32:55 +02:00
(rate (ao-handle-rate handle))
2025-08-13 14:13:35 +02:00
(channels (ao-handle-channels handle))
(endianess (ao-handle-byte-format handle))
(buf-info (ffi:make-BufferInfo_t buf-type bits rate channels endianess))
2025-08-13 14:13:35 +02:00
)
(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)
2026-02-22 16:08:02 +01:00
)
)
2026-02-25 13:47:51 +01:00
(define (ao-pause handle pause)
2026-04-10 08:32:55 +02:00
(ffi:ao_pause_async (ao-handle-async-player handle) (if (eq? pause #f) 0 1))
)
2026-02-25 13:47:51 +01:00
2026-02-22 17:37:15 +01:00
(define (ao-at-second handle)
2026-04-10 08:32:55 +02:00
(ffi:ao_is_at_second_async (ao-handle-async-player handle))
)
2026-02-25 10:15:51 +01:00
(define (ao-music-duration handle)
2026-04-10 08:32:55 +02:00
(ffi:ao_music_duration_async (ao-handle-async-player handle))
)
2026-02-25 10:15:51 +01:00
2026-02-22 23:15:22 +01:00
(define (ao-bufsize-async handle)
2026-04-10 08:32:55 +02:00
(ffi:ao_bufsize_async (ao-handle-async-player handle))
)
2026-02-22 23:15:22 +01:00
2026-02-23 01:23:49 +01:00
(define (ao-clear-async handle)
2026-04-10 08:32:55 +02:00
(ffi:ao_clear_async (ao-handle-async-player handle))
)
2026-02-23 01:23:49 +01:00
2025-08-10 22:09:59 +02:00