Better error handling in audio-player.rkt
This commit is contained in:
+37
-26
@@ -5,6 +5,7 @@
|
|||||||
racket/async-channel
|
racket/async-channel
|
||||||
racket/runtime-path
|
racket/runtime-path
|
||||||
racket/string
|
racket/string
|
||||||
|
"audio-errors.rkt"
|
||||||
uni-channel
|
uni-channel
|
||||||
"audio-placed-player.rkt"
|
"audio-placed-player.rkt"
|
||||||
"opusfile-decoder.rkt"
|
"opusfile-decoder.rkt"
|
||||||
@@ -76,18 +77,23 @@
|
|||||||
(eq? (car retval) sym))
|
(eq? (car retval) sym))
|
||||||
#f))
|
#f))
|
||||||
|
|
||||||
|
(define-struct internal-audio-retval
|
||||||
|
(kind code info retval))
|
||||||
|
|
||||||
(define (to-ret-value ret)
|
(define (to-ret-value ret)
|
||||||
(if (list? ret)
|
(if (list? ret)
|
||||||
(if (null? ret)
|
(if (null? ret)
|
||||||
(error (format "audio-player: no return value in ~a" ret))
|
(raise-audio-error 'audio-error "audio-player: no return value, empty list returned")
|
||||||
(let ((r (car ret)))
|
(let ((r (car ret)))
|
||||||
(when (eq? r 'error)
|
(if (eq? r 'error)
|
||||||
(err-sound "Got an error: ~a" ret))
|
(let ((code (cadr ret))
|
||||||
(car ret)))
|
(msg (caddr ret)))
|
||||||
(begin
|
(err-sound "Got an error: ~a - ~a" code msg)
|
||||||
(when(eq? ret 'error)
|
(make-internal-audio-retval 'error code msg 'error))
|
||||||
(err-sound "Got an error: ~a" ret))
|
(make-internal-audio-retval 'retval #f #f r))))
|
||||||
ret)))
|
(if (eq? ret 'error)
|
||||||
|
(make-internal-audio-retval 'error 'audio-error "An undefined error was raised in the audio-player" 'error)
|
||||||
|
(make-internal-audio-retval 'retval #f #f ret))))
|
||||||
|
|
||||||
(define (is-event? evt sym)
|
(define (is-event? evt sym)
|
||||||
(is-return? evt sym))
|
(is-return? evt sym))
|
||||||
@@ -255,44 +261,49 @@
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
(define-syntax ap-rpc
|
||||||
|
(syntax-rules ()
|
||||||
|
((_ handle cmd args ...)
|
||||||
|
(let ((rv ((audio-play-rpc handle) cmd args ...)))
|
||||||
|
(if (eq? (internal-audio-retval-kind rv) 'error)
|
||||||
|
(raise-audio-error (internal-audio-retval-code rv) (internal-audio-retval-info rv))
|
||||||
|
(internal-audio-retval-retval rv))))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
(define/contract (audio-play! handle audio-file)
|
(define/contract (audio-play! handle audio-file)
|
||||||
(-> audio-play? path-string? number?)
|
(-> audio-play? path-string? number?)
|
||||||
(let ((result ((audio-play-rpc handle) 'open audio-file)))
|
(ap-rpc handle 'open audio-file))
|
||||||
(when (eq? result 'error)
|
|
||||||
(err-sound "audio-play!: error for file ~a" audio-file)
|
|
||||||
(error "Got an error from the placed audio player")
|
|
||||||
)
|
|
||||||
(cadr result)))
|
|
||||||
|
|
||||||
(define/contract (audio-pause! handle paused)
|
(define/contract (audio-pause! handle paused)
|
||||||
(-> audio-play? boolean? symbol?)
|
(-> audio-play? boolean? symbol?)
|
||||||
((audio-play-rpc handle) 'pause paused))
|
(ap-rpc handle 'pause paused))
|
||||||
|
|
||||||
(define/contract (audio-paused? handle)
|
(define/contract (audio-paused? handle)
|
||||||
(-> audio-play? boolean?)
|
(-> audio-play? boolean?)
|
||||||
((audio-play-rpc handle) 'paused))
|
(ap-rpc handle 'paused))
|
||||||
|
|
||||||
(define/contract (audio-stop! handle)
|
(define/contract (audio-stop! handle)
|
||||||
(-> audio-play? symbol?)
|
(-> audio-play? symbol?)
|
||||||
((audio-play-rpc handle) 'stop))
|
(ap-rpc handle 'stop))
|
||||||
|
|
||||||
(define/contract (audio-quit! handle)
|
(define/contract (audio-quit! handle)
|
||||||
(-> audio-play? (or/c number? boolean? symbol?))
|
(-> audio-play? (or/c number? boolean? symbol?))
|
||||||
(let ((r ((audio-play-rpc handle) 'quit)))
|
(let ((r (ap-rpc handle 'quit)))
|
||||||
(set-audio-play-valid?! handle #f)
|
(set-audio-play-valid?! handle #f)
|
||||||
r))
|
r))
|
||||||
|
|
||||||
(define/contract (audio-seek! handle percentage)
|
(define/contract (audio-seek! handle percentage)
|
||||||
(-> audio-play? (max-percentage? 100) symbol?)
|
(-> audio-play? (max-percentage? 100) symbol?)
|
||||||
((audio-play-rpc handle) 'seek percentage))
|
(ap-rpc handle 'seek percentage))
|
||||||
|
|
||||||
(define/contract (audio-volume! handle percentage)
|
(define/contract (audio-volume! handle percentage)
|
||||||
(-> audio-play? percentage? symbol?)
|
(-> audio-play? percentage? symbol?)
|
||||||
((audio-play-rpc handle) 'volume percentage))
|
(ap-rpc handle 'volume percentage))
|
||||||
|
|
||||||
(define/contract (audio-volume handle)
|
(define/contract (audio-volume handle)
|
||||||
(-> audio-play? percentage?)
|
(-> audio-play? percentage?)
|
||||||
((audio-play-rpc handle) 'get-volume))
|
(ap-rpc handle 'get-volume))
|
||||||
|
|
||||||
(define/contract (audio-full-state handle)
|
(define/contract (audio-full-state handle)
|
||||||
(-> audio-play? hash?)
|
(-> audio-play? hash?)
|
||||||
@@ -345,23 +356,23 @@
|
|||||||
(-> audio-play? number? number? (or/c symbol? boolean?))
|
(-> audio-play? number? number? (or/c symbol? boolean?))
|
||||||
(let ((from (if (< min 1) 1 (if (> min 10) 10 min)))
|
(let ((from (if (< min 1) 1 (if (> min 10) 10 min)))
|
||||||
(until (if (< max min) (+ min 1) (if (> max 30) 30 max))))
|
(until (if (< max min) (+ min 1) (if (> max 30) 30 max))))
|
||||||
((audio-play-rpc handle) 'buf-seconds from until)))
|
(ap-rpc handle 'buf-seconds from until)))
|
||||||
|
|
||||||
(define/contract (audio-ao-buf-ms! handle ms)
|
(define/contract (audio-ao-buf-ms! handle ms)
|
||||||
(-> audio-play? integer? (or/c integer? boolean?))
|
(-> audio-play? integer? (or/c integer? boolean?))
|
||||||
((audio-play-rpc handle) 'ao-buf-ms ms))
|
(ap-rpc handle 'ao-buf-ms ms))
|
||||||
|
|
||||||
(define/contract (audio-ao-buf-ms handle)
|
(define/contract (audio-ao-buf-ms handle)
|
||||||
(-> audio-play? (or/c integer? boolean?))
|
(-> audio-play? (or/c integer? boolean?))
|
||||||
((audio-play-rpc handle) 'ao-buf-ms))
|
(ap-rpc handle 'ao-buf-ms))
|
||||||
|
|
||||||
(define/contract (audio-param! handle param value)
|
(define/contract (audio-param! handle param value)
|
||||||
(-> audio-play? symbol? any? any?)
|
(-> audio-play? symbol? any? any?)
|
||||||
((audio-play-rpc handle) 'param! param value))
|
(ap-rpc handle 'param! param value))
|
||||||
|
|
||||||
(define/contract (audio-param handle param)
|
(define/contract (audio-param handle param)
|
||||||
(-> audio-play? symbol? any?)
|
(-> audio-play? symbol? any?)
|
||||||
((audio-play-rpc handle) 'param param))
|
(ap-rpc handle 'param param))
|
||||||
|
|
||||||
(define/contract (audio-parameterize! handle)
|
(define/contract (audio-parameterize! handle)
|
||||||
(-> audio-play? void?)
|
(-> audio-play? void?)
|
||||||
|
|||||||
Reference in New Issue
Block a user