Mercurial > code > home > repos > homeauto
changeset 1470:94abe0983594
playsound is working ok on dash right now
Ignore-this: 3a0b801cb75eb157a7d189f9de5d7b0c
darcs-hash:506ffde859a476f93659b07e086e86e232896bd0
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sun, 22 Dec 2019 00:34:08 -0800 |
parents | 94f14a0823c6 |
children | 90e1ef015f09 |
files | service/playSound/Dockerfile service/playSound/playSound.py service/playSound/requirements.txt service/playSound/serv.n3 |
diffstat | 4 files changed, 21 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/service/playSound/Dockerfile Sat Dec 21 16:01:25 2019 -0800 +++ b/service/playSound/Dockerfile Sun Dec 22 00:34:08 2019 -0800 @@ -2,7 +2,7 @@ WORKDIR /opt -RUN touch need-new-update +RUN touch need-new-update2 RUN apt-get update RUN apt-get install --yes libopenal1 libogg0 pulseaudio-utils python-pygame
--- a/service/playSound/playSound.py Sat Dec 21 16:01:25 2019 -0800 +++ b/service/playSound/playSound.py Sun Dec 22 00:34:08 2019 -0800 @@ -3,9 +3,10 @@ """ from docopt import docopt import cyclone.web -import os, sys, tempfile, itertools +import os, sys, tempfile, itertools, subprocess +import treq from twisted.internet import reactor -from cyclone.httpclient import fetch +from twisted.internet.defer import inlineCallbacks from twisted.web.static import File from standardservice.logsetup import log, verboseLogging @@ -18,24 +19,22 @@ self.queued = [] self.volume = 1 # level for the next sound that's played (or existing instances of the same sound) + @inlineCallbacks def _getSound(self, uri): - def done(resp): - print('save') - body = bytes(resp.body) - path = '/tmp/sound_%s' % hash(uri) - with open(path, 'wb') as out: - out.write(body) - log.info('write %s bytes to %s', len(resp.body), path) - self.buffers[uri] = path - print('donesave') - - return fetch(uri.encode('utf8')).addCallback(done).addErrback(log.error) + resp = yield treq.get(uri.encode('utf8')) + body = yield treq.content(resp) + path = '/tmp/sound_%s' % hash(uri) + with open(path, 'wb') as out: + out.write(body) + log.info('write %s bytes to %s', len(body), path) + self.buffers[uri] = path + print('donesave') def playEffect(self, uri: str): if uri not in self.buffers: self.buffers[uri] = LOADING - self._getSound(uri).addCallback(lambda ret: self.playEffect(uri)) - return + self._getSound(uri).addCallback(lambda ret: self.playEffect(uri)).addErrback(log.error) + return b'will play after load' if self.buffers[uri] is LOADING: # The first playback loads then plays, but any attempts # during that load are dropped, not queued. @@ -43,7 +42,7 @@ snd = self.buffers[uri] print('subp') subprocess.check_call(['paplay', snd]) - return + return b'played' def done(self, src): try: @@ -65,18 +64,17 @@ class PlaySound(cyclone.web.RequestHandler): def post(self): uri = self.get_argument('uri') - self.settings.sfx.playEffect(uri) - return "ok" + return self.settings.sfx.playEffect(uri) class Volume(cyclone.web.RequestHandler): def put(self): self.settings.sfx.setVolume(float(self.get_argument('v'))) - return "ok" + return b"ok" class StopAll(cyclone.web.RequestHandler): def post(self): self.settings.sfx.stopAll() - return "ok" + return b"ok" if __name__ == '__main__':
--- a/service/playSound/requirements.txt Sat Dec 21 16:01:25 2019 -0800 +++ b/service/playSound/requirements.txt Sun Dec 22 00:34:08 2019 -0800 @@ -1,6 +1,6 @@ cyclone mock==1.0.1 Twisted -requests==2.22.0 +treq==18.6.0 standardservice==0.6.0
--- a/service/playSound/serv.n3 Sat Dec 21 16:01:25 2019 -0800 +++ b/service/playSound/serv.n3 Sun Dec 22 00:34:08 2019 -0800 @@ -14,6 +14,7 @@ "--cap-add" "SYS_PTRACE" #"--mount type=bind,source=/etc/pulse,target=/etc/pulse" "--mount" "type=tmpfs,destination=/tmp,tmpfs-size=52428800" + "--mount" "type=bind,source=/my/proj/homeauto/service/playSound,target=/opt" "--mount" "type=bind,source=/home/drewp/.config/pulse/cookie,target=/home/pulseuser/.config/pulse/cookie" ); :localRunCmdline (