-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech-detection.lisp
32 lines (28 loc) · 1.54 KB
/
speech-detection.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
(in-package #:org.shirakumo.fraf.vtryout)
(defclass speech-detection (mixed:virtual)
((main-frequency :initform (vec3) :accessor main-frequency)
(volume-range :initform (setting :audio :volume-range) :accessor volume-range)
(frequency-range :initform (setting :audio :frequency-range) :accessor frequency-range)
(speech-p :initform NIL :accessor speech-p)))
(defmethod mixed:info ((segment speech-detection))
(list :name "speech-detection"
:description "Detects human speech respons in the input."
:flags 0 :min-inputs 1 :max-inputs 1 :outputs 0 :fields ()))
(defun normalize (x lo hi)
(/ (- (clamp lo x hi) lo) (- hi lo)))
(defmethod mixed:mix ((segment speech-detection))
(declare (optimize speed (safety 1)))
(mixed:with-buffer-tx (data start size (aref (mixed:inputs segment) 0))
(declare (type (simple-array single-float (*)) data))
(when (< 0 size)
(let ((mag 0f0) (freq 0f0))
(destructuring-bind (lo . hi) (frequency-range segment)
(declare (type single-float lo hi))
(loop for i of-type (unsigned-byte 32) from start below (1- (+ start size)) by 2
do (when (and (<= lo (aref data (+ i 0)) hi) (< mag (aref data (+ i 1))))
(setf mag (aref data (+ i 1)))
(setf freq (aref data (+ i 0))))))
(let ((volume (destructuring-bind (lo . hi) (volume-range segment) (normalize mag lo hi))))
(vsetf (the vec3 (main-frequency segment)) freq mag volume)
(setf (speech-p segment) (< 0.0 volume))))
(mixed:finish))))