File: SynthDescPlusGUI.sc

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 80,296 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (139 lines) | stat: -rw-r--r-- 3,677 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
+SynthDesc {
	makeWindow{
		if (Platform.makeSynthDescWindowAction.notNil) {
			^Platform.makeSynthDescWindowAction.value(this)
		} {
			^this.makeGui;
		}
	}

	makeGui {
		var w, startButton, sliders;
		var synth, cmdPeriodFunc;
		var usefulControls, numControls;
		var getSliderValues, gui;

		gui = GUI.current;

		usefulControls = controls.select {|controlName, i|
			var ctlname;
			ctlname = controlName.name.asString;
			(ctlname != "?") && (msgFuncKeepGate or: { ctlname != "gate" })
		};

		numControls = usefulControls.collect(_.defaultValue).flatten.size;
		sliders = IdentityDictionary(numControls);

		// make the window
		w = gui.window.new("another control panel", Rect(20, 400, 440, numControls * 28 + 32));
		w.view.decorator = FlowLayout(w.view.bounds);

		w.view.background = Color.rand(0.5, 1.0);

		// add a button to start and stop the sound.
		startButton = gui.button.new(w, 75 @ 24);
		startButton.states = [
			["Start", Color.black, Color.green],
			["Stop", Color.white, Color.red]
		];

		getSliderValues = {
			var envir;

			envir = ();
			usefulControls.do {|controlName, i|
				var ctlname = controlName.name.asString;
				var sliderEntry = sliders[controlName.name];
				if(ctlname[1] == $_ and: { "ti".includes(ctlname[0]) }) {
					ctlname = ctlname[2..];
				};

				if (sliderEntry.isArray) {
					envir.put(controlName.name, sliderEntry.collect(_.value));
				} {
					envir.put(controlName.name, sliderEntry.value);
				}
			};
			envir.use {
				msgFunc.valueEnvir
			};
		};


		startButton.action = { |view|
			var synthEndWatcher, localSynth;
			if (view.value == 1) {
				synth = Synth(name, getSliderValues.value).register;
				localSynth = synth;
				synthEndWatcher = SimpleController(synth).put(\n_end, {
					synthEndWatcher.remove;
					// 'synth' may have changed in the interim --
					// clear 'synth' only if n_end is for the current synth
					if(localSynth === synth) {
						synth = nil;
						defer { view.value = 0 };
					};
				});
			} {
				if(synth.isPlaying) {
					if (this.hasGate) {
						synth.release;
					} {
						synth.free
					};
				};
			};
		};

		// create controls for all parameters
		usefulControls.do {|controlName|
			var ctlname, ctlname2, capname, spec, controlIndex, slider;
			ctlname = controlName.name;
			capname = ctlname.asString;
			capname[0] = capname[0].toUpper;
			w.view.decorator.nextLine;
			ctlname = ctlname.asSymbol;
			if((spec = metadata.tryPerform(\at, \specs).tryPerform(\at, ctlname)).notNil) {
				spec = spec.asSpec
			} {
				spec = ctlname.asSpec;
			};

			if (spec.isKindOf(ControlSpec)) {
				slider = EZSlider(w, 400 @ 24, capname, spec,
					{ |ez|
						if(synth.isPlaying) { synth.set(ctlname, ez.value) }
					}, controlName.defaultValue);
			} {
				spec = ControlSpec(-1e10, 1e10);
				if (controlName.defaultValue.isNumber) {
					slider = EZNumber(w, 400 @ 24, capname, spec,
						{ |ez|
							if(synth.isPlaying) { synth.set(ctlname, ez.value) }
						}, controlName.defaultValue)
				} {
					slider = Array(controlName.defaultValue.size);
					controlName.defaultValue.do {|value, i|
						slider.add(EZNumber(w, 96 @ 24, "%[%]".format(capname, i), spec,
							{ |ez|
								if(synth.isPlaying) { synth.set(controlName.index + i, ez.value) }
							}, value))
					}
				}
			};
			sliders.put(ctlname, slider)
		};

		// 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 = {
			synth.release;
			CmdPeriod.remove(cmdPeriodFunc);
		};

		^w.front; // make window visible and front window.
	}
}