-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain_rgws.py
47 lines (37 loc) · 1.35 KB
/
main_rgws.py
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
39
40
41
42
43
44
45
46
47
import asyncio
import logging
from rgws.interface import WebsocketServer
from sound_generator import SoundGenerator
logging.basicConfig(level=logging.DEBUG)
class SGServerInterface(WebsocketServer):
def __init__(self, **kwargs):
super(SGServerInterface, self).__init__(**kwargs)
self.state = "Initializing"
self.generator = SoundGenerator()
self._register(self.get_prediction)
self._register(self.setup_model)
self._register(self.status)
self.state = "Initialized"
async def _consumer(self, ws, message):
ret = await self.dispatch(message)
async for gen in ret:
await ws.send_json(gen)
async def get_prediction(self, data):
self.state = "Processing"
result = self.generator.get_prediction(data)
yield {
"resp": result.get("audio") or [],
"success": result.get("success") or False,
"measures_sliders": result.get("measures_sliders"),
"z": result.get("z")
}
self.state = "Processed"
async def setup_model(self):
yield {"resp": True}
async def status(self):
yield {"resp": self.state}
if __name__ == "__main__":
s = SGServerInterface(host="localhost", port=8080)
loop = asyncio.get_event_loop()
loop.run_until_complete(s.run())
loop.run_forever()