AJ | Pact | logo
PACT
Supercollider a day

Supercollider a day

1 September 2009

Hackpact (30 posts)

For Hackpact I will attempt to learn the Supercollider audio programming language, updating this page with a new block of code each day.

Beginnings of a Keyboard synth

26/09/09

Beginnings of a Keyboard synth.

/* ==========================================================================
   Supercollider a day - for HackPact 2009
   -------------------
   by Adam Jansch
  
   26 September 2009
   
   Beginnings of a Keyboard synth
   ========================================================================== */


 
(
  // Declare variables

  var ks_window, ks_sine, ks_saw, ks_square, ks_filt_co, ks_filt_res,
    ks_volume, keyboard_synth;
    
  var label_width = 80;
  
  // Define ControlSpecs

  var filter_freq_cs = [10, 16000.0, \exponential, 1.0, 10000.0, "Hz"].asSpec;
  
  // Define UI elements

  ks_window = SCWindow("Keyboard synth", Rect(100, 100, 300, 160)).front;
  ks_window.view.decorator = FlowLayout(ks_window.view.bounds);
  ks_window.view.decorator.gap = 2@2;
  
  ks_sine = EZSlider(ks_window, 292@20, " Sine ",
    unitWidth: 30, numberWidth: 50, labelWidth: label_width).value_(1.0);
  ks_sine.setColors(Color.grey);
  
  ks_saw = EZSlider(ks_window, 292@20, " Saw ",
    unitWidth: 30, numberWidth: 50, labelWidth: label_width);
  ks_saw.setColors(Color.grey);
  
  ks_square = EZSlider(ks_window, 292@20, " Square ",
    unitWidth: 30, numberWidth: 50, labelWidth: label_width);
  ks_square.setColors(Color.grey);
  
  ks_filt_co = EZSlider(ks_window, 292@20, " Cut off ", filter_freq_cs,
    unitWidth: 30, numberWidth: 50, labelWidth: label_width);
  ks_filt_co.setColors(Color.grey);
  
  ks_filt_res = EZSlider(ks_window, 292@20, " Resonance ",
    unitWidth: 30, numberWidth: 50, labelWidth: label_width);
  ks_filt_res.setColors(Color.grey);
  
  ks_volume = EZSlider(ks_window, 292@20, " Volume ",
    unitWidth: 30, numberWidth: 50, labelWidth: label_width).value_(1.0);
  ks_volume.setColors(Color.grey);
  
  
  // SYNTHDEF DEFINITIONS -----------------------------------------

  SynthDef(\keyboard_synth, {
    |sine_level = 1.0, saw_level = 0.0, square_level = 0.0,
      filter_freq = 10000, filter_res = 0.0|
  
    var sine = SinOsc.ar(440.0, 0.0, sine_level);
    var saw = Saw.ar(440.0, saw_level);
    var square = Pulse.ar(440.0, 0.5, square_level);
    var mix = Mix.new([sine, saw, square]);
    
    Out.ar(0, Pan2.ar(MoogFF.ar(mix, Lag2.kr(filter_freq, 0.3), filter_res)));
  }).load(s);
  
  keyboard_synth = Synth(\keyboard_synth);
  
  // UI CONTROL ACTIONS -------------------------------------------

  ks_sine.action_({
    |sine_val|
    
    keyboard_synth.set(\sine_level, sine_val.value);
  });
  
  ks_saw.action_({
    |saw_val|
    
    keyboard_synth.set(\saw_level, saw_val.value);
  });
  
  ks_square.action_({
    |square_val|
    
    keyboard_synth.set(\square_level, square_val.value);
  });
  
  ks_filt_co.action_({
    |filt_co_val|
    
    keyboard_synth.set(\filter_freq, filt_co_val.value);
  });
  
  ks_filt_res.action_({
    |filt_res_val|
    
    keyboard_synth.set(\filter_res, filt_res_val.value);
  });
  
  
  // WINDOW CLOSE ACTION -------------------------------------------

  ks_window.onClose_({
    keyboard_synth.free;
  });
)