generators.scm 202 KB
Newer Older
1
(provide 'snd-generators.scm)
2 3 4
(if (provided? 'snd)
    (require snd-ws.scm)
    (require sndlib-ws.scm))
5

6 7 8 9 10
;;; it is dangerous to use a method within a generator's definition of that method --
;;;   if the gen is used as the environment in with-let, the embedded call
;;;   becomes a recursive call on that method.  You either need to check the type
;;;   of the method argument, or use #_method to override the name lookup, or use
;;;   the explicit call style: (((gen 'embedded-gen) 'shared-method) ...)
11

12 13 14
;;; if gen has embedded gen, mus-copy needs a special copy method (see adjustable-oscil)
;;; a problem with a special copy method: if you change the generator, remember to change its copy method!
;;; also, I think (inlet e) is a way to copy e without accidentally invoking any 'copy method in e
15 16


17 18
(define nearly-zero 1.0e-10) ; 1.0e-14 in clm.c, but that is trouble here (noddcos)
(define two-pi (* 2.0 pi))
19

20 21 22 23 24
(define (convert-frequency g)
  (set! (g 'frequency) (hz->radians (g 'frequency)))
  g)


25 26 27 28
;;; --------------------------------------------------------------------------------

;;; nssb (see nxycos) -- wouldn't a more consistent name be nxycos? but it already exists -- perhaps delete nssb?

29
(defgenerator (nssb :make-wrapper convert-frequency)
30
  (frequency 0.0) (ratio 1.0) (n 1) (angle 0.0) fm)
31 32 33 34


(define nssb 

35
  (let ((+documentation+ "(make-nssb frequency (ratio 1.0) (n 1)) creates an nssb generator,
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
similar to nxysin. (nssb gen (fm 0.0)) returns n sinusoids from frequency spaced by frequency * ratio."))

    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let* ((cx angle)
	       (mx (* cx ratio))
	       (den (sin (* 0.5 mx))))
	  (set! angle (+ angle fm frequency))
	  (if (< (abs den) nearly-zero)
	      -1.0
	      (/ (- (* (sin cx) 
		       (sin (* mx (/ (+ n 1) 2)))
		       (sin (/ (* n mx) 2)))
		    (* (cos cx) 
		       0.5 (+ den (sin (* mx (+ n 0.5))))))
		 (* (+ n 1) den))))))))
  
54 55 56
#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nssb 1000.0 0.1 3)))
57 58
     (do ((i 0 (+ i 1)))
	 ((= i 10000))
59
       (outa i (nssb gen)))))
60 61 62 63 64

(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nssb 1000.0 0.1 3))
	(vib (make-oscil 5.0))
	(ampf (make-env '(0 0 1 1 2 1 3 0) :length 20000 :scaler 1.0)))
65 66 67 68
     (do ((i 0 (+ i 1)))
	 ((= i 20000))
       (outa i (* (env ampf) 
		  (nssb gen (* (hz->radians 100.0) 
69
			       (oscil vib))))))))
70 71 72
|#


73

74 75
;;; --------------------------------------------------------------------------------

76
;;; G&R first col rows 1&2
77

78 79 80 81 82 83 84
(define (nodds x n) 
  (let ((den (sin x))
	(num (sin (* n x))))
    (if (= den 0.0)
	0.0
	(/ (* num num) den))))

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
(define find-nxysin-max 
  (letrec ((find-mid-max 
	    (let ((ns (lambda (x n) 
			(let* ((a2 (/ x 2))
			       (den (sin a2)))
			  (if (= den 0.0)
			      0.0
			      (/ (* (sin (* n a2)) (sin (* (+ 1 n) a2))) den))))))
	      (lambda (n lo hi)
		(let ((mid (/ (+ lo hi) 2))
		      (ylo (ns lo n))
		      (yhi (ns hi n)))
		  (if (< (abs (- ylo yhi)) nearly-zero) ; was e-100 but that hangs if not using doubles
		      (ns mid n)
		      (find-mid-max n (if (> ylo yhi)
					  (values lo mid)
					  (values mid hi))))))))
	   (find-nodds-mid-max 
	    (lambda (n lo hi)
	      (let ((mid (/ (+ lo hi) 2))
		    (ylo (nodds lo n))
		    (yhi (nodds hi n)))
		(if (< (abs (- ylo yhi)) nearly-zero)
		    (nodds mid n)
		    (find-nodds-mid-max n (if (> ylo yhi)
					      (values lo mid)
					      (values mid hi))))))))
    (lambda (n ratio)
      (case ratio
	((1) (find-mid-max n 0.0 (/ pi (+ n .5))))
	((2) (find-nodds-mid-max n 0.0 (/ pi (+ (* 2 n) 0.5))))
	(else n)))))
117 118 119 120


(defgenerator (nxysin
	       :make-wrapper (lambda (g)
121
			       (convert-frequency g)
122
			       (set! (g 'norm) (/ 1.0 (find-nxysin-max (g 'n) (g 'ratio))))
123
			       g))
124
  (frequency 0.0) (ratio 1.0) (n 1) (angle 0.0) fm
125 126 127
  (norm 1.0))


128
(define nxysin 
129

130
  (let ((+documentation+ "(make-nxysin frequency (ratio 1.0) (n 1)) creates an nxysin 
131
generator. (nxysin gen (fm 0.0)) returns n sines from frequency spaced by frequency * ratio."))
132

133 134 135 136 137 138 139 140 141 142 143 144 145
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let* ((x angle)
	       (y (* x ratio))
	       (den (sin (* y 0.5))))
	  (set! angle (+ angle fm frequency))
	  (if (< (abs den) nearly-zero)
	      0.0
	      (/ (* (sin (+ x (* 0.5 (- n 1) y)))
		    (sin (* 0.5 n y))
		    norm)
		 den)))))))
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

;;; if x (angle) is constant (an initial-phase offset for a sum of sines,
;;;  the peak amp is nsin-max(n) + abs(sin(initial-phase))*(1 - nsin-max(n))
;;;  that is, it varys sinusoidally from a sum-of-sines .7245 to a sum-of-cosines 1
;;; since we're treating "x" as the carrier (it's not a constant phase offset in this case)
;;;  the output varies as x does, so we have a maxamp of n? There are special cases
;;;  for low n and low integer ratio:

;;;  ratio (4):    (40):   (400):
;;;    1: 3.23      29.34       290.1
;;;    2: 2.9404    28.97       289.7
;;;    3: 3.85      38.6        346.8
;;; 1.123: n
;;;   .5: 3.55      30.0        290

;;; a ratio of 1 gives a sum of equal amplitude sines, so we could use nsin-max?
;;;            2                                odd harmonics -- use noddsin?
;;; else use n (not so great for ratio: 3, but not way off)
;;; worst case right now is probably ratio .5

#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nxysin 300 1/3 3)))
169 170
     (do ((i 0 (+ i 1)))
	 ((= i 20000))
171
       (outa i (nxysin gen)))))
172 173 174 175

;;; here's the varying initial-phase case:

(with-sound (:clipped #f)
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
   (let ((x 0.0)
	 (ix (/ pi 1000))
	 (n 100))
     (do ((i 0 (+ i 1)))
	 ((= i 1000))
       (let ((pk 0.0)
	     (phi x)
	     (y 0.0)
	     (iy (/ (* 2 pi) 10000)))
	 (set! x (+ x ix))
	 (do ((k 0 (+ k 1)))
	     ((= k 10000))
	   ;; x = phi
	   (let ((den (sin (/ y 2))))
	     (if (not (= den 0.0))
		 (let ((sum (abs (/ (* (sin (+ phi (* y (/ (- n 1) 2)))) (sin (/ (* n y) 2))) den))))
		   (if (> sum pk)
		       (set! pk sum)))))
	   (set! y (+ y iy)))
195
	 (outa i pk)))))
196 197 198
|#


199
(defgenerator (nxycos :make-wrapper convert-frequency)
200
  (frequency 0.0) (ratio 1.0) (n 1) (angle 0.0) fm)
201 202


203
(define nxycos 
204

205
  (let ((+documentation+ "(make-nxycos frequency (ratio 1.0) (n 1)) creates an nxycos generator. (nxycos gen (fm 0.0)) 
206
returns n cosines from frequency spaced by frequency * ratio."))
207

208 209 210 211 212 213 214 215 216 217 218 219
    (lambda* (gen (fm 0.0))  
      (let-set! gen 'fm fm)
      (with-let gen
	(let* ((x angle)
	       (y (* x ratio))
	       (den (sin (* y 0.5))))
	  (set! angle (+ angle fm frequency))
	  (if (< (abs den) nearly-zero)
	      1.0
	      (/ (* (cos (+ x (* 0.5 (- n 1) y)))
		    (sin (* 0.5 n y)))
		 (* n den)))))))) ; n=normalization
220 221 222 223

#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nxycos 300 1/3 3)))
224 225
     (do ((i 0 (+ i 1)))
	 ((= i 20000))
226
       (outa i (* .5 (nxycos gen))))))
227 228 229 230 231 232
|#



;;; --------------------------------------------------------------------------------
;;;
233
;;; G&R first col rows 3 4
234

235
(defgenerator (nxy1cos :make-wrapper convert-frequency)
236
  (frequency 0.0) (ratio 1.0) (n 1) (angle 0.0) fm)
237 238


239
(define nxy1cos 
240

241
  (let ((+documentation+ "(make-nxy1cos frequency (ratio 1.0) (n 1)) creates an nxy1cos 
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
generator. (nxy1cos gen (fm 0.0)) returns 2n cosines from frequency spaced by frequency * ratio with every other cosine multiplied by -1."))
  
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let* ((x angle)
	       (y (* x ratio))
	       (den (cos (* y 0.5))))
	  (set! angle (+ angle fm frequency))
	  (if (< (abs den) nearly-zero)
	      -1.0
	      (max -1.0
		   (min 1.0
			(/ (* (sin (* n y))
			      (sin (+ x (* (- n 0.5) y))))
			   (* 2 n den))))))))))
258 259 260 261

#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nxy1cos 300 1/3 3)))
262
    (do ((i 0 (+ i 1)))
263
	 ((= i 20000))
264
       (outa i (nxy1cos gen)))))
265 266 267 268

(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nxy1cos 300 1/3 3))
	(gen1 (make-nxycos 300 1/3 6)))
269 270
     (do ((i 0 (+ i 1)))
	 ((= i 20000))
271
       (outa i (* 0.4 (+ (nxycos gen1 0.0) (nxy1cos gen)))))))
272 273 274 275 276 277 278 279 280

(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nxy1cos (radians->hz (* .01 pi)) 1.0 3)))
       (do ((i 0 (+ i 1)))
	   ((= i 20000))
	 (outa i (nxy1cos gen)))))
|#


281
(defgenerator (nxy1sin :make-wrapper convert-frequency)
282
  (frequency 0.0) (ratio 1.0) (n 1) (angle 0.0) fm)
283 284


285
(define nxy1sin 
286

287
  (let ((+documentation+ "(make-nxy1sin frequency (ratio 1.0) (n 1)) creates an nxy1sin generator.  (nxy1sin gen (fm 0.0)) 
288 289 290 291 292 293 294 295 296 297 298 299
returns n sines from frequency spaced by frequency * ratio with every other sine multiplied by -1."))
  
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let* ((x angle)
	       (y (* x ratio))
	       (den (cos (* y 0.5))))
	  (set! angle (+ angle fm frequency))
	  (/ (* (sin (+ x (* 0.5 (- n 1) (+ y pi))))
		(sin (* 0.5 n (+ y pi))))
	     (* n den)))))))
300 301 302 303

#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nxy1sin 300 1/3 3)))
304 305
     (do ((i 0 (+ i 1)))
	 ((= i 20000))
306
       (outa i (nxy1sin gen)))))
307 308 309 310 311 312 313 314 315 316 317 318
|#

;;;   we can get the sinusoidally varying maxamp by using e.g. (make-nxy1sin 1 1000 3)
;;;   the peak starts at ca .72 and goes to 1 etc
;;; the peak is just offset from pi (either way)



;;; --------------------------------------------------------------------------------

;;; n odd sinusoids: noddsin, noddcos, noddssb

319
;;; sndclm.html (G&R) first col 5th row (sum of odd sines)
320

321 322 323 324 325 326 327 328 329 330 331 332
(define (find-noddsin-max n) 
  (let find-mid-max ((n n)
		     (lo 0.0000)
		     (hi (/ pi (+ (* 2 n) 0.5))))
    (let ((mid (/ (+ lo hi) 2))
	  (ylo (nodds lo n))
	  (yhi (nodds hi n)))
      (if (< (abs (- ylo yhi)) 1e-09)
	  (nodds mid n)
	  (find-mid-max n (if (> ylo yhi) 
			      (values lo mid) 
			      (values mid hi)))))))
333

334
(define noddsin-maxes (make-float-vector 100))
335 336 337

(defgenerator (noddsin 
	       :make-wrapper (lambda (g)
338
			       (set! (g 'n) (max (g 'n) 1))
339
			       (convert-frequency g)
340 341 342 343
			       (if (not (and (< (g 'n) 100)
					     (> (noddsin-maxes (g 'n)) 0.0)))
				   (set! (noddsin-maxes (g 'n)) (find-noddsin-max (g 'n))))
			       (set! (g 'norm) (/ 1.0 (noddsin-maxes (g 'n))))
344
			       g))
345
  (frequency 0.0) (n 1) (angle 0.0) (norm 1.0) fm)
346 347


348
(define noddsin 
349

350
  (let ((+documentation+ "(make-noddsin frequency (n 1)) creates an noddsin generator. (noddsin gen (fm 0.0)) 
351 352 353 354 355 356 357 358 359 360 361
returns n odd-numbered sines spaced by frequency."))
  
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let ((snx (sin (* n angle)))
	      (den (sin angle)))
	  (set! angle (+ angle fm frequency))
	  (if (< (abs den) nearly-zero)
	      0.0
	      (/ (* norm snx snx) den)))))))
362 363 364 365 366 367 368 369 370 371 372 373 374 375

;;; max is at about: 3*pi/(8*n) -- essentially half of the nsin peak
;;; and we end up with the same max amp as nsin!!
;;; :(/ (* 8 (sin (* pi 3/8)) (sin (* pi 3/8))) (* 3 pi))
;;; 7.245186202974229185687564326622851596478E-1


#|
;;; clarinety
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-noddsin 300 :n 3))
	(ampf (make-env '(0 0 1 1 2 1 3 0) :length 40000 :scaler .5)))
      (do ((i 0 (+ i 1)))
	  ((= i 40000))
376
	(outa i (* (env ampf) (noddsin gen))))))
377 378 379
|#


380

381
(defgenerator (noddcos :make-wrapper convert-frequency)
382
  (frequency 0.0) (n 1) (angle 0.0) fm)
383 384


385 386
(define noddcos 
  
387
  (let ((+documentation+ "(make-noddcos frequency (n 1)) creates an noddcos generator.  (noddcos gen (fm 0.0)) 
388 389 390 391 392 393 394 395 396
returns n odd-numbered cosines spaced by frequency."))
  
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let ((cx angle)
	      (den (* 2 n (sin angle)))) ; "n" here is normalization
	  (set! angle (+ angle fm frequency))
	  (if (< (abs den) nearly-zero)
397
	      (let ((fang (modulo cx (* 2 pi))))
398 399 400 401 402 403
		;; hopefully this almost never happens...
		(if (or (< fang 0.001)
			(< (abs (- fang (* 2 pi))) 0.001))
		    1.0
		    -1.0))
	      (/ (sin (* 2 n cx)) den)))))))
404 405 406 407 408 409 410 411

;;; (Gradshteyn and Ryzhik 1.342)

#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-noddcos 100 :n 10)))
      (do ((i 0 (+ i 1)))
	  ((= i 10000))
412
	(outa i (* .5 (noddcos gen))))))
413 414 415
|#


416

417
(defgenerator (noddssb :make-wrapper convert-frequency)
418
  (frequency 0.0) (ratio 1.0) (n 1) (angle 0.0) fm)
419 420


421
(define noddssb 
422

423
  (let ((+documentation+ "(make-noddssb frequency (ratio 1.0) (n 1)) creates an noddssb generator. (noddssb gen (fm 0.0))
424 425 426 427 428 429
returns n sinusoids from frequency spaced by 2 * ratio * frequency."))
    
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let* ((cx angle)
430 431 432 433 434 435 436 437 438 439 440 441 442 443
	       (mx (* cx ratio)))
	  (let ((x (- cx mx))
		(sinnx (sin (* n mx)))
		(den (* n (sin mx)))) ; "n" is normalization
	    (set! angle (+ angle fm frequency))
	    (if (< (abs den) nearly-zero)
		(if (< (modulo mx (* 2 pi)) .1)
		    -1.0
		    1.0)
		(- (* (sin x)
		      (/ (* sinnx sinnx) den))
		   (* (cos x)
		      (/ (sin (* 2 n mx))
			 (* 2 den)))))))))))
444 445 446 447

#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-noddssb 1000.0 0.1 5)))
448 449 450
    (do ((i 0 (+ i 1)))
	((= i 10000))
      (outa i (* .5 (noddssb gen))))))
451 452 453 454

(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-noddssb 1000.0 0.1 5))
	(vib (make-oscil 5.0)))
455 456 457
    (do ((i 0 (+ i 1)))
	((= i 10000))
      (outa i (* .5 (noddssb gen (* (hz->radians 100.0) (oscil vib))))))))
458 459 460 461
|#



462

463 464 465 466
;;; --------------------------------------------------------------------------------
;;;
;;; various kernels: ncos2 = ncos squared (Fejer), ncos4 = ncos2 squared (Jackson), npcos = Poussin kernel

467
(defgenerator (ncos2 :make-wrapper convert-frequency)
468
  (frequency 0.0) (n 1) (angle 0.0) fm)
469 470


471
(define ncos2 
472

473
  (let ((+documentation+ "(make-ncos2 frequency (n 1)) creates an ncos2 (Fejer kernel) generator.  (ncos2 gen (fm 0.0)) 
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
returns n sinusoids spaced by frequency scaled by (n-k)/(n+1)"))
    
    ;; from "Trigonometric Series" Zygmund p88 with changes suggested by Katznelson "Introduction to Harmonic Analysis" p12, and
    ;;   scaling by an extra factor of 1/n+1 to make sure we always peak at 1.0 (I assume callers in this context are interested 
    ;;   in the pulse-train aspect and want easily predictable peak amp).  Harmonics go as (n-i)/n+1.
    
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let* ((x angle)
	       (den (sin (* 0.5 x))))
	  (set! angle (+ angle fm frequency))
	  (if (< (abs den) nearly-zero)
	      1.0
	      (let ((val (/ (sin (* 0.5 (+ n 1) x)) 
			    (* (+ n 1) den))))
		(* val val))))))))
491 492 493 494 495 496

;;; can't use two oscils here because the angles have to line up perfectly

#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-ncos2 100.0 :n 10)))
497 498 499
    (do ((i 0 (+ i 1)))
	((= i 20000))
      (outa i (* .5 (ncos2 gen))))))
500 501 502
|#


503

504 505
(define make-ncos4 make-ncos2)

506
;; Katznelson p16
507

508 509
(define ncos4 

510
  (let ((+documentation+ "(make-ncos4 frequency (n 1)) creates an ncos4 (Jackson kernel) generator. (ncos4 gen (fm 0.0)) 
511 512 513 514 515
returns n sinusoids spaced by frequency scaled by ((n-k)/(n+1))^2"))
  
    (lambda* (gen (fm 0.0))
      (let ((val (ncos2 gen fm)))
	(* val val))))) ; we already normalized this to 1.0
516 517 518 519

#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-ncos4 100.0 :n 10)))
520 521 522
    (do ((i 0 (+ i 1)))
	((= i 20000))
      (outa i (* .5 (ncos4 gen))))))
523 524 525
|#


526

527
(defgenerator (npcos :make-wrapper convert-frequency)
528
  (frequency 0.0) (n 1) (angle 0.0) fm)
529 530 531 532


(define npcos 
  
533
  (let ((+documentation+ "(make-npcos frequency (n 1)) creates an npcos (Poussin kernel) generator. (npcos gen (fm 0.0)) 
534 535 536 537 538
returns n*2+1 sinusoids spaced by frequency with amplitudes in a sort of tent shape."))
  
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
539 540 541 542 543 544 545 546 547 548 549 550
	(let ((result (let ((den (sin (* 0.5 angle))))
			(if (< (abs den) nearly-zero)
			    1.0
			    (let ((result1 (let ((val (let ((n1 (+ n 1)))
							(/ (sin (* 0.5 n1 angle))
							   (* n1 den)))))
					     (* val val)))
				  (result2 (let ((val (let ((p2n2 (+ (* 2 n) 2)))
							(/ (sin (* 0.5 p2n2 angle)) 
							   (* p2n2 den)))))
					     (* val val))))
			      (- (* 2 result2) result1))))))
551 552
	  (set! angle (+ angle fm frequency))
	  result)))))
553 554 555 556

#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-npcos 100.0 :n 10)))
557 558 559
    (do ((i 0 (+ i 1)))
	((= i 20000))
      (outa i (* .5 (npcos gen))))))
560 561 562 563
|#


#|
564

565 566
;;; ncos5 and nsin5 are minor variants of nsin and ncos -- the last component is at half amplitude

567
(defgenerator (ncos5 :make-wrapper convert-frequency)
568
  (frequency 0.0) (n 1) (angle 0.0) fm)
569 570


571
(define ncos5 
572
  
573
  (let ((+documentation+ "(make-ncos5 frequency (n 1)) creates an ncos5 generator.  (ncos5 gen (fm 0.0)) 
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
returns n cosines spaced by frequency. All are equal amplitude except the first and last at half amp."))
    
    ;; from "Chebyshev Polynomials", Mason and Handscomb, p87
    
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let* ((x angle)
	       (den (tan (* 0.5 x))))
	  (set! angle (+ angle fm frequency))
	  (if (< (abs den) nearly-zero)
	      1.0
	      (/ (- (/ (sin (* n x))
		       (* 2 den))
		    0.5)
		 (- n 0.5))))))))
590 591 592 593


(with-sound (:clipped #f :statistics #t)
  (let ((gen (make-ncos5 100.0 :n 10)))
594 595 596
    (do ((i 0 (+ i 1)))
	((= i 20000))
      (outa i (* .5 (ncos5 gen))))))
597 598 599


(define (find-nsin5-max n)
600
  
601
  (define (find-mid-max n lo hi)
602 603 604 605 606 607
    (define (ns x n)
      (let* ((den (tan (* 0.5 x))))
	(if (< (abs den) nearly-zero)
	    0.0
	    (/ (- 1.0 (cos (* n x)))
	       den))))
608 609 610 611 612 613 614 615
    (let ((mid (/ (+ lo hi) 2)))
      (let ((ylo (ns lo n))
	    (yhi (ns hi n)))
	(if (< (abs (- ylo yhi)) 1e-9)
	    (ns mid n)
	    (if (> ylo yhi)
		(find-mid-max n lo mid)
		(find-mid-max n mid hi))))))
616
  
617 618 619 620 621
  (find-mid-max n 0.0 (/ pi (+ n .5))))


(defgenerator (nsin5
	       :make-wrapper (lambda (g)
622
			       (convert-frequency g)
623 624
			       (set! (g 'n) (max 2 (g 'n)))
			       (set! (g 'norm) (find-nsin5-max (g 'n)))
625
			       g))
