Эх сурвалжийг харах

SC part with MIDI, OSC and Preset 1 working

Thomas GUFFROY 6 сар өмнө
parent
commit
7be83b1cd6

+ 78 - 7
SC/2b_rgb_synthdefs.scd

@@ -1,10 +1,42 @@
 (
+
+// Define the bus to send the audio to the filter
+~bus = Bus.audio(s, 2);
+~synthGroup = Group.head(s);             // synths go here
+~filterGroup = Group.after(~synthGroup); // filter comes after
+
+~lfoBus = Bus.control(s, 1);       // 1-channel control bus for the LFO
+~filterEnvBus = Bus.control(s, 1); // 1-channel control bus for the filter envelope
+
+
+// Low Frequency Oscillator for parameter modulation
+SynthDef(\lfo, { |out=0, freq=2, min=(-0.5), max=0.5, waveform=0|
+    var lfo;
+
+    // Select waveform based on parameter
+    lfo = Select.kr(waveform, [
+        SinOsc.kr(freq),         // Sine wave (waveform=0)
+        LFTri.kr(freq),          // Triangle wave (waveform=1)
+        LFSaw.kr(freq, 0, -1),   // Decreasing sawtooth wave (waveform=2)
+        LFPulse.kr(freq, 0, 0.5) // Square wave (waveform=3)
+    ]);
+
+    // Scale to range
+    lfo = lfo.range(min, max);
+
+    // Output to control bus
+    Out.kr(out, lfo);
+}).add;
+
+
+
 // RGB-controlled synthesizer with amplitude envelope
 SynthDef(\rgbSynth, { |out=0, freq=440, amp=1, ampAttack=0.01, ampRelease=1,
 	pitchAttack=0, pitchRelease=0, pitchRatio=2,
-	redAmt=0.5, greenAmt=0.5, blueAmt=0.5|
+	redAmt=0.5, greenAmt=0.5, blueAmt=0.5,
+	lfoBus=(-1), lfoDepth=0|
 
-    var env, gate, pitchEnv, red, green, blue, sig;
+    var env, gate, pitchEnv, red, green, blue, sig, modFreq, lfo;
 
 	gate = \gate.kr(0);
 
@@ -14,13 +46,17 @@ SynthDef(\rgbSynth, { |out=0, freq=440, amp=1, ampAttack=0.01, ampRelease=1,
     // Pitch envelope
     pitchEnv = EnvGen.kr(Env.adsr(pitchAttack, 0, 1, pitchRelease), gate);
 
+	 // If lfoBus < 0, read from bus 0 (assumed silent)
+	lfo = In.kr(lfoBus.max(0));
+	lfo.postln();
+
     // Calculate modulated frequency
-    freq = freq * (1 + (pitchEnv * (pitchRatio - 1)));
+    modFreq = freq * (1 + (pitchEnv * (pitchRatio - 1))) * (1 + (lfo * lfoDepth));
 
     // Different waveforms for RGB components
-    red = SinOsc.ar(freq) * redAmt;
-    green = Saw.ar(freq) * greenAmt;
-    blue = Pulse.ar(freq, 0.5) * blueAmt;
+    red = SinOsc.ar(modFreq) * redAmt;
+    green = Saw.ar(modFreq) * greenAmt;
+    blue = Pulse.ar(modFreq, 0.5) * blueAmt;
 
     // Mix the waveforms
     sig = (red + green + blue) / 3;
@@ -31,5 +67,40 @@ SynthDef(\rgbSynth, { |out=0, freq=440, amp=1, ampAttack=0.01, ampRelease=1,
 }).add;
 
 
+SynthDef(\filterEnv, {
+    |out=0, gate=1, attack=0.01, decay=0, sustain=1, release=0.5|
+
+    var env = EnvGen.kr(Env.adsr(attack, decay, sustain, release), gate);
+    Out.kr(out, env);
+}).add;
+
+
+// The sound goes into a LPF
+SynthDef(\lpf, { |in=0, out=0, cutoff=18000, resonance=1,
+	lfoBus=(-1), lfoDepth=0, envBus=(-1), envDepth=0|
+
+	var sig, lfo, env, modCutoff, rq;
+
+	// Read audio and control signals from buses
+    sig = In.ar(in, 2);
+    lfo = In.kr(lfoBus.max(0));
+    env = In.kr(envBus.max(0));
+
+	modCutoff = cutoff * (1 + (lfo * lfoDepth)) * (1 + (env * envDepth));
+	modCutoff = modCutoff.clip(20, 18000);  // safety
+
+	rq = resonance.linexp(0, 1, 1, 0.07); // Transforms resonance amount to rq with exp interpolation
+
+    sig = RLPF.ar(sig, modCutoff, rq); // low-pass filter
+
+    Out.ar(out, sig);
+}).add;
+
+
 "RGB-controlled synthesizer loaded".postln;
-)
+)
+
+
+
+
+

+ 28 - 2
SC/2c_voice_allocation_setup.scd

@@ -3,8 +3,18 @@
 var nbVoices = 16;
 
 
+// LFO Synth
+~lfo = Synth(\lfo, [\out, ~lfoBus.index]);
+
+
 // Synth array for voice allocation
-~synths = Array.fill(nbVoices, { Synth(\rgbSynth) });
+~synths = Array.fill(nbVoices, {
+    Synth(\rgbSynth, [\out, ~bus, \lfoBus, ~lfoBus.index], target: ~synthGroup, addAction: \addToTail)
+});
+
+
+// Filter envelope
+~filterEnv = Synth(\filterEnv, [\out, ~filterEnvBus.index]);
 
 
 // Array to keep in memory active synths (i.e. active notes)
@@ -18,5 +28,21 @@ var nbVoices = 16;
     };
 };
 
+
+~filter = Synth(\lpf, [
+	\in, ~bus,
+	\out, 0,
+	\lfoBus, ~lfoBus.index,
+	\envBus, ~filterEnvBus.index
+], target: ~filterGroup, addAction: \addToTail);
+
+
 "Voice allocation arrays loaded".postln
-)
+)
+
+
+//~setSynthsParam.(\lfoDepth, 0.01);
+
+//~filter.set(\cutoff, 10000, \resonance, 0, \lfoDepth, 2, \envDepth, 0);
+
+//~filterEnv.set(\attack, 0, \decay, 0, \sustain, 0);

