changeset 30:1df5ded9f9ff

Fixed some bugs in the grid mapper.
author Tim MB <tim.murraybrowne@eecs.qmul.ac.uk>
date Fri, 25 Feb 2011 11:05:10 +0000
parents d3fdadf86d90
children f7caff1c2fa6 950b839f50ed
files Abletons max connection.maxpat tim_grid_mapper/grid_mapper.py
diffstat 2 files changed, 90 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/Abletons max connection.maxpat	Fri Feb 25 10:17:53 2011 +0000
+++ b/Abletons max connection.maxpat	Fri Feb 25 11:05:10 2011 +0000
@@ -1,9 +1,9 @@
 {
 	"patcher" : 	{
 		"fileversion" : 1,
-		"rect" : [ 93.0, 138.0, 1194.0, 703.0 ],
+		"rect" : [ 815.0, 97.0, 584.0, 712.0 ],
 		"bglocked" : 0,
-		"defrect" : [ 93.0, 138.0, 1194.0, 703.0 ],
+		"defrect" : [ 815.0, 97.0, 584.0, 712.0 ],
 		"openrect" : [ 0.0, 0.0, 0.0, 0.0 ],
 		"openinpresentation" : 0,
 		"default_fontsize" : 12.0,
@@ -20,6 +20,31 @@
 		"devicewidth" : 0.0,
 		"boxes" : [ 			{
 				"box" : 				{
+					"maxclass" : "message",
+					"text" : "1 1 48",
+					"patching_rect" : [ 678.0, 240.0, 50.0, 18.0 ],
+					"id" : "obj-167",
+					"fontname" : "Arial",
+					"numinlets" : 2,
+					"numoutlets" : 1,
+					"fontsize" : 12.0,
+					"outlettype" : [ "" ]
+				}
+
+			}
+, 			{
+				"box" : 				{
+					"maxclass" : "button",
+					"patching_rect" : [ 668.0, 157.0, 20.0, 20.0 ],
+					"id" : "obj-165",
+					"numinlets" : 1,
+					"numoutlets" : 1,
+					"outlettype" : [ "bang" ]
+				}
+
+			}
+, 			{
+				"box" : 				{
 					"maxclass" : "newobj",
 					"text" : "ctlout a",
 					"patching_rect" : [ 325.0, 648.0, 49.0, 20.0 ],
@@ -1417,6 +1442,24 @@
  ],
 		"lines" : [ 			{
 				"patchline" : 				{
+					"source" : [ "obj-8", 0 ],
+					"destination" : [ "obj-167", 1 ],
+					"hidden" : 0,
+					"midpoints" : [  ]
+				}
+
+			}
+, 			{
+				"patchline" : 				{
+					"source" : [ "obj-8", 0 ],
+					"destination" : [ "obj-165", 0 ],
+					"hidden" : 0,
+					"midpoints" : [  ]
+				}
+
+			}
+, 			{
+				"patchline" : 				{
 					"source" : [ "obj-141", 0 ],
 					"destination" : [ "obj-163", 0 ],
 					"hidden" : 0,
--- a/tim_grid_mapper/grid_mapper.py	Fri Feb 25 10:17:53 2011 +0000
+++ b/tim_grid_mapper/grid_mapper.py	Fri Feb 25 11:05:10 2011 +0000
@@ -1,4 +1,5 @@
-'''
+#! /usr/bin/python
+'''
 grid_mapper.py - maintained by Tim.
 
 This module implements a mapping from person positions (id,x,y,z) to generate 
@@ -10,6 +11,7 @@
 from OSC import ThreadingOSCServer, OSCClient, OSCMessage, OSCClientError
 from threading import Thread
 from time import sleep
+import pdb
 
 #### PUBLIC OPTIONS ####
 num_channels = 3 # number of instruments (and max number of people who can 
@@ -51,7 +53,19 @@
 currently_playing = [None] * num_channels
 
 # mapping from personId to time of last update
-last_update_times = {} 
+# ((not yet implemented))
+#last_update_times = {}
+
+# mapping from (channel, CC_number) to last CC value sent:
+last_value_sent = {
+	(1, 1) : 0,
+	(1, 2) : 0,
+	(2, 1) : 0,
+	(2, 2) : 0,
+	(3, 1) : 0,
+	(3, 2) : 0,
+}
+	
 
 # OSC OBJECTS
 server = None # Initialised when start() is run
@@ -69,7 +83,19 @@
 	message = OSCMessage(address)
 	message.extend(data)
 	client.sendto(message, joe)
-	print_d('==OSC Output to Joe %s:==\n    %s' % (joe, data))
+	print_d('\n==OSC Output "%s" to Joe %s:==\n    %s' % (address, joe, data))
+
+
+def send_to_ableton(data, address='/cc'):
+	'''Sends `data` to Ableton (via Max) as an OSC message.
+	'''
+	#pdb.set_trace()
+	message = OSCMessage(address)
+	message.extend(data)
+	client.sendto(message, ableton)
+	print('\n==OSC Output "%s" to Ableton %s:==\n    %s' % (address, ableton, data))
+	
+
 
 def flush(channel):
 	'''Sends note off messages for whatever note is currently playing on 
@@ -82,10 +108,11 @@
 			'Turn off note %i on channel %i' % (pitch, channel), 
 			    # first string is ignored
 			int(pitch),       # pitch to turn off
-			0,                                # 0 to turn note off
-			127,                # doesn't matter for note-off (but never send 0)
+			0,                # 0 to turn note off
+			127,              # doesn't matter for note-off (but never send 0)
 			int(channel),
 		])
+	currently_playing[channel] = None
 
 
 def person_handler(address, tags, data, client_address):
@@ -95,6 +122,14 @@
 	'''
 	pitch, velocity, channel, cc1, cc2 = grid_map(*data)
 	
+	cc1, cc2 = int(round(cc1)), int(round(cc2))
+	if cc1 != last_value_sent[(channel, 1)]:
+		send_to_ableton([channel, 1, cc1], '/cc')
+		last_value_sent[(channel, 1)] = cc1
+	if cc2 != last_value_sent[(channel, 2)]:
+		send_to_ableton([channel, 2, cc2], '/cc')
+		last_value_sent[(channel, 2)] = cc2		
+
 	
 	## Format data for Joe - done using Specification.txt on 2011-02-15
 	
@@ -113,11 +148,13 @@
 			'Turn on note %i on channel %i' % (pitch, channel),
 				# first value is string which is ignored
 			int(pitch),    
-			1,        # 1 to turn note on
+			1,               # 1 to turn note on
 			int(velocity), 
 			int(channel)
 		])
-	
+		currently_playing[channel] = pitch
+
+
 
 
 
@@ -144,7 +181,7 @@
 	                          MIN['x'],     MAX['x'],
 	                          MIN['cc1'], MAX['cc1'])
 	        )
-	cc2 = round(interpolate(x, 
+	cc2 = round(interpolate(y, 
 	                          MIN['y'],     MAX['y'],
 	                          MIN['cc2'], MAX['cc2'])
 	        )