[7b58b4]: src / vug / envelope.lisp  Maximize  Restore  History

Download this file

341 lines (311 with data), 15.8 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
 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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
;;; Copyright (c) 2013 Tito Latini
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
(in-package :incudine.vug)
(eval-when (:compile-toplevel :load-toplevel :execute)
(import
'(incudine:+seg-lin-func+
incudine:envelope
incudine:envelope-data
incudine::envelope-data-size
incudine:envelope-points
incudine:envelope-max-points
incudine:envelope-loop-node
incudine:envelope-release-node
incudine:envelope-restart-level
incudine::%segment-init
incudine::%segment-update-level))
(object-to-free incudine:make-envelope update-local-envelope))
(defmacro make-local-envelope (levels times &key curve (loop-node -1)
(release-node -1) restart-level)
(with-gensyms (%levels %times %curve env)
`(with ((,%levels (locally (declare #.*reduce-warnings*)
,levels))
(,%times (locally (declare #.*reduce-warnings*)
,times))
(,%curve (locally (declare #.*reduce-warnings*)
,curve))
(,env (incudine:make-envelope ,%levels ,%times :curve ,%curve
:loop-node ,loop-node
:release-node ,release-node
:restart-level ,restart-level
:real-time-p *allow-rt-memory-pool-p*)))
,env)))
(defmacro update-local-envelope (vug-varname args)
(with-gensyms (size)
`(let ((,size (max (length ,(first args))
(1+ (length ,(second args))))))
(cond ((> ,size (envelope-max-points ,vug-varname))
(incudine:free ,vug-varname)
(setf ,vug-varname (incudine:make-envelope ,@args)))
(t (incudine:set-envelope ,vug-varname
;; The last keyword is REAL-TIME-P,
;; absent in SET-ENVELOPE
,@(butlast args 2)))))))
(defmacro make-local-linen (attack-time sustain-time release-time
&key (level 1) (curve :lin) restart-level)
`(make-local-envelope (list 0 ,level ,level 0)
(list ,attack-time ,sustain-time ,release-time)
:curve ,curve :restart-level ,restart-level))
(defmacro make-local-perc (attack-time release-time
&key (level 1) (curve -4) restart-level)
`(make-local-envelope (list 0 ,level 0) (list ,attack-time ,release-time)
:curve ,curve :restart-level ,restart-level))
(defmacro make-local-cutoff (release-time &key (level 1) (curve :exp) restart-level)
`(make-local-envelope (list ,level 0) (list ,release-time)
:curve ,curve :release-node 0 :restart-level ,restart-level))
(defmacro make-local-asr (attack-time sustain-level release-time
&key (curve -4) restart-level)
`(make-local-envelope (list 0 ,sustain-level 0)
(list ,attack-time ,release-time)
:curve ,curve :release-node 1 :restart-level ,restart-level))
(defmacro make-local-adsr (attack-time decay-time sustain-level release-time
&key (peak-level 1) (curve -4) restart-level)
`(make-local-envelope (list 0 ,peak-level (* ,peak-level ,sustain-level) 0)
(list ,attack-time ,decay-time ,release-time)
:curve ,curve :release-node 2 :restart-level ,restart-level))
(defmacro make-local-dadsr (delay-time attack-time decay-time sustain-level
release-time &key (peak-level 1) (curve -4) restart-level)
`(make-envelope (list 0 0 ,peak-level (* ,peak-level ,sustain-level) 0)
(list ,delay-time ,attack-time ,decay-time ,release-time)
:curve ,curve :release-node 3 :restart-level ,restart-level))
;;; Simple segments
(define-vug line (start end dur (done-action function))
(with ((done-p nil)
(samples (max 1 (sample->fixnum (* dur *sample-rate*))))
(remain samples)
(value start)
(slope (without-follow (start end)
;; Update only if DUR is changed
(init-only (when done-p
;; Restart
(setf done-p nil))
(/ (- end value) samples)))))
(declare (type sample value slope) (type boolean done-p)
(type non-negative-fixnum samples remain))
(if done-p
value
(cond ((zerop remain)
(done-action done-action)
(setf done-p t)
value)
(t (decf remain)
(incf value slope))))))
(define-vug x-line (start end dur (done-action function))
(with ((done-p nil)
(samples (max 1 (sample->fixnum (* dur *sample-rate*))))
(remain samples)
(%start (if (zerop start) 1d-5 start))
(value %start)
(power (without-follow (start end)
;; Update only if DUR is changed
(init-only (when done-p
;; Restart
(setf done-p nil))
(expt (the non-negative-sample
(/ end value))
(/ (sample samples)))))))
(declare (type sample %start value power) (type boolean done-p)
(type non-negative-fixnum samples remain))
(if done-p
value
(cond ((zerop remain)
(done-action done-action)
(setf done-p t)
value)
(t (decf remain)
(setf value (* value power)))))))
;;; Envelope Generator inspired by EnvGen of SuperCollider
(eval-when (:compile-toplevel :load-toplevel)
(declaim (inline envgen-next-dur))
(defun envgen-next-dur (env-data index time-scale offset)
(max 1 (+ offset (sample->fixnum
(* (smp-ref env-data index)
time-scale *sample-rate*)))))
(defmacro envgen-next-index (data-size index curr-node)
`(the non-negative-fixnum
(cond ((zerop ,index) 0)
((>= ,index ,data-size)
;; point to the last segment
(setf ,index (- ,data-size 4)
,curr-node (1- (the positive-fixnum
(/ ,index 3))))
,index)
(t (- ,index 3)))))
(defmacro envgen-jump-node (node-dest node-src index)
(with-gensyms (dest)
`(let ((,dest ,node-dest))
(incf ,index (the non-negative-fixnum
(* (the non-negative-fixnum
(- ,dest ,node-src))
3)))
(setf ,node-src ,dest))))
(declaim (inline jump-to-loop-node-p))
(defun jump-to-loop-node-p (gate curr-node loop-node release-node)
(and (>= loop-node 0)
(= (1+ curr-node) release-node)
(plusp gate)
(/= curr-node loop-node)))
(declaim (inline envgen-end-of-data-p))
(defun envgen-end-of-data-p (index data-size)
(>= index data-size))
(declaim (inline envgen-to-sustain-p))
(defun envgen-to-sustain-p (gate curr-node release-node)
(and (= curr-node release-node) (plusp gate)))
(declaim (inline release-before-sustain-p))
(defun release-before-sustain-p (gate sustain curr-node release-node)
(and (zerop gate)
(null sustain)
(plusp release-node)
(< release-node curr-node)))
(defmacro envgen-update-sustain (var gate curr-node release-node)
`(setf ,var
(and (>= ,curr-node 0)
(envgen-to-sustain-p ,gate ,curr-node ,release-node))))
(defmacro envgen-sustain (sustain-var)
`(setf ,sustain-var t))
(defmacro envgen-no-sustain (sustain-var)
`(if ,sustain-var (setf ,sustain-var nil)))
(declaim (inline immediate-cutoff-p))
(defun immediate-cutoff-p (gate)
(= gate -1.0))
(declaim (inline release-with-custom-duration-p))
(defun release-with-custom-duration-p (gate)
(< gate -1.0))
(declaim (inline envgen-custom-duration))
(defun envgen-custom-duration (gate)
(sample->fixnum (* (- -1.0 gate) *sample-rate*)))
(declaim (inline envgen-begin-p))
(defun envgen-begin-p (index dur)
(and (zerop index) (zerop dur))))
(define-vug envgen ((env envelope) gate time-scale (done-action function))
(with-samples (tmp end grow a2 b1 y1 y2)
(with ((index 0)
(curr-node -1)
(env-data (envelope-data env))
(data-size (envelope-data-size env))
(last-point (1- (the non-negative-fixnum (envelope-points env))))
(curr-index (envgen-next-index data-size index curr-node))
(prev-index 0)
(loop-node (envelope-loop-node env))
(release-node (envelope-release-node env))
(sustain nil)
(done-p nil)
(dur 0)
(remain 0)
(curve +seg-lin-func+)
(gate-trig (plusp gate))
(level (cond ((release-before-sustain-p gate sustain curr-node release-node)
(envgen-jump-node (1- release-node) curr-node index)
(setf remain 0)
tmp)
((immediate-cutoff-p gate)
(setf sustain nil remain 0)
(envgen-jump-node last-point curr-node index)
+sample-zero+)
((release-with-custom-duration-p gate)
;; Force the release stage with custom duration
(setf dur (envgen-custom-duration gate)
;; Anticipate one sample to avoid the repetition of
;; a vertex because the last value of a segment is
;; the first value of the next segment.
remain (1- dur)
curr-node (1+ curr-node))
(unless (= curr-node last-point)
(envgen-jump-node (1- last-point) curr-node index)
(setf index (+ index 2) ; skip dur
end (smp-ref env-data index)
index (+ index 1)
curve (smp-ref env-data index)
prev-index curr-index))
(setf sustain nil)
(%segment-init tmp end dur curve grow a2 b1 y1 y2)
tmp)
((envgen-begin-p index dur)
(cond (gate-trig
(envgen-update-sustain sustain gate curr-node release-node)
(setf gate-trig nil)
(smp-ref env-data 0))
;; ENVGEN started with GATE zero
(t (setf index data-size)
(samples-zero tmp end))))
(gate-trig
;; Restart
(setf gate-trig nil
remain 0
index 0
curr-node -1
curr-index (envgen-next-index data-size index curr-node)
prev-index curr-index
done-p nil
sustain nil
;; LEVEL is set to END during the performance.
end (or (envelope-restart-level env) tmp)))
((zerop dur)
(envgen-no-sustain sustain)
end)
((or done-p (= curr-index prev-index))
(envgen-no-sustain sustain)
tmp)
(t (envgen-no-sustain sustain)
(setf dur (envgen-next-dur env-data index time-scale
(- remain dur))
;; One sample is subtracted in the previous
;; value of REMAIN
remain dur
index (1+ index)
end (smp-ref env-data index)
index (1+ index)
curve (smp-ref env-data index))
(%segment-init tmp end dur curve grow a2 b1 y1 y2)
tmp))))
(declare (type non-negative-fixnum index data-size last-point dur remain
curr-index prev-index)
(type fixnum curr-node loop-node release-node)
(type sample level curve) (type boolean sustain done-p gate-trig))
(initialize (setf end level))
;; Useful when GATE is modulated. In this case, the expansion
;; of GATE occurs here. If GATE is not modulated, GATE-TRIG is
;; always NIL at this point.
(maybe-expand (and gate-trig level))
(cond ((or done-p sustain) tmp)
(t (cond ((zerop remain)
;; End of segment
(cond ((envgen-end-of-data-p (incf index) data-size)
(done-action done-action)
(setf done-p t tmp end))
(t (incf curr-node)
(cond ((jump-to-loop-node-p gate curr-node loop-node
release-node)
(envgen-jump-node loop-node curr-node index))
((envgen-to-sustain-p gate curr-node release-node)
(envgen-sustain sustain)))
;; Compute the parameters for the next segment
(setf dur (envgen-next-dur env-data index time-scale 0)
remain (1- dur)
index (1+ index)
;; The first value of the segment is the last value
;; of the previous segment
level end
end (smp-ref env-data index)
index (1+ index)
curve (smp-ref env-data index)
prev-index curr-index)
(%segment-init level end dur curve grow a2 b1 y1 y2)
(setf tmp level))))
(t (decf remain)
;; Compute the next point
(%segment-update-level level curve grow a2 b1 y1 y2)
(setf tmp level))))))))

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks