-
Notifications
You must be signed in to change notification settings - Fork 1
/
server3.py
91 lines (68 loc) · 2.52 KB
/
server3.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
from http.server import HTTPServer, BaseHTTPRequestHandler
import time
import numpy as np
import pyaudio
import wave
import io
import json
import base64
import zlib
HOST_NAME='172.31.59.128'
PORT_NUMBER = 8895
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_HEAD(s):
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
def do_GET(self):
# self.send_response(200)
# self.end_headers()
# self.wfile.write(b'Hello, from the server 8895!')
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
# Start Recording
RESPEAKER_WIDTH = 2
RESPEAKER_INDEX = 0
RESPEAKER_CHANNELS = 6
RESPEAKER_RATE = 16000
CHUNK = 1024
RECORD_SECONDS = 5
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(RESPEAKER_WIDTH),
channels=RESPEAKER_CHANNELS,
rate=RESPEAKER_RATE,
input=True,
input_device_index=RESPEAKER_INDEX,
frames_per_buffer=CHUNK)
print ("recording")
frames = []
for i in range(0, int(RESPEAKER_RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
data_array = np.fromstring(data, dtype=np.int16)
# channel11 = data_array[0::RESPEAKER_CHANNELS]
# channel12 = data_array[1::RESPEAKER_CHANNELS]
# channel13 = data_array[2::RESPEAKER_CHANNELS]
# channel14 = data_array[3::RESPEAKER_CHANNELS]
# channel15 = data_array[4::RESPEAKER_CHANNELS]
# channel16 = data_array[5::RESPEAKER_CHANNELS]
# data = channel10.tostring()
frames.append(data)
print ("done recording")
print(data_array.shape)
stream.stop_stream()
stream.close()
p.terminate()
# Compressing using zlib and base64 (might be slower for big array's)
data = zlib.compress(data_array)
data = base64.b64encode(data)
self.wfile.write(data)
# print(data_array.shape)
httpd = HTTPServer((HOST_NAME, PORT_NUMBER), SimpleHTTPRequestHandler)
print(time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER))
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print(time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER))