New attempt
This commit is contained in:
77
sound.py
77
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()
|
||||
|
||||
Reference in New Issue
Block a user