Mercurial > code > home > repos > homeauto
annotate service/playSound/playSound.py @ 1685:6b80a6c58907
minor changes to several services
author | drewp@bigasterisk.com |
---|---|
date | Mon, 27 Sep 2021 23:12:43 -0700 |
parents | b27a4652cd84 |
children |
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 | 4 from docopt import docopt |
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 | 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 | 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 | 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 | 43 print('subp') |
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 | 60 class Index(cyclone.web.RequestHandler): |
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 | 64 class PlaySound(cyclone.web.RequestHandler): |
65 def post(self): | |
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 | 68 |
69 class Volume(cyclone.web.RequestHandler): | |
70 def put(self): | |
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 | 74 class StopAll(cyclone.web.RequestHandler): |
75 def post(self): | |
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 | 79 |
80 if __name__ == '__main__': | |
81 arg = docopt(''' | |
82 Usage: playSound.py [options] | |
83 | |
84 -v Verbose | |
85 ''') | |
86 verboseLogging(arg['-v']) | |
359
b3959142d7d8
move speech_music into docker and into pygame
drewp@bigasterisk.com
parents:
215
diff
changeset
|
87 |
668 | 88 os.environ['PULSE_SERVER'] = '172.17.0.1' |
664 | 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 | 91 reactor.listenTCP(9049, cyclone.web.Application(handlers=[ |
92 (r'/', Index), | |
93 (r'/playSound', PlaySound), | |
94 (r'/volume', Volume), | |
95 (r'/stopAll', StopAll), | |
96 ], template_path='.', sfx=sfx)) | |
97 reactor.run() |