Skip to content

Commit

Permalink
use pactl package
Browse files Browse the repository at this point in the history
  • Loading branch information
incognitojam committed Dec 2, 2022
1 parent 9ebbe2a commit 0c1f3a4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
22 changes: 21 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pillow = "^9.2.0"
poetry = "==1.2.2"
protobuf = "==3.20.1"
psutil = "^5.9.1"
pulsectl = "^22.3.2"
pycapnp = "==1.1.0"
pycryptodome = "^3.15.0"
PyJWT = "^2.5.0"
Expand Down
13 changes: 11 additions & 2 deletions system/hardware/pc/hardware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import random
import subprocess
import time

import pulsectl

from cereal import log
from system.hardware.base import HardwareBase, ThermalConfig
Expand All @@ -9,6 +11,9 @@


class Pc(HardwareBase):
def __init__(self):
self.pulse = pulsectl.Pulse('openpilot')

def get_os_version(self):
return None

Expand All @@ -19,7 +24,11 @@ def get_sound_card_online(self):
return True

def is_sound_playing(self):
return "RUNNING" in subprocess.check_output(["pactl", "list", "short", "sinks"]).decode('utf8')
start_time = time.time()
result = any(s.state == 'RUNNING' for s in self.pulse.sink_input_list())
duration = time.time() - start_time
print(f"is_sound_playing took {duration:.2f}s")
return result

def reboot(self, reason=None):
print("REBOOT!")
Expand Down
11 changes: 10 additions & 1 deletion system/hardware/tici/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from functools import cached_property
from pathlib import Path

import pulsectl

from cereal import log
from common.gpio import gpio_set, gpio_init
from system.hardware.base import HardwareBase, ThermalConfig
Expand Down Expand Up @@ -68,6 +70,9 @@ def affine_irq(val, irq):


class Tici(HardwareBase):
def __init__(self):
self.pulse = pulsectl.Pulse('openpilot')

@cached_property
def bus(self):
import dbus # pylint: disable=import-error
Expand Down Expand Up @@ -97,7 +102,11 @@ def get_sound_card_online(self):
open('/proc/asound/card0/state').read().strip() == 'ONLINE')

def is_sound_playing(self):
return "RUNNING" in subprocess.check_output(["pactl", "list", "short", "sinks"]).decode('utf8')
start_time = time.time()
result = any(s.state == 'RUNNING' for s in self.pulse.sink_input_list())
duration = time.time() - start_time
print(f"is_sound_playing took {duration:.2f}s")
return result

def reboot(self, reason=None):
subprocess.check_output(["sudo", "reboot"])
Expand Down
6 changes: 3 additions & 3 deletions system/micd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from cereal import messaging
from common.filter_simple import FirstOrderFilter
from common.realtime import Ratekeeper
from system.hardware import HARDWARE
from system.swaglog import cloudlog

RATE = 10
Expand Down Expand Up @@ -60,9 +61,8 @@ def update(self):
sound_pressure, _ = calculate_spl(self.measurements)
measurements_weighted = apply_a_weighting(self.measurements)
sound_pressure_weighted, sound_pressure_level_weighted = calculate_spl(measurements_weighted)
# FIXME: HARDWARE.is_sound_playing uses 25%+ CPU onroad
# if not HARDWARE.is_sound_playing():
self.spl_filter_weighted.update(sound_pressure_level_weighted)
if not HARDWARE.is_sound_playing():
self.spl_filter_weighted.update(sound_pressure_level_weighted)
else:
sound_pressure = 0
sound_pressure_weighted = 0
Expand Down

0 comments on commit 0c1f3a4

Please sign in to comment.