Working on bug in leaving

This commit is contained in:
Logan Cusano
2022-02-27 18:36:18 -05:00
parent 17c528c153
commit 7fa11a4abe
2 changed files with 17 additions and 21 deletions

6
bot.py
View File

@@ -91,9 +91,9 @@ class Bot(commands.Bot):
voice_connection = await channel.connect()
# Create an audio stream from selected device
self.streamHandler = sound.PCMStream()
# Ensure the selected device is available and start the audio stream
self.streamHandler.change_device(self.DEVICE_ID)
self.streamHandler = sound.PCMStream(self.DEVICE_ID)
# Start the audio stream
self.streamHandler.stream.start()
# Play the stream
voice_connection.play(discord.PCMAudio(self.streamHandler))

View File

@@ -13,31 +13,27 @@ sd.default.samplerate = 48000
class PCMStream:
globals()
def __init__(self):
self.stream = None
def __init__(self, _device_id):
self.stream = sd.RawInputStream(device=_device_id)
def read(self, num_bytes):
# frame is 4 bytes
frames = int(num_bytes / 4)
data = self.stream.read(frames)[0]
# convert to pcm format
return bytes(data)
def change_device(self, device_ID):
def change_device(self, _device_id):
self.clean_up()
self.stream = sd.RawInputStream(device=device_ID)
self.stream.start()
self.stream = sd.RawInputStream(device=_device_id)
def clean_up(self):
if self.stream is not None:
self.stream.stop()
time.sleep(.1)
self.stream.close()
if not self.stream.closed:
self.stream.stop(ignore_errors=True)
self.stream.close(ignore_errors=True)
def read(self, num_bytes):
if self.stream.active:
# frame is 4 bytes
frames = int(num_bytes / 4)
data = self.stream.read(frames)[0]
# convert to pcm format
return bytes(data)
class DeviceNotFoundError(Exception):