Skip to content

Instantly share code, notes, and snippets.

@Santhoshkumard11
Created March 13, 2022 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Santhoshkumard11/65821c11e8d5c5719c0d8f66271e3ba0 to your computer and use it in GitHub Desktop.
Save Santhoshkumard11/65821c11e8d5c5719c0d8f66271e3ba0 to your computer and use it in GitHub Desktop.
import base64
import json
import websockets
import asyncio
import os
import pyaudio
import sys
import logging
FRAMES_PER_BUFFER = 3200
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
audio_client = pyaudio.PyAudio()
stream_in = audio_client.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
input_device_index=1,
frames_per_buffer=FRAMES_PER_BUFFER,
)
print("stream started")
loghandler = logging.StreamHandler(sys.stdout)
logging.basicConfig(level=logging.DEBUG, handlers=[loghandler])
def connect():
"""Connect to the websocket endpoint."""
return websockets.connect(
## Connect to the secure websocket endpoint.
"wss://api.deepgram.com/v1/listen",
extra_headers={
"Authorization": "Token {}".format(os.getenv("DEEPGRAM_API_KEY"))
},
ping_interval=5
)
async def run():
"""Streams data to/from the service.
We will use `key` to authenticate.
We will read audio data from the `outbox` queue and send it to the server.
When we receive responses from the server, we will put the latest transcript in the `inbox` queue.
"""
async with connect() as ws:
print("Websocket connection initialised")
async def sender(ws):
while True:
try:
input_audio = stream_in.read(3200)
# input_audio = base64.b64encode(input_audio).decode("utf-8")
await ws.send(input_audio)
await asyncio.sleep(0)
except Exception as e:
print(e)
except websockets.ConnectionClosed:
print("connection closed")
await ws.send(b'')
async def receiver(ws):
async for msg in ws:
# while True:
print("receiving something..")
# msg = await ws.recv()
print(msg)
msg = json.loads(msg)
if "alternatives" in msg:
transcript = msg["alternatives"][0]["transcript"]
print("Metadata received:", msg)
print(f"transcript from alternatives - {transcript}")
elif "channel" in msg and msg["is_final"]:
transcript = msg["channel"]["alternatives"][0]["transcript"]
print("Metadata received:", msg)
print(f"transcript channel - {transcript}")
else:
print("Metadata received:", msg)
# send_result, receive_result = await asyncio.gather(sender(), receiver())
await asyncio.wait(
[asyncio.ensure_future(sender(ws)), asyncio.ensure_future(receiver(ws))]
)
if __name__ == "__main__":
asyncio.run(run())
asyncio.get_event_loop().run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment