2_synthdefs.scd 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. (
  2. // Define the bus to send the audio to the filter
  3. ~bus = Bus.audio(s, 2);
  4. ~fxBus = Bus.audio(s, 2);
  5. ~synthGroup = Group.head(s); // synths go here
  6. ~filterGroup = Group.after(~synthGroup); // filter comes after
  7. ~lfoBus = Bus.control(s, 1); // 1-channel control bus for the LFO
  8. ~filterEnvBus = Bus.control(s, 1); // 1-channel control bus for the filter envelope
  9. // Low Frequency Oscillator for parameter modulation
  10. SynthDef(\lfo, { |out=0, freq=2, min=(-0.5), max=0.5, waveform=0|
  11. var lfo;
  12. // Select waveform based on parameter
  13. lfo = Select.kr(waveform, [
  14. SinOsc.kr(freq), // Sine wave (waveform=0)
  15. LFTri.kr(freq), // Triangle wave (waveform=1)
  16. LFSaw.kr(freq, 0, -1), // Decreasing sawtooth wave (waveform=2)
  17. LFPulse.kr(freq, 0, 0.5) // Square wave (waveform=3)
  18. ]);
  19. // Scale to range
  20. lfo = lfo.range(min, max);
  21. // Output to control bus
  22. Out.kr(out, lfo);
  23. }).add;
  24. // RGB-controlled synthesizer with amplitude envelope
  25. SynthDef(\rgbSynth, { |out=0, freq=440, amp=1, ampAttack=0.01, ampRelease=1,
  26. pitchAttack=0, pitchDecay=0, pitchRatio=1,
  27. redAmt=1, greenAmt=1, blueAmt=1,
  28. lfoBus=(-1), lfoDepth=0|
  29. var env, gate, pitchEnv, red, green, blue, sig, modFreq, lfo;
  30. gate = \gate.kr(0);
  31. // Amplitude envelope
  32. env = EnvGen.kr(Env.adsr(ampAttack, 0, 1, ampRelease), gate);
  33. // Pitch envelope
  34. pitchEnv = EnvGen.kr(Env.adsr(pitchAttack, pitchDecay, 0, pitchDecay), gate);
  35. // If lfoBus < 0, read from bus 0 (assumed silent)
  36. lfo = In.kr(lfoBus.max(0));
  37. lfo.postln();
  38. // Calculate modulated frequency
  39. modFreq = freq * (1 + (pitchEnv * (pitchRatio - 1))) * (1 + (lfo * lfoDepth));
  40. // Different waveforms for RGB components
  41. red = SinOsc.ar(modFreq) * redAmt;
  42. green = Saw.ar(modFreq) * greenAmt;
  43. blue = Pulse.ar(modFreq, 0.5) * blueAmt;
  44. // Mix the waveforms
  45. sig = (red + green + blue) / 3;
  46. // Apply envelope and output
  47. sig = sig * env * amp;
  48. Out.ar(out, sig!2);
  49. }).add;
  50. SynthDef(\filterEnv, {
  51. |out=0, gate=1, attack=0.001, decay=0|
  52. var env = EnvGen.kr(Env.adsr(attack, decay, 0, decay), gate);
  53. Out.kr(out, env);
  54. }).add;
  55. // The sound goes into a LPF
  56. SynthDef(\lpf, { |in=0, out=0, cutoff=18000, resonance=1,
  57. lfoBus=(-1), lfoDepth=0, envBus=(-1), envDepth=0|
  58. var sig, lfo, env, modCutoff, rq;
  59. // Read audio and control signals from buses
  60. sig = In.ar(in, 2);
  61. lfo = In.kr(lfoBus.max(0));
  62. env = In.kr(envBus.max(0));
  63. modCutoff = cutoff * (1 + (lfo * lfoDepth)) * (1 + (env * envDepth));
  64. modCutoff = modCutoff.clip(20, 18000); // safety
  65. rq = resonance.linexp(0, 1, 1, 0.07); // Transforms resonance amount to rq with exp interpolation
  66. sig = RLPF.ar(sig, modCutoff, rq); // low-pass filter
  67. Out.ar(out, sig);
  68. }).add;
  69. // Create a SynthDef with the VSTPlugin UGen
  70. SynthDef(\juceVST, { |in, out|
  71. var sound = In.ar(in, 2);
  72. ReplaceOut.ar(out, VSTPlugin.ar(sound, 2))
  73. }).add;
  74. "RGB-controlled synthesizer loaded".postln;
  75. )