Files

175 lines
6.5 KiB
Racket
Raw Permalink Normal View History

2025-08-13 14:13:35 +02:00
#lang racket/base
2026-04-07 13:46:34 +02:00
(require "libao.rkt"
2026-04-21 18:01:33 +02:00
"audio-decoder.rkt"
2026-04-10 08:32:55 +02:00
simple-log
2026-04-21 18:01:33 +02:00
"private/utils.rkt"
2026-04-22 13:28:37 +02:00
racket-sprintf
2026-04-28 15:04:12 +02:00
racket/runtime-path
2026-02-23 08:20:04 +01:00
;data/queue
;racket-sound
2025-08-13 14:13:35 +02:00
)
2026-04-28 15:04:12 +02:00
(define-runtime-path tests "tests")
2026-02-23 00:58:54 +01:00
(define test-file3 #f)
2026-04-22 19:08:57 +02:00
(define test-file4 #f)
2026-04-16 09:13:15 +02:00
(define test-file3-id 3)
2026-04-22 19:08:57 +02:00
(define test-file4-id 4)
2026-02-23 00:58:54 +01:00
(let ((os (system-type 'os)))
(when (eq? os 'unix)
(set! test-file3 (build-path tests "idyll.mp3"))
(set! test-file4 (build-path tests "idyll.flac"))
2026-04-22 13:28:37 +02:00
)
2026-02-23 00:58:54 +01:00
(when (eq? os 'windows)
2026-05-03 14:16:32 +02:00
(set! test-file3 (build-path tests "idyll.mp3"))
2026-05-03 10:13:28 +02:00
(set! test-file4 (build-path tests "idyll.flac"))
2026-02-23 00:58:54 +01:00
)
)
2025-08-13 14:13:35 +02:00
2026-02-23 00:58:54 +01:00
;(define fmt (ao-mk-format 24 48000 2 'big-endian))
;(define ao-h (ao-open-live #f fmt))
2025-08-13 14:13:35 +02:00
2026-02-22 17:37:15 +01:00
(define current-seconds 0)
2026-02-23 00:58:54 +01:00
(define ao-h #f)
2026-04-22 19:08:57 +02:00
(define current-file-id -1)
(define current-audio-h #f)
2026-02-22 17:37:15 +01:00
2026-04-28 15:04:12 +02:00
(define current-bits -1)
(define current-rate -1)
(define current-channels -1)
2026-04-10 08:32:55 +02:00
(sl-log-to-display)
(define wav-output-file #f)
(define seeked #f)
2026-04-10 08:32:55 +02:00
2026-04-22 13:28:37 +02:00
(define (audio-play type ao-type handle buf-info buffer buf-len)
2026-05-03 10:13:28 +02:00
;(dbg-sound "~a ~a ~a ~a ~a" type ao-type handle buf-info buf-len)
2026-04-21 18:01:33 +02:00
(let* ((sample (hash-ref buf-info 'sample))
(rate (hash-ref buf-info 'sample-rate))
2026-02-22 17:37:15 +01:00
(second (/ (* sample 1.0) (* rate 1.0)))
2026-04-21 18:01:33 +02:00
(bits-per-sample (hash-ref buf-info 'bits-per-sample))
2026-02-22 23:15:08 +01:00
(bytes-per-sample (/ bits-per-sample 8))
2026-04-21 18:01:33 +02:00
(channels (hash-ref buf-info 'channels))
2026-02-22 23:15:08 +01:00
(bytes-per-sample-all-channels (* channels bytes-per-sample))
2026-04-21 18:01:33 +02:00
(duration (hash-ref buf-info 'duration))
(cond-seek (λ ()
(when (= (round current-seconds) 10)
(when (and (= current-file-id 3) (not seeked))
(set! seeked #t)
2026-04-28 15:04:12 +02:00
(let ((perc (exact->inexact (* (/ (- duration 15) duration) 100.0))))
(info-sound "Seeking to ~a%" perc)
(audio-seek current-audio-h perc))))))
2026-05-03 10:13:28 +02:00
(cond-volume (λ ()
(when (= (round current-seconds) 20)
(ao-set-volume! ao-h 70.0))
(when (= (round current-seconds) 25)
(ao-set-volume! ao-h 30))
(when (= (round current-seconds) 30)
(ao-set-volume! ao-h 100))
(when (= (round current-seconds) 35)
(ao-set-volume! ao-h 150))
(when (= (round current-seconds) 40)
(ao-set-volume! ao-h 100))))
2026-02-22 17:37:15 +01:00
)
2026-05-03 14:16:32 +02:00
2026-04-28 15:04:12 +02:00
(when (not (eq? ao-h #f))
(when (not (and
(= current-bits bits-per-sample)
(= current-rate rate)
(= current-channels channels)))
(ao-close ao-h)
(set! ao-h #f)))
2026-04-22 13:28:37 +02:00
;(displayln buf-info)
2026-02-23 00:58:54 +01:00
(when (eq? ao-h #f)
2026-04-28 15:04:12 +02:00
(info-sound "Opening ao handle")
(info-sound "bits-per-sample: ~a" bits-per-sample)
(info-sound "rate : ~a" rate)
(info-sound "channels : ~a" channels)
(info-sound "endian : ~a" 'native-endian)
(info-sound "(optional) file: ~a" wav-output-file)
(sync-log-sound)
(set! ao-h (ao-open-file bits-per-sample rate channels 'native-endian wav-output-file))
(set! current-bits bits-per-sample)
(set! current-rate rate)
(set! current-channels channels)
(info-sound "ao bits per sample: ~a" (ao-device-bits ao-h))
(sync-log-sound)
)
2026-04-10 08:32:55 +02:00
;(displayln 'ao-play)
2026-04-28 15:04:12 +02:00
;(dbg-sound "Playing audio at ~a" second)
;(sync-log-sound)
2026-04-22 19:08:57 +02:00
(ao-play ao-h current-file-id second duration buffer buf-len ao-type)
2026-04-22 13:28:37 +02:00
(set! duration (inexact->exact (round duration)))
2026-04-10 08:32:55 +02:00
;(displayln 'done)
(let ((second-printer (λ (buf-seconds)
2026-02-22 23:15:08 +01:00
(let ((s (inexact->exact (round (ao-at-second ao-h)))))
2026-02-23 15:32:50 +01:00
(unless (= s current-seconds)
2026-02-22 23:15:08 +01:00
(set! current-seconds s)
2026-02-23 00:58:54 +01:00
(let ((minutes (quotient s 60))
2026-02-23 01:17:40 +01:00
(seconds (remainder s 60))
(tminutes (quotient duration 60))
(tseconds (remainder duration 60))
2026-04-21 10:10:29 +02:00
(volume (ao-volume ao-h))
2026-02-23 01:17:40 +01:00
)
2026-04-22 13:28:37 +02:00
(info-sound
(sprintf "At time: %02d:%02d (%02d:%02d) - %d - volume: %d"
2026-02-23 01:17:40 +01:00
minutes seconds
tminutes tseconds
2026-04-10 08:32:55 +02:00
buf-seconds
2026-04-21 10:10:29 +02:00
volume
2026-02-23 01:17:40 +01:00
))))))))
2026-02-22 23:15:08 +01:00
(let* ((buf-size (ao-bufsize-async ao-h))
(buf-seconds (exact->inexact (/ buf-size bytes-per-sample-all-channels rate))))
2026-04-10 08:32:55 +02:00
(second-printer buf-seconds)
(cond-seek)
2026-05-03 10:13:28 +02:00
(cond-volume)
(when (> buf-seconds 5)
2026-02-22 23:15:08 +01:00
(letrec ((waiter (λ ()
(let ((buf-seconds-left (exact->inexact
(/ (ao-bufsize-async ao-h)
bytes-per-sample-all-channels
rate))))
(if (< buf-seconds-left 3.0)
2026-04-22 13:28:37 +02:00
(info-sound "Seconds in buffer left: ~a" buf-seconds-left)
2026-02-22 23:15:08 +01:00
(begin
2026-04-21 10:10:29 +02:00
(sleep 0.5)
2026-04-10 08:32:55 +02:00
(second-printer buf-seconds)
2026-05-03 10:13:28 +02:00
(cond-volume)
(cond-seek)
2026-02-22 23:15:08 +01:00
(waiter)))))
))
(waiter))))
)
2026-02-22 17:37:15 +01:00
)
)
2025-08-13 14:13:35 +02:00
2026-04-22 13:28:37 +02:00
(define (audio-meta type ao-type handle meta)
(dbg-sound "type: ~a" type)
(dbg-sound "ao-type: ~a" ao-type)
(dbg-sound "meta: ~a" meta))
2025-08-13 14:13:35 +02:00
2026-02-23 15:32:50 +01:00
(define (play)
2026-02-23 19:00:25 +01:00
(set! ao-h #f)
2026-04-21 18:01:33 +02:00
(let ((audio-h (audio-open test-file3 audio-meta audio-play)))
2026-04-22 19:08:57 +02:00
(set! current-file-id test-file3-id)
(set! current-audio-h audio-h)
2026-04-21 18:01:33 +02:00
(audio-read audio-h)
2026-04-22 19:08:57 +02:00
)
(info-sound "Opening next file: ~a" test-file4)
2026-04-22 19:08:57 +02:00
(let ((audio-h (audio-open test-file4 audio-meta audio-play)))
(set! current-file-id test-file4-id)
(set! current-audio-h audio-h)
(audio-read audio-h)
)
(ao-close ao-h)
(set! ao-h #f))
2026-02-23 15:32:50 +01:00
(play)
2025-08-13 14:13:35 +02:00