-
Notifications
You must be signed in to change notification settings - Fork 0
/
musicPlayer.py
executable file
·157 lines (142 loc) · 4.59 KB
/
musicPlayer.py
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
#!/usr/bin/env python
import threading
import time
import subprocess
import glob
import random
import json
import os
import sys
import traceback
import urllib2
from soundFile import SoundFile
import pygame
from debug import Debug
from soundTrack import SoundTrackManager
from specs import Specs
from hosts import Hosts
from server import Server
import Queue
class MusicBlock(object):
def __init__(self):
self.mute = False
class MusicPlayer(threading.Thread):
def __init__(self,defaultCollection="playerCollection"):
super(MusicPlayer,self).__init__()
self.done = False
self.name= "MusicPlayer"
self.running = None
self.mutex = threading.Lock()
self.queue = Queue.Queue()
self.collection = defaultCollection
self.waitTime = None
self.musicBlocks = {}
for ip in Hosts().getHostIps():
music = Hosts().getAttr(ip,"music")
if music['enabled']:
self.musicBlocks[ip] = MusicBlock()
SoundFile().setCurrentCollection(self.collection)
if Hosts().getLocalAttr("hasServer"):
Server().register({"HaltMusic" : self.haltMusic
,"StartMusic" : self.startMusic
,"Mute" : self.mute
})
def stop(self):
Debug().p("%s: stop request"%self.name)
self.setRunning(False)
def mute(self,args):
self.musicBlocks[args['ip']].mute = args['mute']
return Hosts.jsonStatus(str(args))
def haltMusic(self,cmd):
self.queue.put("__halt__")
return Hosts.jsonStatus(str(cmd))
def startMusic(self,cmd):
self.queue.put("__start__")
return Hosts.jsonStatus(str(cmd))
def setRunning(self,flag):
self.mutex.acquire()
self.running = flag
self.mutex.release()
return flag
def isRunning(self):
self.mutex.acquire()
flag = self.running
self.mutex.release()
return flag
def checkMsg(self):
rval = False
try:
msg = self.queue.get(timeout=self.waitTime)
if msg == "__stop__":
self.stop()
elif msg == "__halt__":
Debug().p("%s halting"%self.name)
self.waitTime = None
elif msg == "__start__":
Debug().p("%s starting"%self.name)
self.waitTime = .1
rval = True
except Queue.Empty:
pass
return rval
def doExit(self):
print ("%s done"%self.name)
for t in SoundTrackManager().eventThreads:
t.stop()
self.done = True
def run(self):
print("%s starting"%self.name)
self.setRunning(SoundFile().testBumpCollection())
loop = Specs().s['musicLoop']
while self.isRunning():
if self.checkMsg():
continue
entry = SoundFile().getSoundEntry()
Debug().p("player choosing %s"%entry)
count = 0
if self.musicBlocks[Hosts().getLocalHost()].mute:
Debug().p("%s local %s mute so ignoring"%(self.name,Hosts().getLocalHost()))
else:
for t in SoundTrackManager().eventThreads:
choice = random.choice(entry)
Debug().p("sending %s request to %s"%(choice,t.name))
t.setCurrentSound(choice)
for ip in self.musicBlocks.keys():
if Hosts().isLocalHost(ip):
Debug().p("%s: ignoring %s"%(self.name,ip))
continue
if self.musicBlocks[ip].mute:
Debug().p("%s: ignoring muted %s %s"%(self.name,ip,self.musicBlocks[ip].mute))
continue
try:
url = "http://"+ip+":8080"
Debug().p("%s: url: %s"%(self.name,url))
cmd = { 'cmd' : "Sound" ,'args' : choice }
req = urllib2.Request(url
,json.dumps(cmd),{'Content-Type': 'application/json'})
timeout = 1
f = urllib2.urlopen(req,None,timeout)
test = f.read()
Debug().p("%s: got response:%s"%(self.name,test))
except urllib2.URLError as ve:
Debug().p("%s: got URLError %s on ip:%s"%(self.name,ve,ip))
continue
except Exception as e:
print("%s got exception %s"%(self.name,e))
continue
self.waitTime = random.randint(Specs().s['minChange'],Specs().s['maxChange'])
Debug().p("next change: %d"%self.waitTime)
#Debug().p("number busy channels %d"%n
if SoundFile().testBumpCollection() is False:
print "waiting for channels to be done"
n = pygame.mixer.get_busy()
while n != 0:
n = pygame.mixer.get_busy()
print "number busy channels",n
if self.checkMsg( ):
continue
if loop:
SoundFile().setCurrentCollection(self.collection)
else:
self.SetRunning(false)
self.doExit()