reuse mp3 ffi handlers

This commit is contained in:
2026-04-22 15:01:54 +02:00
parent 25ba461196
commit ba263bd892
+18 -5
View File
@@ -2,7 +2,9 @@
(require ffi/unsafe (require ffi/unsafe
"libmpg123-ffi.rkt" "libmpg123-ffi.rkt"
"private/utils.rkt") "private/utils.rkt"
(prefix-in fin: finalizer)
)
(provide mp3-open (provide mp3-open
mp3-valid? mp3-valid?
@@ -26,6 +28,13 @@
;; Functions to do the good stuff ;; Functions to do the good stuff
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define mp3-handles '())
(register-finalizer mp3-handles
(λ (handles)
(for-each (λ (handle)
(handle 'delete)) handles)))
(define (mp3-valid? mp3-file) (define (mp3-valid? mp3-file)
#t) #t)
@@ -43,8 +52,13 @@
(define (mp3-open mp3-file* cb-stream-info cb-audio) (define (mp3-open mp3-file* cb-stream-info cb-audio)
(let ((mp3-file (if (path? mp3-file*) (path->string mp3-file*) mp3-file*))) (let ((mp3-file (if (path? mp3-file*) (path->string mp3-file*) mp3-file*)))
(if (file-exists? mp3-file) (if (file-exists? mp3-file)
(let ((handler (mpg123-ffi-decoder-handler))) (let ((handler (if (null? mp3-handles)
(handler 'new) (let ((h (mpg123-ffi-decoder-handler)))
(h 'new)
h)
(let ((h (car mp3-handles)))
(set! mp3-handles (cdr mp3-handles))
h))))
(handler 'init mp3-file) (handler 'init mp3-file)
(let ((h (make-mp3-handle handler (let ((h (make-mp3-handle handler
cb-stream-info cb-stream-info
@@ -81,7 +95,6 @@
(let loop () (let loop ()
(if (eq? (mp3-handle-stop handle) #t) (if (eq? (mp3-handle-stop handle) #t)
(begin (begin
(newline)
(dbg-sound "Stopping mp3 decoding") (dbg-sound "Stopping mp3 decoding")
(set-mp3-handle-reading! handle #f) (set-mp3-handle-reading! handle #f)
'stopped-reading 'stopped-reading
@@ -102,7 +115,7 @@
) )
)) ))
(ffi-handler 'close) (ffi-handler 'close)
(ffi-handler 'delete) (set! mp3-handles (cons ffi-handler mp3-handles))
) )
) )