changeset 53:0dbc2d2d0a33 moodplay

MHD-BCN15 visclient update
author gyorgyf
date Fri, 19 Jun 2015 01:27:01 +0200
parents 4455a77435b0
children
files visualclient2/visclient.py
diffstat 1 files changed, 110 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/visualclient2/visclient.py	Thu Jun 18 18:05:14 2015 +0200
+++ b/visualclient2/visclient.py	Fri Jun 19 01:27:01 2015 +0200
@@ -96,6 +96,73 @@
 
 def DmxSent(state):
 	wrapper.Stop()
+	
+
+class Track(object):
+	def __init__(self,data,header):
+		data = map(lambda x: x.replace('"',''), data.split(","))
+		for attr in header:
+			if not self.__dict__.has_key(attr) :
+				self.__dict__[attr] = data[header.index(attr)]
+	def __repr__(self):
+		return ",".join(map(lambda x: self.__dict__.get(x), self.__dict__))
+
+	
+class CollectionMetadata(object):
+	
+	def __init__(self):
+		print "Loading collection metadata..."
+		self.filename = "ilm10kmetadata.csv"
+		self.tracks = {}
+		self.load()
+		
+	def load(self):
+		with open(self.filename,"r") as f :
+			header = map(lambda x: x.replace('''"''',''), f.readline().split(","))
+			for line in f.readlines()[:] :
+				t = Track(line,header)
+				print t.ilm_id
+				self.tracks[t.ilm_id] = t
+		print "Loaded %i tracks." %len(self.tracks)
+
+	
+class CurrentlyPlaying(object):
+	
+	def __init__(self,bg):
+		self.bg = bg
+		self.ilmid = ""
+		self.visible = True
+		self.text = "whatever song playing"
+		self.color = (205,205,205)
+		self.x = 0.01 
+		self.y = 0.01
+		self.xs = self.x * X
+		self.ys = Y - (self.y * Y)
+		
+	def reinit(self,bg):
+		self.bg = bg
+		
+	def set_song(self,ilmid):
+		self.ilmid = ilmid
+		self.text = ilmid
+		
+	def draw(self):
+		if not self.visible : 
+			return
+		font = pg.font.Font(None, 26)
+		text = font.render(self.text, 1, self.color)
+		textpos = text.get_rect()
+		if self.xs > X- textpos.width:	
+			if self.ys > Y- textpos.height: 
+				self.bg.blit(text, (self.xs - textpos.width,self.ys - textpos.height))
+			else:
+				self.bg.blit(text, (self.xs - textpos.width,self.ys))
+		else :	
+			if self.ys > Y- textpos.height: 
+				self.bg.blit(text, (self.xs,self.ys - textpos.height))
+			else: 
+				self.bg.blit(text, (self.xs,self.ys))
+		
 
 class Indicator(object):
 	
@@ -183,6 +250,9 @@
 		self.target_x = x
 		self.target_y = y
 		
+	def get_position(self):
+		return self.x, self.y
+		
 	def draw(self):
 		if not self.visible : return
 
@@ -559,6 +629,7 @@
 		self.s_age = 10
 		self.s_dist = DIST
 		self.s_ninp = 18
+		self.collection = CollectionMetadata()
 		
 		pg.init()
 		pg.font.init()
@@ -609,6 +680,8 @@
 
 		# self.osc_client = OSC.OSCClient()
 		
+		self.current_playing = CurrentlyPlaying(self.bg)
+		
 		pass
 		
 	
@@ -623,9 +696,9 @@
 				l = map(lambda x:x.replace("'","").strip(),l)
 				mood = Mood(l[0],l[1],l[2])
 				moods.append(mood)
-		print moods
-		for mood in moods:
-			print"['%s',%2.2f,%2.2f,0,0]," %(mood.word,mood.x,mood.y)
+		# print moods
+		# for mood in moods:
+		# 	print"['%s',%2.2f,%2.2f,0,0]," %(mood.word,mood.x,mood.y)
 
 		with open('colors.txt') as ff:
 			data = ff.readlines()[1:]
@@ -676,6 +749,8 @@
 			# self.update()
 			try :
 				self.update()
+				self.send_av_position()
+				self.get_ilm_id()
 				# self.indicators["update"].visible = True
 			except Exception, e:
 				if str(e).strip() : print "Exception: ", str(e), type(e), len(str(e).strip())
@@ -742,6 +817,33 @@
 		self.blobs = self.blobs[:NBLOBS]
 		return True
 		
+	def get_ilm_id(self):
+		'''Read the ILM ID from the server'''
+		self.conn.putrequest("GET","/moodconductor/get_ilmid", skip_host=True)
+		self.conn.putheader("Host", "www.isophonics.net")
+		self.conn.endheaders()
+		res = self.conn.getresponse()
+		ilmid = res.read().replace("'","")
+		if self.collection.tracks.has_key(ilmid):			
+			t = self.collection.tracks[ilmid]
+			data = "%s - %s" %(t.song_title, t.artist_name)
+			
+			self.current_playing.set_song(data)
+		else :
+			self.current_playing.set_song("unknown")
+		# print data
+		
+	def send_av_position(self):
+		'''Update the server with current position of the moving blob.'''
+		x,y = 0.0,0.0
+		if self.movingBlob != None :
+			x,y = self.movingBlob.get_position()
+		self.conn.putrequest("GET","/moodconductor/put_path_av?x=%.4f&y=%.4f" %(x,y), skip_host=True)
+		self.conn.putheader("Host", "www.isophonics.net")
+		self.conn.endheaders()
+		res = self.conn.getresponse()
+		data = res.read()			
+		
 		
 	def add_blob(self,x,y,count=1):
 		'''Insert a blob to the list of blobs'''
@@ -777,7 +879,7 @@
 		l = copy.copy(self.blobs)
 		l.reverse()
 		xt,yt = 0.0,0.0
-		bs = 1
+		bs = 1.0
 		c = 1
 		# captured_by = None
 		
@@ -851,6 +953,9 @@
 		for i in self.indicators.itervalues() :
 			i.draw()
 			
+		# currently playing text
+		self.current_playing.draw()
+			
 	def read_keys(self):
 		'''Read keys'''
 		for event in pg.event.get() :
@@ -968,6 +1073,7 @@
 			if self.movingBlob != None :
 				self.movingBlob.reinit(self.bg)
 				self.movingBlob.draw()
+		self.current_playing.reinit(self.bg)
 		pg.display.toggle_fullscreen()