From fdba3ad8f8b6795dea49aed9d5a6683189548a49 Mon Sep 17 00:00:00 2001 From: Hans Dijkema Date: Tue, 14 Apr 2026 15:07:12 +0200 Subject: [PATCH] seeking support --- flac-decoder.rkt | 14 ++++++++++++++ libflac-ffi.rkt | 2 +- play-test.rkt | 4 ++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/flac-decoder.rkt b/flac-decoder.rkt index c52d25b..142617c 100644 --- a/flac-decoder.rkt +++ b/flac-decoder.rkt @@ -10,6 +10,7 @@ flac-read-meta flac-stream-state flac-stop + flac-seek (all-from-out "flac-definitions.rkt") kinds last-buffer last-buf-len @@ -132,6 +133,19 @@ (flac-handle-stream-info handle)) #f))) + (define (flac-seek handle percentage) + (dbg-sound "seek to percentage ~a" percentage) + (let ((ffi-handler (flac-handle-ffi-decoder-handler handle))) + (let ((total-samples (flac-total-samples handle))) + (unless (eq? total-samples #f) + (let ((sample (inexact->exact (round (* (exact->inexact (/ percentage 100.0)) total-samples))))) + (ffi-handler 'seek-to-sample sample)) + ) + ) + ) + ) + + (define (flac-stop handle) (let ((ct (current-milliseconds))) (dbg-sound "requesting stop at: ~a" ct) diff --git a/libflac-ffi.rkt b/libflac-ffi.rkt index 7afb243..a402459 100644 --- a/libflac-ffi.rkt +++ b/libflac-ffi.rkt @@ -596,7 +596,7 @@ [(eq? cmd 'process-meta-data) (process-meta-data (car args))] [(eq? cmd 'process-write-data) (process-write-data (car args))] [(eq? cmd 'errno) error-no] - + [(eq? cmd 'seek-to-sample) (seek-to-sample (car args))] [(eq? cmd 'file) flac-file] diff --git a/play-test.rkt b/play-test.rkt index 5a26f98..6c12c73 100644 --- a/play-test.rkt +++ b/play-test.rkt @@ -57,7 +57,7 @@ (let* ((buf-size (ao-bufsize-async ao-h)) (buf-seconds (exact->inexact (/ buf-size bytes-per-sample-all-channels rate)))) (second-printer buf-seconds) - (when (> buf-seconds 5) + (when (> buf-seconds 30) (letrec ((waiter (λ () (let ((buf-seconds-left (exact->inexact (/ (ao-bufsize-async ao-h) @@ -66,7 +66,7 @@ (if (< buf-seconds-left 2.0) (displayln (format "Seconds in buffer left: ~a" buf-seconds-left)) (begin - (sleep 0.5) + (sleep 1) (second-printer buf-seconds) (waiter))))) ))