+ 0 - 34
SC/4_modulation_synthdefs.scd

@@ -1,34 +0,0 @@
-(
-// Low Frequency Oscillator for parameter modulation
-SynthDef(\lfo, { |out=0, freq=1, min=0, max=1, waveform=0|
-    var lfo;
-
-    // Select waveform based on parameter
-    lfo = Select.kr(waveform, [
-        SinOsc.kr(freq),         // Sine wave (waveform=0)
-        LFTri.kr(freq),          // Triangle wave (waveform=1)
-        LFSaw.kr(freq, 0, -1),   // Decreasing sawtooth wave (waveform=2)
-        LFPulse.kr(freq, 0, 0.5) // Square wave (waveform=3)
-    ]);
-
-    // Scale to range
-    lfo = lfo.range(min, max);
-
-    // Output to control bus
-    Out.kr(out, lfo);
-}).add;
-
-
-// ADSR envelope generator
-SynthDef(\envelope, { |out=0, gate=1, attack=0.01, decay=0.3, sustain=0.5, release=1|
-    var env = EnvGen.kr(
-        Env.adsr(attack, decay, sustain, release),
-        gate,
-    );
-    Out.kr(out, env);
-}).add;
-
-
-
-"Modulation SynthDefs loaded".postln;
-)

+ 8 - 4
SC/5_osc_communication.scd

@@ -58,19 +58,23 @@ OSCdef.freeAll;
         // Pencil - Effect preset 1
 		"/pencil", {
             // Apply Preset 1 effects
-            ~filterSynth.set(
+            ~filter.set(
                 \cutoff, x.linexp(-0.5, 0.5, 20, 18000),
-                \res, y.linlin(-0.5, 0.5, 0, 1)
+                \resonance, y.linlin(-0.5, 0.5, 0, 1),
+				\lfoDepth, pressure.linlin(1, 8, 0, 3)
             );
 
-            ~lfoSynth.set(
+            ~lfo.set(
                 \freq, x.linlin(-0.5, 0.5, 0, 15),
-                \intensity, pressure.linlin(1, 8, 0, 1)
+				\waveform, 2
             );
 
+
+			/*
             ~reverbSynth.set(
                 \roomsize, y.linlin(-0.5, 0.5, 0.1, 0.9)
             );
+			*/
 
             ["Pencil preset:", "Cutoff", x.linexp(-0.5, 0.5, 20, 18000), "LFO", x.linlin(-0.5, 0.5, 0, 15)].postln;
         },

+ 3 - 0
SC/9_midi_controller.scd

@@ -23,6 +23,7 @@ MIDIdef.noteOn(\noteOn, { |vel, num, chan, src|
 			~synths[index].set(\freq, freq);
 			~synths[index].set(\amp, vel/127);
             ~activeNotes[index] = num; // Map note to synth index
+			~filterEnv.set(\gate, 1);  // Triggers the filter envelope
         } {
             "No available synth slots!".postln;
         }
@@ -32,6 +33,7 @@ MIDIdef.noteOn(\noteOn, { |vel, num, chan, src|
 		if (index.notNil) {
 			~activeNotes[index] = nil;
 			~synths[index].set(\gate, 0);
+			~filterEnv.set(\gate, 0); // Releases the filter envelope
 		}
 	}
 });
@@ -46,6 +48,7 @@ MIDIdef.noteOff(\noteOff, { |vel, num, chan, src|
 	if (index.notNil) {
 		~activeNotes[index] = nil;
 		~synths[index].set(\gate, 0);
+		~filterEnv.set(\gate, 0); // Releases the filter envelope
 	}
 });