Working on bug in leaving
This commit is contained in:
6
bot.py
6
bot.py
@@ -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))
|
||||
|
||||
32
sound.py
32
sound.py
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user