9_midi_controller.scd 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // MIDI note handling
  2. (
  3. var midiIn, synths;
  4. ~synths = IdentityDictionary.new; // Store active synths keyed by note number
  5. // Connect to MIDI
  6. MIDIClient.init;
  7. MIDIIn.connectAll;
  8. // Note On: create synth
  9. MIDIdef.noteOn(\noteOn, { |vel, num, chan, src|
  10. var freq = num.midicps;
  11. //postln("Note On: " + num + " Velocity: " + vel);
  12. if (vel > 0) {
  13. var synth = Synth(\rgbSynth, [
  14. \freq, freq,
  15. \amp, vel/127,
  16. \ampAttack, ~synthParams.ampAttack,
  17. \ampRelease, ~synthParams.ampRelease,
  18. \filterAttack, ~synthParams.filterAttack,
  19. \filterRelease, ~synthParams.filterRelease,
  20. \pitchAttack, ~synthParams.pitchAttack,
  21. \pitchRelease, ~synthParams.pitchRelease,
  22. \redAmt, ~synthParams.redAmt,
  23. \greenAmt, ~synthParams.greenAmt,
  24. \blueAmt, ~synthParams.blueAmt
  25. ]);
  26. ~synths[num] = synth;
  27. } { // Treat as noteOff if it is a noteOn with velocity 0
  28. //postln("Note On: " + num + " Velocity: " + vel);
  29. ~synths[num].set(\gate, 0);
  30. ~synths.removeAt(num);
  31. }
  32. });
  33. // Note Off: release synth
  34. MIDIdef.noteOff(\noteOff, { |vel, num, chan, src|
  35. //postln("Note Off: " + num + " Velocity: " + vel);
  36. ~synths[num].set(\gate, 0);
  37. ~synths.removeAt(num);
  38. });
  39. // Run these functions directly
  40. "MIDI functions loaded.".postln;
  41. )