|
@@ -1,4 +1,4 @@
|
|
|
-// Module 9: MIDI Controller - Modified to use Synth Pool
|
|
|
|
|
|
|
+// Module 9: MIDI Controller - FIXED VERSION
|
|
|
// Save as "9_midi_controller.scd" (REPLACE YOUR EXISTING VERSION)
|
|
// Save as "9_midi_controller.scd" (REPLACE YOUR EXISTING VERSION)
|
|
|
|
|
|
|
|
(
|
|
(
|
|
@@ -20,25 +20,33 @@ MIDIdef.noteOn(\noteOn, { |vel, num, chan, src|
|
|
|
if (vel > 0) {
|
|
if (vel > 0) {
|
|
|
// Check if note is already playing and release it first
|
|
// Check if note is already playing and release it first
|
|
|
if(~midiNoteKeys[num].notNil, {
|
|
if(~midiNoteKeys[num].notNil, {
|
|
|
- ~returnSynthToPool.value(~midiNoteKeys[num]);
|
|
|
|
|
|
|
+ if(~returnSynthToPool.notNil, {
|
|
|
|
|
+ ~returnSynthToPool.value(~midiNoteKeys[num]);
|
|
|
|
|
+ });
|
|
|
~midiNoteKeys.removeAt(num);
|
|
~midiNoteKeys.removeAt(num);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
// Get a synth from the pool instead of creating new one
|
|
// Get a synth from the pool instead of creating new one
|
|
|
- var synth = ~startPoolSynth.value(noteKey, freq, amp);
|
|
|
|
|
-
|
|
|
|
|
- if(synth.notNil, {
|
|
|
|
|
- // Track this note
|
|
|
|
|
- ~midiNoteKeys[num] = noteKey;
|
|
|
|
|
- // Uncomment for debugging: ["MIDI Note ON: % - Got synth from pool".format(num)].postln;
|
|
|
|
|
|
|
+ if(~startPoolSynth.notNil, {
|
|
|
|
|
+ var synth = ~startPoolSynth.value(noteKey, freq, amp);
|
|
|
|
|
+
|
|
|
|
|
+ if(synth.notNil, {
|
|
|
|
|
+ // Track this note
|
|
|
|
|
+ ~midiNoteKeys[num] = noteKey;
|
|
|
|
|
+ // Uncomment for debugging: ["MIDI Note ON: % - Got synth from pool".format(num)].postln;
|
|
|
|
|
+ }, {
|
|
|
|
|
+ ["MIDI Note ON: % - No free synths available!".format(num)].postln;
|
|
|
|
|
+ });
|
|
|
}, {
|
|
}, {
|
|
|
- ["MIDI Note ON: % - No free synths available!".format(num)].postln;
|
|
|
|
|
|
|
+ "ERROR: Synth pool not initialized! Run ~initializeSynthPool.value first".postln;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
} {
|
|
} {
|
|
|
// Treat as noteOff if it is a noteOn with velocity 0
|
|
// Treat as noteOff if it is a noteOn with velocity 0
|
|
|
if(~midiNoteKeys[num].notNil, {
|
|
if(~midiNoteKeys[num].notNil, {
|
|
|
- ~returnSynthToPool.value(~midiNoteKeys[num]);
|
|
|
|
|
|
|
+ if(~returnSynthToPool.notNil, {
|
|
|
|
|
+ ~returnSynthToPool.value(~midiNoteKeys[num]);
|
|
|
|
|
+ });
|
|
|
~midiNoteKeys.removeAt(num);
|
|
~midiNoteKeys.removeAt(num);
|
|
|
// Uncomment for debugging: ["MIDI Note OFF: % (vel 0)".format(num)].postln;
|
|
// Uncomment for debugging: ["MIDI Note OFF: % (vel 0)".format(num)].postln;
|
|
|
});
|
|
});
|
|
@@ -48,7 +56,9 @@ MIDIdef.noteOn(\noteOn, { |vel, num, chan, src|
|
|
|
// Note Off: return synth to pool instead of setting gate
|
|
// Note Off: return synth to pool instead of setting gate
|
|
|
MIDIdef.noteOff(\noteOff, { |vel, num, chan, src|
|
|
MIDIdef.noteOff(\noteOff, { |vel, num, chan, src|
|
|
|
if(~midiNoteKeys[num].notNil, {
|
|
if(~midiNoteKeys[num].notNil, {
|
|
|
- ~returnSynthToPool.value(~midiNoteKeys[num]);
|
|
|
|
|
|
|
+ if(~returnSynthToPool.notNil, {
|
|
|
|
|
+ ~returnSynthToPool.value(~midiNoteKeys[num]);
|
|
|
|
|
+ });
|
|
|
~midiNoteKeys.removeAt(num);
|
|
~midiNoteKeys.removeAt(num);
|
|
|
// Uncomment for debugging: ["MIDI Note OFF: %".format(num)].postln;
|
|
// Uncomment for debugging: ["MIDI Note OFF: %".format(num)].postln;
|
|
|
});
|
|
});
|
|
@@ -57,8 +67,10 @@ MIDIdef.noteOff(\noteOff, { |vel, num, chan, src|
|
|
|
// All Notes Off (MIDI Panic) - return all MIDI synths to pool
|
|
// All Notes Off (MIDI Panic) - return all MIDI synths to pool
|
|
|
MIDIdef.cc(\allNotesOff, { |val, num, chan, src|
|
|
MIDIdef.cc(\allNotesOff, { |val, num, chan, src|
|
|
|
if(num == 123, { // All Notes Off CC
|
|
if(num == 123, { // All Notes Off CC
|
|
|
- ~midiNoteKeys.keysValuesDo({ |noteNum, noteKey|
|
|
|
|
|
- ~returnSynthToPool.value(noteKey);
|
|
|
|
|
|
|
+ if(~returnSynthToPool.notNil, {
|
|
|
|
|
+ ~midiNoteKeys.keysValuesDo({ |noteNum, noteKey|
|
|
|
|
|
+ ~returnSynthToPool.value(noteKey);
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
~midiNoteKeys.clear;
|
|
~midiNoteKeys.clear;
|
|
|
"MIDI: All notes off - All synths returned to pool".postln;
|
|
"MIDI: All notes off - All synths returned to pool".postln;
|
|
@@ -67,8 +79,10 @@ MIDIdef.cc(\allNotesOff, { |val, num, chan, src|
|
|
|
|
|
|
|
|
// Cleanup all MIDI synths function
|
|
// Cleanup all MIDI synths function
|
|
|
~cleanupAllMIDI = {
|
|
~cleanupAllMIDI = {
|
|
|
- ~midiNoteKeys.keysValuesDo({ |noteNum, noteKey|
|
|
|
|
|
- ~returnSynthToPool.value(noteKey);
|
|
|
|
|
|
|
+ if(~returnSynthToPool.notNil, {
|
|
|
|
|
+ ~midiNoteKeys.keysValuesDo({ |noteNum, noteKey|
|
|
|
|
|
+ ~returnSynthToPool.value(noteKey);
|
|
|
|
|
+ });
|
|
|
});
|
|
});
|
|
|
~midiNoteKeys.clear;
|
|
~midiNoteKeys.clear;
|
|
|
"All MIDI synths returned to pool".postln;
|
|
"All MIDI synths returned to pool".postln;
|