-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
100 lines (98 loc) · 2.95 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import socket,threading
import pyaudio
import time,os,sys
from multiprocessing import Process
class server:
host = '127.0.0.1' #IP地址,默认本地ip
port=9092
p = pyaudio.PyAudio()
CHUNK = 1024 #1024B 1KB缓冲区帧数
FORMAT = pyaudio.paInt16 #大小格式
CHANNELS = 2 #声道为双声道
RATE = 44100 #录音的采样率为44.1KHz
RECORD_SECONDS = 5 #采样的时间
count=0
def acceptaudio(self):
#接受来自服务器端的音频
global count
socketserver = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
#绑定地址(包括ip地址和端口号)
socketserver.bind((server.host,server.port))
p=pyaudio.PyAudio()
#打开数据流
stream = p.open(format=server.FORMAT,
channels=server.CHANNELS,
rate=server.RATE,
output=True,
frames_per_buffer=server.CHUNK)
accepteddata,addr=socketserver.recvfrom(1024)
socketserver.sendto('ok'.encode(),addr)
print("正在请求语音匹配客户端的ip地址为:"+accepteddata.decode())
while True:
accepteddata,addr=socketserver.recvfrom(1024)
#播放
stream.write(accepteddata)
#接收成功,返回客户端信息'ok'
socketserver.sendto('ok'.encode(),addr)
#print('已接受'+str(server.count))
server.count+=1
#socketserver.close()
def record(self):
p = pyaudio.PyAudio()
stream = p.open(format=server.FORMAT,
channels=server.CHANNELS,
rate=server.RATE,
input=True,
frames_per_buffer=server.CHUNK)
print("* recording")
frames = []
for i in range(0, int(server.RATE / server.CHUNK * server.RECORD_SECONDS)):
data = stream.read(CHUNK) #将1KB的数据读出赋给data
frames.append(data) #将数据不断添加到数据组末尾
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
data=b''.join(frames)
return data
def playback(self):
"声音回放"
CHUNK = 1024
WIDTH = 2
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(WIDTH),
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK)
print("* recording")
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
stream.write(data, CHUNK)
print("* done")
stream.stop_stream()
stream.close()
p.terminate()
def server(self):
global count
accept_thred=threading.Thread(target=self.acceptaudio)
accept_thred.start()
print('当前count的值为:',server.count)
def getclient():
#获取计算机名称
hostname=socket.gethostname()
#获取本机IP
ip=socket.gethostbyname(hostname)
print(ip)
def main(self):
p=Process(target=self.server,name='server')
p.start()
print('开启进程成功!')
p.join()
if __name__ == '__main__':
s=server()
s.main()