def send_voice(self, wav, target, terminated):
packet = bytearray()
packet.append(self.outgoing_type.value << 5 | target.value)
packet.extend(self._encode_varint(self.outgoing_sequence_number))
nchannels, sampwidth, framerate, n, _, _ = wav.getparams()
logger.debug('Sending audio: %d channels, %d-bit, %dhz, %d frames',
nchannels, sampwidth * 8, framerate, n)
for i in range(n):
pcm = wav.readframes(1)
if sampwidth != 2:
pcm = audioop.lin2lin(pcm, sampwidth, 2)
if nchannels == 2:
pcm = audioop.tomono(pcm, 2, 0.5, 0.5)
if framerate != 48000:
pcm, _ = audioop.ratecv(pcm, 2, 1, framerate, 48000, None)
frame = self.outgoing_codec.encoder.encode(pcm)
if self.outgoing_type == self.PacketType.VOICE_OPUS:
#packet.extend(self._make_opus_header(frame))
# TODO: figure out opus
pass
else:
packet.extend(self._make_celt_header(frame, True))
if i == n - 1 and terminated:
packet.extend(self._make_celt_header(b'', False))
print(packet)
self.transport.sendto(bytes(packet))
self.outgoing_sequence_number += 1
评论列表
文章目录