Advertisement
SoundEngraver

FM Synthesis | FM Basics (Part 6)

Mar 31st, 2023 (edited)
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /////////////////////// FM SYNTHESIS | FM Basics (Part 6) ///////////////////////
  2. /////////////////////////////// Env Perc ////////////////////////////////
  3.  
  4.  
  5. s.boot;
  6.  
  7. // Run these methods below for visual cues.
  8.  
  9. s.meter;
  10. s.scope;
  11. s.plotTree;
  12.  
  13. // I use the following only when necessary.
  14.  
  15. s.reboot;
  16. s.quit;
  17.  
  18. ////////////////// Things To Know Before Running Code //////////////////
  19.  
  20. /*
  21.  
  22. All my original SC documents are in the "dracula" color.
  23. To find or change this, go To SuperCollider ––> Preferences ––> Editor ––> Font & Colors.
  24. ––> Select under Color, dracula.
  25.  
  26. Arguments in Synths are represented with a backslash, "\", in green under the "dracula" color font. Values presented with these arguments can be changed. Only remember to NEVER cross 0, when using an exponential method like "exprand."
  27.  
  28. Arguments also precede a colon ":", as seen in a list of a UGen following a variable, "var."
  29. Example: sig = PlayBuf.ar(2, buf, rate, startPos: spos);
  30.  
  31. Argument and variable names can be changed to your liking.
  32.  
  33. Please be careful with volume output. Always have SuperCollider run through headphones. If you're unsure what the output will be, keep headphones away, with your main volume all the way down. Check your level meter and Stethoscope for visual cues. If everything looks safe (e.g. level meter is NOT in the red), proceed.
  34.  
  35. You can always execute a hard stop with Command Period.
  36.  
  37. */
  38.  
  39. ///////////////////////  Syntax For Locating Sound Files In Folders ///////////////////////
  40.  
  41. ~bufPath = PathName.new(thisProcess.nowExecutingPath).parentPath ++ "ambient"; // <-- insert your sound folder, in place of where my "ambient" folder is. Your folder can have any name.
  42.  
  43.  
  44. (
  45. b = PathName.new(~bufPath).entries.collect({
  46.     arg pathname;
  47.     Buffer.read(s, pathname.fullPath);
  48. });
  49. )
  50.  
  51.  
  52. // Make sure your soundfiles play back. You should see it on the scope and level meters.
  53.  
  54. // I have 3 files in my "ambient" Folder. Thus, I have below: b[0], b[1], b[2].
  55. // 0, being the first sound file indexed in my Folder.
  56.  
  57. b[0].play; // Change between b[0], b[1], b[2].
  58.  
  59. b[0].duration;
  60. b[0].numChannels;
  61. b[0].sampleRate;
  62. b[0].numFrames;
  63.  
  64.  
  65.  
  66. Env([0, 1, 0], [0.01, 3], [1, -5]).plot; // Envelope shape to reference.
  67. // First array, [0, 1, 0], are levels.
  68. // Second array, [0.01, 3], are times traversing between levels.
  69. // Third array, [1, -5], are curve values.
  70.  
  71.  
  72. // Sine Tone
  73.  
  74. (
  75. SynthDef.new(\sine, {
  76.     arg atk=0.01, rel=3, c1=1, c2=(-5),
  77.     freq=400, amp=0.5, out=0; // Be careful with amp values (0-1).
  78.     var sig, sine, env;
  79.     env = Env([0, 1, 0], [atk, rel], [c1, c2]).kr(2); // Env().kr(2) is a shortcut for "doneAction: 2"
  80.     sig = SinOsc.ar(freq);
  81.     sig = sig * env * amp;
  82.     sig = sig!2;
  83.     Out.ar(out, sig);
  84. }).add;
  85. )
  86.  
  87. Synth(\sine);
  88.  
  89. // Add an Env.perc for short, sharp sounds.
  90.  
  91. Env.perc(0.001, 1, 1, -3).plot; // Env with a very sharp attack
  92. // Env.perc(attackTime, releaseTime, level, curve)
  93.  
  94. (
  95. SynthDef.new(\sine, {
  96.     arg freq=400, amp=0.2, gate=1, out=0;
  97.     var sig, sine, env;
  98.     env = EnvGen.ar(
  99.         Env.perc(0.001, 1, 1, -3),
  100.         gate,
  101.         doneAction: 2
  102.     );
  103.     sig = SinOsc.ar(freq);
  104.     sig = sig * env * amp;
  105.     sig = sig!2;
  106.     Out.ar(out, sig);
  107. }).add;
  108. )
  109.  
  110. Synth(\sine, [\freq, rrand(200, 800)]);
  111. // Randomly selects frequencies between 200 and 800Hz
  112.  
  113. ---------------------------------------------------------------
  114.  
  115. // Sound Files
  116.  
  117. Env([0, 1, 0], [0.1, 8], [1, -2]).plot;
  118.  
  119. (
  120. SynthDef.new(\sound, {
  121.     arg buf=0, rate=1, spos=0, amp=1, pan=0,
  122.     atk=0.01, rel=8, c1=1, c2=(-2), out=0;
  123.     var sig, par, env;
  124.     env = Env([0, 1, 0], [atk, rel], [c1, c2]).kr(2);
  125.     par = LFPar.ar(LFPar.kr(0.25, 0, 8, 10), 0, 0.8);
  126.     sig = PlayBuf.ar(
  127.         2,
  128.         buf,
  129.         BufRateScale.kr(buf) * rate,
  130.         spos
  131.     );
  132.     sig = sig * par * env;
  133.     sig = Pan2.ar(sig, pan, amp);
  134.     Out.ar(out, sig);
  135. }).add;
  136. )
  137.  
  138. Synth(\sound, [\buf, b[0]]);
  139.  
  140.  
  141. // Add an Env.perc for short, sharp sounds.
  142.  
  143. Env.perc(0.001, 1, 1, -3).plot;
  144. // Note: My b[1], b[2] in the Folder have the best attacks.
  145. // See which sound files in your Folders have the best attacks.
  146.  
  147.  
  148. (
  149. SynthDef.new(\sound, {
  150.     arg buf=0, rate=1, spos=0,
  151.     amp=1, pan=0, gate=1, out=0;
  152.     var sig, par, env;
  153.     env = EnvGen.ar(
  154.         Env.perc(0.001, 1, 1, -3),
  155.         gate,
  156.         doneAction: 2
  157.     );
  158.     par = LFPar.ar(LFPar.kr(0.25, 0, 9, 12), 0, 0.8);
  159.     sig = PlayBuf.ar(
  160.         2,
  161.         buf,
  162.         BufRateScale.kr(buf) * rate,
  163.         spos
  164.     );
  165.     sig = sig * par * env;
  166.     sig = Pan2.ar(sig, pan, amp);
  167.     Out.ar(out, sig);
  168. }).add;
  169. )
  170.  
  171. (
  172. Synth(\sound, [
  173.     \buf, b[1],
  174.     \amp, rrand(0.1, 1),
  175.     \rate, rrand(0, 24).midiratio
  176. ]);
  177. )
  178.  
Tags: fm synthesis
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement