supernova: fixes for boost-1.49 and gcc-4.7
[supercollider.git] / SCClassLibrary / QtCollider / QStethoscope.sc
blobc610563b0a6119ca309a7bc5ac922ac072e8bbec
1 /*
2 [22.sep.2010]
3   QStethoscope created by copying SCStethoscope and adjusting code for Qt GUI.
4 */
6 QStethoscope {
7   classvar ugenScopes;
8   var <server, <numChannels, <rate,  <index;
9   var <bufsize, buffer, <window, synth;
10   var scopeView, indexView, nChanView, xZoomSlider, yZoomSlider;
11   var style=0, sizeToggle=0, zx, zy, ai=0, ki=0, audiospec, controlspec, zoomspec;
14   *new { arg server, numChannels = 2, index, bufsize = 4096, zoom, rate, view, bufnum;
15     if(server.inProcess.not, { "scope works only with internal server".error; ^nil });
16     ^super.newCopyArgs(server, numChannels, rate ? \audio).makeWindow(view)
17     .index_(index ? 0).zoom_(zoom).allocBuffer(bufsize, bufnum).run;
18   }
20   *tileBounds {
21     var screenBounds = QWindow.screenBounds;
22     var x = 520 + (ugenScopes.size * (223 + 5)) + 5;
23     var right = x + 223;
24     var y = floor(right / screenBounds.width) * 297 + 10;
25     if(right > screenBounds.right) { x = floor(right % screenBounds.width / 223) * (223 + 5) };
26     ^Rect(x, y, 223, 223)
27   }
29   makeBounds { arg size = 223; ^Rect(297, 5, size, size) }
31   makeWindow { arg view;
32     if(view.isNil)
33     {
34       window = QWindow("stethoscope", this.makeBounds);
35       view = window.view;
36       view.decorator = FlowLayout(window.view.bounds);
37       window.front;
38       window.onClose = { this.free };
40     } {
41       if(view.decorator.isNil, {
42         "QStethoscope: makeWindow added a decorator to view".warn;
43         view.decorator = FlowLayout(view.bounds);
44       });
45     };
46     scopeView = QScope(view,
47       Rect(0,0, view.bounds.width - 10 - 20 - 4, view.bounds.height - 40)
48     );
49     scopeView.background = Color.black;
50     scopeView.resize = 5;
51     view.keyDownAction = { arg view, char; this.keyDown(char) };
52     view.background = Color.grey(0.6);
53     zx = scopeView.xZoom.log2;
54     zy = scopeView.yZoom.log2;
56     audiospec = ControlSpec(0, server.options.numAudioBusChannels, step:1);
57     controlspec = ControlSpec(0, server.options.numControlBusChannels, step:1);
58     zoomspec = ControlSpec(0.125, 16, \exp);
60     xZoomSlider = QSlider(view, Rect(10, 10, view.bounds.width - 80, 20));
61     xZoomSlider.action = { arg x;
62         /*var i;
63         i = this.spec.map(x.value);
64         this.index = i;*/
65         this.xZoom = zoomspec.map(x.value)
66       };
67     xZoomSlider.resize = 8;
68     xZoomSlider.value = zoomspec.unmap(this.xZoom);
69     xZoomSlider.background = Color.grey(0.6);
70     xZoomSlider.focusColor = Color.clear;
72     indexView = QNumberBox(view, Rect(10, 10, 30, 20))
73                 .value_(0).decimals_(0).step_(1).scroll_step_(1);
74     indexView.action = { this.index = indexView.value;  };
75     indexView.resize = 9;
76     indexView.font = QFont("Monaco", 9);
77     nChanView = QNumberBox(view, Rect(10, 10, 25, 20))
78                 .value_(numChannels).decimals_(0).step_(1).scroll_step_(1);
79     nChanView.action = { this.numChannels = nChanView.value.asInteger  };
80     nChanView.resize = 9;
81     nChanView.font = QFont("Monaco", 9);
82     QStaticText(view, Rect(10, 10, 20, 20)).visible_(false);
83     this.updateColors;
86     view.decorator.reset;
87     view.decorator.shift(scopeView.bounds.right, 0);
89     yZoomSlider = QSlider(view, Rect(scopeView.bounds.right, 0, 20, scopeView.bounds.height));
90     yZoomSlider.action = { arg x;
91         this.yZoom = zoomspec.map(x.value)
92       };
93     yZoomSlider.resize = 6;
94     yZoomSlider.value = zoomspec.unmap(this.yZoom);
95     yZoomSlider.background = Color.grey(0.6);
96     yZoomSlider.focusColor = Color.clear;
97   }
99   keyDown { arg char;
100         if(char === $i) { this.toInputBus; ^this };
101         if(char === $o) { this.toOutputBus;  ^this  };
102         if(char === $ ) { this.run;  ^this  };
103         if(char === $s) { this.style = (style + 1) % 2; ^this  };
104         if(char === $S) { this.style = 2;  ^this  };
105         if(char === $j or: { char.ascii === 0 }) { this.index = index - 1; ^this  };
106         if(char === $k) { this.switchRate; ^this  };
107         if(char === $l or: { char.ascii === 1 }) { this.index = index + 1 };
108         if(char === $-) {  zx = zx + 0.25; this.xZoom = 2 ** zx; ^this  };
109         if(char === $+) {  zx = zx - 0.25; this.xZoom = 2 ** zx; ^this  };        if(char === $*) {  zy = zy + 0.25; this.yZoom = 2 ** zy; ^this  };
110         if(char === $_) {  zy = zy - 0.25; this.yZoom = 2 ** zy; ^this  };
111         if(char === $A) {  this.adjustBufferSize; ^this  };
112         if(char === $m) { this.toggleSize; ^this  };
113         if(char === $.) { if(synth.isPlaying) { synth.free } };
115   }
117   spec { ^if(rate === \audio) { audiospec } {  controlspec } }
119   setProperties { arg numChannels, index, bufsize=4096, zoom, rate;
121         if(rate.notNil) { this.rate = rate };
122         if(index.notNil) { this.index = index };
123         if(numChannels.notNil) { this.numChannels = numChannels };
124         if(this.bufsize != bufsize) { this.allocBuffer(bufsize) };
125         if(zoom.notNil) { this.zoom = zoom };
126   }
128   allocBuffer { arg argbufsize, argbufnum;
129     bufsize = argbufsize ? bufsize;
130     if(buffer.notNil) { buffer.free };
131     buffer = Buffer.alloc(server, bufsize, numChannels, nil, argbufnum);
132     scopeView.bufnum = buffer.bufnum;
133     if(synth.isPlaying) { synth.set(\bufnum, buffer.bufnum) };
134   }
136   run {
137     if(synth.isPlaying.not) {
138       synth = SynthDef("stethoscope", { arg in, switch, bufnum;
139         var z;
140         z = Select.ar(switch, [In.ar(in, numChannels), K2A.ar(In.kr(in, numChannels))]);
141         ScopeOut.ar(z, bufnum);
142       }).play(RootNode(server), [\bufnum, buffer.bufnum, \in, index, \switch]
143         ++ if('audio' === rate) { 0 } { 1 },
144         \addToTail
145       );
146       synth.isPlaying = true;
147       NodeWatcher.register(synth);
148     }
149   }
151   free {
152     buffer.free;
154     if(synth.isPlaying) {  synth.free };
155     synth = nil;
156     if(server.scopeWindow === this) { server.scopeWindow = nil }
157   }
159   quit {
160     window.close;
161     this.free;
162   }
164   numChannels_ { arg n;
166     var isPlaying;
167     if(n > 128) { "cannot display more than 128 channels at once".inform; n = 128 };
168     if(n != numChannels and: { n > 0 }) {
169       isPlaying = synth.isPlaying;
170       if(isPlaying) { synth.free; synth.isPlaying = false; synth = nil }; // immediate
171       numChannels = n;
173       nChanView.value = n;
174       this.allocBuffer;
175       if(isPlaying) {  this.run };
176       this.updateColors;
177     };
178   }
180   index_ { arg val=0;
181     var spec;
182     spec = this.spec;
183     index = spec.constrain(val);
184     if(synth.isPlaying) { synth.set(\in, index) };
185     if(rate === \audio) { ai = index } { ki = index };
186     indexView.value = index;
187     // xZoomSlider.value = spec.unmap(index)
188   }
190   rate_ { arg argRate=\audio;
191     if(rate === argRate) {  ^this };
192     if(argRate === \audio)
193     {
194         if(synth.isPlaying) { synth.set(\switch, 0) };
195         rate = \audio;
196         this.updateColors;
197         ki = index;
198         this.index = ai;
199     }
200     {
201         if(synth.isPlaying) { synth.set(\switch, 1) };
202         rate = \control;
203         this.updateColors;
204         ai = index;
205         this.index = ki;
206     }
207   }
209   size_ { arg val; if(window.notNil) { window.bounds = this.makeBounds(val) } }
210   toggleSize {  if(sizeToggle == 0)
211           { sizeToggle = 1; this.size_(500) }
212           { sizeToggle = 0; this.size_(212) }
213   }
215   xZoom_ { arg val;
216     scopeView.xZoom = val;
217     zx = val.log2;
218     xZoomSlider.value = zoomspec.unmap(val);
219   }
220   yZoom_ { arg val;
221     scopeView.yZoom = val;
222     zy = val.log2;
223     yZoomSlider.value = zoomspec.unmap(val);
224   }
225   xZoom { ^2.0 ** zx }
226   yZoom { ^2.0 ** zy }
228   zoom_ { arg val; this.xZoom_(val ? 1) }
230   style_ { arg val;
231     if(numChannels < 2 and: { val == 2 }) {
232       "QStethoscope: can't do x/y scoping with one channel".warn;
233       ^this;
234     };
235     scopeView.style = style = val
236   }
240   updateColors {
241     scopeView.waveColors = if(\audio === rate) {
242       Array.fill(numChannels, { Color.new255(255, 218, 000) });
243     } {
244       Array.fill(numChannels, { Color.new255(125, 255, 205) });
245     }
246   }
248   switchRate { if(rate === \control) { this.rate = \audio } {  this.rate = \control } }
250   toInputBus {
251     this.index = server.options.numOutputBusChannels;
252     this.numChannels = server.options.numInputBusChannels;
253   }
254   toOutputBus {
255     this.index = 0;
256     this.numChannels = server.options.numOutputBusChannels;
257   }
258   adjustBufferSize {
259     this.allocBuffer(max(256,nextPowerOfTwo(
260       asInteger(scopeView.bounds.width * scopeView.xZoom))))
261   }
263   // ugenScopes
264   *ugenScopes {
265     if(ugenScopes.isNil, { ugenScopes = Set.new; });
266     ^ugenScopes
267   }
269   /**
270    *  @return (Server) the default server to scope on
271    */
272   *defaultServer {
273     ^Server.internal;
274   }
276   /**
277    *  @param  aServer (Server) a server to test for scoping
278    *  @return     (Boolean) indication whether the server can be scope'ed
279    */
280   *isValidServer { arg aServer;
281     ^aServer.inProcess;
282   }