This commit is contained in:
2025-08-10 22:09:59 +02:00
parent ac7b46a0f7
commit 820c68a1aa
4 changed files with 131 additions and 10 deletions

View File

@@ -0,0 +1,68 @@
#lang racket/base
(require ffi/unsafe
ffi/unsafe/define
)
(provide ;_libao_pointer
AO-FMT-LITTLE AO-FMT-BIG AO-FMT-NATIVE
ao_initialize
ao_default_driver_id
ao_driver_id
ao_open_live
ao_play
ao_close
ao_shutdown
ao_append_option
make-ao_sample_format
(all-from-out ffi/unsafe)
)
(define-ffi-definer define-libao (ffi-lib "libao" '("3" "4" "5" #f)))
(define _libao-pointer (_cpointer 'ao_device))
(define-cstruct _ao_sample_format (
[bits _int] ; bits per sample
[rate _int] ; samples per second in a single channel
[channels _int] ; number of audio channels
[byte_format _int] ; byte ordering in sample, see "constants" below
[matrix _pointer] ; channel input matrix
))
(define-cstruct _ao_option (
[key _pointer]
[value _pointer]
[next _pointer] ; number of audio channels (list)
))
(define AO-FMT-LITTLE 1)
(define AO-FMT-BIG 2)
(define AO-FMT-NATIVE 4)
; void ao_initialize();
(define-libao ao_initialize (_fun -> _void))
; int ao_default_driver_id();
(define-libao ao_default_driver_id (_fun -> _int))
; ao_device* ao_open_live(int driver_id, ao_sample_format *format, ao_option *options);
(define-libao ao_open_live (_fun _int _pointer _pointer -> _libao-pointer))
; int ao_play(ao_device *device, char *output_samples, uint_32 num_bytes);
(define-libao ao_play (_fun _libao-pointer _pointer _ulong -> _int))
; int ao_close(ao_device *device);
(define-libao ao_close (_fun _libao-pointer -> _int))
; void ao_shutdown();
(define-libao ao_shutdown (_fun -> _int))
; int ao_append_option(ao_option **options, const char *key, const char *value);
(define-libao ao_append_option (_fun _pointer _pointer _pointer -> _int))
; int ao_driver_id(char *short_name);
(define-libao ao_driver_id (_fun _pointer -> _int))

View File

@@ -0,0 +1,62 @@
#lang racket/base
(require "libao-ffi.rkt")
;(require finalizer)
(provide ao-open-live
ao-play
ao-mk-format
ao-close
ao-default-driver-id
)
(define currently-open 0)
(define ao-device-handle 0)
(define ao-devices (make-hash))
(define (ao-mk-format bits rate channels byte-format . matrix)
(let ((bf (if (eq? byte-format 'little-endian)
AO-FMT-LITTLE
(if (eq? byte-format 'big-endian)
AO-FMT-BIG
AO-FMT-NATIVE))))
(let ((format (make-ao_sample_format bits rate channels bf #f)))
format)))
(define (ao-default-driver-id)
(ao_default_driver_id))
(define (ao-open-live driver-id sample-format . options)
(when (= currently-open 0)
(ao_initialize))
(let ((ao-device (ao_open_live driver-id sample-format #f)))
(set! currently-open (+ currently-open 1))
(when (eq? ao-device _cpointer/null)
(set! currently-open (- currently-open 1))
(when (= currently-open 0)
(ao_shutdown)))
(if (eq? ao-device _cpointer/null)
#f
(begin
(set! ao-device-handle (+ ao-device-handle 1))
(hash-set! ao-devices ao-device-handle ao-device)
ao-device-handle))))
(define (ao-close ao-handle)
(let ((ao-device (hash-ref ao-devices ao-handle #f)))
(when (eq? ao-device #f)
(error (format "Not a valid ao-handle ~a" ao-handle)))
(hash-remove! ao-devices ao-handle)
(let ((r (ao_close ao-device)))
(set! currently-open (- currently-open 1))
(when (= currently-open 0)
(ao_shutdown))
r)))
(define (ao-play)
#t)

1
libflac/libflac-ffi.rkt Normal file
View File

@@ -0,0 +1 @@
#lang racket/base

View File

@@ -1,12 +1,2 @@
#lang racket/base
(require ffi/unsafe
ffi/unsafe/define
)
(define-ffi-definer define-libao (ffi-lib "libao"))
(define _libao-pointer (_cpointer 'ao_device))
(define _