Skip to content

Instantly share code, notes, and snippets.

@olgierd
Created April 7, 2023 19:54
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 olgierd/4e66500c937ffc3faa26964610dad9d6 to your computer and use it in GitHub Desktop.
Save olgierd/4e66500c937ffc3faa26964610dad9d6 to your computer and use it in GitHub Desktop.
Stream audio from trusdx over serial port
#!/usr/bin/env python3
import serial
import time
import pyaudio
import threading
import time
ser = serial.Serial("/dev/ttyUSB0", 115200)
stream = pyaudio.PyAudio().open(format = pyaudio.paInt8, channels = 1, rate = 7820, output = True)
time.sleep(5)
ser.write(b"UA1;")
buf = []
def serial_writer(serport):
while True:
d = serport.read(500)
buf.append(d)
def audio_reader(pastream):
while True:
if len(buf) < 5:
print("UNDERRUN - refilling")
while len(buf) < 10:
time.sleep(0.01)
pastream.write(buf[0])
buf.remove(buf[0])
threading.Thread(target=serial_writer, args=(ser,)).start()
threading.Thread(target=audio_reader, args=(stream,)).start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment