ScummVM API documentation
sound.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef NANCY_SOUND_H
23 #define NANCY_SOUND_H
24 
25 #include "engines/nancy/commontypes.h"
26 
27 #include "audio/mixer.h"
28 
29 namespace Common {
30 class SeekableReadStream;
31 }
32 
33 namespace Audio {
34 class SeekableAudioStream;
35 class QueuingAudioStream;
36 }
37 
38 namespace Nancy {
39 
40 class IFF;
41 class NancyConsole;
42 class NancyEngine;
43 
44 class SoundManager {
45  friend class NancyConsole;
46 public:
47  // Settings for playing a sound, used in nancy3 and up
48  // Older versions had a different, non-bitflag enum, but testing
49  // indicates those were never actually implemented
50  enum PlayCommandFlags {
51  kPlaySequential = 0x0001, // Play normally
52  kPlaySequentialPosition = 0x0003, // Play at fixed position in 3D space
53  kPlaySequentialFrameAnchor = 0x0007, // Position in 3D space is tied to a background frame, ignoring 3D coordinates
54 
55  kPlayRandomTime = 0x0010, // Play at random time intervals
56  kPlayRandomPosition = 0x0020, // Play at random 3D positions
57 
58  kPlayMoveLinear = 0x0100, // Move sound position in 3D space. The movement is linear unless kPlayMoveCircular is also set
59  kPlayMoveCircular = 0x0300, // Move sound position in a circular direction (see SoundRotationAxis)
60  kPlayRandomMove = 0x0500 // Move along random vector. Does not combine with kPlayMoveCircular
61  };
62 
63  SoundManager();
64  ~SoundManager();
65 
66  void loadCommonSounds(IFF *boot);
67  void initSoundChannels();
68 
69  // Load a sound into a channel without starting it
70  void loadSound(const SoundDescription &description, SoundEffectDescription **effectData = nullptr, bool forceReload = false);
71 
72  void playSound(uint16 channelID);
73  void playSound(const SoundDescription &description);
74  void playSound(const Common::String &chunkName);
75 
76  void pauseSound(uint16 channelID, bool pause);
77  void pauseSound(const SoundDescription &description, bool pause);
78  void pauseSound(const Common::String &chunkName, bool pause);
79  void pauseAllSounds(bool pause);
80 
81  bool isSoundPlaying(uint16 channelID) const;
82  bool isSoundPlaying(const SoundDescription &description) const;
83  bool isSoundPlaying(const Common::String &chunkName) const;
84 
85  void stopSound(uint16 channelID);
86  void stopSound(const SoundDescription &description);
87  void stopSound(const Common::String &chunkName);
88  void stopAllSounds();
89 
90  byte getVolume(uint16 channelID);
91  byte getVolume(const SoundDescription &description);
92  byte getVolume(const Common::String &chunkName);
93 
94  void setVolume(uint16 channelID, uint16 volume);
95  void setVolume(const SoundDescription &description, uint16 volume);
96  void setVolume(const Common::String &chunkName, uint16 volume);
97 
98  uint32 getRate(uint16 channelID);
99  uint32 getRate(const SoundDescription &description);
100  uint32 getRate(const Common::String &chunkName);
101 
102  uint32 getBaseRate(uint16 channelID);
103  uint32 getBaseRate(const SoundDescription &description);
104  uint32 getBaseRate(const Common::String &chunkName);
105 
106  void setRate(uint16 channelID, uint32 rate);
107  void setRate(const SoundDescription &description, uint32 rate);
108  void setRate(const Common::String &chunkName, uint32 rate);
109 
110  Audio::Timestamp getLength(uint16 channelID);
111  Audio::Timestamp getLength(const SoundDescription &description);
112  Audio::Timestamp getLength(const Common::String &chunkName);
113 
114  void soundEffectMaintenance();
115  void recalculateSoundEffects();
116 
117  // Used when changing scenes
118  void stopAndUnloadSceneSpecificSounds();
119  void pauseSceneSpecificSounds(bool pause);
120 
121  static Audio::SeekableAudioStream *makeHISStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, uint32 overrideSamplesPerSec = 0);
122 
123 protected:
124  struct Channel {
125  ~Channel();
126  Common::String name;
127  Audio::Mixer::SoundType type = Audio::Mixer::SoundType::kPlainSoundType;
128  uint16 playCommands = 1;
129  uint16 numLoops = 0;
130  uint volume = 0;
131  uint16 panAnchorFrame = 0;
132  bool isPanning = false;
133  Audio::SeekableAudioStream *stream = nullptr;
134  Audio::AudioStream *streamForMixer = nullptr;
135  Audio::SoundHandle handle;
136  bool isPersistent = false;
137 
138  // Sound effect data, not applicable to nancy2 and below
139  SoundEffectDescription *effectData = nullptr;
140  Math::Vector3d position;
141  Math::Vector3d positionDelta;
142  uint32 nextStepTime = 0;
143  uint16 stepsLeft = 0;
144  uint32 nextRepeatTime = 0;
145  };
146 
147  void soundEffectMaintenance(uint16 channelID, bool force = false);
148 
149  Audio::Mixer *_mixer;
150 
151  Common::Array<Channel> _channels;
153 
154  bool _shouldRecalculate;
155 
156  Math::Vector3d _orientation;
157  Math::Vector3d _position;
158  uint _positionLerp = 0;
159 };
160 
161 } // End of namespace Nancy
162 
163 #endif // NANCY_SOUND_H
Definition: str.h:59
Definition: array.h:52
Definition: timestamp.h:83
Definition: stream.h:745
Definition: commontypes.h:217
Definition: audiostream.h:212
Definition: mixer.h:49
SoundType
Definition: mixer.h:62
Definition: mixer.h:59
Definition: hashmap.h:85
Definition: algorithm.h:29
Definition: audiostream.h:50
Definition: sound.h:44
Definition: commontypes.h:254
Definition: sound.h:124
Definition: console.h:36
Definition: iff.h:37
Definition: system.h:38
Definition: actionmanager.h:32