-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecorder.py
117 lines (96 loc) · 3.86 KB
/
recorder.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from __future__ import annotations
import asyncio
import logging
import time
from select import select
from socket import socket
import nextcord
import decrypter
from typing import Final
import speech_recognition as sr
import array
import audioop
__all__ = [
'VoiceRecvClient',
]
log = logging.getLogger(__name__)
# this file takes HEAVY inspiration from both https://github.com/imayhaveborkedit/discord-ext-voice-recv/ and https://github.com/nextcord/nextcord/pull/1113
class VoiceRecvClient(nextcord.VoiceClient):
def __init__(self, client: nextcord.Client, channel: nextcord.abc.Connectable):
super().__init__(client, channel)
async def connect(self, *, reconnect: bool, timeout: float) -> None:
await super().connect(reconnect=reconnect, timeout=timeout)
async def listen(self):
"""Just yields received data. It's your choice what to do after that :). Doesn't stop until the client disconnects."""
await self.ws.loop.sock_connect(self.socket, (self.endpoint_ip, self.voice_port))
connect_time = time.time() # Sometimes likes to stop recieving data? so disconnect and reconnect every once in a while.
error_count = 0
while self.is_connected() and error_count < 10:
# idk doesn't seem to work yet
# if connect_time < time.time() - 60: # reconnect every 1 minute
# connect_time = time.time()
# self.socket.shutdown()
# self.socket.close()
# await self.ws.loop.sock_connect(self.socket, (self.endpoint_ip, self.voice_port))
try:
ready, _, _ = select([self.socket], [], [], 30)
except Exception as e:
print(repr(e))
continue
while not ready:
print("Unable to read")
time.sleep(1.0)
error_count += 1
try:
data = self.socket.recv(4096) # lower means faster latency? probably
except Exception as e:
print("Exception while receiving data:")
print(repr(e))
error_count += 1
else:
yield data
def decrypt(self, header, data):
return getattr(decrypter, f"decrypt_{self.mode}")(self.secret_key, header, data)
def _wait_for_user_id(self, ssrc: int) -> int:
ssrc_cache = self.ws.ssrc_cache
while not (user_data := ssrc_cache.get(ssrc)):
time.sleep(0.01)
return user_data["user_id"]
# class entirely from https://github.com/imayhaveborkedit/discord-ext-voice-recv/blob/main/discord/ext/voice_recv/extras/speechrecognition.py
class BytesSRAudioSource(sr.AudioSource):
little_endian: Final[bool] = True
SAMPLE_RATE: Final[int] = 48_000
SAMPLE_WIDTH: Final[int] = 2
CHANNELS: Final[int] = 2
CHUNK: Final[int] = 960
def __init__(self, buffer: array.array[int]):
self.buffer = buffer
self._entered: bool = False
@property
def stream(self):
return self
def __enter__(self):
if self._entered:
log.warning('Already entered sr audio source')
self._entered = True
return self
def __exit__(self, *exc) -> None:
self._entered = False
if any(exc):
log.exception('Error closing sr audio source')
def read(self, size: int) -> bytes:
for _ in range(10):
if len(self.buffer) < size * self.CHANNELS:
time.sleep(0.1)
else:
break
else:
if len(self.buffer) == 0:
return b''
chunksize = size * self.CHANNELS
audiochunk = self.buffer[:chunksize].tobytes()
del self.buffer[: min(chunksize, len(audiochunk))]
audiochunk = audioop.tomono(audiochunk, 2, 1, 1)
return audiochunk
def close(self) -> None:
self.buffer.clear()