From 0c1f3a4b865e44052affa57323ae4a21d274d6e3 Mon Sep 17 00:00:00 2001 From: Cameron Clough Date: Fri, 2 Dec 2022 14:12:56 -0800 Subject: [PATCH] use pactl package --- poetry.lock | 22 +++++++++++++++++++++- pyproject.toml | 1 + system/hardware/pc/hardware.py | 13 +++++++++++-- system/hardware/tici/hardware.py | 11 ++++++++++- system/micd.py | 6 +++--- 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/poetry.lock b/poetry.lock index fbe0009a26eb86..2557b6d84dbf45 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2844,6 +2844,14 @@ category = "main" optional = false python-versions = "*" +[[package]] +name = "pulsectl" +version = "22.3.2" +description = "Python high-level interface and ctypes-based bindings for PulseAudio (libpulse)" +category = "main" +optional = false +python-versions = "*" + [[package]] name = "pure-eval" version = "0.2.2" @@ -4400,7 +4408,7 @@ testing = ["coverage (>=5.0.3)", "zope.event", "zope.testing"] [metadata] lock-version = "1.1" python-versions = "~3.8" -content-hash = "a41b1d669ec4f0a94abe12af8a9a7d709b2bb56e9c83aa87801cdf5d9af074f8" +content-hash = "8fe695f1f439cda08346e33477f73eb9dd4ef86104340a02eda22e7887db7ec6" [metadata.files] adal = [ @@ -6576,6 +6584,11 @@ pillow-avif-plugin = [ {file = "pillow_avif_plugin-1.2.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:017e5e52cb4320414e8ce3e2089eae2cb87c22c73ff6012b17ae326fc5753b20"}, {file = "pillow_avif_plugin-1.2.2-cp310-cp310-musllinux_1_1_x86_64.whl", hash = "sha256:2a57136d4866de5dc80cfb24d66655955fbdd87acf1d11d88c8dc2ab41023e46"}, {file = "pillow_avif_plugin-1.2.2-cp310-cp310-win_amd64.whl", hash = "sha256:f339511d0fccb69e3a5e3af39f8fe6700b0a07279015006ea56f8f49e7fecff4"}, + {file = "pillow_avif_plugin-1.2.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:05e821ecd90bb0b8d2dc7610625372cc47de9cb893d09662528bad572f669d1c"}, + {file = "pillow_avif_plugin-1.2.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d33886a5f9796fe9a8a3bc25ccfdeba7db119adb50b7004f1928a14b07d0213a"}, + {file = "pillow_avif_plugin-1.2.2-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:75b7ed186c2f740dd26e556f6a966c59a170b70263e429a2c81920fe444da8a7"}, + {file = "pillow_avif_plugin-1.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a11aef6b79078b8dad25c928e5871c146ab94424472851d5bf539ba62abde9ac"}, + {file = "pillow_avif_plugin-1.2.2-cp311-cp311-musllinux_1_1_x86_64.whl", hash = "sha256:10696c536d68a14cefea3b98edb8d5a7ae29e8e07458f1d59c5d1cd780a8bf2a"}, {file = "pillow_avif_plugin-1.2.2-cp36-cp36m-macosx_10_10_x86_64.whl", hash = "sha256:1a7291d6a5fb7336e72685a31d193e0b3a6bee9986c9ac4d8bd4b68dbe6d4f7f"}, {file = "pillow_avif_plugin-1.2.2-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl", hash = "sha256:14b9c5dbf237e7dc12f69819ea181a457b3bd4f59f8cd71d028d3635fd3bcab4"}, {file = "pillow_avif_plugin-1.2.2-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:799cbfbeee831332d280c80df9ce16b5c3b1224c318264e97e89df8da32e870e"}, @@ -6734,6 +6747,10 @@ ptyprocess = [ {file = "ptyprocess-0.7.0-py2.py3-none-any.whl", hash = "sha256:4b41f3967fce3af57cc7e94b888626c18bf37a083e3651ca8feeb66d492fef35"}, {file = "ptyprocess-0.7.0.tar.gz", hash = "sha256:5c5d0a3b48ceee0b48485e0c26037c0acd7d29765ca3fbb5cb3831d347423220"}, ] +pulsectl = [ + {file = "pulsectl-22.3.2-py2.py3-none-any.whl", hash = "sha256:6fded96a8e6a9ac6c387ed3a9bdb1afb20d80256b0c9df4b98b58461d74ba0c4"}, + {file = "pulsectl-22.3.2.tar.gz", hash = "sha256:cc174e1ceebd4e6222c5b78f4f415f12e80753c9ab75ab354159b4cb5950b084"}, +] pure-eval = [ {file = "pure_eval-0.2.2-py3-none-any.whl", hash = "sha256:01eaab343580944bc56080ebe0a674b39ec44a945e6d09ba7db3cb8cec289350"}, {file = "pure_eval-0.2.2.tar.gz", hash = "sha256:2b45320af6dfaa1750f543d714b6d1c520a1688dec6fd24d339063ce0aaa9ac3"}, @@ -6964,11 +6981,13 @@ pyprof2calltree = [ ] pyproj = [ {file = "pyproj-3.4.0-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:f343725566267a296b09ee7e591894f1fdc90f84f8ad5ec476aeb53bd4479c07"}, + {file = "pyproj-3.4.0-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:5816807ca0bdc7256558770c6206a6783a3f02bcf844f94ee245f197bb5f7285"}, {file = "pyproj-3.4.0-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e7e609903572a56cca758bbaee5c1663c3e829ddce5eec4f368e68277e37022b"}, {file = "pyproj-3.4.0-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:4fd425ee8b6781c249c7adb7daa2e6c41ce573afabe4f380f5eecd913b56a3be"}, {file = "pyproj-3.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:954b068136518b3174d0a99448056e97af62b63392a95c420894f7de2229dae6"}, {file = "pyproj-3.4.0-cp310-cp310-win_amd64.whl", hash = "sha256:4a23d84c5ffc383c7d9f0bde3a06fc1f6697b1b96725597f8f01e7b4bef0a2b5"}, {file = "pyproj-3.4.0-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:1f9c100fd0fd80edbc7e4daa303600a8cbef6f0de43d005617acb38276b88dc0"}, + {file = "pyproj-3.4.0-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:aa5171f700f174777a9e9ed8f4655583243967c0f9cf2c90e3f54e54ff740134"}, {file = "pyproj-3.4.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:9a496d9057b2128db9d733e66b206f2d5954bbae6b800d412f562d780561478c"}, {file = "pyproj-3.4.0-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:52e54796e2d9554a5eb8f11df4748af1fbbc47f76aa234d6faf09216a84554c5"}, {file = "pyproj-3.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:a454a7c4423faa2a14e939d08ef293ee347fa529c9df79022b0585a6e1d8310c"}, @@ -6979,6 +6998,7 @@ pyproj = [ {file = "pyproj-3.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:f80adda8c54b84271a93829477a01aa57bc178c834362e9f74e1de1b5033c74c"}, {file = "pyproj-3.4.0-cp38-cp38-win_amd64.whl", hash = "sha256:221d8939685e0c43ee594c9f04b6a73a10e8e1cc0e85f28be0b4eb2f1bc8777d"}, {file = "pyproj-3.4.0-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:d94afed99f31673d3d19fe750283621e193e2a53ca9e0443bf9d092c3905833b"}, + {file = "pyproj-3.4.0-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:0fff9c3a991508f16027be27d153f6c5583d03799443639d13c681e60f49e2d7"}, {file = "pyproj-3.4.0-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:3b85acf09e5a9e35cd9ee72989793adb7089b4e611be02a43d3d0bda50ad116b"}, {file = "pyproj-3.4.0-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:45554f47d1a12a84b0620e4abc08a2a1b5d9f273a4759eaef75e74788ec7162a"}, {file = "pyproj-3.4.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:12f62c20656ac9b6076ebb213e9a635d52f4f01fef95310121d337e62e910cb6"}, diff --git a/pyproject.toml b/pyproject.toml index c890b7887595f3..d4e3584f2420e7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/system/hardware/pc/hardware.py b/system/hardware/pc/hardware.py index 2c83eb35f49f38..d6a27f8d5e9d45 100644 --- a/system/hardware/pc/hardware.py +++ b/system/hardware/pc/hardware.py @@ -1,5 +1,7 @@ import random -import subprocess +import time + +import pulsectl from cereal import log from system.hardware.base import HardwareBase, ThermalConfig @@ -9,6 +11,9 @@ class Pc(HardwareBase): + def __init__(self): + self.pulse = pulsectl.Pulse('openpilot') + def get_os_version(self): return None @@ -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!") diff --git a/system/hardware/tici/hardware.py b/system/hardware/tici/hardware.py index 8371d6ef70baed..93410752b5d9aa 100644 --- a/system/hardware/tici/hardware.py +++ b/system/hardware/tici/hardware.py @@ -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 @@ -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 @@ -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"]) diff --git a/system/micd.py b/system/micd.py index 4070e31ed7c19b..150dcb9cbe4103 100755 --- a/system/micd.py +++ b/system/micd.py @@ -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 @@ -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