From 0329ff7a813f43e57f3cad2b1d76ec1b116249f8 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sat, 26 Mar 2022 03:07:24 -0400 Subject: [PATCH] New attempt --- sound.py | 77 ++++++++++++++++++++++++++------------------------------ 1 file changed, 36 insertions(+), 41 deletions(-) diff --git a/sound.py b/sound.py index d65f134..9bc76c3 100644 --- a/sound.py +++ b/sound.py @@ -12,47 +12,6 @@ sd.default.dtype = "int16" sd.default.latency = "low" sd.default.samplerate = 48000 -class PCMStream: - globals() - - def __init__(self, _device_id, _voice_connection): - self.stream = sd.RawInputStream(device=_device_id) - self.NoiseGate = NoiseGate(_voice_connection, self) - - def change_device(self, _device_id): - self.clean_up() - - self.stream = sd.RawInputStream(device=_device_id) - - async def connect(self): - self.NoiseGate.start() - - # Stops and destroys the current stream - def clean_up(self): - if not self.stream.closed: - self.stream.stop(ignore_errors=True) - self.stream.close(ignore_errors=True) - - # Stops the current running stream but does not destroy it - async def pause(self): - if self.stream.active: - self.stream.stop(ignore_errors=True) - - # Plays the stream - async def play(self): - if not self.stream.active: - self.stream.start() - await self.connect() - - # call back read function for the stream - 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 NoiseGate(threading.Thread): def __init__(self, _voice_connection, _pcmstream_instance): @@ -73,6 +32,42 @@ class NoiseGate(threading.Thread): time.sleep(.1) +class PCMStream(NoiseGate): + globals() + def __init__(self, _device_id, _voice_connection): + super(PCMStream, self).__init__(_pcmstream_instance=self, _voice_connection=_voice_connection) + self.stream = sd.RawInputStream(device=_device_id) + + def change_device(self, _device_id): + self.clean_up() + self.stream = sd.RawInputStream(device=_device_id) + + # Stops and destroys the current stream + def clean_up(self): + if not self.stream.closed: + self.stream.stop(ignore_errors=True) + self.stream.close(ignore_errors=True) + + # Stops the current running stream but does not destroy it + async def pause(self): + if self.stream.active: + self.stream.stop(ignore_errors=True) + + # Plays the stream + async def play(self): + if not self.stream.active: + self.stream.start() + self.start() + + # call back read function for the stream + 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): def __init__(self): self.devices = sd.query_devices()