150 likes | 292 Views
Computer Programming For Musical Applications II. Tutorial10.GUI 5 December, 2008. TODAY. Quick GUI Review EZSlider Exercises. Windows. GUI.window.new(name,bounds,resizable,border,server,scroll). name – A text string displayed at the top of the window
E N D
Computer Programming For Musical Applications II Tutorial10.GUI 5 December, 2008
TODAY Quick GUI Review EZSlider Exercises MTE2007 Computer Programming For Musical Applications II
Windows GUI.window.new(name,bounds,resizable,border,server,scroll) • name – A text string displayed at the top of the window • bounds – A Rect object specifying location and size • resizable –Boolean allowing window to be resized • border –Boolean drawing a border and title bar. If false, the window can only be closed from within SC code • server – does nothing • scroll – draws scrollbars if content goes beyond bounds Create a window MTE2007 Computer Programming For Musical Applications II
Basic Window Example //create a 600x360 window, at x=256, y=64 (from bottom left) //make it resizable, with borders w = SCWindow("My Window", Rect(256, 64, 600, 360), true, true, 0, false); w.front; // bring it to the front w.close; // close the window MTE2007 Computer Programming For Musical Applications II
Buttons SCButton.new(parent,bounds) Members: • states – an array containing the sequence of states of the button • each state is an array itself of the form: • [label, textcolor, background color] • action – a function that is called when the button is released • value – returns the current state (not settable) • valueAction – sets the current state MTE2007 Computer Programming For Musical Applications II
Button Example ( w = SCWindow.new; // uses default values for bounds b = SCButton(w, Rect(20,20,340,30)); // create a button with 2 states b.states = [ ["Touch Me", Color.black, Color.yellow], ["Push Me", Color.white, Color.red], ]; b.action = { |state| if(state.value == 1) { "Mmmm nice".postln;} { "Aah, too hard".postln; } }; w.front; ) MTE2007 Computer Programming For Musical Applications II
Basic Window Example Task Create a basic window on the screen that contains a button with two states: Start and Stop Make the button play a Synth when you press the Start button and make it stop the Synth when you press the Stop button MTE2007 Computer Programming For Musical Applications II
EZSlider EZSlider is a wrapper class for managing a label, slider and number box. EZSlider(window, dimensions, label, controlSpec, action, initVal, initAction, labelWidth, numberWidth) • controlSpec: An easy way to map your slider values to something more useful e.g. • ControlSpec(24,60,\lin,1) • action: A function to be called when the sliders value is changed e.g. • {|ez| mySynth.set(\note,ez.value)} MTE2007 Computer Programming For Musical Applications II
Connecting your GUI to a Synth • First define a Synth - ( // define a synth SynthDef(”synthy", { arg note = 36, fc = 1000, rq = 0.25, bal=0, amp=0.4, gate = 1; var x;x = Mix.fill(4, { LFSaw.ar((note + {0.1.rand2}.dup).midicps, 0, 0.02) }); x = RLPF.ar(x, fc, rq).softclip; x = RLPF.ar(x, fc, rq, amp).softclip; x = Balance2.ar(x[0], x[1], bal); x = x * EnvGen.kr(Env.cutoff, gate, doneAction: 2); Out.ar(0, x); }).load(s); ) MTE2007 Computer Programming For Musical Applications II
Connecting your GUI to a Synth (//Start of main code bracket var w, startButton, node, noteControl, cutControl, resControl, balControl, ampControl,cmdPeriodFunc; //Make a window w = GUI.window.new("DEMO WINDOW",Rect(20,400,440,180)); w.front; //Make the window visible and bring it to the front w.view.decorator = FlowLayout(w.view.bounds); w.view.background = Color(0,0,0); //Set the background color startButton = GUI.button.nstartButton = GUI.button.new(w,75@24); startButton.states = [ ["Start", Color.white, Color.green],["Stop", Color.white, Color.red] ]; //Code continues on next slide…. MTE2007 Computer Programming For Musical Applications II
Connecting your GUI to a Synth startButton.action = { |view| if(view.value == 1){ //Start the sound node = Synth(\synthy, [ \note, noteControl.value, \fc, cutControl.value, \rq, resControl.value, \bal, balControl.value, \amp, ampControl.value.dbamp ]); }{ //Set gate to zero to cause envelope to release node.release; node = nil; }; }; //Code continues on next slide…. MTE2007 Computer Programming For Musical Applications II
Connecting your GUI to a Synth //Create controls for all parameters w.view.decorator.nextLine; //Move down a line in the GUI screen noteControl = EZSlider(w,400@24, "Note", ControlSpec(24,60,\lin,1), {|ez| node.set(\note,ez.value)},36); w.view.decorator.nextLine; cutControl = EZSlider(w,400@24, "CutOff", ControlSpec(200,5000,\exp,1), {|ez| node.set(\fc,ez.value)},1000); w.view.decorator.nextLine; resControl = EZSlider(w, 400 @ 24, "Resonance", ControlSpec(0.1, 0.7), {|ez| node.set( \rq, ez.value )}, 0.2); w.view.decorator.nextLine; balControl = EZSlider(w, 400 @ 24, "Balance", \bipolar, {|ez| node.set( \bal, ez.value )}, 0); w.view.decorator.nextLine; ampControl = EZSlider(w, 400 @ 24, "Amp", \db, {|ez| node.set( \amp, ez.value.dbamp )}, -6); //Code continues on next slide…. MTE2007 Computer Programming For Musical Applications II
Connecting your GUI to a Synth // set start button to zero upon a cmd-period cmdPeriodFunc = { startButton.value = 0; }; CmdPeriod.add(cmdPeriodFunc); // stop the sound when window closes and remove cmdPeriodFunc.w.onClose = { node.free; node = nil; CmdPeriod.remove(cmdPeriodFunc); }; ) //End of main code bracket MTE2007 Computer Programming For Musical Applications II
Connecting your GUI to a Synth Task: Create a window with one button and 4 EZ sliders Make the button trigger a Synth and make the sliders control 4 parameters of the Synth MTE2007 Computer Programming For Musical Applications II
Create a Mean Filter Task: Create a window with one EZ Slider + a separate text box Create a function that will take the value from the EZ Slider and filters the current value using a mean filter. Display the filtered value of the EZ Slider in the separate text box. A mean filter is very easy to create…all you need is an array of size N (for example 10) which should be filled with 0’s at the start of the program. Then every time you want to filter a value you do the following steps: Move the position of the values in the filter down by 1 Insert the latest value to the end of the array Add up all the values in the array and divide by N – this gives you your filtered value MTE2007 Computer Programming For Musical Applications II