Play sound from python/ALSA instead QtMultimedia

GStreamer sucks
This commit is contained in:
miruka
2021-03-01 00:00:04 -04:00
parent 31f6e5320a
commit 92c49140e7
7 changed files with 33 additions and 38 deletions

View File

@@ -2,12 +2,14 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import asyncio
import io
import logging as log
import os
import re
import sys
import time
import traceback
import wave
from datetime import datetime, timedelta
from pathlib import Path
from typing import Any, DefaultDict, Dict, List, Optional, Tuple, Union
@@ -16,6 +18,8 @@ from urllib.parse import urlparse
import aiohttp
import nio
import plyer
import pyotherside
import simpleaudio
from appdirs import AppDirs
from . import __app_name__
@@ -584,3 +588,18 @@ class Backend:
trace = traceback.format_exc().rstrip()
log.error("Sending desktop notification failed\n%s", trace)
self.notifications_working = False
async def sound_notify(self) -> None:
path = self.settings.Notifications.default_sound
if path == "default.wav":
path = "src/sounds/default.wav"
try:
content = pyotherside.qrc_get_file_contents(path)
except ValueError:
simpleaudio.WaveObject.from_wave_file(path).play()
else:
wave_read = wave.open(io.BytesIO(content))
simpleaudio.WaveObject.from_wave_read(wave_read).play()

View File

@@ -58,12 +58,9 @@ class Notifications:
# Default sound to play for notifications. Can be the filename
# of a builtin sound (only "default.wav" currently exists), or the
# absolute path to an audio file, preferably in the WAV format.
# absolute path to a WAV file.
default_sound: str = "default.wav"
# Volume at which the notification sound will be played, 0-100.
volume: int = 75
# How long in seconds the window will flash in your dock or taskbar when
# a new message, which matches a notification push rule with a
# flash (lightbulb) action, is posted in a room.

View File

@@ -23,13 +23,8 @@ QtObject {
if (Qt.application.state === Qt.ApplicationActive)
return
if (bubble)
py.callCoro("desktop_notify", [title, body, image])
if (sound) {
window.mainUI.defaultNotificationSound.seek(0)
window.mainUI.defaultNotificationSound.play()
}
if (bubble) py.callCoro("desktop_notify", [title, body, image])
if (sound) py.callCoro("sound_notify")
if (urgencyHint) {
const msec =

View File

@@ -6,7 +6,6 @@ import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtQuick.Window 2.12
import QtGraphicalEffects 1.12
import QtMultimedia 5.12
import "."
import "Base"
import "MainPane"
@@ -36,7 +35,6 @@ Item {
return ids
}
readonly property alias defaultNotificationSound: defaultNotificationSound
readonly property alias debugConsole: debugConsole
readonly property alias mainPane: mainPane
readonly property alias pageLoader: pageLoader
@@ -116,17 +114,6 @@ Item {
font.pointSize: -1
}
Audio {
id: defaultNotificationSound
readonly property string sfx:
window.settings.Notifications.default_sound
audioRole: Audio.NotificationRole
volume: window.settings.Notifications.volume / 100
source: sfx.trim() === "default.wav" ? "../sounds/default.wav" : sfx
}
IdleManager {
id: idleManager
}