Skip to content

Instantly share code, notes, and snippets.

@zdimension
Created August 5, 2023 22:18
Show Gist options
  • Save zdimension/2b193e5d1ba403aa1ad1f570becd5399 to your computer and use it in GitHub Desktop.
Save zdimension/2b193e5d1ba403aa1ad1f570becd5399 to your computer and use it in GitHub Desktop.
from nicegui import ui, app
import alsaaudio as audio
cards = audio.cards()
card = cards[2]
assert card == "ICUSBAUDIO7D"
mixer = audio.Mixer("Speaker", cardindex=2)
def get_volume():
return muted_volume or mixer.getvolume()[0]
def set_volume(value):
if muted_volume is not None:
toggle_mute()
if value < 0:
value = 0
elif value > 100:
value = 100
mixer.setvolume(value)
lbl_vol.set_text(f'{value}%')
sld_vol.set_value(value)
muted_volume = None
if get_volume() == 0:
muted_volume = 15
def toggle_mute():
global muted_volume
if muted_volume is None:
old = get_volume()
set_volume(0)
muted_volume = old
btn_mute.classes(add="bg-red")
else:
old = muted_volume
muted_volume = None
set_volume(old)
btn_mute.classes(remove="bg-red")
def restart_service():
from os import system
system("sudo systemctl restart spdifloop")
with ui.column().classes(add="w-full").style(add="height: 100vh"):
with ui.row():
ui.label('Volume serveur')
ui.button('🔄', on_click=lambda: restart_service())
lbl_vol = ui.label('0%')
sld_vol = ui.slider(min=0, max=100, value=0, on_change=lambda args: set_volume(args.value))
with ui.grid(rows=3).classes(add="w-full").style(add="flex-grow: 1"):
ui.button('🔊', on_click=lambda: set_volume(get_volume() + 7)).classes(add="big")
ui.button('🔉', on_click=lambda: set_volume(get_volume() - 7)).classes(add="big")
btn_mute = ui.button("🔇", on_click=lambda: toggle_mute()).classes(add="big")
set_volume(get_volume())
ui.query(".nicegui-content").style("height: calc(100vh - 10em)")
ui.query("body, html").style("margin: 0; padding: 0")
ui.query(".big").classes(add="w-full").style("font-size: 500%; padding: 0")
@app.get("/spdifloop.webmanifest")
def manifest():
return {
"name": "Volume",
"short_name": "Volume",
"start_url": "/",
"display": "standalone",
"background_color": "#000",
"theme_color": "#000",
"icons": [
{
"src": "https://domino.zdimension.fr/apps/domino.svg",
"sizes": "512x512",
"type": "image/svg+xml"
}
]
}
@app.get("/sw.js")
def sw():
return """
self.addEventListener('install', function(event) {});
self.addEventListener('fetch', function(event) {});
self.addEventListener('activate', function(event) {});
"""
ui.add_head_html('<link rel="manifest" href="/spdifloop.webmanifest">')
ui.add_body_html("""
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js');
}
</script>
""")
ui.run(port=8765, show=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment