You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 line
7.1KB

  1. ;;; noise.scm -- CLM -> Snd/Scheme translation of noise.ins
  2. ;; Translator/Author: Michael Scholz <scholz-micha@gmx.de>
  3. ;; Last: Wed Apr 02 02:47:21 CEST 2003
  4. ;; Version: $Revision: 1.9 $
  5. ;;; Comments not otherwise noted are taken from noise.ins!
  6. ;; Included functions:
  7. ;; (attack-point duration attack decay (total-x 100.0))
  8. ;; (fm-noise ...)
  9. ;; (make-fm-noise len freq ...)
  10. ;;; The "noise" instrument (useful for Oceanic Music):
  11. (provide 'snd-noise.scm)
  12. (require snd-ws.scm snd-env.scm)
  13. (define *locsig-type* mus-interp-sinusoidal)
  14. (define* (attack-point duration attack decay (total-x 100.0))
  15. (* total-x (/ (if (= 0.0 attack)
  16. (/ (if (= 0.0 decay) duration (- duration decay)) 4)
  17. attack)
  18. duration)))
  19. (definstrument (fm-noise startime dur freq0 amp ampfun ampat ampdc
  20. freq1 glissfun freqat freqdc rfreq0 rfreq1 rfreqfun rfreqat rfreqdc
  21. dev0 dev1 devfun devat devdc
  22. (degree 0.0)
  23. (distance 1.0)
  24. (reverb-amount 0.005))
  25. ;; ampat = amp envelope attack time, and so on -- this instrument
  26. ;; assumes your envelopes go from 0 to 100 on the x-axis, and that
  27. ;; the "attack" portion ends at 25, the "decay" portion starts at
  28. ;; 75. "rfreq" is the frequency of the random number generator --
  29. ;; if below about 25 hz you get automatic composition, above that
  30. ;; you start to get noise. well, you get a different kind of noise.
  31. ;; "dev" is the bandwidth of the noise -- very narrow gives a
  32. ;; whistle, very broad more of a whoosh. this is basically "simple
  33. ;; fm", but the modulating signal is white noise.
  34. (let ((beg (seconds->samples startime))
  35. (end (seconds->samples (+ startime dur)))
  36. (carrier (make-oscil freq0))
  37. (modulator (make-rand :frequency rfreq0 :amplitude 1.0))
  38. (loc (make-locsig :degree degree
  39. :distance distance
  40. :reverb reverb-amount
  41. :type *locsig-type*))
  42. ;; now make the actual envelopes -- these all assume we are
  43. ;; thinking in terms of the "value when the envelope is 1"
  44. ;; (i.e. dev1 and friends), and the "value when the envelope
  45. ;; is 0" (i.e. dev0 and friends) -- over the years this
  46. ;; seemed to make beginners happier than various other ways
  47. ;; of describing the y-axis behaviour of the envelope. all
  48. ;; this boiler-plate for envelopes might seem overly
  49. ;; elaborate when our basic instrument is really simple, but
  50. ;; in most cases, and this one in particular, nearly all the
  51. ;; musical interest comes from the envelopes, not the
  52. ;; somewhat dull spectrum generated by the basic patch.
  53. (dev-f (let ((dev-attack (attack-point dur devat devdc))
  54. (dev-decay (- 100.0 (attack-point dur devdc devat))))
  55. (make-env (stretch-envelope devfun 25 dev-attack 75 dev-decay)
  56. :duration dur
  57. :offset (hz->radians dev0)
  58. :scaler (hz->radians (- dev1 dev0)))))
  59. (amp-f (let ((amp-attack (attack-point dur ampat ampdc))
  60. (amp-decay (- 100.0 (attack-point dur ampdc ampat))))
  61. (make-env (stretch-envelope ampfun 25 amp-attack 75 amp-decay)
  62. :duration dur :scaler amp)))
  63. (freq-f (let ((freq-attack (attack-point dur freqat freqdc))
  64. (freq-decay (- 100.0 (attack-point dur freqdc freqat))))
  65. (make-env (stretch-envelope glissfun 25 freq-attack 75 freq-decay)
  66. :duration dur :scaler (hz->radians (- freq1 freq0)))))
  67. (rfreq-f (let ((rfreq-attack (attack-point dur rfreqat rfreqdc))
  68. (rfreq-decay (- 100.0 (attack-point dur rfreqdc rfreqat))))
  69. (make-env (stretch-envelope rfreqfun 25 rfreq-attack 75 rfreq-decay)
  70. :duration dur :scaler (hz->radians (- rfreq1 rfreq0))))))
  71. (do ((i beg (+ i 1)))
  72. ((= i end))
  73. (locsig loc i (* (env amp-f)
  74. (oscil carrier (+ (env freq-f)
  75. (* (env dev-f) (rand modulator (env rfreq-f))))))))))
  76. ;;; (with-sound () (fm-noise 0 0.5 500 0.25 '(0 0 25 1 75 1 100 0) 0.1 0.1 1000 '(0 0 100 1) 0.1 0.1 10 1000 '(0 0 100 1) 0 0 100 500 '(0 0 100 1) 0 0))
  77. ;; (let* ((ofile "test.snd")
  78. ;; (snd (find-sound ofile)))
  79. ;; (if snd
  80. ;; (close-sound snd))
  81. ;; (with-sound (:output ofile :play 1 :statistics #t)
  82. ;; (fm-noise 0 2.0 500 0.25 '(0 0 25 1 75 1 100 0) 0.1 0.1
  83. ;; 1000 '(0 0 100 1) 0.1 0.1
  84. ;; 10 1000 '(0 0 100 1) 0 0
  85. ;; 100 500 '(0 0 100 1) 0 0)))
  86. ;;; And here is a generator-like instrument, see make-fm-violin in
  87. ;;; fmv.scm. [MS]
  88. (define* (make-fm-noise len freq
  89. (amp 0.25)
  90. (ampfun '(0 0 25 1 75 1 100 0))
  91. (ampat 0.1)
  92. (ampdc 0.1)
  93. (freq1 1000)
  94. (glissfun '(0 0 100 1))
  95. (freqat 0.1)
  96. (freqdc 0.1)
  97. (rfreq0 10)
  98. (rfreq1 1000)
  99. (rfreqfun '(0 0 100 1))
  100. (rfreqat 0)
  101. (rfreqdc 0)
  102. (dev0 100)
  103. (dev1 500)
  104. (devfun '(0 0 100 1))
  105. (devat 0)
  106. (devdc 0)
  107. ; (degree (random 90.0))
  108. ; (distance 1.0)
  109. ; (reverb-amount 0.005)
  110. )
  111. (let ((dur (/ len (floor (srate)))))
  112. (let ((dev-ff (let ((dev-attack (attack-point dur devat devdc))
  113. (dev-decay (- 100.0 (attack-point dur devdc devat))))
  114. (make-env (stretch-envelope devfun 25 dev-attack 75 dev-decay)
  115. :duration dur :scaler (hz->radians (- dev1 dev0)))))
  116. (amp-ff (let ((amp-attack (attack-point dur ampat ampdc))
  117. (amp-decay (- 100.0 (attack-point dur ampdc ampat))))
  118. (make-env (stretch-envelope ampfun 25 amp-attack 75 amp-decay)
  119. :duration dur :scaler amp)))
  120. (freq-ff (let ((freq-attack (attack-point dur freqat freqdc))
  121. (freq-decay (- 100.0 (attack-point dur freqdc freqat))))
  122. (make-env (stretch-envelope glissfun 25 freq-attack 75 freq-decay)
  123. :duration dur :scaler (hz->radians (- freq1 freq)))))
  124. (rfreq-ff (let ((rfreq-attack (attack-point dur rfreqat rfreqdc))
  125. (rfreq-decay (- 100.0 (attack-point dur rfreqdc rfreqat))))
  126. (make-env (stretch-envelope rfreqfun 25 rfreq-attack 75 rfreq-decay)
  127. :duration dur :scaler (hz->radians (- rfreq1 rfreq0)))))
  128. (carrier (make-oscil freq))
  129. (modulator (make-rand :frequency rfreq0 :amplitude 1.0))
  130. (dev-0 (hz->radians dev0)))
  131. (let ((dev-f (lambda () (env dev-ff)))
  132. (amp-f (lambda () (env amp-ff)))
  133. (freq-f (lambda () (env freq-ff)))
  134. (rfreq-f (lambda () (env rfreq-ff))))
  135. (lambda ()
  136. (* (amp-f) (oscil carrier (+ (freq-f) (* (+ dev-0 (dev-f)) (rand modulator (rfreq-f)))))))))))
  137. ;; (let* ((beg 0)
  138. ;; (dur 9.8)
  139. ;; (len (+ beg (floor (* dur (srate)))))
  140. ;; (chns 4)
  141. ;; (outfile "test.snd")
  142. ;; (snd (find-sound outfile))
  143. ;; (loc (make-locsig :degree (random 3535.0) :channels chns))
  144. ;; (data (make-float-vector len)))
  145. ;; (do ((i 0 (+ i 1)))
  146. ;; ((= i len))
  147. ;; (set! (data i) (make-fm-noise len 500)))
  148. ;; (if snd
  149. ;; (close-sound snd))
  150. ;; (set! snd (new-sound outfile chns *clm-srate* mus-bshort mus-next))
  151. ;; (do ((i 0 (+ i 1)))
  152. ;; ((= i chns))
  153. ;; (mix-float-vector (float-vector-scale! (copy data) (locsig-ref loc i)) beg snd i #f))
  154. ;; (let* ((beg (floor (* 10 (srate))))
  155. ;; (len (+ beg (floor (* dur (srate)))))
  156. ;; (loc (make-locsig :degree (random 3535.0) :channels chns))
  157. ;; (data (make-float-vector len)))
  158. ;; (do ((i 0 (+ i 1)))
  159. ;; ((= i len))
  160. ;; (set! (data i) (make-fm-noise len 200)))
  161. ;; (do ((i 0 (+ i 1)))
  162. ;; ((= i chns))
  163. ;; (mix-float-vector (float-vector-scale! (copy data) (locsig-ref loc i)) beg snd i #f))
  164. ;; (play snd 0)))
  165. ;; noise.scm ends here