Created
May 29, 2025 14:43
-
-
Save Maxime-Favier/88a6f73b38783229e47a2127ed5240d2 to your computer and use it in GitHub Desktop.
spectrumLab python trying udp streaming
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
import socket, time, math, struct | |
import numpy as np | |
import serial | |
UDP_IP = "127.0.0.1" | |
UDP_PORT = 6999 | |
SAMPLE_RATE = 48000 | |
PACKET_SIZE = 10000 | |
PACKET_DURATION = PACKET_SIZE/SAMPLE_RATE | |
print("Packet duration=", PACKET_DURATION) | |
SINE_FREQUENCY = 1000 | |
AMPLITUDE = 0.5 | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.bind(("0.0.0.0", UDP_PORT+1)) | |
#ser = serial.Serial('COM19', 921600, timeout=0) | |
current_phase = 0 | |
print("starting udp stream") | |
try: | |
while True: | |
t = np.linspace(current_phase, current_phase+PACKET_DURATION, PACKET_SIZE, endpoint=None) | |
#print(t) | |
sample_float = AMPLITUDE * np.sin(2*math.pi*SINE_FREQUENCY*t) | |
current_phase += PACKET_DURATION | |
sample_int16 = np.int16(sample_float*32767) | |
packed_data = struct.pack("<"+str(PACKET_SIZE)+"h",*sample_int16) | |
#print(packed_data) | |
#print(len(packed_data)) | |
sock.sendto(packed_data,(UDP_IP, UDP_PORT)) | |
#ser.write(packed_data) | |
time_to_sleep = PACKET_DURATION | |
if time_to_sleep > 0: | |
#print(time_to_sleep) | |
time.sleep(0.2) | |
pass | |
except KeyboardInterrupt: | |
print("stopping") | |
pass | |
finally: | |
sock.close() | |
#ser.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
works with
ffplay.exe -stats -ar 48000 -analyzeduration 0 -probesize 32 -f s16le -i udp://127.0.0.1:6999