{
   "description" : "I tried to increase the modularity of the code synthdef. It remains to improve the modularity and conciseness of code pattern. I find it too \"heavy\", too \"long\" ...\r\n\r\nI also added comments in English this time!",
   "ancestor_list" : [],
   "name" : "Little FM Synth (Second Version)",
   "author" : "yl0506",
   "id" : "1-4Tx",
   "is_private" : null,
   "code" : "/* My simple synth with some controls, and then a small musical pattern to test it.\r\nI'm a newbie, so your help will be welcome if you learn me how improve these codes.\r\n\r\nGoal :\r\n\r\nThis little exercise is to understand how the modulation of parameters of a synthesizer is possible with SuperCollider\r\n\r\nday 2 :\r\n\r\nI tried to increase the modularity of the code synthdef. It remains to improve the modularity and conciseness of code pattern. I find it too \"heavy\", too \"long\" ...\r\n\r\n\r\n\r\n*/\r\n// Execute this code first\r\n(\r\nSynthDef(\r\n\t/*\r\n\tIt is probably possible to further improve the modularity of the code. Your help is welcome.\r\n\t*/\r\n\tname:\"fm3\",\r\n\tugenGraphFunc:{\r\n\t\t// We declare arguments (corresponding to variables, if I understood correctly)\r\n\t\targ f1 = 220,\r\n\t\t\tfreqmu = 2,\r\n\t\t\tf2,\r\n\t\t\tindex = 8,\r\n\t\t\tdecay = 0.25,\r\n\t\t\tvol = 0.1,\r\n\t\t\tfilterstart = 2000,\r\n\t\t\tfilterend = 10,\r\n\t\t\tphasemod = 10,\r\n\t\t/* Try to declare an argument \"signal\" that will be used to better visualize the\r\n\t\tstructure of the synth. Otherwise it's easy to be lost by different nesting, if\r\n\t\twe want to add multiple modules. Then I continue to declare variables representing\r\n\t\teach module, I guess that's the way to go !*/\r\n\t\t\tsignal,\r\n\t\t\tfilter, oscillo, env1, env2, lfo1, lfo2,\r\n\t\t// ----> variable \"volu\" allows you to easily increase the output volume\r\n\t\t\tvolu = 1;\r\n\t\t// Once variable declarations made​​, we can do calculations\r\n\t\t\tf2 = freqmu * f1;\r\n\t\t\tlfo1 = SinOsc.kr(55,0,1,0);\r\n\t\t\tlfo2 = SinOsc.kr(27.5,0,1,0);\r\n\t\t\tenv1 = Line.kr(\r\n\t\t\t\t\tstart: 0.5,\r\n\t\t\t\t\tend: 0,\r\n\t\t\t\t\tdur: decay,\r\n\t\t\t\t\tdoneAction: 2);\r\n\t\t\tenv2 = XLine.kr(\r\n\t\t\t\t\tstart: filterstart,\r\n\t\t\t\t\tend: filterend,\r\n\t\t\t\t\tdur: decay - 0.09);\r\n\t\t\toscillo = PMOsc.ar(\t\t\t\t// a simple FM oscillator\r\n\t\t\t\t\tcarfreq:[ f1, f1 + lfo1 * 2], // with small modulation on chanel 1\r\n\t\t\t\t\tmodfreq:[f2 - lfo2 * 3, f2 + lfo2 * 3], // with modulation on the 2 chanels\r\n\t\t\t\t\tpmindex: index * (env1 * 2),\r\n\t\t\t\t\tmodphase: phasemod + lfo1, // Is it a good idea ?\r\n\t\t\t\t\tmul: vol,\r\n\t\t\t\t\tadd: 0);\r\n\t\t\tfilter = Resonz.ar(\r\n\t\t\t\t\tin: oscillo * env1,\r\n\t\t\t\t\tfreq: env2,\r\n\t\t\t\t\tbwr: 2 * env1,\r\n\t\t\t\t\tmul: (2 + volu) * env1 );\r\n\t\t\tsignal = Out.ar(\r\n\t\t\tbus: 0,\r\n\t\t\tchannelsArray: filter\r\n\t\t\t);\r\n}).add;\r\n);\r\n\r\n/* Let's add reverb, for this we create a synth that does that. I seem to have taken a code from somewhere... help system perhaps... I don't remember...\r\n\r\n\r\nThen execute this code\r\n*/\r\n(\r\nSynthDef(\\FreeVerb2x2, {|outbus, mix = 0.25, room = 0.70, damp = 0.5, amp = 1.0|\r\n    var signal;\r\n\r\n    signal = In.ar(outbus, 2);\r\n\r\n    ReplaceOut.ar(outbus,\r\n        FreeVerb2.ar( // FreeVerb2 - true stereo UGen\r\n            signal[0], // Left channel\r\n            signal[1], // Right Channel\r\n            mix, room, damp, amp)); // same params as FreeVerb 1 chn version\r\n\r\n}).add;\r\n);\r\n\r\n// Finally execute next code\r\n\r\n(\r\n/* We declare some variables to modularize the code. You can probably do better!\r\n\r\nI suppose there are other ways to easily enter notes in these patterns. But I do not yet know enough supercollider to successfully do so.\r\n\r\nThe code is very long, I'd like it to be more concise. Unfortunately, I think I need help to successfully shorten it. All suggestions are welcome.\r\n\r\n*/\r\n\r\nvar a, b, c, d, e, f, n1 = 45, n2, n3, o1, o2, o3, p1, p2, p3, r1 = 1, r2, r3, r4;\r\n\r\n/*\r\na, b, c, d, e ,f : some patterns\r\n\r\nn1, n2, n3, o1, o2, o3, p1, p2, p3 : some notes in midi format\r\n\r\nn1 is the starting note, here A3 in midi format.\r\n\r\nLet's calculate other notes :\r\n\r\n*/\r\n\r\n\r\nn2 = n1 + 12;\r\nn3 = n2 + 12;\r\no1 = n1 + 2;\r\no2 = o1 + 12;\r\no3 = o2 + 12;\r\np1 = n1 + 4;\r\np2 = p1 + 12;\r\np3 = p2 + 12;\r\n\r\n/* r1, r2, r3, r4 : pattern repeat\r\n\r\nI think I made a mistake but where ?\r\n\r\n*/\r\n\r\nr2 = r1 * 2;\r\nr3 = r1 * 4;\r\nr4 = r1 * 8;\r\n\r\n// pattern a\r\n\r\na = Pbind(\r\n\t\\instrument, 'fm3',\r\n\t\\dur, Pseq([0.2, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.2], r1),\r\n\t//\\freqmu, Pshuf([1, 2, 3, 4, 5, 6, 7, 8], 128),\r\n\t\\freqmu, Pshuf([1, 2, 3, 4], r2),\r\n\t\\f1, Pseq([n2,n3,n3,n2], r2).midicps,\r\n//\t\\index, Prand([3,4,5,6],256),\r\n\t\\vol, Prand([0.1,0.05,0.04,0.08],repeats:r4),\r\n);\r\n\r\n// pattern b\r\n\r\nb = Pbind(\r\n\t\\instrument, 'fm3',\r\n\t\\dur, Pseq([0.4, 0.2, 0.2, 0.4, 0.4, 0.4, 0.4, 0.2], r1),\r\n\t//\\freqmu, Pshuf([1, 2, 3, 4, 5, 6, 7, 8], 128),\r\n\t\\freqmu, Pshuf([4, 5, 2, 7], r1),\r\n\t\\f1, Pseq([n1,n2,n2,n1], r1).midicps,\r\n\t\\index, Pseq([3,4,5,6], r1),\r\n\t\\vol, Prand([0.2,0.15,0.1,0.2],repeats:r3),\r\n\t\\filterend, Pseq(list:[10,100,1,500],repeats: r1),\r\n\t\\filterstart, Pseq(list:[1000,2000,500,3000],repeats: r1),\r\n\t\\phasemod, Pseq(list:[100,5,10,15,20],repeats: r1),\r\n\t\\decay, Pseq(list:[0.25,0.40,0.2,0.55,0.15],repeats: r1),\r\n);\r\n\r\n// pattern c\r\n\r\nc = Pbind(\r\n\t\\instrument, 'fm3',\r\n\t\\dur, Pseq([0.2, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.2], r1),\r\n\t//\\freqmu, Pshuf([1, 2, 3, 4, 5, 6, 7, 8], 128),\r\n\t\\freqmu, Pshuf([1, 2, 3, 4], r2),\r\n\t\\f1, Pseq([o2,o3,o3,o2], r2).midicps,\r\n//\t\\index, Prand([3,4,5,6],256),\r\n\t\\vol, Prand([0.1,0.05,0.04,0.08],repeats:r4),\r\n);\r\n\r\n// pattern d\r\n\r\nd = Pbind(\r\n\t\\instrument, 'fm3',\r\n\t\\dur, Pseq([0.4, 0.2, 0.2, 0.4, 0.4, 0.4, 0.4, 0.2], r1),\r\n\t//\\freqmu, Pshuf([1, 2, 3, 4, 5, 6, 7, 8], 128),\r\n\t\\freqmu, Pshuf([4, 5, 2, 7], r1),\r\n\t\\f1, Pseq([o1,o2,o2,o1], r1).midicps,\r\n\t\\index, Pseq([3,4,5,6], r1),\r\n\t\\vol, Prand([0.2,0.15,0.1,0.2],repeats:r3),\r\n\t\\filterend, Pseq(list:[10,100,1,500],repeats: r1),\r\n\t\\filterstart, Pseq(list:[1000,2000,500,3000],repeats: r1),\r\n\t\\phasemod, Pseq(list:[100,5,10,15,20],repeats: r1),\r\n\t\\decay, Pseq(list:[0.25,0.40,0.2,0.55,0.15],repeats: r1),\r\n);\r\n\r\n// pattern e\r\n\r\ne = Pbind(\r\n\t\\instrument, 'fm3',\r\n\t\\dur, Pseq([0.2, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.2], r1),\r\n\t//\\freqmu, Pshuf([1, 2, 3, 4, 5, 6, 7, 8], 128),\r\n\t\\freqmu, Pshuf([1, 2, 3, 4], r2),\r\n\t\\f1, Pseq([p2,p3,p3,p2], r2).midicps,\r\n//\t\\index, Prand([3,4,5,6],256),\r\n\t\\vol, Prand([0.1,0.05,0.04,0.08],repeats:r4),\r\n);\r\n\r\n// pattern f\r\n\r\nf = Pbind(\r\n\t\\instrument, 'fm3',\r\n\t\\dur, Pseq([0.4, 0.2, 0.2, 0.4, 0.4, 0.4, 0.4, 0.2], r1),\r\n\t//\\freqmu, Pshuf([1, 2, 3, 4, 5, 6, 7, 8], 128),\r\n\t\\freqmu, Pshuf([4, 5, 2, 7], r1),\r\n\t\\f1, Pseq([p1,p2,p2,p1], r1).midicps,\r\n\t\\index, Pseq([3,4,5,6], r1),\r\n\t\\vol, Prand([0.2,0.15,0.1,0.2],repeats:r3),\r\n\t\\filterend, Pseq(list:[10,100,1,500],repeats: r1),\r\n\t\\filterstart, Pseq(list:[1000,2000,500,3000],repeats: r1),\r\n\t\\phasemod, Pseq(list:[100,5,10,15,20],repeats: r1),\r\n\t\\decay, Pseq(list:[0.25,0.40,0.2,0.55,0.15],repeats: r1),\r\n);\r\n\r\nz = Synth(\\FreeVerb2x2, [\\outbus, 0], addAction:\\addToTail);\r\n\r\n// It plays two patterns in parallel, ones after the others\r\nPseq([Ppar([a, b]),Ppar([c, d]),Ppar([e, f]),Ppar([c, d]),Ppar([a, b])], 3).play;\r\n)\r\n\r\n‎",
   "labels" : [
      "pattern",
      "educational"
   ]
}
