aboutsummaryrefslogtreecommitdiff
path: root/ignore/oscmidi_to_midi.sc
blob: 971b60e0f603df08374af9412d7efd5ef12b39c5 (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
#!/usr/bin/env sclang

/*
 * Copyright (c) 2015 Hanspeter Portner (dev@open-music-kontrollers.ch)
 * 
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 * 
 * This source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Artistic License 2.0 for more details.
 * 
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

{
	var chimconf, midio, func;

	chimconf = ChimaeraConf(s,
		addr:"chimaera.local", prot:\tcp);

	chimconf.sendMsg("/sensors/group/reset");

	chimconf.sendMsg("/sensors/group/attributes/0/min", 0.0);
	chimconf.sendMsg("/sensors/group/attributes/0/max", 1.0);
	chimconf.sendMsg("/sensors/group/attributes/0/north", false);
	chimconf.sendMsg("/sensors/group/attributes/0/south", true);
	chimconf.sendMsg("/sensors/group/attributes/0/scale", false);

	chimconf.sendMsg("/sensors/group/attributes/1/min", 0.0);
	chimconf.sendMsg("/sensors/group/attributes/1/max", 1.0);
	chimconf.sendMsg("/sensors/group/attributes/1/north", true);
	chimconf.sendMsg("/sensors/group/attributes/1/south", false);
	chimconf.sendMsg("/sensors/group/attributes/1/scale", false);

	chimconf.sendMsg("/engines/oscmidi/enabled", true);
	chimconf.sendMsg("/engines/oscmidi/multi", true);
	chimconf.sendMsg("/engines/oscmidi/format", "int32");
	chimconf.sendMsg("/engines/oscmidi/path", "/midi");

	chimconf.sendMsg("/sensors/number", {|msg|
		var n=msg[0];
		Routine.run({
			var bot = 3*12 - 0.5 - (n % 18 / 6);
			var ran = n/3;
			chimconf.sendMsg("/engines/oscmidi/reset");

			chimconf.sendMsg("/engines/oscmidi/attributes/0/mapping", "control_change");
			chimconf.sendMsg("/engines/oscmidi/attributes/0/offset", bot);
			chimconf.sendMsg("/engines/oscmidi/attributes/0/range", ran);
			chimconf.sendMsg("/engines/oscmidi/attributes/0/controller", 0x07);

			chimconf.sendMsg("/engines/oscmidi/attributes/1/mapping", "control_change");
			chimconf.sendMsg("/engines/oscmidi/attributes/1/offset", bot);
			chimconf.sendMsg("/engines/oscmidi/attributes/1/range", ran);
			chimconf.sendMsg("/engines/oscmidi/attributes/1/controller", 0x07);
		}, clock:AppClock);
	});

	MIDIClient.init;
	//midio = MIDIOut(0, MIDIClient.destinations[0].uid); // use this on Windows
	midio = MIDIOut(0); // use this on Linux/OSX

	// OSC MIDI responder
	func = OSCFunc({|msg, time, addr, port|
		midio.latency = ChimaeraOut.timeToLatency(time);

		msg.removeAt(0); // remove path
		msg.do({|i| // iterate over all MIDI messages
			var hiStatus = i & 0xf0; // upper status byte
			var loStatus = i & 0x0f; // lower status byte (channel)
			var dat0 = (i & 0x7f00) >> 8; // data byte 0
			var dat1 = (i & 0x7f0000) >> 16; // data byte 1
			midio.write(3, hiStatus, loStatus, dat0, dat1);
		});
	}, "/midi", chimconf.rx);
}.value;