VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
SoundManager.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SOUNDMANAGER_H
18 #define SOUNDMANAGER_H
19 
20 #include <QMediaPlayer>
21 #include <QObject>
22 #include <QSoundEffect>
23 #include <QString>
24 #include <QUrl>
25 #include "SoundEffectInstance.h"
26 
39 class SoundManager : public QObject {
40  Q_OBJECT
41 
45  Q_PROPERTY(bool bgmMuted READ isBGMMuted WRITE setBGMMuted NOTIFY bgmMutedChanged)
51  Q_PROPERTY(bool paused READ isPaused WRITE setPaused NOTIFY pausedChanged)
52 
53 public:
59  Q_ENUMS(BGMPriority)
60  enum BGMPriority {
77  };
78 
84  explicit SoundManager(QObject* parent = nullptr);
85 
89  bool isBGMMuted() const;
94  void setBGMMuted(bool value);
98  bool isPaused() const { return mPaused; }
106  void setPaused(bool value);
107 
125  Q_INVOKABLE void playSoundEffect(const QString& path);
126 
135  Q_INVOKABLE void clearBGMTrack(BGMPriority priority);
141  Q_INVOKABLE void setBGMTrack(BGMPriority priority, const QString& path);
142 
143 signals:
147  void bgmMutedChanged();
151  void pausedChanged();
152 
153 private slots:
154  void onBGMMediaStatusChanged(QMediaPlayer::MediaStatus status);
155 
156 private:
157  void updateCurrentBGMTrack();
158  void pauseAll();
159  void resumeAll();
160  void applyVolume();
161  QSharedPointer<QSoundEffect> cacheSoundEffect(const QString& path, bool lookupOnly);
162 
163  QMediaPlayer mBGMPlayer;
164  QMap<BGMPriority, QUrl> mBGMTracks;
165  QUrl mCurrentBGMTrack;
167 
168  bool mBGMMuted = false;
169  bool mPaused = true;
170 
171  constexpr static const float BASE_MUSIC_GAIN = 0.30f;
172  constexpr static const int QMEDIA_PLAYER_GAIN_FACTOR = 100;
173 };
174 Q_DECLARE_METATYPE(SoundManager*)
175 
176 #endif // SOUNDMANAGER_H
Q_INVOKABLE void clearBGMTrack(BGMPriority priority)
Clears the audio track of the specified background music priority level.
An instance of a sound effect.
Definition: SoundEffectInstance.h:36
Represents the highest priority level an audio track can have.
Definition: SoundManager.h:76
bool isPaused() const
Returns paused.
Definition: SoundManager.h:98
void pausedChanged()
Emitted when paused changes.
SoundManager(QObject *parent=nullptr)
Constructs a SoundManager.
Represents an invalid priority level and serves as a lower bound on priority.
Definition: SoundManager.h:64
bool isBGMMuted() const
Returns bgmMuted.
Represents a priority level for audio playing in game Environments.
Definition: SoundManager.h:72
Controls the sound effects and background music (BGM) audio tracks needed to be played during the gam...
Definition: SoundManager.h:39
Represents a priority level for audio playing on menus.
Definition: SoundManager.h:68
BGMPriority
Ordered priority levels for background music tracks.
Definition: SoundManager.h:60
void bgmMutedChanged()
Emitted when bgmMuted changes.
void setBGMMuted(bool value)
Sets bgmMuted.
void setPaused(bool value)
Sets paused.
Q_INVOKABLE void playSoundEffect(const QString &path)
Plays a one-shot sound effect.
bool paused
Whether or not the SoundManager is currently paused (i.e. not playing audio).
Definition: SoundManager.h:51
SoundEffectInstance getSoundEffect(const QString &path)
Loads a sound effect with sample data obtained from file.
bool bgmMuted
Whether or not the background music is currently muted.
Definition: SoundManager.h:45
Q_INVOKABLE void setBGMTrack(BGMPriority priority, const QString &path)
Sets the audio track of the specified background priority level.
QObject * parent() const