From 50f594f7a3bab005023482bc793147a8c8dae5d7 Mon Sep 17 00:00:00 2001 From: Cameron Clough Date: Tue, 8 Nov 2022 16:12:36 -0800 Subject: [PATCH] hardware: get sound input device --- system/hardware/base.py | 4 ++++ system/hardware/pc/hardware.py | 3 +++ system/hardware/tici/hardware.py | 3 +++ system/micd.py | 8 ++++++-- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/system/hardware/base.py b/system/hardware/base.py index 31df1babe0f21b..1ff10c609fc1bb 100644 --- a/system/hardware/base.py +++ b/system/hardware/base.py @@ -43,6 +43,10 @@ def get_device_type(self): def get_sound_card_online(self): pass + @abstractmethod + def get_sound_input_device(self): + pass + @abstractmethod def get_imei(self, slot) -> str: pass diff --git a/system/hardware/pc/hardware.py b/system/hardware/pc/hardware.py index 564f9e483add9d..4cb514f87d5213 100644 --- a/system/hardware/pc/hardware.py +++ b/system/hardware/pc/hardware.py @@ -17,6 +17,9 @@ def get_device_type(self): def get_sound_card_online(self): return True + def get_sound_input_device(self): + return None + def reboot(self, reason=None): print("REBOOT!") diff --git a/system/hardware/tici/hardware.py b/system/hardware/tici/hardware.py index b5f5e00410f863..9872b68a6c04c8 100644 --- a/system/hardware/tici/hardware.py +++ b/system/hardware/tici/hardware.py @@ -96,6 +96,9 @@ def get_sound_card_online(self): return (os.path.isfile('/proc/asound/card0/state') and open('/proc/asound/card0/state').read().strip() == 'ONLINE') + def get_sound_input_device(self): + return "(hw:0,0)" + def reboot(self, reason=None): subprocess.check_output(["sudo", "reboot"]) diff --git a/system/micd.py b/system/micd.py index 0b2f5b07d1f053..519634a03c4caa 100755 --- a/system/micd.py +++ b/system/micd.py @@ -4,6 +4,7 @@ from cereal import messaging from common.realtime import Ratekeeper +from system.hardware import HARDWARE class Mic: @@ -23,8 +24,11 @@ def update(self): def calculate_volume(self, indata, frames, time, status): self.noise_level = np.linalg.norm(indata) - def micd_thread(self): - with sd.InputStream(callback=self.calculate_volume): + def micd_thread(self, device=None): + if device is None: + device = HARDWARE.get_sound_input_device() + + with sd.InputStream(callback=self.calculate_volume, device=device): while True: self.update()