Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

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

Download this file

oscillator.lisp    660 lines (634 with data), 32.3 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
;;; 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)
(define-vug %phasor (rate init end)
(with-samples ((phase init))
(prog1 phase
(incf phase rate)
(cond ((>= phase end) (decf phase end))
((minusp phase) (incf phase end))))))
(define-vug phasor (freq init)
(with-samples ((rate (* freq *sample-duration*)))
(%phasor rate init 1)))
(define-vug phasor-loop (rate start-pos loopstart loopend)
(with-samples ((pos start-pos)
(old-pos pos)
(loopsize (- loopend loopstart))
(start-plus-end (+ loopend loopstart)))
(prog1 pos
(incf pos rate)
(cond ((> pos loopend) (decf pos loopsize))
((< pos 0) (setf pos +sample-zero+))
((and (< pos loopstart)
(>= old-pos loopstart))
(setf pos (- start-plus-end pos))))
(setf old-pos pos))))
(defmacro %with-osc-interp ((buffer phs frac) &body body)
(with-gensyms (lodiv lomask)
`(with ((,frac 0.0d0)
(,lodiv (buffer-lodiv ,buffer))
(,lomask (buffer-lomask ,buffer)))
(declare (type sample ,frac ,lodiv)
(type non-negative-fixnum ,lomask))
(setf ,frac (* ,lodiv (logand ,phs ,lomask)))
,@body)))
(defmacro %osc-linear-interp (buffer data phs index)
(with-gensyms (frac mask)
`(%with-osc-interp (,buffer ,phs ,frac)
(with ((,mask (buffer-mask ,buffer)))
(declare (type non-negative-fixnum ,mask))
(linear-interp ,frac (smp-ref ,data ,index)
(smp-ref ,data (logand (the fixnum (1+ ,index))
,mask)))))))
(defmacro %osc-cubic-interp (buffer data phs index)
(with-gensyms (frac index0 index2 index3 mask)
`(%with-osc-interp (,buffer ,phs ,frac)
(with ((,mask (buffer-mask ,buffer)))
(declare (type non-negative-fixnum ,mask))
(let ((,index0 (logand (the fixnum (- ,index 1)) ,mask))
(,index2 (logand (the fixnum (+ ,index 1)) ,mask))
(,index3 (logand (the fixnum (+ ,index 2)) ,mask)))
(cubic-interp ,frac (smp-ref ,data ,index0)
(smp-ref ,data ,index)
(smp-ref ,data ,index2)
(smp-ref ,data ,index3)))))))
;;; Select the interpolation for a table lookup.
;;; The size of the table is a power of two.
(defmacro %osc-select-interpolation (interpolation buffer data phs index)
(case interpolation
(:linear `(%osc-linear-interp ,buffer ,data ,phs ,index))
(:cubic `(%osc-cubic-interp ,buffer ,data ,phs ,index))
(otherwise `(smp-ref ,data ,index))))
(defmacro %osc-phase-modulation (phs phase phase-modulation-p)
(cond (phase-modulation-p
(with-gensyms (phase-old %phase)
`(with-samples ((,%phase (vug-input
,(coerce-number phase 'sample)))
(,phase-old 0.0d0))
(unless (= ,%phase ,phase-old)
(setf ,phs (logand (the fixnum
(+ ,phs (sample->fixnum
(* (- ,%phase ,phase-old)
+rad2inc+))))
+phase-mask+)
,phase-old ,%phase)))))
((and (numberp phase) (zerop phase)) '(values))
(t `(initialize
(setf ,phs (logand (the fixnum
(+ ,phs (sample->fixnum
(* ,phase +rad2inc+))))
+phase-mask+))))))
(defmacro %osc (buffer freq amp phase phase-modulation-p interpolation)
(with-gensyms (freq-inc phs minus-lobits data index)
`(with ((,freq-inc (sample->fixnum (* ,freq *cps2inc*)))
(,phs 0)
(,minus-lobits (- (buffer-lobits ,buffer)))
(,data (buffer-data ,buffer)))
(declare (type fixnum ,phs ,freq-inc)
(type (integer #.(- +max-lobits+) 0) ,minus-lobits)
(type foreign-pointer ,data))
(%osc-phase-modulation ,phs ,phase ,phase-modulation-p)
(let ((,index (the fixnum (ash ,phs ,minus-lobits))))
(prog1 (* ,amp (%osc-select-interpolation ,interpolation ,buffer
,data ,phs ,index))
(setf ,phs (logand (the fixnum (+ ,phs ,freq-inc)) +phase-mask+)))))))
(defmacro %impulse (freq amp phase phase-modulation-p)
(with-gensyms (phase-old phs freq-inc)
`(with-samples ((,phs (if (zerop ,phase) (sample 1) ,phase))
(,freq-inc (* ,freq *sample-duration*)))
,amp
(prog1 (cond ((>= ,phs 1.0) (decf ,phs 1.0) ,amp)
(t +sample-zero+))
,@(when phase-modulation-p
`((with-samples (,phase-old)
(unless (= ,phase ,phase-old)
(incf ,phs (- ,phase ,phase-old))
(setf ,phase-old ,phase)))))
(incf ,phs ,freq-inc)))))
(defmacro %buzz-numerator (buffer data phs two-nh-plus-one minus-lobits mask
interpolation)
(with-gensyms (phs-num index)
(if (member interpolation '(:linear :cubic))
`(let* ((,phs-num (* ,phs ,two-nh-plus-one))
(,index (logand (the fixnum
(ash ,phs-num ,minus-lobits))
,mask)))
(declare (type fixnum ,phs-num ,index))
(%osc-select-interpolation ,interpolation ,buffer
,data ,phs-num ,index))
;; The follow calc of the index is better
;; for a lookup table without interpolation
`(let ((,index (logand (the fixnum
(* (the fixnum
(ash ,phs ,minus-lobits))
(the fixnum
,two-nh-plus-one)))
,mask)))
(smp-ref ,data ,index)))))
(defmacro %buzz (buffer freq amp num-harm phase phase-modulation-p
harm-change-lag interpolation)
(with-gensyms (freq-inc phs minus-lobits mask data nh-lag old-num-harm
nh0 nh1 res0 res1 num0 num1 denom old-two-nh-plus-one
two-nh-plus-one amp0 mult0 mult1 index count cross inc-interp)
`(with ((,freq-inc (sample->fixnum
;; The angle is theta/2
(* 0.5 ,freq *cps2inc*)))
(,phs 0)
(,minus-lobits (- (buffer-lobits ,buffer)))
(,mask (buffer-mask ,buffer))
;; The buffer contains a sine wave
(,data (buffer-data ,buffer))
(,count 0)
(,cross 1.0d0)
(,nh-lag (sample->fixnum (* ,harm-change-lag *sample-rate*)))
(,inc-interp (/ (sample 1) ,nh-lag))
(,amp0 (* ,amp 0.5))
(,old-num-harm 0)
(,nh0 1)
(,nh1 1)
(,old-two-nh-plus-one 3)
(,two-nh-plus-one 3)
(,mult0 (/ ,amp0 ,nh0))
(,mult1 (/ ,amp0 ,nh1))
(,res0 0.0d0)
(,res1 0.0d0)
(,num0 0.0d0)
(,num1 0.0d0)
(,denom 0.0d0))
(declare (type fixnum ,phs ,mask ,freq-inc ,nh0 ,nh1 ,nh-lag ,old-num-harm
,count ,old-two-nh-plus-one ,two-nh-plus-one)
(type (integer #.(- +max-lobits+) 0) ,minus-lobits)
(type sample ,res0 ,res1 ,num0 ,num1 ,denom ,amp0 ,mult0 ,mult1
,cross ,inc-interp)
(type foreign-pointer ,data))
(initialize (setf ,nh1 (max 1 (abs ,num-harm))
,two-nh-plus-one (1+ (* 2 ,nh1))))
(%osc-phase-modulation ,phs ,phase ,phase-modulation-p)
(let ((,index (the fixnum (ash ,phs ,minus-lobits))))
(unless (or (= ,old-num-harm ,num-harm)
(plusp ,count))
(setf ,old-num-harm ,num-harm
,count ,nh-lag
,cross +sample-zero+
,nh0 ,nh1
,nh1 (max 1 (abs ,num-harm))
,old-two-nh-plus-one ,two-nh-plus-one
,two-nh-plus-one (1+ (* 2 ,nh1))
,mult0 (/ ,amp0 ,nh0)
,mult1 (/ ,amp0 ,nh1)))
(setf ,denom (%osc-select-interpolation ,interpolation ,buffer
,data ,phs ,index))
(prog1
(cond ((or (> ,denom 1.d-5) (< ,denom -1.d-5))
(setf ,num1
(%buzz-numerator ,buffer ,data ,phs ,two-nh-plus-one
,minus-lobits ,mask ,interpolation))
(setf ,res1 (* ,mult1 (- (/ ,num1 ,denom) 1.0)))
(cond ((plusp ,count)
(decf ,count)
(setf ,num0
(%buzz-numerator ,buffer ,data ,phs ,old-two-nh-plus-one
,minus-lobits ,mask ,interpolation))
(setf ,res0 (* ,mult0 (- (/ ,num0 ,denom) 1.0)))
(prog1 (linear-interp ,cross ,res0 ,res1)
(incf ,cross ,inc-interp)))
(t ,res1)))
(t (when (plusp ,count)
(decf ,count)
(incf ,cross ,inc-interp))
,amp))
(setf ,phs (logand (the fixnum (+ ,phs ,freq-inc)) +phase-mask+)))))))
(define-vug buzz-hq (freq amp (num-harm fixnum) phase harm-change-lag)
(with ((count 0)
(cross 1.0d0)
(nh-lag (sample->fixnum (* harm-change-lag *sample-rate*)))
(inc-interp (/ (sample 1) nh-lag))
(old-num-harm 0)
(nh0 1)
(nh1 1)
(old-two-nh-plus-one 3)
(two-nh-plus-one 3)
(res0 0.0d0)
(res1 0.0d0)
(num0 0.0d0)
(num1 0.0d0)
(denom 0.0d0)
(amp0 (* amp 0.5))
(mult0 (/ amp0 nh0))
(mult1 (/ amp0 nh1))
(fdiv2 (* 0.5 freq))
(angle 0.0d0))
(declare (type sample cross res0 res1 num0 num1 denom amp0 mult0 mult1
fdiv2 angle inc-interp)
(type fixnum count nh0 nh1 nh-lag old-num-harm
old-two-nh-plus-one two-nh-plus-one))
(initialize (setf nh1 (max 1 (abs num-harm))
two-nh-plus-one (1+ (* 2 nh1))))
(unless (or (= old-num-harm num-harm)
(plusp count))
(setf old-num-harm num-harm
count nh-lag
cross +sample-zero+
nh0 nh1
nh1 (max 1 (abs num-harm))
old-two-nh-plus-one two-nh-plus-one
two-nh-plus-one (1+ (* 2 nh1))
mult0 (/ amp0 nh0)
mult1 (/ amp0 nh1)))
(setf angle (+ (* +twopi+ (phasor fdiv2 0)) phase))
(setf denom (sin angle))
(cond ((or (> denom 1.d-5) (< denom -1.d-5))
(setf num1 (sin (* angle two-nh-plus-one)))
(setf res1 (* mult1 (- (/ num1 denom) 1.0)))
(cond ((plusp count)
(decf count)
(setf num0 (sin (* angle old-two-nh-plus-one)))
(setf res0 (* mult0 (- (/ num0 denom) 1.0)))
(prog1 (linear-interp cross res0 res1)
(incf cross inc-interp)))
(t res1)))
(t (when (plusp count)
(decf count)
(incf cross inc-interp))
amp))))
(defmacro %gbuzz-numerator (buffer data phs c0 c1 c2 c3 mult1 mult2 mult3
minus-lobits mask interpolation)
(with-gensyms (phs-num0 phs-num1 phs-num2 phs-num3
index0 index1 index2 index3)
(if (member interpolation '(:linear :cubic))
`(let* ((,phs-num0 (* ,phs ,c0))
(,phs-num1 (* ,phs ,c1))
(,phs-num2 (* ,phs ,c2))
(,phs-num3 (* ,phs ,c3))
(,index0 (logand (the fixnum
(ash ,phs-num0 ,minus-lobits))
,mask))
(,index1 (logand (the fixnum
(ash ,phs-num1 ,minus-lobits))
,mask))
(,index2 (logand (the fixnum
(ash ,phs-num2 ,minus-lobits))
,mask))
(,index3 (logand (the fixnum
(ash ,phs-num3 ,minus-lobits))
,mask)))
(declare (type fixnum ,phs-num0 ,phs-num1 ,phs-num2 ,phs-num3
,index0 ,index1 ,index2 ,index3))
(+ (- (%osc-select-interpolation ,interpolation ,buffer
,data ,phs-num0 ,index0)
(* ,mult1 (%osc-select-interpolation ,interpolation ,buffer
,data ,phs-num1 ,index1))
(* ,mult2 (%osc-select-interpolation ,interpolation ,buffer
,data ,phs-num2 ,index2)))
(* ,mult3 (%osc-select-interpolation ,interpolation ,buffer
,data ,phs-num3 ,index3))))
`(let ((,index0 (logand (the fixnum
(* (the fixnum
(ash ,phs ,minus-lobits))
(the fixnum ,c0)))
,mask))
(,index1 (logand (the fixnum
(* (the fixnum
(ash ,phs ,minus-lobits))
(the fixnum ,c1)))
,mask))
(,index2 (logand (the fixnum
(* (the fixnum
(ash ,phs ,minus-lobits))
(the fixnum ,c2)))
,mask))
(,index3 (logand (the fixnum
(* (the fixnum
(ash ,phs ,minus-lobits))
(the fixnum ,c3)))
,mask)))
(declare (type fixnum ,index0 ,index1 ,index2 ,index3))
(+ (- (smp-ref ,data ,index0)
(* ,mult1 (smp-ref ,data ,index1))
(* ,mult2 (smp-ref ,data ,index2)))
(* ,mult3 (smp-ref ,data ,index3)))))))
(defmacro gbuzz-update-multipliers (c2-mult-var c3-mult-var rsum-var
nh mul abs-mul)
`(progn
;; No optimization with "(expt ,mul ,nh)"
(setf ,c2-mult-var (expt (the non-negative-sample ,abs-mul) ,nh))
(when (and (minusp ,mul)
(plusp (logand ,nh 1)))
(setf ,c2-mult-var (- ,c2-mult-var)))
(setf ,c3-mult-var (* ,c2-mult-var ,mul) ; mul^(nh + 1)
,rsum-var (if (and (> ,abs-mul 0.999)
(< ,abs-mul 1.001))
(/ (sample 1) ,nh)
(/ (- 1.0 ,abs-mul)
(- 1.0 (abs ,c2-mult-var)))))))
(defmacro %gbuzz (buffer freq amp num-harm lowest-harm mul phase phase-modulation-p
harm-change-lag interpolation)
(with-gensyms (freq-inc phs minus-lobits mask data nh-lag old-num-harm
c0 c1 c2 c3 c1-mult c2-mult c3-mult old-lowest-harm old-c0 old-c1
old-c2 old-c3 old-c1-mult old-c2-mult old-c3-mult old-mul abs-mul
two-mul squared-mul-plus-one old-two-mul old-squared-mul-plus-one
rsum0 rsum1 nh0 nh1 res0 res1 num0 num1 denom0 denom1 denom-osc
index count cross inc-interp out)
`(with ((,freq-inc (sample->fixnum (* ,freq *cps2inc*)))
(,phs 0)
(,minus-lobits (- (buffer-lobits ,buffer)))
(,mask (buffer-mask ,buffer))
;; The buffer contains a cosine wave
(,data (buffer-data ,buffer))
(,count 0)
(,nh-lag (sample->fixnum (* ,harm-change-lag *sample-rate*)))
(,inc-interp (/ (sample 1) ,nh-lag))
(,old-num-harm 999999)
(,nh0 1)
(,nh1 1)
(,c0 0)
(,c1 0)
(,c2 0)
(,c3 0)
(,old-lowest-harm 0)
(,old-c0 0)
(,old-c1 0)
(,old-c2 0)
(,old-c3 0))
(declare (type fixnum ,phs ,mask ,freq-inc ,nh-lag ,count ,old-lowest-harm
,c0 ,c1 ,c2 ,c3 ,old-c0 ,old-c1 ,old-c2 ,old-c3)
(type (integer #.(- +max-lobits+) 0) ,minus-lobits)
(type (integer 0 1000000) ,nh0 ,nh1 ,old-num-harm)
(type sample ,inc-interp) (type foreign-pointer ,data))
(with-samples (,c1-mult ,c2-mult ,c3-mult ,old-c1-mult ,old-c2-mult
,old-c3-mult ,old-mul ,abs-mul ,old-two-mul ,two-mul
,squared-mul-plus-one ,old-squared-mul-plus-one
,rsum0 ,rsum1 ,res0 ,res1 ,num0 ,num1 ,denom0 ,denom1
,denom-osc ,cross ,out)
(initialize (setf ,nh1 (max 1 (abs ,num-harm))
,c0 ,lowest-harm ; lh
,c1 (1- ,c0) ; lh - 1
,c2 (+ ,c0 ,nh1) ; lh + nh
,c3 (1- ,c2) ; lh + nh - 1
,abs-mul (abs ,mul)
,two-mul (+ ,mul ,mul)
,squared-mul-plus-one (+ (* ,mul ,mul) 1.0)
,c1-mult ,mul)
(gbuzz-update-multipliers ,c2-mult ,c3-mult ,rsum1
,nh1 ,mul ,abs-mul))
(%osc-phase-modulation ,phs ,phase ,phase-modulation-p)
(let ((,index (the fixnum (ash ,phs ,minus-lobits))))
;; Expand here if MUL is modulated
(maybe-expand ,mul)
(unless (or (and (= ,old-num-harm ,num-harm)
(= ,old-lowest-harm ,lowest-harm))
(plusp ,count))
(setf ,old-num-harm ,num-harm
,old-lowest-harm ,lowest-harm
,count ,nh-lag
,cross +sample-zero+
,nh0 ,nh1
,nh1 (max 1 (abs ,num-harm))
,old-c0 ,c0
,old-c1 ,c1
,old-c2 ,c2
,old-c3 ,c3
,old-c1-mult ,c1-mult
,old-c2-mult ,c2-mult
,old-c3-mult ,c3-mult
,c0 ,lowest-harm
,c1 (1- ,c0)
,c2 (+ ,c0 ,nh1)
,c3 (1- ,c2)
,old-two-mul ,two-mul
,old-squared-mul-plus-one ,squared-mul-plus-one
,rsum0 ,rsum1)
(when (= ,old-mul ,mul)
(gbuzz-update-multipliers ,c2-mult ,c3-mult ,rsum1
,nh1 ,mul ,abs-mul)))
(unless (= ,old-mul ,mul)
(setf ,old-mul ,mul
,abs-mul (abs ,mul)
,two-mul (+ ,mul ,mul)
,squared-mul-plus-one (+ (* ,mul ,mul) 1.0)
,c1-mult ,mul)
(gbuzz-update-multipliers ,c2-mult ,c3-mult ,rsum1
,nh1 ,mul ,abs-mul))
(setf ,denom-osc (%osc-select-interpolation ,interpolation ,buffer
,data ,phs ,index))
(setf ,denom1 (- ,squared-mul-plus-one (* ,two-mul ,denom-osc)))
(setf ,out
(cond ((or (> ,denom1 1.d-5) (< ,denom1 -1.d-5))
(setf ,num1
(%gbuzz-numerator ,buffer ,data ,phs ,c0 ,c1 ,c2 ,c3
,c1-mult ,c2-mult ,c3-mult ,minus-lobits
,mask ,interpolation))
(setf ,res1 (* ,rsum1 (/ ,num1 ,denom1)))
(* ,amp
(cond ((plusp ,count)
(decf ,count)
(setf ,num0
(%gbuzz-numerator ,buffer ,data ,phs ,old-c0 ,old-c1
,old-c2 ,old-c3 ,old-c1-mult
,old-c2-mult ,old-c3-mult
,minus-lobits ,mask ,interpolation))
(setf ,denom0 (- ,old-squared-mul-plus-one
(* ,old-two-mul ,denom-osc)))
(setf ,res0 (if (or (> ,denom0 1.d-5) (< ,denom0 -1.d-5))
(* ,rsum0 (/ ,num0 ,denom0))
(sample 1)))
(prog1 (linear-interp ,cross ,res0 ,res1)
(incf ,cross ,inc-interp)))
(t ,res1))))
(t (when (plusp ,count)
(decf ,count)
(incf ,cross ,inc-interp))
(if (minusp ,out) (- ,amp) ,amp))))
(setf ,phs (logand (the fixnum (+ ,phs ,freq-inc)) +phase-mask+))
,out)))))
(define-vug gbuzz-hq (freq amp (num-harm (integer 0 1000000))
(lowest-harm fixnum) mul phase harm-change-lag)
(with ((count 0)
(nh-lag (sample->fixnum (* harm-change-lag *sample-rate*)))
(inc-interp (/ (sample 1) nh-lag))
(old-num-harm 999999)
(nh0 1)
(nh1 1)
(c0 0)
(c1 0)
(c2 0)
(c3 0)
(old-lowest-harm 0)
(old-c0 0)
(old-c1 0)
(old-c2 0)
(old-c3 0))
(declare (type fixnum nh-lag count old-lowest-harm c0 c1 c2 c3
old-c0 old-c1 old-c2 old-c3)
(type (integer 0 1000000) nh0 nh1 old-num-harm)
(type sample inc-interp))
(with-samples (c1-mult c2-mult c3-mult old-c1-mult old-c2-mult
old-c3-mult old-mul abs-mul old-two-mul two-mul
squared-mul-plus-one old-squared-mul-plus-one
rsum0 rsum1 res0 res1 num0 num1 denom0 denom1
denom-osc cross angle out)
(initialize (setf nh1 (max 1 (abs num-harm))
c0 lowest-harm
c1 (1- c0)
c2 (+ c0 nh1)
c3 (1- c2)
abs-mul (abs mul)
c1-mult mul
two-mul (+ mul mul)
squared-mul-plus-one (+ (* mul mul) 1.0))
(gbuzz-update-multipliers c2-mult c3-mult rsum1
nh1 mul abs-mul))
;; Expand here if MUL is modulated
(maybe-expand mul)
(unless (or (and (= old-num-harm num-harm)
(= old-lowest-harm lowest-harm))
(plusp count))
(setf old-num-harm num-harm
old-lowest-harm lowest-harm
count nh-lag
cross +sample-zero+
nh0 nh1
nh1 (max 1 (abs num-harm))
old-c0 c0
old-c1 c1
old-c2 c2
old-c3 c3
old-c1-mult c1-mult
old-c2-mult c2-mult
old-c3-mult c3-mult
c0 lowest-harm
c1 (1- c0)
c2 (+ c0 nh1)
c3 (1- c2)
old-two-mul two-mul
old-squared-mul-plus-one squared-mul-plus-one
rsum0 rsum1)
(when (= old-mul mul)
(gbuzz-update-multipliers c2-mult c3-mult rsum1 nh1 mul abs-mul)))
(unless (= old-mul mul)
(setf old-mul mul
abs-mul (abs mul)
c1-mult mul
two-mul (+ mul mul)
squared-mul-plus-one (+ (* mul mul) 1.0))
(gbuzz-update-multipliers c2-mult c3-mult rsum1 nh1 mul abs-mul))
(setf angle (+ (* +twopi+ (phasor freq 0)) phase))
(setf denom-osc (cos angle))
(setf denom1 (- squared-mul-plus-one (* two-mul denom-osc)))
(setf out
(cond ((or (> denom1 1.d-5) (< denom1 -1.d-5))
(setf num1 (+ (- (cos (* c0 angle))
(* c1-mult (cos (* c1 angle)))
(* c2-mult (cos (* c2 angle))))
(* c3-mult (cos (* c3 angle)))))
(setf res1 (* rsum1 (/ num1 denom1)))
(* amp
(cond ((plusp count)
(decf count)
(setf num0 (+ (- (cos (* old-c0 angle))
(* old-c1-mult (cos (* old-c1 angle)))
(* old-c2-mult (cos (* old-c2 angle))))
(* old-c3-mult (cos (* old-c3 angle)))))
(setf denom0 (- old-squared-mul-plus-one
(* old-two-mul denom-osc)))
(setf res0 (if (or (> denom0 1.d-5) (< denom0 -1.d-5))
(* rsum0 (/ num0 denom0))
(sample 1)))
(prog1 (linear-interp cross res0 res1)
(incf cross inc-interp)))
(t res1))))
(t (when (plusp count)
(decf count)
(incf cross inc-interp))
(if (minusp out) (- amp) amp))))))))
;;; Wavetable lookup oscillator
(define-vug-macro osc (buffer &optional (freq 440.0) (amp 1.0d0)
(phase 0.0d0) interpolation)
(with-gensyms (%buffer %freq %amp)
(with-coerce-arguments (freq amp phase)
`(with-vug-inputs ((,%buffer ,buffer)
(,%freq ,freq)
(,%amp ,amp))
(declare (type buffer ,%buffer) (type sample ,%freq ,%amp))
(%osc ,%buffer ,%freq ,%amp ,phase ,(null (constantp phase))
,interpolation)))))
;;; Sine wave oscillator
(define-vug sine (freq amp phase)
(* amp (sin (+ (* +twopi+ (phasor freq 0)) phase))))
;;; Pulse wave oscillator
(define-vug pulse (freq amp width)
(if (< (phasor freq 0) width) amp (- amp)))
;;; Train of impulses
(define-vug-macro impulse (&optional (freq 1.0d0) (amp 1.0d0) (phase 0.0d0))
(with-gensyms (%freq %amp)
(with-coerce-arguments (freq amp phase)
`(with-vug-inputs ((,%freq ,freq)
(,%amp ,amp))
(declare (type sample ,%freq ,%amp))
(%impulse ,%freq ,%amp ,phase ,(null (constantp phase)))))))
;;; Band limited impulse generator used in Music N languages.
;;; The output is a set of harmonically related sine partials.
(define-vug-macro buzz (freq amp num-harm &key (phase 0.0d0) (table-lookup-p t)
buffer (harm-change-lag 0.001d0) (interpolation :linear))
(if table-lookup-p
;; Version with table lookup
(with-gensyms (%buffer %freq %amp %num-harm %harm-change-lag)
(with-coerce-arguments (freq amp phase harm-change-lag)
`(with-vug-inputs ((,%buffer ,(or buffer '*sine-table*))
(,%freq ,freq)
(,%amp ,amp)
(,%num-harm ,num-harm)
(,%harm-change-lag ,harm-change-lag))
(declare (type buffer ,%buffer) (type fixnum ,%num-harm)
(type sample ,%freq ,%amp ,%harm-change-lag))
(%buzz ,%buffer ,%freq ,%amp ,%num-harm ,phase
,(null (constantp phase)) ,%harm-change-lag ,interpolation))))
;; This version uses directly the SIN function
`(buzz-hq ,freq ,amp ,num-harm ,phase ,harm-change-lag)))
(define-vug-macro gbuzz (freq amp num-harm lowest-harm mul &key (phase 0.0d0)
(table-lookup-p t) buffer (harm-change-lag 0.001d0)
(interpolation :linear))
(if table-lookup-p
;; Version with table lookup
(with-gensyms (%buffer %freq %amp %num-harm %harm-change-lag
%lowest-harm %mul)
(with-coerce-arguments (freq amp phase mul harm-change-lag)
`(with-vug-inputs ((,%buffer ,(or buffer '*cosine-table*))
(,%freq ,freq)
(,%amp ,amp)
(,%num-harm ,num-harm)
(,%lowest-harm ,lowest-harm)
(,%mul ,mul)
(,%harm-change-lag ,harm-change-lag))
(declare (type buffer ,%buffer)
(type (integer 0 1000000) ,%num-harm)
(type fixnum ,%lowest-harm)
(type sample ,%freq ,%amp ,%harm-change-lag ,%mul))
(%gbuzz ,%buffer ,%freq ,%amp ,%num-harm ,%lowest-harm ,%mul ,phase
,(null (constantp phase)) ,%harm-change-lag ,interpolation))))
`(gbuzz-hq ,freq ,amp ,num-harm ,lowest-harm ,mul ,phase ,harm-change-lag)))
(define-vug buffer-play ((buffer buffer) rate start-pos (loop-p boolean)
(done-action function))
(prog1 (buffer-read buffer (%phasor (* rate (buffer-sample-rate buffer)
*sample-duration*)
start-pos
(if loop-p
(sample (buffer-frames buffer))
most-positive-sample))
:wrap-p loop-p :interpolation :cubic)
(when (done-self)
(done-action done-action))))