Created
April 7, 2023 19:54
-
-
Save olgierd/4e66500c937ffc3faa26964610dad9d6 to your computer and use it in GitHub Desktop.
Stream audio from trusdx over serial port
This file contains 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 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