premature flac decode deletion solved

This commit is contained in:
2026-10-30 09:17:54 +01:00
parent 1d9899c0d2
commit b1c6e3976a
6 changed files with 139 additions and 26 deletions
+32 -8
View File
@@ -92,6 +92,15 @@
)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; goal : Decode FLAC frames and release the decoder on every exit.
; pre : Handle is open and is not already being read.
; post : The native decoder is released and reading is false.
; result : Void on completion; decoding and callback errors propagate.
; internals: reader processes frames and callbacks. dynamic-wind deletes
; the native decoder before clearing reading, so flac-stop
; cannot return while decoder cleanup is still in progress.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (flac-read handle)
(let* ((ffi-handler (flac-handle-ffi-decoder-handler handle))
(state (ffi-handler 'state))
@@ -103,10 +112,14 @@
(if (flac-handle-stop-reading handle)
(begin
(dbg-sound "handling stop at: ~a" (current-milliseconds))
(set-flac-handle-reading! handle #f)
'stopped-reading)
(let* ((st (ffi-handler 'state)))
(ffi-handler 'process-single)
(when (memq st '(ogg-error seek-error aborted
memory-allocation-error uninitialized))
(error 'flac-read "Decoder cannot continue in state ~a" st))
(unless (ffi-handler 'process-single)
(error 'flac-read "Could not decode FLAC frame ~a (state ~a)"
frame-nr (ffi-handler 'state)))
(unless (eq? state st)
(set! state st)
(dbg-sound "Now in state ~a (frame-nr = ~a) (int-state = ~a)"
@@ -131,14 +144,17 @@
)
(if (eq? st 'end-of-stream)
(begin
(set-flac-handle-reading! handle #f)
st)
st
(reader (+ frame-nr 1))))))
))
(reader 0)
; done reading, delete flac encoder
(ffi-handler 'delete)
(dynamic-wind
void
(λ () (reader 0) (void))
(λ ()
(dynamic-wind
void
(λ () (ffi-handler 'delete))
(λ () (set-flac-handle-reading! handle #f)))))
)
)
)
@@ -175,6 +191,14 @@
(ffi-handler 'seek-to-sample sample)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; goal : Stop decoding and safely release an idle FLAC decoder.
; pre : Handle was returned by flac-open.
; post : Reading and native decoder cleanup have finished.
; result : Void; repeated stops and stops after EOF are harmless.
; internals: An active reader observes stop-reading and owns cleanup.
; Otherwise the FFI handler performs idempotent deletion.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (flac-stop handle)
(let ((ct (current-milliseconds)))
(dbg-sound "requesting stop at: ~a" ct)