forked from Shirakumo/harmony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfadable.lisp
More file actions
47 lines (42 loc) · 1.88 KB
/
fadable.lisp
File metadata and controls
47 lines (42 loc) · 1.88 KB
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
#|
This file is a part of harmony
(c) 2017 Shirakumo http://tymoon.eu (shinmera@tymoon.eu)
Author: Nicolas Hafner <shinmera@tymoon.eu>
|#
(in-package #:org.shirakumo.fraf.harmony)
(defclass fadable (segment)
((start-volume :initform 1.0 :accessor start-volume)
(target-volume :initform 1.0 :accessor target-volume)
(fade-count :initform 0 :accessor fade-count)
(fade-end :initform 0 :accessor fade-end)
(easing-function :initform #'ease-linear :accessor easing-function)))
(defmethod initialize-instance :after ((fadable fadable) &key volume fade-time fade-from)
(when fade-time
(fade fadable volume fade-time :from (or fade-from 0.0))))
(defmethod fade ((fadable fadable) to time &key (by (easing-function fadable)) from)
(let ((target (floor (* time (samplerate (context fadable)))))
(from (coerce (or from (volume fadable)) 'single-float))
(to (coerce to 'single-float)))
(check-type by function)
(with-body-in-mixing-context ((context fadable))
(setf (fade-count fadable) 0)
(setf (fade-end fadable) target)
(setf (easing-function fadable) by)
(setf (start-volume fadable) from)
(setf (target-volume fadable) to)
(setf (volume fadable) from))))
(declaim (inline perform-fading))
(defun perform-fading (fadable samples)
(declare (optimize speed))
(let ((count (fade-count fadable))
(end (fade-end fadable)))
(declare (type (unsigned-byte 32) count end))
(when (< count end)
(let* ((start (start-volume fadable))
(target (target-volume fadable))
(x (/ (coerce count 'single-float) end))
(volume (+ start (* (- target start)
(the single-float (funcall (the function (easing-function fadable)) x))))))
(declare (type single-float start target))
(setf (volume fadable) volume))
(incf (fade-count fadable) samples))))