#lang racket
(require "../read-wav.rkt"
"../write-wav.rkt"
"../rsound.rkt"
racket/runtime-path
racket/math
rackunit
ffi/vector)
(define-runtime-path short-test-wav "./short-test.wav")
(define test-rsound (apply rsound (read-sound/s16vector short-test-wav 0 #f)))
(define (desired-nth-sample n)
(round (* #x8000 (sin (* 2 pi (/ n 44100) 700)))))
(define first-sample (desired-nth-sample 1))
(define second-sample (desired-nth-sample 2))
(define thirtieth-sample (desired-nth-sample 30))
(define fiftieth-sample (desired-nth-sample 50))
(check-= (rsound-nth-sample/left test-rsound 0) 0.0 1e-4)
(check-= (rsound-nth-sample/left test-rsound 1) first-sample 1e-4)
(check-= (rsound-nth-sample/right test-rsound 2) second-sample 1e-4)
(check-= (rsound-nth-sample/right test-rsound 50) fiftieth-sample 1)
(define test-sub-rsound (apply rsound (read-sound/s16vector short-test-wav 30 40)))
(check-equal? (rsound-frames test-sub-rsound) 10)
(check-= (rsound-nth-sample/left test-sub-rsound 0) (desired-nth-sample 30) 1e-4)
(check-= (rsound-nth-sample/right test-sub-rsound 1) (desired-nth-sample 31) 1e-4)
(define-runtime-path kick-wav "./kick_01.wav")
(define kick-rsound (apply rsound (read-sound/s16vector kick-wav 0 #f)))
(check-equal? (rsound-frames kick-rsound) 4410)
(check-equal? (rsound-nth-sample/left kick-rsound 4310) -4195)
(check-equal? (rsound-nth-sample/right kick-rsound 4310) -4195)
(define-runtime-path short-with-pad-wav "./short-with-pad.wav")
(define short-with-pad (apply rsound (read-sound/s16vector short-with-pad-wav 0 #f)))
(check-equal? (rsound-frames short-with-pad) #x21)
(check-equal? (rsound-nth-sample/left short-with-pad 5) #x892)
(check-equal? (rsound-nth-sample/right short-with-pad 6) #x478)
(define-runtime-path non-integral-frames-wav "./test-non-integer-frames.wav")
(check-exn exn:fail? (lambda () (read-sound/s16vector non-integral-frames-wav 0 #f)))
(define-runtime-path short-with-LIST-wav "./test-with-list.wav")
(define short-with-LIST (apply rsound (read-sound/s16vector short-with-LIST-wav 0 #f)))
(check-equal? (rsound-frames short-with-LIST) (/ #x78 4))
(check-equal? (rsound-nth-sample/right short-with-LIST 2) -4416)