summaryrefslogtreecommitdiff
path: root/lib/NanoEvent.ck
blob: a3381338a77e01e9d49d2ad7804e1ad328ecdae0 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * BUG WORKAROUND
 * You just cannot declare plain static string arrays, so
 * we must wrap them in "real" objects
 */
class String {
	string v;
}
class StringArray {
	String @v[];
}

/*
 * nanoKONTROL event class
 */
public class NanoEvent extends Event {
	/* map channel (0-15) to scene name */
	static String @__channelToScene[]; /* pseudo-private */
	/* map scene name and control id (0-255) to control name */
	static StringArray @__controlToName[]; /* pseudo-private */

	string wantScene;

	string scene;
	string control;
	int CCId;
	float value;

	fun int
	isScene(string s)
	{
		return scene == s;
	}
	fun int
	isControl(string c)
	{
		return control == c;
	}

	fun float
	getFloat()
	{
		return value;
	}
	fun float
	getFloat(float max)
	{
		return max*value;
	}
	fun float
	getFloat(float min, float max)
	{
		return min + (max - min)*value;
	}

	fun dur
	getDur(dur max)
	{
		return max*value;
	}
	fun dur
	getDur(dur min, dur max)
	{
		return min + (max - min)*value;
	}

	fun int
	getBool()
	{
		return value $ int;
	}

	fun void
	__midi_loop(int device) /* pseudo-private */
	{
		MidiIn min;

		if (!min.open(device)) {
			<<< "Cannot open MIDI device", device >>>;
			me.exit();
		}

		while (min => now) {
			while (MidiMsg msg => min.recv) {
				__channelToScene[msg.data1 & 0x0F].v @=> scene;
				if (scene == "") {
					<<< "Unknown channel", msg.data1 & 0x0F >>>;
					msg.data1 & 0x0F => Std.itoa @=> scene;
				}

				msg.data1 & 0xF0 => int cmd;
				msg.data2 => CCId;

				__controlToName[scene].v[CCId].v @=> control;
				if (control == "") {
					<<< "Unknown controller", CCId >>>;
					CCId => Std.itoa @=> control;
				}

				(msg.data3 $ float)/127 => value;

				if (cmd == 0xB0 && (wantScene == "" || scene == wantScene))
				    	broadcast();
			}
		}
	}
	/* always open MIDI Through port, actual connection is done by Jack */
	spork ~ __midi_loop(0);

	fun static NanoEvent @
	init(string scene) /* pseudo-constructor */
	{
		NanoEvent obj;

		scene @=> obj.wantScene;

		return obj;
	}

	fun static void
	registerScene(int channel, string name)
	{
		if (__channelToScene[channel].v != "")
			<<< "Warning: Already registered channel", channel >>>;
		if (__controlToName[name] != null)
			<<< "Warning: Already registered scene name", name >>>;

		name @=> __channelToScene[channel].v;
		new StringArray @=> __controlToName[name];
		new String[0x100] @=> __controlToName[name].v;
	}

	fun static void
	registerControl(string sceneName, int id, string controlName)
	{
		if (__controlToName[sceneName].v[id].v != "")
			<<< "Warning: Already registered control", id,
			    "on scene", sceneName >>>;

		controlName @=> __controlToName[sceneName].v[id].v;
	}
}
/* static initialization */
new String[0x10] @=> NanoEvent.__channelToScene;
new StringArray[0] @=> NanoEvent.__controlToName;

/*
 * global mappings
 */

NanoEvent.registerScene(0, "primary");
NanoEvent.registerScene(1, "secondary");
NanoEvent.registerScene(3, "oscope");

NanoEvent.registerControl("primary", 14, "feedbackDistKnob");
NanoEvent.registerControl("primary", 02, "feedbackPitchSlider");
NanoEvent.registerControl("primary", 03, "feedbackPregainSlider");
NanoEvent.registerControl("primary", 15, "feedbackGainKnob");

for (23 => int i; i <= 29; i++)
	NanoEvent.registerControl("primary", i, "samplerBankButton#"+i);
NanoEvent.registerControl("primary", 19, "samplerVolumeKnob");
NanoEvent.registerControl("primary", 08, "samplerPitchSlider");

fun void
registerLFO(string scene)
{
	NanoEvent.registerControl(scene, 22, "lfoVolumeKnob");
	NanoEvent.registerControl(scene, 13, "lfoPitchSlider");
	NanoEvent.registerControl(scene, 12, "lfoDepthSlider");
	NanoEvent.registerControl(scene, 21, "lfoFreqKnob");
	NanoEvent.registerControl(scene, 31, "lfoSinOscButton");
	NanoEvent.registerControl(scene, 41, "lfoPulseOscButton");
	NanoEvent.registerControl(scene, 30, "lfoSampOscButton");
	NanoEvent.registerControl(scene, 40, "lfoWaveToggle");
	NanoEvent.registerControl(scene, 09, "lfoEchoSlider");
}
"primary" => registerLFO;
"secondary" => registerLFO;

fun void
registerTransport(string scene)
{
	NanoEvent.registerControl(scene, 44, "recordToggle");
	NanoEvent.registerControl(scene, 45, "playButton");
	NanoEvent.registerControl(scene, 46, "stopButton");
	NanoEvent.registerControl(scene, 49, "loopToggle");
}
"primary" => registerTransport;
"secondary" => registerTransport;

NanoEvent.registerControl("oscope", 67, "modeToggle");
NanoEvent.registerControl("oscope", 76, "fillToggle");
NanoEvent.registerControl("oscope", 42, "frameSlider");
NanoEvent.registerControl("oscope", 57, "delayKnob");