swift
Loading...
Searching...
No Matches
notificationplayer.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2016 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QTimer>
7
8#include "misc/logmessage.h"
11
12using namespace swift::misc;
13using namespace swift::misc::audio;
14using namespace swift::sound::sample_provider;
15
16namespace swift::sound
17{
22
24 {
25 QSoundEffect *effect = m_effects.value(notification, nullptr);
26 if (effect)
27 {
28 if (!m_playingEffect && effect->isLoaded() && !effect->isPlaying())
29 {
30 const int currentEffect = ++m_playingEffectCounter;
31 const qreal v = volume / 100.0f;
32 m_playingEffect = effect;
33 effect->setVolume(v); // 0..1
34 effect->play();
35
36 // used for too long or hanging sounds
38 QTimer::singleShot(3000, effect, [=, this] {
39 if (!myself || !m_playingEffect)
40 {
41 return;
42 }
43 if (currentEffect != m_playingEffectCounter)
44 {
45 return;
46 }
47 m_playingEffect->stop();
48 });
49 }
50 }
51 }
52
54 {
55 if (directory == m_directory && !m_effects.isEmpty())
56 {
57 return;
58 }
59 m_directory = directory;
60
61 const QStringList types = QSoundEffect::supportedMimeTypes();
62 CLogMessage(this).info(u"Notification mime types: %1") << types.join(", ");
63
64 this->updateEffect(CNotificationSounds::NotificationLogin, directory, "login.wav");
65 this->updateEffect(CNotificationSounds::NotificationLogoff, directory, "logoff.wav");
66 this->updateEffect(CNotificationSounds::NotificationTextMessageFrequency, directory, "frequencymessage.wav");
67 this->updateEffect(CNotificationSounds::NotificationTextMessagePrivate, directory, "privatemessage.wav");
68 this->updateEffect(CNotificationSounds::NotificationTextMessageSupervisor, directory, "supervisormessage.wav");
69 this->updateEffect(CNotificationSounds::NotificationTextMessageUnicom, directory, "unicommessage.wav");
70 this->updateEffect(CNotificationSounds::NotificationTextCallsignMentioned, directory, "callsignmentioned.wav");
71 this->updateEffect(CNotificationSounds::PTTClickKeyDown, directory, "pttclick.wav");
72 this->updateEffect(CNotificationSounds::PTTClickKeyUp, directory, "pttclick.wav");
73
74 // CNotificationSounds::AFVBlocked is generated
75 this->updateEffect(CNotificationSounds::AFVClicked, directory, Samples::fnClick());
76 }
77
78 void CNotificationPlayer::onPlayingChanged()
79 {
80 if (!m_playingEffect)
81 {
82 return;
83 }
84 if (m_playingEffect->isPlaying())
85 {
86 return;
87 }
88 m_playingEffect.clear();
89 }
90
91 void CNotificationPlayer::updateEffect(CNotificationSounds::NotificationFlag f, const QString &directory,
92 const QString &name)
93 {
94 QSoundEffect *e = nullptr;
95 if (m_effects.contains(f))
96 {
97 e = m_effects[f];
98 }
99 if (e)
100 {
101 e->deleteLater();
102 }
103
104 // file if existing
106 if (url.isEmpty() || !url.isLocalFile())
107 {
108 // remove notification as not existing
109 m_effects.remove(f);
110 return;
111 }
112
113 // new effect
114 // QString fn = url.toLocalFile();
115 auto effect = new QSoundEffect(this);
116 effect->setSource(url);
117 effect->setLoopCount(1);
118 effect->setMuted(false);
119 m_effects[f] = effect;
120 connect(effect, &QSoundEffect::playingChanged, this, &CNotificationPlayer::onPlayingChanged,
122 }
123} // namespace swift::sound
Class for emitting a log message.
Definition logmessage.h:27
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
static QString soundFilePathOrDefaultPath(const QString &directory, const QString &fileName)
File path (with file name) of file name and.
CNotificationPlayer(QObject *parent=nullptr)
Constructor.
void updateDirectory(const QString &directory)
Update the directory.
void play(swift::misc::audio::CNotificationSounds::NotificationFlag notification, int volume=100)
Play notification sound.
static const QString & fnClick()
File names.
Definition samples.h:46
Free functions in swift::misc.
QObject(QObject *parent)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
void clear()
bool isEmpty() const const
QString join(QChar separator) const const
QueuedConnection
QUrl fromLocalFile(const QString &localFile)
bool isEmpty() const const
bool isLocalFile() const const