From 167c06f33101216c24b55de67df7d5a020cb1e94 Mon Sep 17 00:00:00 2001 From: Logan Cusano Date: Sun, 27 Feb 2022 17:21:53 -0500 Subject: [PATCH] Error catching - PCMStream.clean_up - PCMStream.read --- sound.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/sound.py b/sound.py index 0b38cfc..5d428a5 100644 --- a/sound.py +++ b/sound.py @@ -1,3 +1,4 @@ +import sounddevice import sounddevice as sd from pprint import pformat @@ -14,12 +15,15 @@ class PCMStream: self.stream = None def read(self, num_bytes): - # frame is 4 bytes - frames = int(num_bytes / 4) - data = self.stream.read(frames)[0] + try: + # frame is 4 bytes + frames = int(num_bytes / 4) + data = self.stream.read(frames)[0] - # convert to pcm format - return bytes(data) + # convert to pcm format + return bytes(data) + except sounddevice.PortAudioError as err: + print(f"Error in PCMStream.read: '{err}'") def change_device(self, device_ID): self.clean_up() @@ -35,8 +39,8 @@ class PCMStream: self.stream.close() - except Exception as e: - print(f"Error in clean_up: '{e}'") + except sounddevice.PortAudioError as err: + print(f"Error in PCMStream.clean_up: '{err}'")