626
  (frequency 0.0) (n 2) (angle 0.0) (norm 1.0) fm)
627 628


629
(define nsin5 
630

631
  (let ((+documentation+ "(make-nsin5 frequency (n 1)) creates an nsin5 generator. (nsin5 gen (fm 0.0)) 
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
returns n sines spaced by frequency. All are equal amplitude except last at half amp."))
    
    ;; from "Chebyshev Polynomials", Mason and Handscomb, p100
    
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let* ((x angle)
	       (den (tan (* 0.5 x))))
	  (set! angle (+ angle fm frequency))
	  (if (< (abs den) nearly-zero)
	      0.0
	      (/ (- 1.0 (cos (* n x)))
		 (* den norm))))))))
    
647 648

(define (find-nsin-max n)
649
  
650
  (define (find-mid-max n lo hi)
651 652 653 654 655 656
    (define (ns x n) 
      (let* ((a2 (/ x 2))
	     (den (sin a2)))
	(if (= den 0.0)
	    0.0
	    (/ (* (sin (* n a2)) (sin (* (+ 1 n) a2))) den))))
657 658 659 660 661 662 663 664
    (let ((mid (/ (+ lo hi) 2)))
      (let ((ylo (ns lo n))
	    (yhi (ns hi n)))
	(if (< (abs (- ylo yhi)) 1e-14)
	    (ns mid n) ; rationalize (/ mid pi) for location
	    (if (> ylo yhi)
		(find-mid-max n lo mid)
		(find-mid-max n mid hi))))))
665
  
666 667 668 669 670
  (find-mid-max n 0.0 (/ pi (+ n .5))))


