* flac support

* taglib support
It is possible to play some music already.
This commit is contained in:
2025-08-13 14:13:35 +02:00
parent 820c68a1aa
commit ad7f1345ad
16 changed files with 1522 additions and 35 deletions

View File

@@ -2,6 +2,7 @@
(require ffi/unsafe
ffi/unsafe/define
setup/dirs
)
(provide ;_libao_pointer
@@ -15,10 +16,22 @@
ao_shutdown
ao_append_option
make-ao_sample_format
ao_sample_format-bits
ao_sample_format-rate
ao_sample_format-channels
ao_sample_format-byte_format
ao_sample_format-matrix
(all-from-out ffi/unsafe)
)
(define-ffi-definer define-libao (ffi-lib "libao" '("3" "4" "5" #f)))
(define-ffi-definer define-libao
(ffi-lib "libao" '("3" "4" "5" #f)
#:get-lib-dirs (lambda ()
(cons (build-path ".") (get-lib-search-dirs)))
#:fail (lambda ()
(ffi-lib (get-lib-path "libao-4.dll")))
))
(define _libao-pointer (_cpointer 'ao_device))
@@ -50,7 +63,7 @@
(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))
(define-libao ao_play (_fun _libao-pointer _pointer _uint32 -> _int))
; int ao_close(ao_device *device);
(define-libao ao_close (_fun _libao-pointer -> _int))

View File

@@ -1,7 +1,8 @@
#lang racket/base
(require "libao-ffi.rkt")
;(require finalizer)
(require "libao-ffi.rkt"
(prefix-in fin: finalizer)
ffi/unsafe)
(provide ao-open-live
ao-play
@@ -10,9 +11,34 @@
ao-default-driver-id
)
(define currently-open 0)
(define ao-device-handle 0)
(define ao-devices (make-hash))
(define devices (make-hash))
(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]
)
#:auto-value #f
)
(ao_initialize)
(define libao-plumber-flus-handle
(plumber-add-flush! (current-plumber)
(lambda (my-handle)
(hash-for-each devices
(lambda (handle-num device)
(ao-close handle-num)))
(set! devices (make-hash))
(ao_shutdown)
(plumber-flush-handle-remove! my-handle)
)))
(define (ao-mk-format bits rate channels byte-format . matrix)
(let ((bf (if (eq? byte-format 'little-endian)
@@ -23,39 +49,114 @@
(let ((format (make-ao_sample_format bits rate channels bf #f)))
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)
(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))))
(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)
(let ((handle-num device-number))
(set! device-number (+ device-number 1))
(let ((handle (ao-handle handle-num)))
(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)
(hash-set! devices handle-num ao-device)
(fin:register-finalizer handle
(lambda (handle)
(ao-close handle)))
handle))
))
)))
(define (ao-close handle)
(if (number? handle)
(let ((ao-device (hash-ref devices handle #f)))
(unless (eq? ao-device #f)
(let ((r (ao_close ao-device)))
(when (= r 0)
(printf "Unexpected: cannot close ao-device"))))
'internally-closed)
(let ((handle-num (ao-handle-handle-num handle)))
(let ((ao-device (hash-ref devices handle-num #f)))
(if (eq? ao-device #f)
'error-ao-device-non-existent
(let ((r (ao_close ao-device)))
(hash-remove! devices handle-num)
(if (= r 0)
'error-closing-ao-device
'ok)))))))
(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)
(define count 0)
(define (abs x) (if (>= x 0) x (* x -1)))
(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 buffer)
(let* ((bytes-per-sample (ao-handle-bytes-per-sample handle))
(bits (ao-handle-bits handle))
(channels (ao-handle-channels handle))
(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))
(get-sample (lambda (k channel)
(let ((chan-buf (list-ref buffer channel)))
(vector-ref chan-buf k))))
)
;(displayln (format "bps: ~a, buf-len: ~a, endianess: ~a, channels: ~a, bits ~a"
; bytes-per-sample buf-len endianess channels bits))
(letrec ((i 0)
(fill (lambda (k channel)
(if (< k buf-len)
(if (< channel channels)
(let* ((sample (get-sample k channel))
(bytes (make-sample-bytes sample bytes-per-sample endianess))
)
(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* ((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))))))