Make SoundOff a true singleton
This commit is contained in:
@@ -33,39 +33,53 @@ class SoundOff(QObject):
|
|||||||
if cls._instance:
|
if cls._instance:
|
||||||
return cls._instance
|
return cls._instance
|
||||||
cls._instance = super(SoundOff, cls).__new__(cls)
|
cls._instance = super(SoundOff, cls).__new__(cls)
|
||||||
|
#
|
||||||
|
# Setup devices
|
||||||
|
#
|
||||||
|
cls.virtualDevice = None
|
||||||
|
|
||||||
|
for output in QMediaDevices.audioOutputs():
|
||||||
|
if output.id().data().decode("utf-8") == "virt-input":
|
||||||
|
cls.virtualDevice = output
|
||||||
|
if output.isDefault():
|
||||||
|
cls.localDevice = output
|
||||||
|
cls.alertEffect = QSoundEffect()
|
||||||
|
cls.alertEffect.setSource(QUrl("qrc:/beep.wav"))
|
||||||
|
cls.alertEffect.setAudioDevice(cls.localDevice)
|
||||||
|
cls.alertEffect.setVolume(0.25)
|
||||||
|
cls.alertEffect.setLoopCount(1)
|
||||||
|
|
||||||
|
cls.localPlayer = QMediaPlayer()
|
||||||
|
cls.localPlayer.setObjectName("localPlayer")
|
||||||
|
cls.localOutput = QAudioOutput()
|
||||||
|
cls.localOutput.setDevice(cls.localDevice)
|
||||||
|
cls.localPlayer.setAudioOutput(cls.localOutput)
|
||||||
|
if cls.virtualDevice:
|
||||||
|
cls.virtualPlayer = QMediaPlayer()
|
||||||
|
cls.virtualPlayer.setObjectName("virtualPlayer")
|
||||||
|
cls.virtualOutput = QAudioOutput()
|
||||||
|
cls.virtualOutput.setVolume(1.0)
|
||||||
|
cls.virtualOutput.setDevice(cls.virtualDevice)
|
||||||
|
cls.virtualPlayer.setAudioOutput(cls.virtualOutput)
|
||||||
|
|
||||||
|
cacheDir = QDir(
|
||||||
|
QStandardPaths.writableLocation(
|
||||||
|
QStandardPaths.StandardLocation.GenericCacheLocation
|
||||||
|
)
|
||||||
|
)
|
||||||
|
cacheDir.mkdir("Troglodite")
|
||||||
|
cacheDir = QDir(cacheDir.path() + QDir.separator() + "Troglodite")
|
||||||
|
netCache = QNetworkDiskCache()
|
||||||
|
netCache.setCacheDirectory(cacheDir.path())
|
||||||
|
cls.nam = QNetworkAccessManager()
|
||||||
|
cls.nam.setCache(netCache)
|
||||||
return cls._instance
|
return cls._instance
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
#
|
if self.localPlayer.receivers(self.localPlayer.errorOccurred) > 0:
|
||||||
# Setup devices
|
print("SoundOff, __init__() after __init__()")
|
||||||
#
|
|
||||||
self.virtualDevice = None
|
|
||||||
|
|
||||||
for output in QMediaDevices.audioOutputs():
|
|
||||||
if output.id().data().decode("utf-8") == "virt-input":
|
|
||||||
self.virtualDevice = output
|
|
||||||
if output.isDefault():
|
|
||||||
self.localDevice = output
|
|
||||||
|
|
||||||
self.alertEffect = QSoundEffect()
|
|
||||||
self.alertEffect.setSource(QUrl("qrc:/beep.wav"))
|
|
||||||
self.alertEffect.setAudioDevice(self.localDevice)
|
|
||||||
self.alertEffect.setVolume(0.25)
|
|
||||||
self.alertEffect.setLoopCount(1)
|
|
||||||
|
|
||||||
self.localPlayer = QMediaPlayer()
|
|
||||||
self.localPlayer.setObjectName("localPlayer")
|
|
||||||
self.localOutput = QAudioOutput()
|
|
||||||
self.localOutput.setDevice(self.localDevice)
|
|
||||||
self.localPlayer.setAudioOutput(self.localOutput)
|
|
||||||
if self.virtualDevice:
|
|
||||||
self.virtualPlayer = QMediaPlayer()
|
|
||||||
self.virtualPlayer.setObjectName("virtualPlayer")
|
|
||||||
self.virtualOutput = QAudioOutput()
|
|
||||||
self.virtualOutput.setVolume(1.0)
|
|
||||||
self.virtualOutput.setDevice(self.virtualDevice)
|
|
||||||
self.virtualPlayer.setAudioOutput(self.virtualOutput)
|
|
||||||
#
|
#
|
||||||
# Connections
|
# Connections
|
||||||
#
|
#
|
||||||
@@ -76,17 +90,6 @@ class SoundOff(QObject):
|
|||||||
self.virtualPlayer.errorOccurred.connect(self.mediaError)
|
self.virtualPlayer.errorOccurred.connect(self.mediaError)
|
||||||
self.virtualPlayer.mediaStatusChanged.connect(self.mediaStatus)
|
self.virtualPlayer.mediaStatusChanged.connect(self.mediaStatus)
|
||||||
self.virtualPlayer.playbackStateChanged.connect(self.playbackState)
|
self.virtualPlayer.playbackStateChanged.connect(self.playbackState)
|
||||||
cacheDir = QDir(
|
|
||||||
QStandardPaths.writableLocation(
|
|
||||||
QStandardPaths.StandardLocation.GenericCacheLocation
|
|
||||||
)
|
|
||||||
)
|
|
||||||
cacheDir.mkdir("Troglodite")
|
|
||||||
cacheDir = QDir(cacheDir.path() + QDir.separator() + "Troglodite")
|
|
||||||
netCache = QNetworkDiskCache()
|
|
||||||
netCache.setCacheDirectory(cacheDir.path())
|
|
||||||
self.nam = QNetworkAccessManager()
|
|
||||||
self.nam.setCache(netCache)
|
|
||||||
self.nam.finished.connect(self.finished)
|
self.nam.finished.connect(self.finished)
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -98,7 +101,7 @@ class SoundOff(QObject):
|
|||||||
|
|
||||||
@pyqtSlot(QMediaPlayer.MediaStatus)
|
@pyqtSlot(QMediaPlayer.MediaStatus)
|
||||||
def mediaStatus(self, status: QMediaPlayer.MediaStatus) -> None:
|
def mediaStatus(self, status: QMediaPlayer.MediaStatus) -> None:
|
||||||
# print(f"mediaStatus: {status}")
|
#print(f"mediaStatus: {status}")
|
||||||
if status == QMediaPlayer.MediaStatus.LoadedMedia:
|
if status == QMediaPlayer.MediaStatus.LoadedMedia:
|
||||||
player: Optional[QMediaPlayer] = cast(QMediaPlayer, self.sender())
|
player: Optional[QMediaPlayer] = cast(QMediaPlayer, self.sender())
|
||||||
assert player is not None
|
assert player is not None
|
||||||
@@ -107,7 +110,7 @@ class SoundOff(QObject):
|
|||||||
|
|
||||||
@pyqtSlot(QMediaPlayer.PlaybackState)
|
@pyqtSlot(QMediaPlayer.PlaybackState)
|
||||||
def playbackState(self, state: QMediaPlayer.PlaybackState) -> None:
|
def playbackState(self, state: QMediaPlayer.PlaybackState) -> None:
|
||||||
# print(f"playbackState: {state}")
|
#print(f"playbackState: {state}")
|
||||||
return
|
return
|
||||||
|
|
||||||
#
|
#
|
||||||
@@ -154,14 +157,14 @@ class SoundOff(QObject):
|
|||||||
@pyqtSlot(QNetworkReply)
|
@pyqtSlot(QNetworkReply)
|
||||||
def finished(self, reply: QNetworkReply) -> None:
|
def finished(self, reply: QNetworkReply) -> None:
|
||||||
storage = reply.readAll()
|
storage = reply.readAll()
|
||||||
|
reply.close()
|
||||||
crypto = QCryptographicHash(QCryptographicHash.Algorithm.Sha256)
|
crypto = QCryptographicHash(QCryptographicHash.Algorithm.Sha256)
|
||||||
for player in [self.localPlayer, self.virtualPlayer]:
|
for player in [self.localPlayer, self.virtualPlayer]:
|
||||||
if not player:
|
if not player:
|
||||||
continue
|
continue
|
||||||
self._storage[player] = QByteArray(storage)
|
self._storage[player] = QByteArray(storage)
|
||||||
crypto.addData(self._storage[player])
|
crypto.addData(self._storage[player])
|
||||||
print(player, crypto.result().toHex())
|
#print(player, crypto.result().toHex())
|
||||||
crypto.reset()
|
crypto.reset()
|
||||||
self._buffer[player] = QBuffer(self._storage[player])
|
self._buffer[player] = QBuffer(self._storage[player])
|
||||||
url = reply.request().url()
|
url = reply.request().url()
|
||||||
@@ -169,5 +172,5 @@ class SoundOff(QObject):
|
|||||||
player.setPosition(0)
|
player.setPosition(0)
|
||||||
if player.mediaStatus() == QMediaPlayer.MediaStatus.LoadedMedia:
|
if player.mediaStatus() == QMediaPlayer.MediaStatus.LoadedMedia:
|
||||||
player.play()
|
player.play()
|
||||||
print("play")
|
#print("play")
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user