Created
May 29, 2025 14:43
Revisions
-
Maxime-Favier created this gist
May 29, 2025 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ 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()