-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecorder.py
More file actions
95 lines (77 loc) · 3.3 KB
/
recorder.py
File metadata and controls
95 lines (77 loc) · 3.3 KB
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
import json
import wave
import asyncio
import pyaudio
import websockets
from os import path
from datetime import datetime
from threading import Thread
from config import CLOUDFRONT_SERVER, AUDIO_ENDPOINT
class RecordingWorker(Thread):
def __init__(self, filename, caller):
super().__init__()
self.record = False
self.chunk = 1536
self.sample_format = pyaudio.paInt16
self.channels = 1
self.sample_rate = 16000
self.filename = filename
self.caller = caller
# Starlette's websockets don't enable creating connections, so plain websockets is used instead (requires async).
async def stream_audio(self, pyaudio_stream):
async with websockets.connect("wss://" + CLOUDFRONT_SERVER + AUDIO_ENDPOINT) as websocket:
await websocket.send(json.dumps({"ch": self.channels,
"sw": self.caller.pyaudio.get_sample_size(self.sample_format),
"fr": self.sample_rate,
"session": self.filename}))
while self.caller.flag:
await websocket.send(pyaudio_stream.read(self.chunk))
pyaudio_stream.stop_stream()
pyaudio_stream.close()
def run(self):
pyaudio_stream = self.caller.pyaudio.open(format=self.sample_format,
channels=self.channels,
rate=self.sample_rate,
frames_per_buffer=self.chunk,
input=True
)
if self.caller.stream:
# An asynchronous function inside a synchronous function? Madness!
asyncio.run(self.stream_audio(pyaudio_stream))
else:
frames = []
if self.filename is None:
self.filename = datetime.now().strftime("%F-%H-%M-%S-%f")[:-3] + ".wav"
while self.caller.flag:
frames.append(pyaudio_stream.read(self.chunk))
pyaudio_stream.stop_stream()
pyaudio_stream.close()
with wave.open(path.join("data", "recordings", "audio", self.filename), "wb") as wav_file:
wav_file.setnchannels(self.channels)
wav_file.setsampwidth(self.caller.pyaudio.get_sample_size(self.sample_format))
wav_file.setframerate(self.sample_rate)
wav_file.writeframes(b''.join(frames))
class Recorder:
def __init__(self, stream=False):
self.stream = stream
self.worker = None
self.flag = False
self.pyaudio = pyaudio.PyAudio()
def record(self, filename=None):
if self.worker is not None:
print("Already recording!")
return
self.flag = True
self.worker = RecordingWorker(filename, self)
self.worker.start()
print("Recording started.")
def stop_recording(self):
if self.worker is None:
print("No recording in progress, nothing to stop.")
return
self.flag = False
self.worker.join()
filename = self.worker.filename
self.worker = None
print("Recording finished.")
return filename