Skip to content

Commit

Permalink
hardware: get sound input device
Browse files Browse the repository at this point in the history
  • Loading branch information
incognitojam committed Nov 28, 2022
1 parent 9301d6a commit 50f594f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions system/hardware/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions system/hardware/pc/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!")

Expand Down
3 changes: 3 additions & 0 deletions system/hardware/tici/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down
8 changes: 6 additions & 2 deletions system/micd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from cereal import messaging
from common.realtime import Ratekeeper
from system.hardware import HARDWARE


class Mic:
Expand All @@ -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()

Expand Down

0 comments on commit 50f594f

Please sign in to comment.