代码之家  ›  专栏  ›  技术社区  ›  yesko

当我试图把我的声音放在我的机器人不和谐上时,我听到了不必要的噪音

  •  0
  • yesko  · 技术社区  · 6 月前

    我试图把我的声音放在我的机器人不和谐上,我成功了,但非常不确定,我确信它只适用于我的麦克风(我不希望它适用于任何麦克风)。

    我所说的非常不确定,是指我的声音是可以听到的,但有很多不必要的噪音

    我试图修改一些变量,比如:

    块、格式、通道、速率

    并且通过参数获得了最具结论性的结果:

    CHUNK = 1024
    FORMAT = pyaudio.paInt32
    CHANNELS = 1
    RATE = 44800 
    

    我也更喜欢不使用ffmpeg(但是,如果ffmpeg是强制性的,那也没问题)

    这是我的代码:

    
    import discord
    from discord.ext import commands
    import pyaudio
    
    CHUNK = 1024
    FORMAT = pyaudio.paInt32
    CHANNELS = 1
    RATE = 44800
    
    p = pyaudio.PyAudio()
    
    class MicrophoneAudioSource(discord.AudioSource):
        def __init__(self):
            self.stream = p.open(format=FORMAT,
                                 channels=CHANNELS,
                                 rate=RATE,
                                 input=True,
                                 frames_per_buffer=CHUNK)
    
        def read(self):
            data = self.stream.read(CHUNK)
            return data
    
        def is_opus(self):
            return False
    
    intents = discord.Intents.default()
    intents.message_content = True
    intents.voice_states = True
    intents.guilds = True
    
    bot = commands.Bot(command_prefix="!", intents=intents)
    
    @bot.event
    async def on_ready():
        await bot.tree.sync()
        print(f'{bot.user}')
    
    @bot.tree.command(name="join", description="Fait rejoindre le bot au canal vocal de l'utilisateur.")
    @commands.has_permissions(administrator=True)
    async def join(interaction: discord.Interaction):
        if interaction.user.voice:
            channel = interaction.user.voice.channel
            voice_client = await channel.connect()
            await interaction.response.send_message(f'Le bot a rejoint le canal vocal : {channel}', ephemeral=True)
            audio_source = MicrophoneAudioSource()
            voice_client.play(audio_source, after=lambda e: print(f'Erreur audio : {e}') if e else None)
        else:
            await interaction.response.send_message("Vous n'êtes pas connecté à un canal vocal.", ephemeral=True)
    
    @bot.tree.command(name="leave", description="Fait quitter le bot du canal vocal.")
    @commands.has_permissions(administrator=True)
    async def leave(interaction: discord.Interaction):
        if interaction.guild.voice_client:
            await interaction.guild.voice_client.disconnect()
            await interaction.response.send_message("Le bot a quitté le canal vocal.", ephemeral=True)
        else:
            await interaction.response.send_message("Le bot n'est pas connecté à un canal vocal.", ephemeral=True)
    
    
    bot.run('TOKEN')
    
    0 回复  |  直到 6 月前