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.

преди 2 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # noise.rb -- CLM -> Snd/Ruby translation of noise.ins
  2. # Translator/Author: Michael Scholz <mi-scholz@users.sourceforge.net>
  3. # Created: Wed Mar 19 05:16:56 CET 2003
  4. # Changed: Thu Oct 15 00:17:48 CEST 2009
  5. # Comments beginning with ;; are taken from noise.ins!
  6. # attack_point(dur, attack, decay, total_x = 100.0)
  7. # fm_noise(...)
  8. # ;;; The "noise" instrument (useful for Oceanic Music):
  9. require "ws"
  10. require "env"
  11. include Env
  12. def attack_point(dur, attack, decay, total_x = 100.0)
  13. x = if 0.0 == attack
  14. if 0.0 == decay
  15. dur / 4.0
  16. else
  17. (dur - decay) / 4.0
  18. end
  19. else
  20. attack.to_f
  21. end
  22. total_x * (x / dur)
  23. end
  24. def fm_noise(start, dur, freq0,
  25. amp, ampfun, ampat, ampdc,
  26. freq1, glissfun, freqat, freqdc,
  27. rfreq0, rfreq1, rfreqfun, rfreqat, rfreqdc,
  28. dev0, dev1, devfun, devat, devdc,
  29. *args)
  30. degree, distance, reverb = nil
  31. optkey(args, binding,
  32. [:degree, kernel_rand(90.0)],
  33. [:distance, 1.0],
  34. [:reverb, 0.005])
  35. # ;; ampat = amp envelope attack time, and so on -- this instrument
  36. # ;; assumes your envelopes go from 0 to 100 on the x-axis, and that
  37. # ;; the "attack" portion ends at 25, the "decay" portion starts at
  38. # ;; 75. "rfreq" is the frequency of the random number generator --
  39. # ;; if below about 25 hz you get automatic composition, above that
  40. # ;; you start to get noise. well, you get a different kind of
  41. # ;; noise. "dev" is the bandwidth of the noise -- very narrow
  42. # ;; gives a whistle, very broad more of a whoosh. this is
  43. # ;; basically "simple fm", but the modulating signal is white
  44. # ;; noise.
  45. car = make_oscil(:frequency, freq0)
  46. mod = make_rand(:frequency, rfreq0, :amplitude, 1.0)
  47. dev_0 = hz2radians(dev0)
  48. # ;; next fix-up troubles in attack and decay times (there are lots
  49. # ;; of ways to handle this -- the basic problem is that these
  50. # ;; durned instruments end up having way too many parameters. rick
  51. # ;; taube's common music replacement for pla should help, but just
  52. # ;; for old time's sake, we'll do it the way the ancients did it.
  53. # ;; (we could also package up this stuff in our own function,
  54. # ;; somewhat like the allvln function in vln.clm, leaving the
  55. # ;; instrument code to apply envelopes and other data to some
  56. # ;; patch).
  57. amp_attack = attack_point(dur, ampat, ampdc)
  58. amp_decay = 100.0 - attack_point(dur, ampdc, ampat)
  59. freq_attack = attack_point(dur, freqat, freqdc)
  60. freq_decay = 100.0 - attack_point(dur, freqdc, freqat)
  61. dev_attack = attack_point(dur, devat, devdc)
  62. dev_decay = 100.0 - attack_point(dur, devdc, devat)
  63. rfreq_attack = attack_point(dur, rfreqat, rfreqdc)
  64. rfreq_decay = 100.0 - attack_point(dur, rfreqdc, rfreqat)
  65. # ;; now make the actual envelopes -- these all assume we are
  66. # ;; thinking in terms of the "value when the envelope is 1"
  67. # ;; (i.e. dev1 and friends), and the "value when the envelope is 0"
  68. # ;; (i.e. dev0 and friends) -- over the years this seemed to make
  69. # ;; beginners happier than various other ways of describing the
  70. # ;; y-axis behaviour of the envelope. all this boiler-plate for
  71. # ;; envelopes might seem overly elaborate when our basic instrument
  72. # ;; is really simple, but in most cases, and this one in
  73. # ;; particular, nearly all the musical interest comes from the
  74. # ;; envelopes, not the somewhat dull spectrum generated by the
  75. # ;; basic patch.
  76. dev_f = make_env(stretch_envelope(devfun, 25, dev_attack, 75, dev_decay),
  77. hz2radians(dev1 - dev0), dur)
  78. amp_f = make_env(stretch_envelope(ampfun, 25, amp_attack, 75, amp_decay), amp, dur)
  79. freq_f = make_env(stretch_envelope(glissfun, 25, freq_attack, 75, freq_decay),
  80. hz2radians(freq1 - freq0), dur)
  81. rfreq_f = make_env(stretch_envelope(rfreqfun, 25, rfreq_attack, 75, rfreq_decay),
  82. hz2radians(rfreq1 - rfreq0), dur)
  83. run_instrument(start, dur, :degree, degree, :distance, distance, :reverb_amount, reverb) do
  84. env(amp_f) * oscil(car, env(freq_f) + (dev_0 + env(dev_f)) * rand(mod, env(rfreq_f)))
  85. end
  86. end
  87. =begin
  88. with_sound(:statistics, true, :play, 1) do
  89. fm_noise(0, 1.8, 500,
  90. 0.25, [0, 0, 25, 1, 75, 1, 100, 0], 0.1, 0.1,
  91. 1000, [0, 0, 100, 1], 0.1, 0.1,
  92. 10, 1000, [0, 0, 100, 1], 0, 0,
  93. 100, 500, [0, 0, 100, 1], 0, 0)
  94. fm_noise(2, 1.8, 200,
  95. 0.25, [0, 0, 25, 1, 75, 1, 100, 0], 0.1, 0.1,
  96. 1000, [0, 0, 100, 1], 0.1, 0.1,
  97. 10, 1000, [0, 0, 100, 1], 0, 0,
  98. 100, 500, [0, 0, 100, 1], 0, 0)
  99. end
  100. =end
  101. # noise.rb ends here