-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsound_player.py
More file actions
38 lines (30 loc) · 1.16 KB
/
sound_player.py
File metadata and controls
38 lines (30 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import paho.mqtt.client as mqtt
import requests
from requests.auth import HTTPBasicAuth
import uuid
from subprocess import call
class Receiver:
def on_connect(self, client, userdata, flags, rc):
print("Connected to sound with result code ", str(rc))
client.subscribe("leapLescoSound")
def on_disconnect(self, client, userdata, rc):
print("Desconectado")
def on_message(self, client, userdata, msg):
data = msg.payload.decode("utf-8")
url = "https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?accept=audio/wav&text=" + data + "&voice=es-LA_SofiaVoice"
username = "e034115b-f434-4cac-a248-bdeccf00498f"
password = "DeMiAWipCouP"
r = requests.get(url, auth=HTTPBasicAuth(username, password))
unique_filename = str(uuid.uuid4())
with open(unique_filename + ".wav", "wb") as temp:
temp.write(r.content)
print("Reproduciendo " + temp.name)
call(["cvlc", "--play-and-exit", temp.name])
def __init__(self):
client = mqtt.Client()
client.on_connect = self.on_connect
client.on_message = self.on_message
client.on_disconnect = self.on_disconnect
client.connect("iot.eclipse.org", 1883, 60)
client.loop_forever()
r = Receiver()