ServerOptions: different input & output device names only works on osx
[supercollider.git] / examples / other / KeyboardWindow.scd
bloba662886c93c44f2c32caa44cafa35bf2432937eb
2 // james mccartney 
4 /*
5 Triggering functions from the Keyboard
7 Drag Functions to keys in the window.
8 Typing keys will execute the Function.
10 If the Function returns another Function, the new Function will replace the action of a key.
14 GUI.cocoa;      // use CocoaGUI (Mac OS X native)
15 GUI.swing;      // use SwingGUI (Java based GUI)
18 var w; // window object
19 var courier; // font object
21 // an Array of Strings representing the key layout.
22 var keyboard = #["`1234567890-=", "QWERTYUIOP[]\\", 
23                                                         "ASDFGHJKL;'", "ZXCVBNM,./"];
25 // horizontal offsets for keys.
26 var offsets = #[42, 48, 57, 117];
28 var actions; // an IdentityDictionary mapping keys to action functions.
29 var makeKey; // function to create an SCDragSink for a key.
31 courier = Font("Courier", 14).boldVariant;
33 // an IdentityDictionary is used to map keys to functions so that
34 // we can look up the action for a key
35 actions = IdentityDictionary.new; // create actions dictionary
37 // define a function that will create an SCDragSink for a key.
38 makeKey = {|char, keyname, bounds|
39         var v, clz;
41         keyname = keyname ? char.asString;
42         bounds = bounds ? (24 @ 24);
44         v = DragBoth(w, bounds);
45         v.font = courier;
46         v.string = keyname;
47         v.align = \center;
48         v.setBoth = false;
49         clz = View;
50         v.canReceiveDragHandler = {
51                 clz.currentDrag.isKindOf(String)
52         };
53         v.action = {
54                 var function = v.object.interpret;
55                 ("added key action : " ++ keyname).postln;
56                 if (char.isAlpha) {
57                         actions[char.toUpper] = function;
58                         actions[char.toLower] = function;
59                 }{
60                         actions[char] = function;
61                 };
62                 w.front;
63         };
66 w = Window("keyboard", Rect(128, 320, 420, 150));
68 w.view.decorator = FlowLayout(w.view.bounds);
70 // define a function to handle key downs.
71 w.view.keyDownAction = {|view, char, modifiers, unicode, keycode|
72          var result;
73          
74         // call the function
75         result = actions[char].value(char, modifiers);
76         
77         // if the result is a function, that function becomes the 
78         // new action for the key
79         if (result.isKindOf(Function)) {
80                 actions[char] = result;
81         };
84 // make the rows of the keyboard
85 keyboard.do {|row, i| 
86         row.do {|key| makeKey.(key) };
87         if (i==0) { makeKey.(127.asAscii, "del", 38 @ 24) };
88         if (i==2) { makeKey.($\r, "rtrn", 46 @ 24) };
89         w.view.decorator.nextLine;
90         w.view.decorator.shift(offsets[i]);
93 // make the last row
94 makeKey.($ , "space", 150 @ 24);
95 makeKey.(3.asAscii, "entr", 48 @ 24);
97 w.front;
103 ////////////////////
105 // Drag these things to the keyboard to test it.
109         var synth, original;
110         original = thisFunction;
111         synth = { SinOsc.ar(exprand(500,1200),0,0.2) }.play;
112         { synth.free; original }
119         { 
120                 Pan2.ar(
121                         SinOsc.ar(
122                                 ExpRand(300,3000), 
123                                 0,
124                                 SinOsc.kr(ExpRand(1,15),0,0.05).max(0)), 
125                         Rand(-1,1)) 
126         }.play;
130 { s.sendMsg(\n_free, \h, 0); } // kill head
132 { s.sendMsg(\n_free, \t, 0); } // kill tail
136         var eg, o, freq, noise;
137         eg = EnvGen.kr(Env.linen(0.1,2,0.4,0.2), doneAction: 2);
138         freq = Rand(600,1200);
139         noise = {LFNoise2.ar(freq*0.1, eg)}.dup;
140         o = SinOsc.ar(freq,0,noise);
141         Out.ar(0, o);
142 }.play})
147         var in, sr;
148     in = LFSaw.ar([21000,21001], 0, LFPulse.kr(ExpRand(0.1,1),0,0.3,0.2,0.02));
149     sr = ExpRand(300,3000) + [-0.6,0.6];
150         Out.ar(0,  RLPF.ar(in * LFPulse.ar(sr, 0, MouseY.kr(0.01, 0.99)), sr * (LFPulse.kr(ExpRand(0.1,12),0,0.4,0.2,0.2) + LFPulse.kr(ExpRand(0.1,12),0,0.7,0.2)), 0.1));
151 }.play;})
154 {{ var in;
155         in = In.ar(0,2);
156         ReplaceOut.ar(0, CombN.ar(in, 0.24, 0.24, 8, 1, in.reverse).distort);
157 }.play})
160 {{ var in;
161         in = In.ar(0,2);
162         ReplaceOut.ar(0, in * SinOsc.ar(MouseX.kr(2,2000,1)));
163 }.play})