Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help Needed to output to speakers #20

Open
cunhar opened this issue Aug 13, 2024 · 1 comment
Open

Help Needed to output to speakers #20

cunhar opened this issue Aug 13, 2024 · 1 comment

Comments

@cunhar
Copy link

cunhar commented Aug 13, 2024

Im trying to output to defauld audio device the things being said in the discord channel. Could you please help me:

What i have to far just outputs ticking to the speakers:

import discord
from discord.ext import commands, voice_recv
import random
import pyaudio


description = '''An example bot to showcase the discord.ext.commands extension
module.

There are a number of utility commands being showcased here.'''

intents = discord.Intents.default()
intents.members = True
intents.message_content = True

bot = commands.Bot(command_prefix='?', description=description, intents=intents)
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32,
                channels=1,
                rate=44100,
                output=True)


@bot.event
async def on_ready():
    print(f'Logged in as {bot.user} (ID: {bot.user.id})')
    print('------')


@bot.command()
async def add(ctx, left: int, right: int):
    """Adds two numbers together."""
    await ctx.send(left + right)


@bot.command()
async def test(ctx):
    def callback(user, data: voice_recv.VoiceData):
        print(f"Got packet from {user}")
        ## voice power level, how loud the user is speaking
        ext_data = data.packet.extension_data.get(voice_recv.ExtensionID.audio_power)
        value = int.from_bytes(ext_data, 'big')
        power = 127-(value & 127)
        print('#' * int(power * (79/128)))

        stream.write(data.packet.decrypted_data)
        ## instead of 79 you can use shutil.get_terminal_size().columns-1

    vc = await ctx.author.voice.channel.connect(cls=voice_recv.VoiceRecvClient)
    vc.listen(voice_recv.BasicSink(callback))

@bot.command()
async def stop(ctx):
    await ctx.voice_client.disconnect()

@bot.command()
async def die(ctx):
    ctx.voice_client.stop()
    await ctx.bot.close()


bot.run("xxxx")
@cunhar
Copy link
Author

cunhar commented Aug 13, 2024

everything works im just not sure how to convert the packets into the audio stream

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant