添加到远程git

This commit is contained in:
zhangsheng 2025-05-27 09:57:59 +08:00
parent 9b5792329d
commit b7908f278d
2 changed files with 2 additions and 56 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
build/ build/
.idea/ .idea/
dist/ dist/
*.pyc
mainwindow - 副本.py

View File

@ -1,56 +0,0 @@
import socket
import numpy as np
import time
# 设置服务端IP和端口
HOST = '0.0.0.0' # 监听所有网络接口
PORT = 12345 # 端口号
def generate_data(sample_rate=96000, duration=1):
"""生成模拟的采样数据"""
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
# 使用正弦波生成模拟数据,可以替换为其他类型的数据
data = np.sin(2 * np.pi * 1000 * t) # 1kHz 正弦波
return data.astype(np.float32)
def start_server():
"""启动socket服务端"""
# 创建socket对象
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOST, PORT))
server_socket.listen(1)
print(f"服务器启动,等待客户端连接...")
# 等待客户端连接
client_socket, client_address = server_socket.accept()
print(f"客户端 {client_address} 已连接")
try:
while True:
# 生成96k采样的数据
data = generate_data(sample_rate=96000, duration=1) # 生成 1 秒钟的采样数据
# 将数据转换为字节流
data_bytes = data.tobytes()
# 发送数据
client_socket.sendall(data_bytes)
print("发送数据给客户端...")
# 等待 1 秒后再发送下一组数据
time.sleep(1)
except Exception as e:
print(f"发生错误: {e}")
finally:
# 关闭连接
client_socket.close()
server_socket.close()
print("连接已关闭")
if __name__ == '__main__':
start_server()