2b_rgb_synthdefs.scd 2.8 KB

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