I'm making a Discord bot, and I need to record audio from a voice channel and produce one single wave audio file, where all the audio is playing at the same time, like the Discord client hears when others are in the channel. I'm using the Pycord library, and all the audio files are sent as separate users. The closest I've got to the result that I want is the code below, which just overlays one onto another, so you can only really hear one audio stream at a time. I want to be able to hear every audio stream at once (This code was generated with the help of ChatGPT, by the way).
async def after_recording(self, sink: discord.sinks.WaveSink, channel: discord.VoiceChannel):
audio_segments = []
for audio_data in sink.audio_data.values():
audio_data.file.seek(0)
audio_segments.append(AudioSegment.from_file(audio_data.file))
# Determine the maximum duration of the audio files
max_duration = max([len(segment) for segment in audio_segments])
# Pad the shorter audio files with silence
for i, segment in enumerate(audio_segments):
if len(segment) < max_duration:
audio_segments[i] = segment + AudioSegment.silent(duration=max_duration - len(segment))
# Overlay the audio files
overlaid_segment = audio_segments[0]
for segment in audio_segments[1:]:
overlaid_segment = overlaid_segment.overlay(segment)
# Export the overlaid audio to a BytesIO object
overlaid_bytes = BytesIO()
overlaid_segment.export(overlaid_bytes, format='wav')
overlaid_bytes.seek(0)
# Send the overlaid audio as a message attachment to the specified channel
await channel.send(file=discord.File(overlaid_bytes, filename='overlaid_audio.wav'))
# Cleanup
for audio_data in sink.audio_data.values():
audio_data.file.close()
NOTE: While this code was generated with ChatGPT, I looked it over and made edits multiple times. Answers such as this were the only similar answers I could find anywhere as well, it's not generated by it alone.