Mercurial > code > home > repos > homeauto
annotate service/speechMusic/speechMusic.py @ 809:bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
Ignore-this: a11e90f9d2cd9470565c743f54943c4b
darcs-hash:20110808073131-312f9-a7f420d66388cedae458276d672a27a9249f1e2f.gz
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Mon, 08 Aug 2011 00:31:31 -0700 |
parents | |
children | 5ed239235b69 |
rev | line source |
---|---|
809
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
1 #!bin/python |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
2 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
3 """ |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
4 play sounds according to POST requests. cooperate with pubsubhubbub |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
5 """ |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
6 import web, sys, jsonlib, subprocess, os, tempfile, logging |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
7 from subprocess import check_call |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
8 sys.path.append("/my/proj/csigen") |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
9 from generator import tts |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
10 import xml.etree.ElementTree as ET |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
11 logging.basicConfig(level=logging.INFO, format="%(created)f %(asctime)s %(levelname)s %(message)s") |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
12 log = logging.getLogger() |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
13 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
14 sensorWords = {"wifi" : "why fi", |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
15 "bluetooth" : "bluetooth"} |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
16 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
17 def aplay(device, filename): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
18 paDeviceName = { |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
19 'garage' : 'alsa_output.pci-0000_01_07.0.analog-stereo', |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
20 'living' : 'alsa_output.pci-0000_00_04.0.analog-stereo', |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
21 }[device] |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
22 subprocess.call(['paplay', |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
23 '-d', paDeviceName, |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
24 filename]) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
25 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
26 def soundOut(preSound=None, speech='', postSound=None, fast=False): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
27 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
28 speechWav = tempfile.NamedTemporaryFile(suffix='.wav') |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
29 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
30 root = ET.Element("SABLE") |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
31 r = ET.SubElement(root, "RATE", |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
32 attrib=dict(SPEED="+50%" if fast else "+0%")) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
33 for sentence in speech.split('.'): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
34 div = ET.SubElement(r, "DIV") |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
35 div.set("TYPE", "sentence") |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
36 div.text = sentence |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
37 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
38 sounds = [] |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
39 delays = [] |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
40 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
41 if preSound is not None: |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
42 sounds.append(preSound) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
43 delays.extend([0,0]) # assume stereo |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
44 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
45 speechSecs = tts(root, speechWav.name) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
46 sounds.append(speechWav.name) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
47 delays.append(.4) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
48 if postSound is not None: |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
49 sounds.append(postSound) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
50 delays.extend([speechSecs + .4]*2) # assume stereo |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
51 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
52 if len(sounds) == 1: |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
53 outName = sounds[0] |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
54 else: |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
55 outWav = tempfile.NamedTemporaryFile(suffix='.wav') |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
56 check_call(['/usr/bin/sox', '--norm', '--combine', 'merge', |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
57 ]+sounds+[ |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
58 outWav.name, |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
59 'delay', ]+map(str, delays)+[ |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
60 'channels', '1']) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
61 outName = outWav.name |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
62 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
63 aplay('living', outName) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
64 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
65 class visitorNet(object): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
66 def POST(self): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
67 data = jsonlib.loads(web.data()) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
68 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
69 if data.get('action') == 'arrive': |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
70 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
71 snd = ('/my/music/entrance/%s.wav' % |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
72 data['name'].replace(' ', '_').replace(':', '_')) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
73 if not os.path.exists(snd): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
74 snd = None |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
75 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
76 soundOut(preSound="/my/music/snd/angel_ogg/angel_question.wav", |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
77 # sic: |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
78 speech="Neew %s. %s" % (sensorWords[data['sensor']], |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
79 data['name']), |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
80 postSound=snd, fast=True) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
81 return 'ok' |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
82 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
83 if data.get('action') == 'leave': |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
84 soundOut(preSound='/my/music/entrance/leave.wav', |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
85 speech="lost %s. %s" % (sensorWords[data['sensor']], |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
86 data['name']), |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
87 fast=True) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
88 return 'ok' |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
89 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
90 return "nothing to do" |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
91 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
92 class index(object): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
93 def GET(self): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
94 web.header('Content-type', 'text/html') |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
95 return ''' |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
96 <p><form action="speak" method="post">say: <input type="text" name="say"> <input type="submit"></form></p> |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
97 <p><form action="testSound" method="post"> <input type="submit" value="test sound"></form></p> |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
98 ''' |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
99 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
100 class speak(object): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
101 def POST(self): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
102 txt = web.input()['say'] |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
103 log.info("speak: %r", txt) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
104 soundOut(preSound='/my/music/snd/Oxygen/KDE-Im-Highlight-Msg-44100.wav', |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
105 speech=txt) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
106 return "sent" |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
107 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
108 class testSound(object): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
109 def POST(self): |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
110 soundOut(preSound='/my/music/entrance/leave.wav') |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
111 return 'ok' |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
112 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
113 urls = ( |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
114 r'/', 'index', |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
115 r'/speak', 'speak', |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
116 r'/testSound', 'testSound', |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
117 r'/visitorNet', 'visitorNet', |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
118 ) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
119 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
120 app = web.application(urls, globals(), autoreload=True) |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
121 |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
122 if __name__ == '__main__': |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
123 sys.argv.append("9049") |
bebb8f7c5a3e
move a bunch of services into this tree, give them all web status pages
drewp <drewp@bigasterisk.com>
parents:
diff
changeset
|
124 app.run() |