mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-09-07 16:13:42 +08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 311d7b2fee | |||
| 71c1645157 | |||
| a26939ea86 |
+1
-1
Submodule opendbc_repo updated: f9f1223de9...f95f996f59
@@ -62,7 +62,25 @@ class MiciFccModal(NavRawScrollPanel):
|
||||
rl.draw_texture_ex(self._fcc_logo, fcc_pos, 0.0, 1.0, rl.WHITE)
|
||||
|
||||
|
||||
def _engaged_confirmation_click(callback: Callable, action_text: str, icon: rl.Texture, exit_on_confirm: bool = True, red: bool = False):
|
||||
class DisengageDialog(BigDialog):
|
||||
def __init__(self, title: str, description: str, disengaged_callback: Callable[[], None]):
|
||||
super().__init__(title, description)
|
||||
self._disengaged_callback = disengaged_callback
|
||||
|
||||
def _update_state(self):
|
||||
super()._update_state()
|
||||
if not ui_state.engaged and not self.is_dismissing:
|
||||
self.dismiss(self._disengaged_callback)
|
||||
|
||||
|
||||
class EngagedConfirmationDialog(BigConfirmationDialog):
|
||||
def _update_state(self):
|
||||
super()._update_state()
|
||||
if ui_state.engaged and not self.is_dismissing:
|
||||
self.dismiss()
|
||||
|
||||
|
||||
def engaged_confirmation_click(callback: Callable, action_text: str, icon: rl.Texture, exit_on_confirm: bool = True, red: bool = False):
|
||||
if not ui_state.engaged:
|
||||
def confirm_callback():
|
||||
# Check engaged again in case it changed while the dialog was open
|
||||
@@ -70,23 +88,26 @@ def _engaged_confirmation_click(callback: Callable, action_text: str, icon: rl.T
|
||||
if not ui_state.engaged:
|
||||
callback()
|
||||
|
||||
gui_app.push_widget(BigConfirmationDialog(f"slide to\n{action_text.lower()}", icon, confirm_callback, exit_on_confirm=exit_on_confirm, red=red))
|
||||
gui_app.push_widget(EngagedConfirmationDialog(f"slide to\n{action_text.lower()}", icon, confirm_callback,
|
||||
exit_on_confirm=exit_on_confirm, red=red))
|
||||
else:
|
||||
gui_app.push_widget(BigDialog("", f"Disengage to {action_text}"))
|
||||
gui_app.push_widget(DisengageDialog("", f"Disengage to {action_text}",
|
||||
lambda: engaged_confirmation_click(callback, action_text, icon,
|
||||
exit_on_confirm=exit_on_confirm, red=red)))
|
||||
|
||||
|
||||
class EngagedConfirmationCircleButton(BigCircleButton):
|
||||
def __init__(self, title: str, icon: rl.Texture, callback: Callable[[], None], exit_on_confirm: bool = True,
|
||||
red: bool = False, icon_offset: tuple[int, int] = (0, 0)):
|
||||
super().__init__(icon, red, icon_offset)
|
||||
self.set_click_callback(lambda: _engaged_confirmation_click(callback, title, icon, exit_on_confirm=exit_on_confirm, red=red))
|
||||
self.set_click_callback(lambda: engaged_confirmation_click(callback, title, icon, exit_on_confirm=exit_on_confirm, red=red))
|
||||
|
||||
|
||||
class EngagedConfirmationButton(BigButton):
|
||||
def __init__(self, text: str, action_text: str, icon: rl.Texture, callback: Callable[[], None],
|
||||
exit_on_confirm: bool = True, red: bool = False):
|
||||
super().__init__(text, "", icon)
|
||||
self.set_click_callback(lambda: _engaged_confirmation_click(callback, action_text, icon, exit_on_confirm=exit_on_confirm, red=red))
|
||||
self.set_click_callback(lambda: engaged_confirmation_click(callback, action_text, icon, exit_on_confirm=exit_on_confirm, red=red))
|
||||
|
||||
|
||||
class DeviceInfoLayoutMici(Widget):
|
||||
|
||||
@@ -211,9 +211,7 @@ class Soundd(QuietMode):
|
||||
|
||||
|
||||
def main():
|
||||
from openpilot.sunnypilot.selfdrive.ui.soundd import SounddSP
|
||||
|
||||
s = SounddSP()
|
||||
s = Soundd()
|
||||
s.soundd_thread()
|
||||
|
||||
|
||||
|
||||
@@ -6,9 +6,9 @@ See the LICENSE.md file in the root directory for more details.
|
||||
"""
|
||||
from openpilot.selfdrive.ui.mici.layouts.settings import settings as OP
|
||||
from openpilot.selfdrive.ui.mici.layouts.settings.settings import SettingsBigButton
|
||||
from openpilot.selfdrive.ui.mici.layouts.settings.device import DeviceLayoutMici
|
||||
from openpilot.selfdrive.ui.mici.layouts.settings.device import DeviceLayoutMici, engaged_confirmation_click
|
||||
from openpilot.selfdrive.ui.mici.widgets.button import BigCircleButton
|
||||
from openpilot.selfdrive.ui.mici.widgets.dialog import BigConfirmationDialog, BigDialog
|
||||
from openpilot.selfdrive.ui.mici.widgets.dialog import BigConfirmationDialog
|
||||
from openpilot.selfdrive.ui.sunnypilot.mici.layouts.sunnylink import SunnylinkLayoutMici
|
||||
from openpilot.selfdrive.ui.sunnypilot.mici.layouts.models import ModelsLayoutMici
|
||||
from openpilot.selfdrive.ui.ui_state import ui_state
|
||||
@@ -93,10 +93,6 @@ class SettingsLayoutSP(OP.SettingsLayout):
|
||||
dlg = BigConfirmationDialog(tr("slide to exit always offroad"), self.icon_offroad_slider, red=False,
|
||||
confirm_callback=lambda: _set_offroad_status(False))
|
||||
else:
|
||||
if ui_state.engaged:
|
||||
gui_app.push_widget(BigDialog(tr("disengage to enable always offroad"), "", ))
|
||||
return
|
||||
|
||||
dlg = BigConfirmationDialog(tr("slide to force offroad"), self.icon_offroad_slider, red=True,
|
||||
confirm_callback=lambda: _set_offroad_status(True))
|
||||
engaged_confirmation_click(lambda: _set_offroad_status(True), "force offroad", self.icon_offroad_slider, red=True)
|
||||
return
|
||||
gui_app.push_widget(dlg)
|
||||
|
||||
@@ -1,113 +0,0 @@
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
import time
|
||||
import wave
|
||||
|
||||
import numpy as np
|
||||
|
||||
from openpilot.common.swaglog import cloudlog
|
||||
|
||||
AUDIO_DIR = Path("/data/media/0/custom_audio")
|
||||
SAMPLE_RATE = 48000
|
||||
MAX_SECONDS = 300
|
||||
GEAR_TIMEOUT = 0.5
|
||||
DUCK_FRAMES = int(0.05 * SAMPLE_RATE)
|
||||
RESTORE_FRAMES = int(0.15 * SAMPLE_RATE)
|
||||
|
||||
|
||||
class GearTransition:
|
||||
def __init__(self):
|
||||
self.previous = None
|
||||
self.last_time = None
|
||||
|
||||
def update(self, gear, timestamp, valid=True):
|
||||
if not valid or gear == "unknown":
|
||||
self.previous = self.last_time = None
|
||||
return None
|
||||
if self.last_time is None or not 0 < timestamp - self.last_time <= GEAR_TIMEOUT:
|
||||
self.previous = None
|
||||
transition = gear if self.previous is not None and gear != self.previous else None
|
||||
self.previous = gear
|
||||
self.last_time = timestamp
|
||||
return transition
|
||||
|
||||
|
||||
def load_audio(filename):
|
||||
path = AUDIO_DIR / filename
|
||||
if not path.is_file():
|
||||
return None
|
||||
with wave.open(str(path), "rb") as wav:
|
||||
if (wav.getnchannels(), wav.getsampwidth(), wav.getframerate(), wav.getcomptype()) != (1, 2, SAMPLE_RATE, "NONE"):
|
||||
raise ValueError("Custom audio requires 48 kHz mono 16-bit PCM WAV")
|
||||
count = wav.getnframes()
|
||||
if not 0 < count <= MAX_SECONDS * SAMPLE_RATE:
|
||||
raise ValueError("Custom audio must be between 0 and 300 seconds")
|
||||
raw = wav.readframes(count)
|
||||
if len(raw) != count * 2:
|
||||
raise ValueError("Truncated custom audio")
|
||||
return np.frombuffer(raw, dtype="<i2").astype(np.float32) / 32768
|
||||
|
||||
|
||||
class AudioPlayer:
|
||||
def __init__(self):
|
||||
self.command = None
|
||||
self.seen_command = None
|
||||
self.samples = np.empty(0, dtype=np.float32)
|
||||
self.position = 0
|
||||
self.gain = 1.0
|
||||
|
||||
def render(self, frames, alert_active):
|
||||
command = self.command
|
||||
if command is not self.seen_command:
|
||||
self.seen_command = command
|
||||
self.samples = command if command is not None else np.empty(0, dtype=np.float32)
|
||||
self.position = 0
|
||||
self.gain = 1.0
|
||||
out = np.zeros(frames, dtype=np.float32)
|
||||
if self.position == len(self.samples):
|
||||
return out
|
||||
n = min(frames, len(self.samples) - self.position)
|
||||
step = -0.5 / DUCK_FRAMES if alert_active else 0.5 / RESTORE_FRAMES
|
||||
gains = np.clip(self.gain + np.arange(n) * step, 0.5, 1.0)
|
||||
out[:n] = self.samples[self.position:self.position + n] * gains
|
||||
self.position += n
|
||||
self.gain = float(np.clip(self.gain + n * step, 0.5, 1.0))
|
||||
return out
|
||||
|
||||
|
||||
class CustomAudio:
|
||||
def __init__(self):
|
||||
self.player = AudioPlayer()
|
||||
self.transition = GearTransition()
|
||||
self.loader = ThreadPoolExecutor(max_workers=1, thread_name_prefix="custom-audio-source")
|
||||
self.pending = None
|
||||
|
||||
def update(self, messages):
|
||||
now = time.monotonic()
|
||||
event = None
|
||||
if self.transition.last_time is not None and now - self.transition.last_time > GEAR_TIMEOUT:
|
||||
self.transition.update(None, now, False)
|
||||
for msg in messages:
|
||||
timestamp = msg.logMonoTime / 1e9
|
||||
valid = msg.valid and msg.carState.canValid and msg.carState.gearShifter != "unknown" and 0 <= now - timestamp <= GEAR_TIMEOUT
|
||||
transition = self.transition.update(str(msg.carState.gearShifter), timestamp, valid)
|
||||
if not valid:
|
||||
event = None
|
||||
elif transition:
|
||||
event = transition
|
||||
|
||||
if event:
|
||||
self.player.command = None
|
||||
if self.pending:
|
||||
self.pending.cancel()
|
||||
self.pending = self.loader.submit(load_audio, f"{event}.wav")
|
||||
|
||||
if self.pending and self.pending.done():
|
||||
try:
|
||||
samples = self.pending.result()
|
||||
if samples is not None:
|
||||
self.player.command = samples
|
||||
except Exception:
|
||||
cloudlog.exception("Unable to load custom audio")
|
||||
self.pending = None
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
# Custom Audio
|
||||
|
||||
Put short **48 kHz, mono, 16-bit PCM WAV** files in `/data/media/0/custom_audio/` (maximum five minutes each).
|
||||
|
||||
Each gear change plays `<gear>.wav`: `park.wav`, `drive.wav`, `sport.wav`, `reverse.wav`, `neutral.wav`, `low.wav`, `brake.wav`, `eco.wav`, or `manumatic.wav`.
|
||||
|
||||
- Entering a gear starts its audio from the beginning at 25% volume, with no fade-in.
|
||||
- Changing gears cuts the previous audio without a fade-out. If the new file is missing or invalid, playback stays silent.
|
||||
- Returning to a previous gear restarts its audio from the beginning. Staying in the same gear does not replay it.
|
||||
- While any alert sounds, custom audio keeps playing and fades to **half its normal level (12.5%) over 50 ms**. Afterward it fades back to 25% over **150 ms**.
|
||||
- Alert volume is unchanged. Custom audio may be reduced further to prevent clipping when both sounds are loud.
|
||||
- Startup and recovery from unknown/invalid gear data or data gaps establish a silent baseline.
|
||||
|
||||
No settings or dismiss control. Replace or remove files to change future playback.
|
||||
|
||||
Convert files with:
|
||||
|
||||
```sh
|
||||
ffmpeg -i input_audio -ar 48000 -ac 1 -c:a pcm_s16le drive.wav
|
||||
```
|
||||
@@ -1,25 +0,0 @@
|
||||
import numpy as np
|
||||
|
||||
from openpilot.cereal import messaging
|
||||
from openpilot.selfdrive.ui.soundd import Soundd
|
||||
from openpilot.sunnypilot.selfdrive.ui.custom_audio import CustomAudio
|
||||
|
||||
|
||||
class SounddSP(Soundd):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.custom_audio = CustomAudio()
|
||||
self.gear_sock = messaging.sub_sock("carState", conflate=False)
|
||||
|
||||
def callback(self, data_out: np.ndarray, frames: int, time, status) -> None:
|
||||
super().callback(data_out, frames, time, status)
|
||||
alert_data = data_out[:frames, 0]
|
||||
alert_peak = float(np.max(np.abs(alert_data)))
|
||||
custom_data = self.custom_audio.player.render(frames, alert_peak > 0) * 0.25
|
||||
custom_peak = float(np.max(np.abs(custom_data)))
|
||||
gain = min(1.0, max(0.0, 1.0 - alert_peak) / max(custom_peak, 1e-9))
|
||||
alert_data += custom_data * gain
|
||||
|
||||
def get_audible_alert(self, sm):
|
||||
super().get_audible_alert(sm)
|
||||
self.custom_audio.update(messaging.drain_sock(self.gear_sock, wait_for_one=False))
|
||||
Reference in New Issue
Block a user