我正在尝试制作一个 Discord 机器人,将声音从一个通道转发到另一个通道(征得被录制者的同意)。
我遇到的问题是,当多人讲话时,音频在他们之间快速切换,这使得声音断断续续且难以理解。
免责声明:我从未真正使用过音频,所以如果这是一个愚蠢的问题,但答案很明显,我深表歉意。
import discord
import pyaudio
from discord.ext import commands, voice_recv
class MyAudioSource(discord.PCMAudio):
def __init__(self, stream):
self.stream = stream
def read(self):
return self.stream.read(CHUNK)
def is_opus(self):
return False
class MyAudioSink(voice_recv.AudioSink):
def __init__(self, stream):
self.stream = stream
def write(self, user, data):
self.stream.write(data.pcm)
def wants_opus(self):
return False
def cleanup(self):
return
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 48000
CHUNK = (RATE // 1000) * 20
audio = pyaudio.PyAudio()
loopback_stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
output=True,
input_device_index=2, # VAC's that lead into eachother for playback
output_device_index=6, # VAC's that lead into eachother for playback
frames_per_buffer=CHUNK)
intents = discord.Intents().all()
bot = commands.Bot(command_prefix='.', intents=intents)
@bot.event
async def on_ready():
print("Bot is ready")
target_channel = bot.get_channel(1262072403698257973)
target_client = await target_channel.connect(cls=voice_recv.VoiceRecvClient)
#secret_channel = bot.get_channel(1262069846259142701)
#secret_client = await secret_channel.connect()
my_audio_sink = MyAudioSink(loopback_stream)
target_client.listen(my_audio_sink)
my_audio_source = MyAudioSource(loopback_stream)
target_client.play(my_audio_source)
guild = discord.Object(id=1262072402997678140)
bot.tree.copy_global_to(guild=guild)
await bot.tree.sync(guild=guild)
bot.run("TOKEN")
编辑: 这是我正在使用的存储库: discord-ext-voice-recv
如果我没记错的话,有一个选项可以只监听一个用户,因为接收器有一个参数用户:
def write(self, user: Optional[User], data: VoiceData) -> None: self.cb(user, data)
,您也许可以列出已连接的用户并为每个用户创建一个监听接收器