annotate service/playSound/playSound.py @ 669:b27a4652cd84

playsound is working ok on dash right now Ignore-this: 3a0b801cb75eb157a7d189f9de5d7b0c
author drewp@bigasterisk.com
date Sun, 22 Dec 2019 00:34:08 -0800
parents aaabba10ce04
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
75
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
1 """
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
2 play sounds according to POST requests.
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
3 """
664
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
4 from docopt import docopt
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
5 import cyclone.web
669
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
6 import os, sys, tempfile, itertools, subprocess
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
7 import treq
75
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
8 from twisted.internet import reactor
669
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
9 from twisted.internet.defer import inlineCallbacks
75
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
10 from twisted.web.static import File
664
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
11 from standardservice.logsetup import log, verboseLogging
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
12
454
ccde9f432e4e WIP speechmusic to load from http, but pulseaudio out is broken
drewp@bigasterisk.com
parents: 453
diff changeset
13 class LOADING(object): pass
ccde9f432e4e WIP speechmusic to load from http, but pulseaudio out is broken
drewp@bigasterisk.com
parents: 453
diff changeset
14
75
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
15 class SoundEffects(object):
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
16 def __init__(self):
668
aaabba10ce04 try using tcp to talk to host pulse
drewp@bigasterisk.com
parents: 667
diff changeset
17 self.buffers = {} # URIRef : path
75
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
18 self.playingSources = []
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
19 self.queued = []
453
9fd92202c886 WIP updating to read sounds from http
drewp@bigasterisk.com
parents: 359
diff changeset
20 self.volume = 1 # level for the next sound that's played (or existing instances of the same sound)
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
21
669
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
22 @inlineCallbacks
454
ccde9f432e4e WIP speechmusic to load from http, but pulseaudio out is broken
drewp@bigasterisk.com
parents: 453
diff changeset
23 def _getSound(self, uri):
669
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
24 resp = yield treq.get(uri.encode('utf8'))
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
25 body = yield treq.content(resp)
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
26 path = '/tmp/sound_%s' % hash(uri)
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
27 with open(path, 'wb') as out:
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
28 out.write(body)
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
29 log.info('write %s bytes to %s', len(body), path)
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
30 self.buffers[uri] = path
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
31 print('donesave')
454
ccde9f432e4e WIP speechmusic to load from http, but pulseaudio out is broken
drewp@bigasterisk.com
parents: 453
diff changeset
32
668
aaabba10ce04 try using tcp to talk to host pulse
drewp@bigasterisk.com
parents: 667
diff changeset
33 def playEffect(self, uri: str):
454
ccde9f432e4e WIP speechmusic to load from http, but pulseaudio out is broken
drewp@bigasterisk.com
parents: 453
diff changeset
34 if uri not in self.buffers:
ccde9f432e4e WIP speechmusic to load from http, but pulseaudio out is broken
drewp@bigasterisk.com
parents: 453
diff changeset
35 self.buffers[uri] = LOADING
669
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
36 self._getSound(uri).addCallback(lambda ret: self.playEffect(uri)).addErrback(log.error)
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
37 return b'will play after load'
454
ccde9f432e4e WIP speechmusic to load from http, but pulseaudio out is broken
drewp@bigasterisk.com
parents: 453
diff changeset
38 if self.buffers[uri] is LOADING:
ccde9f432e4e WIP speechmusic to load from http, but pulseaudio out is broken
drewp@bigasterisk.com
parents: 453
diff changeset
39 # The first playback loads then plays, but any attempts
ccde9f432e4e WIP speechmusic to load from http, but pulseaudio out is broken
drewp@bigasterisk.com
parents: 453
diff changeset
40 # during that load are dropped, not queued.
ccde9f432e4e WIP speechmusic to load from http, but pulseaudio out is broken
drewp@bigasterisk.com
parents: 453
diff changeset
41 return
ccde9f432e4e WIP speechmusic to load from http, but pulseaudio out is broken
drewp@bigasterisk.com
parents: 453
diff changeset
42 snd = self.buffers[uri]
668
aaabba10ce04 try using tcp to talk to host pulse
drewp@bigasterisk.com
parents: 667
diff changeset
43 print('subp')
aaabba10ce04 try using tcp to talk to host pulse
drewp@bigasterisk.com
parents: 667
diff changeset
44 subprocess.check_call(['paplay', snd])
669
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
45 return b'played'
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
46
75
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
47 def done(self, src):
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
48 try:
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
49 self.playingSources.remove(src)
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
50 except ValueError:
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
51 pass
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
52
75
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
53 def stopAll(self):
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
54 while self.playingSources:
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
55 self.playingSources.pop().stop()
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
56 for q in self.queued:
1132ab1ade80 big rewrite of speechMusic to use klein and openal
drewp@bigasterisk.com
parents: 36
diff changeset
57 q.cancel()
454
ccde9f432e4e WIP speechmusic to load from http, but pulseaudio out is broken
drewp@bigasterisk.com
parents: 453
diff changeset
58 # doesn't cover the callLater ones
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
59
664
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
60 class Index(cyclone.web.RequestHandler):
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
61 def get(self):
666
91d17a68c21b drop speech synth, try to get pulse client working
drewp@bigasterisk.com
parents: 664
diff changeset
62 self.render('index.html')
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
63
664
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
64 class PlaySound(cyclone.web.RequestHandler):
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
65 def post(self):
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
66 uri = self.get_argument('uri')
669
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
67 return self.settings.sfx.playEffect(uri)
664
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
68
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
69 class Volume(cyclone.web.RequestHandler):
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
70 def put(self):
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
71 self.settings.sfx.setVolume(float(self.get_argument('v')))
669
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
72 return b"ok"
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
73
664
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
74 class StopAll(cyclone.web.RequestHandler):
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
75 def post(self):
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
76 self.settings.sfx.stopAll()
669
b27a4652cd84 playsound is working ok on dash right now
drewp@bigasterisk.com
parents: 668
diff changeset
77 return b"ok"
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
78
664
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
79
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
80 if __name__ == '__main__':
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
81 arg = docopt('''
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
82 Usage: playSound.py [options]
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
83
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
84 -v Verbose
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
85 ''')
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
86 verboseLogging(arg['-v'])
359
b3959142d7d8 move speech_music into docker and into pygame
drewp@bigasterisk.com
parents: 215
diff changeset
87
668
aaabba10ce04 try using tcp to talk to host pulse
drewp@bigasterisk.com
parents: 667
diff changeset
88 os.environ['PULSE_SERVER'] = '172.17.0.1'
664
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
89 sfx = SoundEffects()
4
be855a111619 move a bunch of services into this tree, give them all web status pages
drewp@bigasterisk.com
parents:
diff changeset
90
664
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
91 reactor.listenTCP(9049, cyclone.web.Application(handlers=[
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
92 (r'/', Index),
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
93 (r'/playSound', PlaySound),
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
94 (r'/volume', Volume),
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
95 (r'/stopAll', StopAll),
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
96 ], template_path='.', sfx=sfx))
28cc07978a71 start rewrite for playSound
drewp@bigasterisk.com
parents: 455
diff changeset
97 reactor.run()