Mercurial > code > home > repos > homeauto
diff service/speechMusic/speechMusic.py @ 1258:f0bbab217983
speechmusic can now fetch from http
Ignore-this: 111d84089591287c786f9119bbe546a
darcs-hash:29345c38a1f76d6aaf0f58a34d26c5344b234356
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Fri, 19 Apr 2019 14:24:09 -0700 |
parents | c0721332a9fe |
children |
line wrap: on
line diff
--- a/service/speechMusic/speechMusic.py Fri Apr 19 13:51:54 2019 -0700 +++ b/service/speechMusic/speechMusic.py Fri Apr 19 14:24:09 2019 -0700 @@ -3,7 +3,7 @@ play sounds according to POST requests. """ from __future__ import division -import sys, tempfile +import sys, tempfile, itertools from pyjade.ext.mako import preprocessor as mako_preprocessor from mako.lookup import TemplateLookup from twisted.internet import reactor @@ -16,6 +16,7 @@ import pygame.mixer class URIRef(str): pass +soundCount = itertools.count() templates = TemplateLookup(directories=['.'], preprocessor=mako_preprocessor, filesystem_checks=True) @@ -45,14 +46,19 @@ def _getSound(self, uri): def done(resp): - print('got', len(resp.body)) + path = '/tmp/sound_%s' % next(soundCount) + with open(path, 'w') as out: + out.write(resp.body) + log.info('write %s bytes to %s', len(resp.body), path) + self.buffers[uri] = pygame.mixer.Sound(path) - return fetch(uri).addCallback(done) + return fetch(uri).addCallback(done).addErrback(log.error) def playEffect(self, uri): if uri not in self.buffers: self.buffers[uri] = LOADING - self._getSound(uri).addCallback(self.playEffect, uri) + self._getSound(uri).addCallback(lambda ret: self.playEffect(uri)) + return if self.buffers[uri] is LOADING: # The first playback loads then plays, but any attempts # during that load are dropped, not queued.