Last active
September 19, 2022 15:00
-
-
Save MatthewScholefield/e842d9af5a10b5c9e96ea1a07d12e4dc to your computer and use it in GitHub Desktop.
Precise streaming over sockets example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright 2018 Matthew Scholefield | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import pyaudio | |
import socket | |
ADDRESS = ('localhost', 10000) | |
CHUNK_SIZE = 2048 | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
sock.connect(ADDRESS) | |
audio = pyaudio.PyAudio() | |
stream = audio.open( | |
format=pyaudio.paInt16, channels=1, | |
rate=16000, input=True, frames_per_buffer=CHUNK_SIZE | |
) | |
try: | |
while True: | |
sock.sendall(stream.read(CHUNK_SIZE)) | |
finally: | |
stream.stop_stream() | |
stream.close() | |
audio.terminate() | |
sock.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import socket | |
from threading import Thread | |
from precise.network_runner import Listener | |
from precise_runner import PreciseRunner, ReadWriteStream, PreciseEngine | |
from precise_runner.runner import ListenerEngine | |
ADDRESS = ('localhost', 10000) | |
MODEL_NAME = 'my-model.pb.or.net' | |
CHUNK_SIZE = 2048 | |
stream = ReadWriteStream() | |
runner = PreciseRunner( | |
PreciseEngine('.venv/bin/precise-engine', MODEL_NAME), | |
stream=stream, on_activation=lambda: print('Activated!') | |
) | |
runner.start() | |
class PreciseConnection: | |
"""Represents a socket connection routed to a precise process""" | |
def __init__(self, connection, address): | |
self.address = address | |
self.connection = connection # type: socket.socket | |
self.stream = ReadWriteStream() | |
self.runner = PreciseRunner( | |
ListenerEngine(Listener(MODEL_NAME, CHUNK_SIZE), CHUNK_SIZE), | |
1, stream=self.stream, on_activation=self.on_activation, | |
on_prediction=self.on_prediction | |
) | |
self.runner.start() | |
def on_activation(self): | |
print(' --- ACTIVATION from:', self.address, '--- ') | |
def on_prediction(self, conf): | |
print('!' if conf > 0.5 else '.', end='', flush=True) | |
def update(self): | |
"""Return whether connection still alive""" | |
data = self.connection.recv(CHUNK_SIZE) | |
self.stream.write(data) | |
return bool(data) | |
def close(self): | |
print('Closing connection from', self.address) | |
self.runner.stop() | |
self.connection.close() | |
class ConnectionManager: | |
def __init__(self): | |
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.sock.bind(ADDRESS) | |
self.sock.listen(1) | |
self.connections = [] | |
def wait_for_connection(self): | |
print('Waiting for a connection...') | |
connection, client_address = self.sock.accept() | |
print('New connection:', client_address) | |
self.connections.append(PreciseConnection(connection, client_address)) | |
Thread(target=self.wait_for_connection, daemon=True).start() | |
def update(self): | |
for i, conn in enumerate(list(self.connections)): | |
if not conn.update(): | |
conn.close() | |
del self.connections[i] | |
def close(self): | |
for i in self.connections: | |
i.close() | |
def main(): | |
manager = ConnectionManager() | |
manager.wait_for_connection() | |
try: | |
while True: | |
manager.update() | |
finally: | |
manager.close() | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mycroft-precise | |
precise-runner | |
pyaudio |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment