server_microphone.py hinzugefügt
This commit is contained in:
89
server_microphone.py
Normal file
89
server_microphone.py
Normal file
@@ -0,0 +1,89 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import asyncio
|
||||
import websockets
|
||||
import logging
|
||||
import sounddevice as sd
|
||||
import argparse
|
||||
|
||||
# meine imports
|
||||
import time
|
||||
from ledpixels import Pixels
|
||||
pixels = Pixels()
|
||||
import avm_sid
|
||||
|
||||
def int_or_str(text):
|
||||
"""Helper function for argument parsing."""
|
||||
try:
|
||||
return int(text)
|
||||
except ValueError:
|
||||
return text
|
||||
|
||||
def callback(indata, frames, time, status):
|
||||
"""This is called (from a separate thread) for each audio block."""
|
||||
loop.call_soon_threadsafe(audio_queue.put_nowait, bytes(indata))
|
||||
|
||||
async def run_test():
|
||||
|
||||
with sd.RawInputStream(samplerate=args.samplerate, blocksize = 4000, device=args.device, dtype='int16',
|
||||
channels=1, callback=callback) as device:
|
||||
|
||||
async with websockets.connect(args.uri) as websocket:
|
||||
await websocket.send('{ "config" : { "sample_rate" : %d } }' % (device.samplerate))
|
||||
|
||||
while True:
|
||||
data = await audio_queue.get()
|
||||
await websocket.send(data)
|
||||
finalResult = await websocket.recv()
|
||||
print(finalResult)
|
||||
if "licht" in finalResult and "an" in finalResult and "hex" in finalResult and "result" in finalResult:
|
||||
print("SCHALTER EIN!")
|
||||
pixels.wakeup()
|
||||
time.sleep(1)
|
||||
pixels.off()
|
||||
avm_sid.lights_on()
|
||||
if "licht" in finalResult and "aus" in finalResult and "hex" in finalResult and "result" in finalResult:
|
||||
print("SCHALTER AUS!")
|
||||
pixels.wakeup()
|
||||
time.sleep(1)
|
||||
pixels.off()
|
||||
avm_sid.lights_off()
|
||||
if "blinken" in finalResult and "hex" in finalResult and "result" in finalResult:
|
||||
print("Blinken!")
|
||||
pixels.wakeup()
|
||||
time.sleep(2)
|
||||
pixels.off()
|
||||
|
||||
async def main():
|
||||
|
||||
global args
|
||||
global loop
|
||||
global audio_queue
|
||||
|
||||
parser = argparse.ArgumentParser(add_help=False)
|
||||
parser.add_argument('-l', '--list-devices', action='store_true',
|
||||
help='show list of audio devices and exit')
|
||||
args, remaining = parser.parse_known_args()
|
||||
if args.list_devices:
|
||||
print(sd.query_devices())
|
||||
parser.exit(0)
|
||||
parser = argparse.ArgumentParser(description="ASR Server",
|
||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||
parents=[parser])
|
||||
parser.add_argument('-u', '--uri', type=str, metavar='URL',
|
||||
help='Server URL', default='ws://IP-DES-VOSK-SERVERS:2700')
|
||||
parser.add_argument('-d', '--device', type=int_or_str,
|
||||
help='input device (numeric ID or substring)')
|
||||
parser.add_argument('-r', '--samplerate', type=int, help='sampling rate', default=16000)
|
||||
args = parser.parse_args(remaining)
|
||||
loop = asyncio.get_running_loop()
|
||||
audio_queue = asyncio.Queue()
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
await run_test()
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user