Skip to content

Commit

Permalink
refactor and clean up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbclements committed Aug 4, 2020
1 parent f641cc7 commit 77a03c8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 72 deletions.
48 changes: 3 additions & 45 deletions portaudio/callback-support.rkt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#lang racket/base

(require setup/collection-search
ffi/vector
(require ffi/vector
ffi/unsafe
(rename-in racket/contract [-> c->])
racket/runtime-path
"portaudio.rkt"
"callbacks-lib.rkt"
(only-in racket/match match-define))

;; this module provides an intermediate layer between
Expand Down Expand Up @@ -131,28 +130,7 @@

;; ... how to make sure that it doesn't get freed before it's copied out?

;; STREAMING CALLBACK STRUCT

(define-cstruct _stream-rec
(;; the number of frames in the circular buffer
[buffer-frames _int]
;; the circular buffer
[buffer _pointer]
;; the last frame read by the callback
[last-frame-read _uint]
;; the offset of the last byte read by the callback.
[last-offset-read _uint]
;; the last frame written by Racket
[last-frame-written _uint]
;; the offset of the last byte written by Racket.
[last-offset-written _uint]
;; number of faults:
[fault-count _int]
;; a pointer to a 4-byte cell; when it's nonzero,
;; the supplying procedure should shut down, and
;; free this cell. If it doesn't get freed, well,
;; that's four bytes wasted until the next store-prompt.
[all-done _pointer]))



;; how many fails have occurred on the stream?
Expand Down Expand Up @@ -228,26 +206,6 @@
(set-stream-rec-last-frame-written! stream-info last-frame-to-write)
(set-stream-rec-last-offset-written! stream-info last-offset-to-write)))

;; FFI OBJECTS FROM THE C CALLBACK LIBRARY

(define not-false? (λ (x) x))

;; the library containing the C copying callbacks
(define callbacks-lib
(let ()
(or
;; search in all portaudio/lib collection dirs:
(collection-search
'(lib "portaudio/lib")
#:combine
(λ (_ path)
(ffi-lib (build-path path "callbacks")
#:fail (λ () #f)))
#:break?
not-false?)
;; also look in "standard locations". useful
;; for people building executables.
(ffi-lib "callbacks"))))

;; in order to get a raw pointer to pass back to C, we declare
;; the function pointers as being simple structs:
Expand Down
54 changes: 54 additions & 0 deletions portaudio/callbacks-lib.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#lang racket/base

(require setup/collection-search
ffi/unsafe)

(provide callbacks-lib
(struct-out stream-rec)
;; no obvious way to provide names for the underscore
;; version, add as needed...
_stream-rec
_stream-rec-pointer)

(define not-false? (λ (x) x))


;; the library containing the C copying callbacks
(define callbacks-lib
(let ()
(or
;; search in all portaudio/lib collection dirs:
(collection-search
'(lib "portaudio/lib")
#:combine
(λ (_ path)
(ffi-lib (build-path path "callbacks")
#:fail (λ () #f)))
#:break?
not-false?)
;; also look in "standard locations". useful
;; for people building executables.
(ffi-lib "callbacks"))))

;; STREAMING CALLBACK STRUCT

(define-cstruct _stream-rec
(;; the number of frames in the circular buffer
[buffer-frames _int]
;; the circular buffer
[buffer _pointer]
;; the last frame read by the callback
[last-frame-read _uint]
;; the offset of the last byte read by the callback.
[last-offset-read _uint]
;; the last frame written by Racket
[last-frame-written _uint]
;; the offset of the last byte written by Racket.
[last-offset-written _uint]
;; number of faults:
[fault-count _int]
;; a pointer to a 4-byte cell; when it's nonzero,
;; the supplying procedure should shut down, and
;; free this cell. If it doesn't get freed, well,
;; that's four bytes wasted until the next store-prompt.
[all-done _pointer]))
4 changes: 2 additions & 2 deletions portaudio/test/test-s16vec-record.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

(check-equal? (* 1000 2) (s16vector-length s))

#:(define data (for/list ([p (s16vector->list s)]
(define data (for/list ([p (s16vector->list s)]
[i (in-naturals)])
(vector i p)))
#;(display (plot (points data)))
(display (plot (points data)))
)))


27 changes: 2 additions & 25 deletions portaudio/test/test-stream-callback.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

(require "helpers.rkt"
"../callback-support.rkt"
"../callbacks-lib.rkt"
ffi/unsafe
ffi/vector
rackunit
Expand All @@ -19,33 +20,9 @@
(test-suite "portaudio"
(let ()

(define callback-lib
(ffi-lib (build-path libs (system-library-subpath) "callbacks")))

(define-cstruct _stream-rec
(;; the number of frames in the circular buffer
[buffer-frames _int]
;; the circular buffer
[buffer _pointer]
;; the last frame read by the callback
[last-frame-read _uint]
;; the offset of the last byte read by the callback.
[last-offset-read _uint]
;; the last frame written by Racket
[last-frame-written _uint]
;; the offset of the last byte written by Racket.
[last-offset-written _uint]
;; number of faults:
[fault-count _int]
;; a pointer to a 4-byte cell; when it's nonzero,
;; the supplying procedure should shut down, and
;; free this cell. If it doesn't get freed, well,
;; that's four bytes wasted forever.
[all-done _pointer]))

(define streaming-callback
(get-ffi-obj "streamingCallback"
callback-lib
callbacks-lib
(_fun
(_pointer = #f)
_pointer
Expand Down

0 comments on commit 77a03c8

Please sign in to comment.