(with-sound (:clipped #f :statistics #t)
  (let ((gen (make-nsin5 100.0 :n 10)))
671 672 673
    (do ((i 0 (+ i 1)))
	((= i 20000))
      (outa i (nsin5 gen)))))
674 675 676 677 678 679

(let ((norms (list 1.0 0.0)))
  (do ((i 2 (+ i 1)))
      ((= i 40))
    (let* ((res (with-sound (:clipped #f)
 	          (let ((gen (make-nsin5 100.0 :n i)))
680 681 682
		    (do ((i 0 (+ i 1)))
			((= i 20000))
		      (outa i (nsin5 gen))))))
683
	   (snd (find-sound res)))
684
      (format () ";~D: ~A" i (maxamp snd 0))
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
      (set! norms (cons (maxamp snd 0) norms))))
  (reverse norms))

;;; from the same book p 110 is atan(x)/x, if x=cos we get:

(with-sound (:clipped #f :statistics #t)
  (let* ((x 0.0)
	 (freq (hz->radians 100.0)))
    (do ((i 0 (+ i 1)))
	((= i 20000))
      (outa i (/ (- (/ (atan (cos x))
		       (cos x))
		    (* 0.5 1.76275))
		 -0.1187))
      (set! x (+ x freq)))))

(let ((sum 0.0))
  (do ((s 1 (+ s 2)))
      ((>= s 100))
    (set! sum (+ sum (* 4 (/ (expt (- (sqrt 2.0) 1.0) (+ (* 2 s) 1))
705
			     (+ (* 2 s) 1))))))
706 707 708 709 710 711 712
  sum) ; ~ 0.096

;;; the evens cancel, each of the odds gets through once
|#



713

714 715 716 717 718 719 720 721 722 723
(define generator-max-r 0.999999)
(define generator-min-r -0.999999)
(define (generator-clamp-r r)
  (min generator-max-r (max generator-min-r r)))


;;; --------------------------------------------------------------------------------
;;;
;;; n sinusoids scaled by r: nrsin, nrcos, nrssb

724 725 726 727 728 729 730 731 732 733 734 735
#|
(define nrsin-methods
  (list
   (cons 'mus-frequency
	 (dilambda
	  (lambda (g) (mus-frequency (g 'gen)))
	  (lambda (g val) (set! (mus-frequency (g 'gen)) val))))
   (cons 'mus-scaler
	 (dilambda
	  (lambda (g) (mus-scaler (g 'gen)))
	  (lambda (g val) (set! (mus-scaler (g 'gen)) val))))))

736 737
(defgenerator (nrsin
	       :make-wrapper (lambda (g)
738 739
			       (set! (g 'r) (generator-clamp-r (g 'r)))
			       (set! (g 'gen) (make-nrxysin (g 'frequency) 1.0 (g 'n) (g 'r)))
740
			       g)
741
	       :methods nrsin-methods)
742
  (frequency 0.0) (n 1) (r 0.5) 
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
  (gen #f))
|#

(define make-nrsin make-nrxysin)
(define nrsin nrxysin)
(define nrsin? nrxysin?)

;;  "(make-nrsin frequency (n 1) (r 0.5)) creates an nrsin generator.\n\
;;   (nrsin gen (fm 0.0)) returns n sines spaced by frequency with amplitudes scaled by r^k."


(define (nrcos-set-scaler g val)
  (set! (g 'r) (min 0.999999 (max -0.999999 val)))
  (with-let g
    (let ((absr (abs r)))
      (set! rr (* r r))
      (set! r1 (+ 1.0 rr))
      (set! norm (- (/ (- (expt absr n) 1) (- absr 1)) 1.0))
      (set! trouble (or (= n 1) 
			(< absr 1.0e-12)))))
  val)
  
(define nrcos-methods
  (list
   (cons 'mus-order
	 (dilambda
	  (lambda (g) (- (g 'n) 1))
	  (lambda (g val) 
	    (set! (g 'n) (+ 1 val))
	    (set! (g 'e1) (expt (g 'r) (g 'n)))
	    (set! (g 'e2) (expt (g 'r) (+ (g 'n) 1)))
	    (set! (g 'norm) (- (/ (- (expt (abs (g 'r)) (g 'n)) 1) (- (abs (g 'r)) 1)) 1.0))
	    (set! (g 'trouble) (or (= (g 'n) 1) (< (abs (g 'r)) nearly-zero)))
	    val)))
   (cons 'mus-frequency
	 (dilambda
	  (lambda (g) (radians->hz (g 'frequency)))
	  (lambda (g val) (set! (g 'frequency) (hz->radians val)))))
   (cons 'mus-scaler
	 (dilambda
	  (lambda (g) (g 'r))
	  nrcos-set-scaler))))
785 786 787

(defgenerator (nrcos
	       :make-wrapper (lambda (g)
788
			       (convert-frequency g)
789 790 791 792 793 794 795 796
			       (set! (g 'n) (+ 1 (g 'n)))
			       (set! (g 'r) (generator-clamp-r (g 'r)))
			       (set! (g 'rr) (* (g 'r) (g 'r)))
			       (set! (g 'r1) (+ 1.0 (g 'rr)))
			       (set! (g 'e1) (expt (g 'r) (g 'n)))
			       (set! (g 'e2) (expt (g 'r) (+ (g 'n) 1)))
			       (set! (g 'norm) (- (/ (- (expt (abs (g 'r)) (g 'n)) 1) (- (abs (g 'r)) 1)) 1.0)) ; n+1??
			       (set! (g 'trouble) (or (= (g 'n) 1) (< (abs (g 'r)) nearly-zero)))
797
			       g)
798
	       :methods nrcos-methods)
799
  (frequency 0.0) (n 1) (r 0.5) (angle 0.0) fm rr r1 e1 e2 norm trouble)
800 801 802 803


(define nrcos 

804
  (let ((+documentation+ "(make-nrcos frequency (n 1) (r 0.5)) creates an nrcos generator. (nrcos gen (fm 0.0)) 
805 806 807 808 809 810 811 812 813 814
returns n cosines spaced by frequency with amplitudes scaled by r^k."))
  
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let ((x angle)
	      (rcos (* r (cos angle))))
	  (set! angle (+ angle fm frequency))
	  (if trouble
	      0.0
815 816
	      (/ (- (+ rcos (* e2 (cos (* (- n 1) x))))
		    (* e1 (cos (* n x))) rr)
817 818 819
		 (* norm (+ r1 (* -2.0 rcos))))))))))

;; it's faster to use polywave here and nrcos->polywave for the partials list (animals.scm) if n is not enormous
820 821

;;; formula changed to start at k=1 and n increased so we get 1 to n
822 823 824 825 826 827 828 829 830 831 832 833 834 835
;;; here is the preoptimization form:
#|
  (with-let gen
    (let ((x angle))
      (set! angle (+ angle fm frequency))
      (if (or (= n 1)
	      (< (abs r) nearly-zero))
	  0.0
	  (let ((norm (- (/ (- (expt (abs r) n) 1) (- (abs r) 1)) 1.0))) ; n+1??
	    (/ (+ (- (* r (cos x)) 
		     (* (expt r n) (cos (* n x))) (* r r)) 
		  (* (expt r (+ n 1)) (cos (* (- n 1) x))))
	       (* norm (+ 1.0 (* -2.0 r (cos x)) (* r r))))))))
|#
836 837 838 839

#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nrcos 400.0 :n 5 :r 0.5)))
840 841 842
    (do ((i 0 (+ i 1)))
	((= i 10000))
      (outa i (* .5 (nrcos gen))))))
843 844 845 846 847

(with-sound (:clipped #f :statistics #t :play #t :scaled-to .1)
  (let ((gen (make-nrcos 1200.0 :n 3 :r 0.99))
	(mod (make-oscil 400.0)) ; multi-carrier fm
	(index 0.01))
848 849 850
    (do ((i 0 (+ i 1)))
	((= i 30000))
      (outa i (nrcos gen (* index (oscil mod)))))))
851 852 853 854 855

(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nrcos 2000.0 :n 3 :r 0.5))
	(mod (make-oscil 400.0)) ; multi-carrier fm
	(index 0.02))
856 857 858
    (do ((i 0 (+ i 1)))
	((= i 30000))
      (outa i (* .5 (nrcos gen (* index (oscil mod))))))))
859 860 861 862 863

(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nrcos 2000.0 :n 3 :r 0.5))
	(mod (make-oscil 400.0))
	(index (make-env '(0 0 1 .1) :length 30000))) ; or '(0 .4 1 0)
864 865 866
    (do ((i 0 (+ i 1)))
	((= i 30000))
      (outa i (* .5 (nrcos gen (* (env index) (oscil mod))))))))
867 868 869
|#

(definstrument (lutish beg dur freq amp)
870 871 872 873 874 875 876 877 878 879 880 881 882 883
  (let ((res1 (max 1 (round (/ 1000.0 (max 1.0 (min 1000.0 freq))))))
	(maxind (max .01 (min .3 (/ (- (log freq) 3.5) 8.0)))))
    (let ((gen (make-nrcos (* freq res1) :n (max 1 (- res1 2))))
	  (mod (make-oscil freq))
	  (start (seconds->samples beg))
	  (stop (seconds->samples (+ beg dur)))
	  (index (make-env (list 0 maxind 1 (* maxind .25) (max dur 2.0) 0.0) :duration dur))
	  (amplitude (make-env (list 0 0  .01 1  .2 1  .5 .5  1 .25  (max dur 2.0) 0.0) :duration dur :scaler amp)))
      (do ((i start (+ i 1)))
	  ((= i stop))
	(let ((ind (env index)))
	  (set! (gen 'r) ind)
	  (outa i (* (env amplitude)
		     (nrcos gen (* ind (oscil mod))))))))))
884 885 886 887 888 889 890 891 892 893 894 895

#|
(with-sound (:clipped #f :statistics #t :play #t)
  (lutish 0 1 440 .1))

(with-sound (:clipped #f :statistics #t :play #t)
  (do ((i 0 (+ i 1)))
      ((= i 10))
    (lutish (* i .1) 2 (* 100 (+ i 1)) .05)))
|#


896 897

;;; G&R second col first and second rows
898 899 900

(defgenerator (nrssb
	       :make-wrapper (lambda (g)
901
			       (convert-frequency g)
902
			       (set! (g 'r) (generator-clamp-r (g 'r)))
903
			       (set! (g 'r) (max (g 'r) 0.0))
904 905 906
			       (set! (g 'rn) (- (expt (g 'r) (g 'n))))
			       (set! (g 'rn1) (expt (g 'r) (+ (g 'n) 1)))
			       (set! (g 'norm) (/ (- (g 'rn) 1) (- (g 'r) 1)))
907
			       g))
908
  (frequency 0.0) (ratio 1.0) (n 1) (r 0.5) (angle 0.0) fm interp rn rn1 norm)
909 910 911 912


(define nrssb 

913
  (let ((+documentation+ "(make-nrssb frequency (ratio 1.0) (n 1) (r 0.5)) creates an nrssb generator. (nrssb gen (fm 0.0)) 
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
returns n sinusoids from frequency spaced by frequency * ratio with amplitudes scaled by r^k."))
  
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let* ((cx angle)
	       (mx (* cx ratio)))
	  (let ((nmx (* n mx))
		(n1mx (* (- n 1) mx))
		(den (* norm (+ 1.0 (* -2.0 r (cos mx)) (* r r)))))
	    (set! angle (+ angle fm frequency))
	    (/ (- (* (sin cx)
		     (+ (* r (sin mx))
			(* rn (sin nmx))
			(* rn1 (sin n1mx))))
		  (* (cos cx)
		     (+ 1.0
			(* -1.0 r (cos mx))
			(* rn (cos nmx))
			(* rn1 (cos n1mx)))))
	       den)))))))


(define nrssb-interp 

939
  (let ((+documentation+ "(make-nrssb frequency (ratio 1.0) (n 1) (r 0.5)) creates an nrssb generator for use with 
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
nrssb-interp. (nrssb-interp gen fm interp) returns n sinusoids from frequency spaced by frequency * ratio with amplitudes 
scaled by r^k. The 'interp' argument determines whether the sidebands are above (1.0) or below (-1.0) frequency."))

    (lambda (gen fm interp)
      (let-set! gen 'fm fm)
      (let-set! gen 'interp interp)
      (with-let gen
	(let* ((cx angle)
	       (mx (* cx ratio)))
	  (let ((nmx (* n mx))
		(n1mx (* (- n 1) mx))
		(den (* norm (+ 1.0 (* -2.0 r (cos mx)) (* r r)))))
	    (set! angle (+ angle fm frequency))
	    (/ (- (* interp 
		     (sin cx)
		     (+ (* r (sin mx))
			(* rn (sin nmx))
			(* rn1 (sin n1mx))))
		  (* (cos cx)
		     (+ 1.0
			(* -1.0 r (cos mx))
			(* rn (cos nmx))
			(* rn1 (cos n1mx)))))
	       den)))))))

965 966 967 968

#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nrssb 1000 0.1 5 0.5)))
969 970 971
    (do ((i 0 (+ i 1)))
	((= i 10000))
      (outa i (nrssb gen)))))
972 973 974 975

(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nrssb 1000 0.1 5 0.5))
	(vib (make-oscil 5)))
976 977 978
    (do ((i 0 (+ i 1)))
	((= i 10000))
      (outa i (nrssb gen (* (hz->radians 100) (oscil vib)))))))
979 980 981
|#

(definstrument (oboish beg dur freq amp aenv)
982 983
  (let ((res1 (max 1 (round (/ 1400.0 (max 1.0 (min 1400.0 freq))))))
	 (mod1 (make-oscil 5.0))
984 985 986 987 988 989 990
	 (res2 (max 1 (round (/ 2400.0 (max 1.0 (min 2400.0 freq))))))
	 (gen3 (make-oscil freq))
	 (start (seconds->samples beg))
	 (amplitude (make-env aenv :duration dur :base 4 :scaler amp))
	 (skenv (make-env (list 0.0 0.0 1 1 2.0 (mus-random 1.0) 3.0 0.0 (max 4.0 (* dur 20.0)) 0.0) 
			  :duration dur :scaler (hz->radians (random (* freq .05)))))
	 (relamp (+ .85 (random .1)))
991 992 993 994 995 996 997 998 999
	 (avib (make-rand-interp 5 .2))
	 (hfreq (hz->radians freq))
	 (h3freq (hz->radians (* .003 freq)))
	 (scl (/ 0.05 amp)))
    (let ((gen (make-nrssb (* freq res1) (/ res1) :n res1 :r 0.75))
	  (gen2 (make-oscil (* freq res2)))
	  (stop (+ start (seconds->samples dur))))
      (do ((i start (+ i 1)))
	  ((= i stop))
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
	(let ((result (let* ((vol (* (+ .8 (rand-interp avib)) 
				     (env amplitude)))
			     (vola (* scl vol))
			     (vib (+ (* h3freq (oscil mod1))
				     (env skenv))))
			 (* vol
			    (+ (* (- relamp vola) 
				  (nrssb-interp gen (* res1 vib) -1.0))
			       (* (- (+ 1.0 vola) relamp) 
				  (oscil gen2 (+ (* vib res2) 
						 (* hfreq (oscil gen3 vib))))))))))
1011 1012
	  (outa i result)
	  (if *reverb* (outa i (* .01 result) *reverb*)))))))
1013 1014 1015

#|
(with-sound (:clipped #f :statistics #t :play #t)
1016
  (oboish 0 1 300 .1 '(0 0 1 1 2 0)))
1017 1018

(with-sound (:clipped #f :statistics #t :play #t)
1019 1020 1021
  (do ((i 0 (+ i 1)))
      ((= i 10))
    (oboish (* i .3) .4 (+ 100 (* 50 i)) .05 '(0 0 1 1 2 1 3 0))))
1022 1023

(with-sound (:clipped #f :statistics #t :play #t)
1024 1025
  (let ((rats (vector 1 256/243 9/8 32/27 81/64 4/3 1024/729 3/2 128/81 27/16 16/9 243/128 2))
	(mode (vector 0 0 2 4 11 11 5 6 7 9 2 12 0)))
1026 1027 1028
    (do ((i 0 (+ i 1)))
	((= i 20))
      (oboish (/ (random 32) 8) 
1029 1030 1031 1032 1033 1034 1035
	      (/ (+ 3 (random 8)) 8)
	      (* 16.351 16 (rats (mode (random 12))))
	      (+ .25 (random .25))
	      (let* ((pt1 (random 1.0))
		     (pt2 (+ pt1 (random 1.0)))
		     (pt3 (+ pt2 (random 1.0))))
		(list 0 0 pt1 1 pt2 .5 pt3 0))))))
1036 1037 1038 1039 1040

;;; .85 .15 (* 2 freq) 300, 2400 + 0.5*vib
|#


1041 1042


1043 1044 1045 1046 1047
;;; --------------------------------------------------------------------------------
;;;
;;; n sinusoids scaled by k: nkssb


1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
;;; G&R first col ksinkx cases

(define nkssb-methods
  (list
   (cons 'mus-order
	 (dilambda
	  (lambda (g) (- (g 'n) 1))
	  (lambda (g val) 
	    (set! (g 'n) (+ 1 val))
	    (set! (g 'norm) (/ (* 0.5 val (- val 1))))))))) ; nominal n is off by 1
1058 1059 1060

(defgenerator (nkssb
	       :make-wrapper (lambda (g)
1061
			       (convert-frequency g)
1062 1063
			       (set! (g 'n) (+ 1 (g 'n))) ; sum goes 1 to n-1
			       (set! (g 'norm) (/ (* 0.5 (g 'n) (- (g 'n) 1))))
1064
			       g)
1065
	       :methods nkssb-methods)
1066
  (frequency 0.0) (ratio 1.0) (n 1) (angle 0.0) fm interp norm)
1067 1068


1069 1070
(define nkssb 

1071
  (let ((+documentation+ "(make-nkssb frequency (ratio 1.0) (n 1)) creates an nkssb generator. (nkssb gen (fm 0.0))
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
returns n sinusoids from frequency spaced by frequency * ratio with amplitude k."))
  
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let ((x (* angle ratio)))
	  (let ((cxx (- angle x))
		(sx2 (sin (* 0.5 x)))
		(nx (* n x))
		(nx2 (* 0.5 (- (* 2 n) 1) x)))
	    (let ((sx22 (* 2 sx2))
		  (sxsx (* 4 sx2 sx2)))
	      (set! angle (+ angle fm frequency))
	      (if (< (abs sx2) 1.0e-8)
		  -1.0
		  (let ((s1 (- (/ (sin nx) sxsx)
			       (/ (* n (cos nx2)) sx22)))
			(c1 (- (/ (* n (sin nx2)) sx22)
			       (/ (- 1.0 (cos nx)) sxsx))))
		    (* (- (* s1 (sin cxx))
			  (* c1 (cos cxx)))
		       norm))))))))))


(define nkssb-interp 

1098
  (let ((+documentation+ "  (make-nkssb-interp frequency (ratio 1.0) (n 1)) creates an nkssb generator for 
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
nkssb-interp. (nkssb-interp gen fm interp) returns n sinusoids from frequency spaced by frequency * ratio 
with amplitude k. The 'interp' argument determines whether the sidebands are above (1.0) or below (-1.0) frequency."))
  
    (lambda (gen fm interp)
      (let-set! gen 'fm fm)
      (let-set! gen 'interp interp)
      (with-let gen
	(let ((x (* angle ratio)))
	  (let ((cxx (- angle x))
		(sx2 (sin (* 0.5 x))))
	    (let ((sx22 (* 2 sx2))
		  (sxsx (* 4 sx2 sx2))
		  (nx (* n x))
		  (nx2 (* 0.5 (- (* 2 n) 1) x)))
	      (set! angle (+ angle fm frequency))
	      (if (< (abs sx2) 1.0e-8)
		  1.0
		  (let ((s1 (- (/ (sin nx) sxsx)
			       (/ (* n (cos nx2)) sx22)))
			(c1 (- (/ (* n (sin nx2)) sx22)
			       (/ (- 1.0 (cos nx)) sxsx))))
		    (* (- (* c1 (cos cxx))
			  (* interp (sin cxx) s1))
		       norm))))))))))                    ; peak seems to be solid right through the interpolation

1124 1125 1126
#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nkssb 1000.0 0.1 5)))
1127 1128 1129
    (do ((i 0 (+ i 1)))
	((= i 10000))
      (outa i (nkssb gen)))))
1130 1131 1132 1133 1134

(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nkssb 1000.0 0.1 5))
	(vib (make-oscil 5.0))
	(vibamp (hz->radians 50.0)))
1135 1136 1137
    (do ((i 0 (+ i 1)))
	((= i 30000))
      (outa i (nkssb gen (* vibamp (oscil vib)))))))
1138 1139 1140
|#

(definstrument (nkssber beg dur freq mfreq n vibfreq amp)
1141 1142
  (let ((start (seconds->samples beg))
	 (stop (seconds->samples (+ beg dur)))
1143 1144
	 (gen (make-nkssb freq (/ mfreq freq) n))
	 (move (make-env '(0 1 1 -1) :duration dur))
1145
	 (vib (make-polywave vibfreq (list 1 (hz->radians (* (/ freq mfreq) 5.0))) mus-chebyshev-second-kind))
1146
	 (ampf (make-env '(0 0 1 1 5 1 6 0) :scaler amp :duration dur)))
1147 1148 1149 1150 1151 1152 1153 1154
    (do ((i start (+ i 1)))
	((= i stop))
      (outa i (* (env ampf)
		 (nkssb-interp gen 
			       (polywave vib)
			       (env move))) ; interp env
	    ))))

1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
#|
(with-sound (:play #t)
  (nkssber 0 1 1000 100 5 5 0.5)
  (nkssber 1 2 600 100 4 1 0.5)
  (nkssber 3 2 1000 540 3 3 0.5)
  (nkssber 5 4 300 120 2 0.25 0.5)
  (nkssber 9 1 30 4 40 0.5 0.5)
  (nkssber 10 1 20 6 80 0.5 0.5))

(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nkssb 1000.0 0.1 5))
	(move (make-env '(0 1 1 -1) :length 30000))
	(vib (make-oscil 5.0))
	(vibamp (hz->radians 50.0)))
1169 1170 1171 1172 1173 1174
    (do ((i 0 (+ i 1)))
	((= i 30000))
      (outa i (* 0.5 (nkssb-interp gen 
				   (* vibamp (oscil vib))
				   (env move))) ; interp env
	    ))))
1175 1176 1177 1178 1179

(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nkssb 600.0 1/6 4))
	(vib (make-oscil 1.0))
	(vibamp (hz->radians 30.0)))
1180 1181 1182 1183 1184 1185
    (do ((i 0 (+ i 1)))
	((= i 100000)) 
      (let ((intrp (oscil vib)))
	(outa i (* 0.5 (nkssb-interp gen 
				     (* vibamp intrp)
				     intrp)))))))
1186 1187 1188 1189 1190

(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nkssb 1000.0 (/ 540 1000) 3))
	(vib (make-oscil 3.0)) ; 0.3  or 125 + 0.25 and 2 -> circling sound
	(vibamp (hz->radians (* (/ 1000 540) 5.0))))
1191 1192 1193 1194 1195 1196
    (do ((i 0 (+ i 1)))
	((= i 100000)) 
      (let ((intrp (oscil vib)))
	(outa i (* 0.5 (nkssb-interp gen 
				     (* vibamp intrp)
				     intrp)))))))
1197 1198

(with-sound (:clipped #f :statistics #t :play #t)
1199
  (let ((gen (make-nkssb 300.0 (/ 120 300) 2))
1200 1201
	(vib (make-oscil 0.25))
	(vibamp (hz->radians (* (/ 300 120) 5.0))))
1202 1203 1204 1205 1206 1207
    (do ((i 0 (+ i 1)))
	((= i 300000)) 
      (let ((intrp (oscil vib)))
	(outa i (* 0.5 (nkssb-interp gen 
				     (* vibamp intrp)
				     intrp)))))))
1208 1209 1210 1211 1212

(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nkssb 30.0 (/ 4 30) 40))
	(vib (make-oscil 0.5))
	(vibamp (hz->radians (* (/ 30 4) 5.0))))
1213 1214 1215 1216 1217 1218
    (do ((i 0 (+ i 1)))
	((= i 300000)) 
      (let ((intrp (oscil vib)))
	(outa i (* 0.5 (nkssb-interp gen 
				     (* vibamp intrp)
				     intrp)))))))
1219 1220 1221 1222 1223 1224

(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-nkssb 20.0 (/ 6 20) 80)) ; 120 8 80 (100), 6 400
	
	(vib (make-oscil 0.5))
	(vibamp (hz->radians (* (/ 20 6) 5.0))))
1225 1226 1227 1228 1229 1230
    (do ((i 0 (+ i 1)))
	((= i 300000))
      (let ((intrp (oscil vib)))
	(outa i (* 0.5 (nkssb-interp gen 
				     (* vibamp intrp)
				     intrp)))))))
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
|#


;;; --------------------------------------------------------------------------------

;;; n cos scaled by sin(k*pi/(n+1))/sin(pi/(n+1))
;;; "Biased Trigonometric Polynomials", Montgomery and Vorhauer
;;; American Math Monthly vol 114 no 9 Nov 2007

(defgenerator (nsincos
	       :make-wrapper (lambda (g)
1242
			       (let ((n (g 'n)))
1243
				 (convert-frequency g)
1244 1245 1246
				 (set! (g 'n2) (/ (+ n 1) 2))
				 (set! (g 'cosn) (cos (/ pi (+ n 1))))
				 (do ((k 1 (+ k 1)))
1247
				     ((> k n))
1248 1249 1250
				   (set! (g 'norm) (+ (g 'norm) 
						      (/ (sin (/ (* k pi) (+ n 1))) 
							 (sin (/ pi (+ n 1)))))))
1251
				 g)))
1252
  (frequency 0.0) (n 1) 
1253
  (angle 0.0) (n2 1.0) (cosn 1.0) (norm 0.0) fm)
1254 1255


1256
(define nsincos 
1257

1258
  (let ((+documentation+ "(make-nsincos frequency (n 1)) creates an nsincos generator.  (nsincos gen (fm 0.0)) 
1259
returns n cosines spaced by frequency with amplitude sin(k*pi/(n+1))/sin(pi/(n+1))"))
1260

1261 1262 1263 1264 1265 1266 1267 1268
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let* ((x angle)
	       (num (cos (* n2 x))))
	  (set! angle (+ angle fm frequency))
	  (/ (* num num)
	     (* norm (- (cos x) cosn))))))))
1269 1270 1271 1272

#|
(with-sound (:clipped #f :statistics #t :play #f)
  (let ((gen (make-nsincos 100.0 3)))
1273 1274 1275
    (do ((i 0 (+ i 1)))
	((= i 20000))
      (outa i (nsincos gen)))))
1276 1277 1278
|#


1279 1280


1281 1282 1283 1284
;;; --------------------------------------------------------------------------------
;;;
;;; Ramanujan, "On certain Arithmetical Functions"

1285
(defgenerator (n1cos :make-wrapper convert-frequency)
1286
  (frequency 0.0) (n 1) (angle 0.0) fm)
1287

1288
(define* (n1cos gen (fm 0.0))
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
  (let-set! gen 'fm fm)
  (with-let gen
    (let* ((x angle)
	   (tn (tan (* 0.5 x))))
      (set! angle (+ angle fm frequency))
      (if (< (abs tn) 1.0e-6)
	  1.0
	  (/ (- 1.0 (cos (* n x)))
	     (* tn tn
		n n 2)))))) ; normalization -- this still has the very large DC term
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316

#|
(with-sound (:clipped #f)
  (let ((gen (make-n1cos 100.0 10)))
    (do ((i 0 (+ i 1)))
	((= i 44100))
      (outa i (n1cos gen)))))
|#



#|
;;; --------------------------------------------------------------------------------

;;; not sure the next two are interesting -- 2 more kernels

;;; Dimitrov and Merlo

1317
(defgenerator (npos1cos :make-wrapper convert-frequency)
1318
  (frequency 0.0) (n 1) (angle 0.0) fm)
1319 1320


1321
(define npos1cos 
1322

1323
  (let ((+documentation+ "(make-npos1cos frequency (n 1)) creates an npos1cos generator. (npos1cos gen (fm 0.0)) 
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
returns n cosines spaced by frequency."))
  
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let* ((x angle)
	       (num (- (* (+ n 2) (sin (/ (* n x) 2)))
		       (* n (sin (/ (* (+ n 2) x) 2)))))
	       (sx (sin (/ x 2)))
	       (den (* 4 n (+ n 1) (+ n 2) sx sx sx sx)))
	  (set! angle (+ angle fm frequency))
	  (if (< (abs den) nearly-zero)
	      0.0
	      (/ (* 3 num num)
		 den)))))))
1339 1340 1341 1342 1343 1344

;;; needs normalization and no DC.   side amps seem close


(with-sound (:clipped #f :statistics #t :play #f)
  (let ((gen (make-npos1cos 100.0 3)))
1345 1346 1347
    (do ((i 0 (+ i 1)))
	((= i 20000))
      (outa i (npos1cos gen)))))
1348 1349


1350
(defgenerator (npos3cos :make-wrapper convert-frequency)
1351
  (frequency 0.0) (n 1) (angle 0.0) fm)
1352 1353


1354
(define npos3cos 
1355

1356
  (let ((+documentation+ "(make-npos3cos frequency (n 1)) creates an npos3cos generator. (npos3cos gen (fm 0.0)) 
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
returns n cosines spaced by frequency."))
  
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(let* ((x angle)
	       (sx (sin (/ x 2)))
	       (den (* (+ (* 4 n) 2) sx sx)))
	  (set! angle (+ angle fm frequency))
	  (if (< (abs den) nearly-zero)
	      (* 1.0 n)
	      (/ (- 2 (cos (* n x)) (cos (* (+ n 1) x)))
		 den)))))))
1370 1371 1372 1373 1374

;;; needs normalization and no DC, peak at den=0 not right.   side amps seem close

(with-sound (:clipped #f :statistics #t :play #f)
  (let ((gen (make-npos3cos 100.0 3)))
1375 1376 1377
    (do ((i 0 (+ i 1)))
	((= i 20000))
      (outa i (npos3cos gen)))))
1378 1379 1380 1381
|#



1382

1383 1384 1385 1386
;;; --------------------------------------------------------------------------------
;;;
;;; inf sinusoids scaled by r: rcos, rssb

1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
(define rcos-methods
  (list
   (cons 'mus-frequency
	 (dilambda
	  (lambda (g) (mus-frequency (g 'osc)))
	  (lambda (g val) (set! (mus-frequency (g 'osc)) val))))
   
   (cons 'mus-scaler
	 (dilambda
	  (lambda (g) (g 'r))
	  (lambda (g val) 
	    (set! (g 'r) (generator-clamp-r val))
	    (set! (g 'rr) (* (g 'r) (g 'r)))
	    (set! (g 'rr+1) (+ 1.0 (g 'rr)))
	    (set! (g 'rr-1) (- 1.0 (g 'rr)))
	    (set! (g 'r2) (* 2.0 (g 'r)))
	    (let ((absr (abs (g 'r))))
1404
	      (set! (g 'norm) (if (< absr nearly-zero) 0.0 (/ (- 1.0 absr) (* 2.0 absr)))))
1405 1406 1407 1408 1409 1410 1411
	    val)))
   
   (cons 'mus-phase
	 (dilambda
	  (lambda (g) (mus-phase (g 'osc)))
	  (lambda (g val) (set! (mus-phase (g 'osc)) val))))))

1412 1413
(defgenerator (rcos
	       :make-wrapper (lambda (g)
1414 1415 1416 1417 1418 1419 1420
			       (set! (g 'osc) (make-oscil (g 'frequency) (* 0.5 pi)))
			       (set! (g 'r) (generator-clamp-r (g 'r)))
			       (set! (g 'rr) (* (g 'r) (g 'r)))
			       (set! (g 'rr+1) (+ 1.0 (g 'rr)))
			       (set! (g 'rr-1) (- 1.0 (g 'rr)))
			       (set! (g 'r2) (* 2.0 (g 'r)))
			       (let ((absr (abs (g 'r))))
1421
				 (set! (g 'norm) (if (< absr nearly-zero) 0.0 (/ (- 1.0 absr) (* 2.0 absr)))))
1422
			       g)
1423
	       :methods rcos-methods)
1424
  (frequency 0.0) (r 0.5) fm
1425 1426 1427 1428
  (osc #f) rr norm rr+1 rr-1 r2)

(define rcos 
  
1429
  (let ((+documentation+ "(make-rcos frequency (r 0.5)) creates an rcos generator. (rcos gen (fm 0.0)) 
1430 1431 1432 1433 1434 1435 1436 1437 1438
returns many cosines spaced by frequency with amplitude r^k."))
  
    ;; from Andrews, Askey, Roy "Special Functions" 5.1.16, p243. r^k cos sum
    ;; a variant of the G&R second col 4th row
    
    (lambda* (gen (fm 0.0))
      (let-set! gen 'fm fm)
      (with-let gen
	(* (- (/ rr-1 (- rr+1 (* r2 (oscil osc fm)))) 1.0) norm)))))
1439 1440

#|
1441 1442 1443 1444 1445 1446 1447 1448 1449 1450
  (with-let gen
    (let ((absr (abs r))
	  (rr (* r r)))
      (if (< absr nearly-zero)
	  0.0                       ; 1.0 from the formula, but we're subtracting out DC
	  (* (- (/ (- 1.0 rr)
		   (- (+ 1.0 rr)
		      (* 2.0 r (oscil osc fm))))
		1.0)
	     (/ (- 1.0 absr) (* 2.0 absr))))))) ; normalization
1451 1452
|#

1453 1454
#|
;;; G&R form:
1455
(define* (rcos gen (fm 0.0))
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
  (let-set! gen 'fm fm)
  (with-let gen
    (let* ((absr (abs r))
	   (rcosx (* r (oscil osc fm))))
      (* (- (/ (- 1.0 rcosx)
	       (+ 1.0 
		  (* r r)
		  (* -2.0 rcosx)))
	    1.0)
	 (/ (- 1.0 absr) absr))))) ; normalization
|#
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478

;;; if r>0 we get the spike at multiples of 2pi, since the k*pi case is flipping -1 1 -1 etc
;;; if r<0, we get the spike at multiples of (2k-1)pi since the r sign now counteracts the cos k*pi sign
;;;  so the peak amp is the same in the two cases, so the normalization has to use abs(r)!
;;;  but in the k*pi case we tend to miss k*pi (whereas we never miss 0 since we start there),
;;;  so the actual maxamp may be less than 1.0



#|
(with-sound (:clipped #f :statistics #t :play #t)
  (let ((gen (make-rcos 100.0 :r 0.5)))
1479 1480 1481
    (do ((i 0 (+ i 1)))
	((= i 20000))
      (outa i (rcos gen)))))
1482 1483
|#

1484 1485
;; this uses rkoddssb below

1486
(definstrument (stringy beg dur freq amp)
1487 1488 1489 1490 1491 1492 1493
  (let ((n (floor (/ *clm-srate* (* 3 freq)))))
    (let ((start (seconds->samples beg))
	  (stop (seconds->samples (+ beg dur)))
	  (r (expt .001 (/ n))))
      (let ((carrier (make-rcos freq (* .5 r)))
	    (clang (make-rkoddssb (* freq 2) (/ 1.618 2) r))
	    (ampf (make-env '(0 0 1 1 2 .5 4 .25 10 0) :scaler amp :duration dur))
1494 1495 1496
	    (clangf (make-env '(0 0 .1 1 .2 .1 .3 0) :scaler (* amp .5) :duration .1))
	    (rf (make-env '(0 1 1 0) :scaler (* 0.5 r) :duration dur))
	    (crf (make-env '(0 1 1 0) :scaler r :duration .1)))
1497
	(let ((set-clang-scaler (setter (clang 'mus-scaler))))
1498 1499 1500 1501 1502 1503 1504 1505
	  (do ((i start (+ i 1)))
	      ((= i stop))
	    (set-clang-scaler clang (env crf))  ;(set! (mus-scaler clang) (env crf))
	    (set! (carrier 'r) (env rf))
	    (outa i (+ (* (env clangf)
			  (rkoddssb clang 0.0))
		       (* (env ampf)
			  (rcos carrier 0.0))))))))))
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517

#|
(with-sound (:clipped #f :statistics #t :play #t)
  (stringy 0 1 1000 .5))

(with-sound (:clipped #f :statistics #t :play #t)
  (do ((i 0 (+ i 1)))
      ((= i 10))
    (stringy (* i .3) .3 (+ 200 (* 100 i)) .5)))
|#


1518 1519 1520 1521 1522 1523 1524
(define rssb-methods
  (list
   (cons 'mus-scaler
	 (dilambda
	  (lambda (g) (g 'r))
	  (lambda (g val) (set! (g 'r) (generator-clamp-r val)))))))