Files
racket-sound/play-test.rkt
T

134 lines
5.7 KiB
Racket

#lang racket/base
(require "libao.rkt"
"audio-decoder.rkt"
simple-log
"private/utils.rkt"
racket-sprintf
;data/queue
;racket-sound
)
(define test-file3 #f)
(define test-file4 #f)
(define test-file3-id 3)
(define test-file4-id 4)
(let ((os (system-type 'os)))
(when (eq? os 'unix)
;(set! test-file3 "/muziek/Klassiek-Viool/Alina Ibragimova/Paganini_24 Caprices (2021)/24. 24 Caprices, Op 1 - No. 24 in A minor- Tema con variazioni. Quasi presto.flac")
(set! test-file3 "/tmp/test.mp3")
(set! test-file4 "/tmp/test1.mp3")
)
(when (eq? os 'windows)
;(set! test-file3 "C:\\Muziek\\Klassiek-Strijkkwartet\\Quatuor Zaïde\\Franz\\01 Erlkönig, D. 328 (Arr. For String Quartet by Eric Mouret).flac")
;(set! test-file3 "C:\\Muziek\\Klassiek-Viool\\Janine Jansen\\Janine Jansen - Sibelius en Prokovief 1 (2024)\\02 - Violin Concerto in D Minor, Op. 47 II. Adagio di molto.flac")
(set! test-file3 "c:\\tmp\\test.mp3")
(set! test-file4 "c:\\tmp\\test1.mp3")
)
)
;(define fmt (ao-mk-format 24 48000 2 'big-endian))
;(define ao-h (ao-open-live #f fmt))
(define current-seconds 0)
(define ao-h #f)
(define current-file-id -1)
(define current-audio-h #f)
(sl-log-to-display)
(define wav-output-file #f)
(define (audio-play type ao-type handle buf-info buffer buf-len)
(let* ((sample (hash-ref buf-info 'sample))
(rate (hash-ref buf-info 'sample-rate))
(second (/ (* sample 1.0) (* rate 1.0)))
(bits-per-sample (hash-ref buf-info 'bits-per-sample))
(bytes-per-sample (/ bits-per-sample 8))
(channels (hash-ref buf-info 'channels))
(bytes-per-sample-all-channels (* channels bytes-per-sample))
(duration (hash-ref buf-info 'duration))
)
;(displayln buf-info)
(when (eq? ao-h #f)
(set! ao-h (ao-open-file bits-per-sample rate channels 'native-endian wav-output-file)))
;(displayln 'ao-play)
(ao-play ao-h current-file-id second duration buffer buf-len ao-type)
(set! duration (inexact->exact (round duration)))
;(displayln 'done)
(let ((second-printer (λ (buf-seconds)
(let ((s (inexact->exact (round (ao-at-second ao-h)))))
(unless (= s current-seconds)
(set! current-seconds s)
(let ((minutes (quotient s 60))
(seconds (remainder s 60))
(tminutes (quotient duration 60))
(tseconds (remainder duration 60))
(volume (ao-volume ao-h))
)
(info-sound
(sprintf "At time: %02d:%02d (%02d:%02d) - %d - volume: %d"
minutes seconds
tminutes tseconds
buf-seconds
volume
))))))))
(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 (= (round current-seconds) 10)
(when (= current-file-id 3)
(audio-seek current-audio-h 97.0)))
(when (> buf-seconds 5)
(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)
(info-sound "Seconds in buffer left: ~a" buf-seconds-left)
(begin
(sleep 0.5)
(second-printer buf-seconds)
(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))
(when (= (round current-seconds) 10)
(when (= current-file-id 3)
(audio-seek current-audio-h 97.0)))
(waiter)))))
))
(waiter))))
)
)
)
(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))
(define (play)
(set! ao-h #f)
(let ((audio-h (audio-open test-file3 audio-meta audio-play)))
(set! current-file-id test-file3-id)
(set! current-audio-h audio-h)
(audio-read audio-h)
)
(info-sound "Opening next file: ~a" test-file4)
(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))
(play)