summaryrefslogtreecommitdiff
path: root/lib/Oscope.ck
blob: b18a0b23997d706523e090c82fbbb686438edbd8 (plain)
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
/*
 * Oscilloscope (jack.scope) helpers
 */
public class Oscope {
	static OscSend @jack_scope;

	/*
	 * "signal" or "embed"
	 */
	fun static void
	mode(string m)
	{
		jack_scope.startMsg("/mode", "s");
		m => jack_scope.addString;
	}

	/*
	 * "dot", "fill" or "line"
	 */
	fun static void
	style(string s)
	{
		jack_scope.startMsg("/style", "s");
		s => jack_scope.addString;
	}

	fun static void
	frames(dur f)
	{
		// <<< "set frame size:", (f / samp) $ int >>>;
		jack_scope.startMsg("/frames", "i");
		(f / samp) $ int => jack_scope.addInt;
	}

	fun static void
	delay(dur d)
	{
		// <<< "set delay length:", d / ms >>>;
		jack_scope.startMsg("/delay", "f");
		d / ms => jack_scope.addFloat;
	}

	/*
	 * "sample delay" in "embed" mode
	 * Can this be a ChucK duration???
	 */
	fun static void
	embed(int em)
	{
		jack_scope.startMsg("/embed", "i");
		em => jack_scope.addInt;
	}

	fun static void
	incr(float i)
	{
		jack_scope.startMsg("/incr", "f");
		i => jack_scope.addFloat;
	}
}
/* initialization */
new OscSend @=> Oscope.jack_scope;
Oscope.jack_scope.setHost("localhost", 57140);

"signal" => Oscope.mode;
"line" => Oscope.style;
512::samp => Oscope.frames;
100::ms => Oscope.delay;

/*
 * connect oscilloscope Bus channels to dedicated output ports that are patched
 * to jack.scope
 */
for (0 => int i; i < Bus.oscope.cap(); i++)
	Bus.oscope[i] => dac.chan(4 + i);

/*
 * jack.scope configuration via MIDI (Channel/Scene 1)
 */
/* FIXME: custom nanoKONTROL events */
if (me.args() > 0)
	me.exit();

MidiIn min;

/* always open MIDI Through port, actual connection is done by Jack */
if (!min.open(0))
	me.exit();
<<< "MIDI device:", min.num(), " -> ", min.name() >>>;

3 => int on_channel; /* scene 4 */

while (min => now) {
	while (MidiMsg msg => min.recv) {
		msg.data1 & 0x0F => int channel;
		msg.data1 & 0xF0 => int cmd;
		(msg.data3 $ float)/127 => float value;

		if (channel == on_channel && cmd == 0xB0) {
			<<< "Channel:", channel, "Command:", cmd, "Controller:", msg.data2, "Value:", value >>>;

			if (msg.data2 == 67) {
				if (value $ int)
					"embed" => Oscope.mode;
				else
					"signal" => Oscope.mode;
			} else if (msg.data2 == 76) {
				if (value $ int)
					"fill" => Oscope.style;
				else
					"line" => Oscope.style;
			} else if (msg.data2 == 42) {
				512::samp + value*2::second => Oscope.frames;
			} else if (msg.data2 == 57) {
				50::ms + value*second => Oscope.delay;
			}
		}
	}
}