-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
81 lines (72 loc) · 3.92 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
from flask import Flask, Response,render_template
import pyaudio
def create_app(DEVICEINDEX, CHANNELS, RATE, BITDEPTH, CHUNK):
print("Starting Stream for Device: " + str(DEVICEINDEX) + " with Sample Rate: " + str(RATE) + " and Bit Depth: " + str(BITDEPTH) + " and Chunk Size: " + str(CHUNK) + " and Channels: " + str(CHANNELS))
app = Flask(__name__)
audio1 = pyaudio.PyAudio()
# Get format from bit depth
if BITDEPTH == 16:
FORMAT = pyaudio.paInt16
elif BITDEPTH == 24:
FORMAT = pyaudio.paInt24
elif BITDEPTH == 32:
FORMAT = pyaudio.paInt32
def genHeader(sampleRate, bitsPerSample, channels):
datasize = 2000*10**6
o = bytes("RIFF",'ascii') # (4byte) Marks file as RIFF
o += (datasize + 36).to_bytes(4,'little') # (4byte) File size in bytes excluding this and RIFF marker
o += bytes("WAVE",'ascii') # (4byte) File type
o += bytes("fmt ",'ascii') # (4byte) Format Chunk Marker
o += (16).to_bytes(4,'little') # (4byte) Length of above format data
o += (1).to_bytes(2,'little') # (2byte) Format type (1 - PCM)
o += (channels).to_bytes(2,'little') # (2byte)
o += (sampleRate).to_bytes(4,'little') # (4byte)
o += (sampleRate * channels * bitsPerSample // 8).to_bytes(4,'little') # (4byte)
o += (channels * bitsPerSample // 8).to_bytes(2,'little') # (2byte)
o += (bitsPerSample).to_bytes(2,'little') # (2byte)
o += bytes("data",'ascii') # (4byte) Data Chunk Marker
o += (datasize).to_bytes(4,'little') # (4byte) Data size in bytes
return o
@app.route('/audio')
def audio():
# start Recording
def sound():
wav_header = genHeader(RATE, BITDEPTH, CHANNELS)
stream = audio1.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,input_device_index=DEVICEINDEX,
frames_per_buffer=CHUNK)
#frames = []
first_buffer = True
while True:
if first_buffer:
first_buffer = False
buffer = wav_header + stream.read(CHUNK,exception_on_overflow = False)
else:
buffer = stream.read(CHUNK,exception_on_overflow = False)
yield(buffer)
return Response(sound())
@app.route('/')
def index():
html="""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css">
<title>AudioStream</title>
</head>
<body>
<h1>Click play on the audio object below</h1>
<audio controls preload="none">
<source src="/audio" type="audio/x-wav;codec=pcm">
Your browser does not support the audio element.
</audio>
<br/>
AudioStream is developed and maintained by Michael Hagen
<br />
See the <a href="https://github.com/michaelphagen/AudioStream" target="_blank">GitHub</a> page for more information
</body>
</html>"""
return html
return app