{
   "labels" : [
      "cumulative pulses",
      "control signals",
      "palindrome"
   ],
   "code" : "/*\r\nCumulative pulses\r\nImagine pulse-generators with different frequencies.\r\nPulses are added together to generate control signals, e.g. for pitch, or maybe time.\r\nThe signals can be presented in an array to be used in a Pbind.\r\nThe length of this array is determinded by the lcm of all reciprocal frequencies / periods. The number of pulse-generators determines the ambitus of the signals. The signals are repeated palindromes.\r\n\r\nExample:\r\n \r\nperiod | pulses\r\n1\t\t\t1\t1\t1\t1\t1\t1\t1\t1\t1\t1\r\n2\t\t\t1\t0\t1\t0\t1\t0\t1\t0\t1\t0\r\nSignal:\t2\t1\t2\t1\t2\t1\t2\t1\t2\t1\r\n\r\n1\t\t\t1\t1\t1\t1\t1\t1\t1\t1\t1\t1\r\n2\t\t\t1\t0\t1\t0\t1\t0\t1\t0\t1\t0\r\n3\t\t\t1\t0\t0\t1\t0\t0\t1\t0\t0\t1\r\nSignal:\t3\t1\t2\t2\t2\t1\t3\t1\t2\t2\r\n\r\n1:\t1\t1\t1\t1\t1\t1\t1\t1\t1\t1\t1\t1\r\n2:\t1\t0\t1\t0\t1\t0\t1\t0\t1\t0\t1\t0\r\n3:\t1\t0\t0\t1\t0\t0\t1\t0\t0\t1\t0\t0\r\n4:\t1\t0\t0\t0\t1\t0\t0\t0\t1\t0\t0\t0\r\ns:\t4\t1\t2\t2\t3\t1\t3\t1\t3\t2\t2\t1\r\n\r\nIn this case, the signal is used to play a melody. The separate pulse-generators are shown bij different steps on a randomly chosen scale. For the bass-line the melody is cut in half and each half is played on a different channel in half tempo.\r\n\r\n*/\r\ns.boot;\r\n\r\n(\r\nSynthDef(\\theSine, { | freq = 440, amp = 1 , pan = 0|\r\n\tOut.ar(0, Pan2.ar( \r\n\t\tSinOsc.ar(freq) * \r\n\t\t\tEnvGen.kr(Env.perc(0.001, 1, 1, -8), doneAction: 2), \t\tpan, \r\n\t\tamp\r\n\t\t);\r\n\t)\r\n}).send;\r\n\r\nSynthDef(\\theBlip, { |freq=440, numharm=8, pan = 0, amp=1|\r\n\tOut.ar( 0, Pan2.ar(\r\n\t\tBlip.ar(freq, numharm, 1) *\r\n\t\t\tEnvGen.kr(Env.perc(0.1, 1, 1, -8), doneAction: 2), \t\t\t\tpan, \r\n\t\tamp\r\n\t\t);\r\n\t);\r\n}).send;\r\n)\r\n\r\n(\r\nvar theNumber = 8, theDuration = 0.2, theScale=Scale.choose.postln;\r\nvar theValues, thePulses, bass1, bass2;\r\nvar grandLcm;\r\n\r\n/*\r\ngrandLcm takes 1 argument n and computes the least common multiple of a series of numbers, 1..n \r\n*/\r\n\r\ngrandLcm={arg n;\r\n\tif (n>=2, \r\n\t\t{g=lcm (thisFunction.value(n-1), n)},\r\n\t\t{g=1}\r\n\t);\r\n\tg;\r\n};\r\n\r\n//thePulses contains the patterns of each individual pulse-generator\r\nthePulses=Array.new(theNumber+1);\r\nthePulses.add([0]);\r\n//thePulses.add([1]);\r\nfor (1, theNumber, {\r\n\targ i;\r\n\tvar thePattern;\r\n\tthePattern=Array.newClear(grandLcm.value(theNumber)+1);\r\n\t//thePattern.size.postln;\r\n\tfor(0, grandLcm.value(theNumber), {\r\n\t\targ j;\r\n\t\tif (j.mod(i)==0,\r\n\t\t\t{thePattern[j]=1},\r\n\t\t\t{thePattern[j]=0}\r\n\t\t);\r\n\t});\r\n\tthePulses.add(thePattern);\r\n});\r\n//thePulses.postln;\r\n\r\n//theValues contains the result of all pulse-generators\r\ntheValues=Array.newClear(grandLcm.value(theNumber)+1);\r\nfor (0, theValues.size-1, {\r\n\targ i;\r\n\ttheValues[i]=0;\r\n});\r\nfor (2, theNumber, {\r\n\targ i;\r\n\tfor (0, theValues.size-1, {\r\n\t\targ j;\r\n\t\tif (j.mod(i)==0,{theValues[j]=theValues[j]+1});\r\n\t});\r\n});\r\n//theValues.postln;\r\ntheValues.plot;\r\n\r\n//the pattern of each pulse-generator gets its own pitch and stereo-position\r\nfor (1, theNumber ,{\r\n\targ i;\r\n\tPbind(\r\n\t\\instrument, \\theSine,\r\n\t\\scale, theScale,\r\n\t\\degree, Pseq(thePulses[i]*i, 1), \r\n\t\\octave, 5,\r\n\t\\dur, theDuration,\r\n\t\\amp, 1/theNumber,\r\n\t\\pan, (2*i/theNumber-1)\r\n).play;\r\n});\r\n\r\n//resulting melody in the center\r\nPbind(\r\n\t\\instrument, \\theBlip,\r\n\t\\scale, theScale, \r\n\t\\degree, Pseq(theValues, 1),\r\n\t\\numharm, theNumber,\r\n\t\\pan, \t0,\r\n\t\\dur, theDuration,\r\n\t\\amp, 0.05\r\n).play;\r\n\r\n\r\n//adding basslines\r\nbass1=Array.newClear(theValues.size / 2 +1);\r\nfor (0, bass1.size-1, {arg i;\r\n\tbass1[i]=theValues[i]\r\n});\r\nPbind(\r\n\t\\instrument, \\theBlip,\r\n\t\\degree, Pseq(bass1, 1),\r\n\t\\numharm, theNumber/2,\r\n\t\\pan, \t-1,\r\n\t\\scale, theScale,\r\n\t\\octave, \t2, \r\n\t\\dur, theDuration*2,\r\n\t\\amp, 0.25\r\n).play;\r\n\r\nbass2=bass1.reverse;\r\nPbind(\r\n\t\\instrument, \\theBlip,\r\n\t\\degree, Pseq(bass2, 1),\r\n\t\\numharm, theNumber/2,\r\n\t\\pan, \t1,\r\n\t\\scale, theScale,\r\n\t\\octave, \t2, \r\n\t\\dur, theDuration*2,\r\n\t\\amp, 0.25\r\n).play;\r\n\r\n)",
   "is_private" : null,
   "id" : "1-53Y",
   "author" : "henklass",
   "name" : "Cumulative Pulses",
   "ancestor_list" : [],
   "description" : "Cumulative pulses\r\nImagine pulse-generators with different frequencies.\r\nPulses are added together to generate control signals, e.g. for pitch, or maybe time.\r\nThe signals can be presented in an array to be used in a Pbind.\r\nThe length of this array is determinded by the lcm of all reciprocal frequencies / periods. The number of pulse-generators determines the ambitus of the signals. The signals are repeated palindromes.\r\n\r\nExample:\r\n \r\nperiod | pulses\r\n\r\n1:\t1\t1\t1\t1\t1\t1\t1\t1\t1\t1\r\n\r\n2:\t1\t0\t1\t0\t1\t0\t1\t0\t1\t0\r\n\r\nSi:\t2\t1\t2\t1\t2\t1\t2\t1\t2\t1\r\n\r\n\r\n1:\t1\t1\t1\t1\t1\t1\t1\t1\t1\t1\r\n\r\n2:\t1\t0\t1\t0\t1\t0\t1\t0\t1\t0\r\n\r\n3:\t1\t0\t0\t1\t0\t0\t1\t0\t0\t1\r\n\r\nS:\t3\t1\t2\t2\t2\t1\t3\t1\t2\t2\r\n\r\n\r\n1:\t1\t1\t1\t1\t1\t1\t1\t1\t1\t1\t1\t1\r\n\r\n2:\t1\t0\t1\t0\t1\t0\t1\t0\t1\t0\t1\t0\r\n\r\n3:\t1\t0\t0\t1\t0\t0\t1\t0\t0\t1\t0\t0\r\n\r\n4:\t1\t0\t0\t0\t1\t0\t0\t0\t1\t0\t0\t0\r\n\r\ns:\t4\t1\t2\t2\t3\t1\t3\t1\t3\t2\t2\t1\r\n\r\n\r\nIn this case, the signal is used to play a melody. The separate pulse-generators are shown bij different steps on a randomly chosen scale. For the bass-line the melody is cut in half and each half is played on a different channel in half tempo."
}
