-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
pack.lisp
94 lines (80 loc) · 3.89 KB
/
pack.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(in-package #:org.shirakumo.fraf.mixed)
(defclass pack (bip-buffer c-object)
((data :reader data)))
(defmethod initialize-instance :after ((pack pack) &key frames encoding channels samplerate)
(let* ((size (* frames channels (samplesize encoding)))
(data (static-vectors:make-static-vector size :element-type '(unsigned-byte 8) :initial-element 0)))
(setf (slot-value pack 'data) data)
(let ((handle (handle pack)))
(setf (mixed:pack-data handle) (static-vectors:static-vector-pointer data))
(setf (mixed:pack-size handle) size)
(setf (mixed:pack-encoding handle) encoding)
(setf (mixed:pack-channels handle) channels)
(setf (mixed:pack-samplerate handle) samplerate))))
(defun make-pack (&key (encoding :float) (channels 2) (samplerate *default-samplerate*) (frames (floor samplerate 100)))
(make-instance 'pack :frames frames
:encoding encoding
:channels channels
:samplerate samplerate))
(defmethod allocate-handle ((pack pack))
(calloc '(:struct mixed:pack)))
(defmethod free ((pack pack))
(when (slot-boundp pack 'data)
(static-vectors:free-static-vector (data pack))
(slot-makunbound pack 'data)))
(define-accessor size pack mixed:pack-size)
(define-accessor encoding pack mixed:pack-encoding)
(define-accessor channels pack mixed:pack-channels)
(define-accessor samplerate pack mixed:pack-samplerate)
(declaim (ftype (function (T) (unsigned-byte 8)) channels))
(declaim (ftype (function (T) (unsigned-byte 16)) framesize))
(declaim (ftype (function (T) (unsigned-byte 32)) samplerate))
(declaim (ftype (function (T) (unsigned-byte 32)) size))
(defmethod clear ((pack pack))
(mixed:clear-pack (handle pack)))
(defmethod (setf size) :before (size (pack pack))
(unless (= size (size pack))
(let ((old (data pack))
(new (static-vectors:make-static-vector size :element-type '(unsigned-byte 8))))
(static-vectors:replace-foreign-memory
(static-vectors:static-vector-pointer new) (static-vectors:static-vector-pointer old)
(size pack))
(setf (slot-value pack 'data) new)
(setf (mixed:pack-data (handle pack)) (static-vectors:static-vector-pointer new))
(setf (mixed:pack-size (handle pack)) (length new))
(static-vectors:free-static-vector old)))
size)
(defmethod transfer ((from buffer) (to pack))
(cffi:with-foreign-objects ((buffers :pointer)
(volume :float))
(setf (cffi:mem-ref buffers :pointer) (handle from))
(setf (cffi:mem-ref volume :float) 1f0)
(with-error-on-failure ()
(mixed:buffer-to-pack buffers (handle to) volume 1f0))))
(defmethod transfer ((from sequence) (to pack))
(cffi:with-foreign-objects ((buffers :pointer (length from))
(volume :float))
(setf (cffi:mem-ref volume :float) 1f0)
(do-sequence (i buffer from)
(setf (cffi:mem-aref buffers :pointer i) (handle buffer)))
(with-error-on-failure ()
(mixed:buffer-to-pack buffers (handle to) volume 1f0))))
(defmethod transfer ((from pack) (to buffer))
(cffi:with-foreign-objects ((buffers :pointer)
(volume :float))
(setf (cffi:mem-ref volume :float) 1f0)
(setf (cffi:mem-ref buffers :pointer) (handle to))
(with-error-on-failure ()
(mixed:buffer-from-pack (handle from) buffers volume 1f0))))
(defmethod transfer ((from pack) (to sequence))
(cffi:with-foreign-objects ((buffers :pointer (length from))
(volume :float))
(setf (cffi:mem-ref volume :float) 1f0)
(do-sequence (i buffer to)
(setf (cffi:mem-aref buffers :pointer i) (handle buffer)))
(with-error-on-failure ()
(mixed:buffer-from-pack (handle from) buffers volume 1f0))))
(defmethod framesize ((pack pack))
(let ((handle (handle pack)))
(* (mixed:pack-channels handle)
(samplesize (mixed:pack-encoding handle)))))