Advertisement
SoundEngraver

FM Synthesis | Sound Files

Jan 14th, 2023 (edited)
990
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /////////////////////// FM SYNTHESIS | SOUND FILES ///////////////////////
  2.  
  3. s.boot;
  4.  
  5. // Run these methods below for visual cues.
  6.  
  7. s.meter;
  8. s.scope;
  9. s.plotTree;
  10.  
  11. // I use the following only when necessary.
  12.  
  13. s.reboot;
  14. s.quit;
  15.  
  16. /////////////////////// Things To Know Before Running Code ///////////////////////
  17.  
  18. /*
  19.  
  20. All my original SC documents are in the "dracula" color.
  21. To find or change this, go To SuperCollider ––> Preferences ––> Editor ––> Font & Colors.
  22. ––> Select under Color, dracula.
  23.  
  24. 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."
  25.  
  26. Arguments also precede a colon ":", as seen in a list of a UGen following a variable, "var."
  27. Example: sig = PlayBuf.ar(2, buf, rate, startPos: spos);
  28.  
  29. Argument and variable names can be changed to your liking.
  30.  
  31. 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.
  32.  
  33. You can always execute a hard stop with Command Period.
  34.  
  35.  
  36. */
  37.  
  38. ///////////////////////  Syntax For Locating Sound Files In Folders ///////////////////////
  39.  
  40. ~bufPath = PathName.new(thisProcess.nowExecutingPath).parentPath ++ "ambient";
  41. // <-- insert your sound folder, in place of where my "ambient" folder is. Your folder can have any name.
  42.  
  43. (
  44. b = PathName.new(~bufPath).entries.collect({
  45.     arg pathname;
  46.     Buffer.read(s, pathname.fullPath);
  47. });
  48. )
  49.  
  50. // Make sure your sound files play back. You should see it on the scope and level meters.
  51.  
  52. // I have 3 files in my "ambient" Folder. Thus, I have below: b[0], b[1], b[2].
  53. // 0, being the first sound file indexed in my Folder.
  54.  
  55. b[0].play;
  56. b[1].play;
  57. b[2].play;
  58.  
  59. b[0].duration;
  60. b[1].duration;
  61. b[2].duration;
  62.  
  63.  
  64. ///////////////////////  SynthDefs ///////////////////////
  65.  
  66.  
  67. Env([0, 1, 0], [1, 1], [3, -1]).plot; // Envelope shape to reference.
  68. // First array, [0, 1, 0], are levels.
  69. // Second array, [1, 1], are times; in this case, you're crossing 1st array values in 1 second.
  70. // Third array, [3, -1], are curve values.
  71.  
  72. (
  73. SynthDef.new(\play, {
  74.     arg amp=0.5, atk=0.1, rel=1, c1=3, c2=(-1),
  75.     buf=0, rate=1, spos=0, pan=0, out=0;
  76.     var env, sig;
  77.     env = Env([0, 1, 0], [atk, rel], [c1, c2]).kr(2);
  78.     sig = PlayBuf.ar(2, buf, BufRateScale.kr(buf) * rate, spos);
  79.     sig = sig * env;
  80.     sig = Pan2.ar(sig, pan, amp);
  81.     Out.ar(out, sig);
  82. }).add;
  83. )
  84.  
  85.  
  86. Synth(\play, [\buf, b[0], \amp, 0.5, \rel, 12]);
  87. // Feel free to change the number in your array.
  88. // For example: \buf, b[1], (or b[2]). Values depend on the number and order of files in your Folder.
  89. Synth(\play, [\buf, b[0], \amp, 0.5, \rate, 5.midiratio, \rel, 12]);
  90.  
  91.  
  92. // LFPar - Parabolic Oscillator
  93.  
  94. Env([0, 1, 0], [1, 1], [3, -1]).plot;
  95. {LFPar.ar(2)}.plot(1); // LFPar is smooth.
  96. {LFCub.ar(2)}.plot(1); // Compare with LFPar.
  97. {LFSaw.ar(2)}.plot(1); // Compare with the above.
  98. {LFTri.ar(2)}.plot(1); // Compare with the above.
  99. {LFPulse.ar(2)}.plot(1); // Please use the Lag UGen to clean up any clipping.
  100.  
  101.  
  102. /* For a more comprehensive explanation on Lag, please check out the following YouTube videos.
  103.  
  104. Eli Fieldsteel's:
  105.  
  106. https://www.youtube.com/watch?v=JCqBPmpj8Gc
  107.  
  108.  
  109. My video: Basic Sound Syntheis (Lag UGen)
  110.  
  111. https://www.youtube.com/watch?v=DM0zGrPQ5cc&t=580s
  112.  
  113. */
  114.  
  115. // Note: You can write out the PlayBuf UGen arguments vertically (I do for clarity).
  116. // The same is true for Env, or anything involving a list or an array.
  117.  
  118.  
  119. (
  120. SynthDef(\playPar, {
  121.     arg amp=0.5, parFreq=3, atk=0.01, rel=10, c1=4, c2=(-2),
  122.     buf=0, rate=1, spos=0, pan=0, out=0;
  123.     var env, par, sig;
  124.     par = LFPar.ar(parFreq);
  125.     env = Env([0, 1, 0], [atk, rel], [c1, c2]).kr(2);
  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. // As mentioned above, you can change argument values to your liking.
  139. // As also mentioned, be sure never to cross 0 when using exponentially dynamic methods.
  140.  
  141. (
  142. Synth.new(\playPar, [
  143.     \buf, b[0],
  144.     \amp, rrand(0.1, 0.8),
  145.     \rate, rrand(-12, 12).midiratio,
  146.     \parFreq, rrand(1, 10),
  147.     \rel, 10
  148. ]);
  149. )
  150.  
  151. // Frequency Modulation
  152.  
  153. // Be sure to expand the windows for better accuracy.
  154.  
  155. {LFPar.ar(Line.kr(50, 500, 0.1))}.plot(0.1); // Example from LFPar Help doc.
  156.  
  157. {LFPar.ar(Line.kr(200, 20, 10), 0, 1)}.plot(1); // decreasing frequency
  158. {LFPar.ar(Line.kr(20, 200, 10), 0, 0.5)}.plot(1); // increasing frequency
  159.  
  160.  
  161. // Increasing Frequency
  162.  
  163. Env([0, 1, 0], [0.1, 10], [1, -3]).plot;
  164.  
  165. // Here's another way of writing out the arguments.
  166. // Check PlayBuf in the Help browser, in order to see the designated argument names.
  167.  
  168. (
  169. SynthDef(\parFM, {
  170.     arg amp=0.5, atk=0.1, rel=1, c1=1, c2=(-3),
  171.     buf=0, rate=1, spos=0, pan=0, out=0;
  172.     var env, par, sig;
  173.     env = Env([0, 1, 0], [atk, rel], [c1, c2]).kr(2);
  174.     par = LFPar.ar(Line.kr(2, 200, 10));
  175.     sig = PlayBuf.ar(
  176.         numChannels: 2,
  177.         bufnum: buf,
  178.         rate: BufRateScale.kr(buf) * rate,
  179.         startPos: spos
  180.     );
  181.     sig = sig * par * env;
  182.     sig = Pan2.ar(sig, pan, amp);
  183.     Out.ar(out, sig);
  184. }).add;
  185. )
  186.  
  187. (
  188. Synth(\parFM, [
  189.     \buf, b[1],
  190.     \amp, rrand(0.1, 0.8),
  191.     \rate, rrand(-12, 12).midiratio,
  192.     \atk, 0.001,
  193.     \rel, 1,
  194. ]);
  195. )
  196.  
  197. // Add FM Control
  198. // Add a sustain argument (ASR Envelope):
  199.  
  200. Env([0, 0.8, 1, 0], [0.1, 2, 1], [1, -3]).plot;
  201.  
  202. (
  203. SynthDef(\parFMControl, {
  204.     arg amp=0.5, atk=0.1, sus=2, rel=1, c1=1, c2=(-3),
  205.     min=1, max=100, dur=10,
  206.     buf=0, rate=1, spos=0, pan=0, out=0;
  207.     var env, par, sig;
  208.     env = Env([0, 0.8, 1, 0], [atk, sus, rel], [c1, c2]).kr(2);
  209.     par = LFPar.ar(Line.kr(min, max, dur));
  210.     sig = PlayBuf.ar(
  211.         numChannels: 2,
  212.         bufnum: buf,
  213.         rate: BufRateScale.kr(buf) * rate,
  214.         startPos: spos
  215.     );
  216.     sig = sig * par * env;
  217.     sig = Pan2.ar(sig, pan, amp);
  218.     Out.ar(out, sig);
  219. }).add;
  220. )
  221.  
  222. (
  223. Synth(\parFMControl, [
  224.     \buf, b[2],
  225.     \amp, rrand(0.1, 0.8),
  226.     \rate, rrand(0, 12).midiratio,
  227.     \min, rrand(1, 20),
  228.     \max, rrand(40, 80),
  229.     \atk, 0.01,
  230.     \sus, 2,
  231.     \rel, 2
  232. ]);
  233. )
  234.  
  235. // Decreasing Frequency
  236.  
  237. (
  238. SynthDef(\parFMd, {
  239.     arg amp=0.5, atk=0.1, sus=3, rel=10, c1=1, c2=(-3), c3=(-1),
  240.     buf=0, rate=1, spos=0, pan=0, out=0;
  241.     var env, par, sig;
  242.     env = Env([0, 1, 1, 0], [atk, sus, rel], [c1, c2, c3]).kr(2);
  243.     par = LFPar.kr(Line.kr(200, 4, 8));
  244.     sig = PlayBuf.ar(
  245.         numChannels: 2,
  246.         bufnum: buf,
  247.         rate: BufRateScale.kr(buf) * rate,
  248.         startPos: spos
  249.     );
  250.     sig = sig * par * env;
  251.     sig = Pan2.ar(sig, pan, amp);
  252.     Out.ar(out, sig);
  253. }).add;
  254. )
  255.  
  256. (
  257. Synth.new(\parFMd, [
  258.     \buf, b[2],
  259.     \amp, rrand(0.1, 1),
  260.     \rate, rrand(-12, 18).midiratio,
  261.     \sus, 12,
  262.     \rel, 2
  263. ]);
  264. )
  265.  
  266. // Add FM Control
  267.  
  268. (
  269. SynthDef(\parFMdControl, {
  270.     arg amp=0.5, atk=0.1, sus=3, rel=1, c1=1, c2=(-3), c3=(-1),
  271.     max=100, min=2, dur=10,
  272.     buf=0, rate=1, spos=0, pan=0, out=0;
  273.     var env, par, sig;
  274.     env = Env([0, 1, 1, 0], [atk, sus, rel], [c1, c2, c3]).kr(2);
  275.     par = LFPar.ar(Line.kr(max, min, dur));
  276.     sig = PlayBuf.ar(
  277.         numChannels: 2,
  278.         bufnum: buf,
  279.         rate: BufRateScale.kr(buf) * rate,
  280.         startPos: spos
  281.     );
  282.     sig = sig * par * env;
  283.     sig = Pan2.ar(sig, pan, amp);
  284.     Out.ar(out, sig);
  285. }).add;
  286. )
  287.  
  288. // Add randomness
  289.  
  290. (
  291. Synth.new(\parFMdControl, [
  292.     \buf, b[0],
  293.     \amp, rrand(0.1, 0.8),
  294.     \rate, rrand(-4, 4).midiratio,
  295.     \min, rrand(2, 8),
  296.     \max, rrand(20, 80),
  297.     \dur, rrand(10, 24),
  298.     \atk, rrand(0.01, 1),
  299.     \sus, rrand(10, 20),
  300.     \rel, rrand(1, 4)
  301. ])
  302. )
  303.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement