ScummVM API documentation
sound_clip.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 //=============================================================================
23 //
24 // ACSOUND - AGS sound system wrapper
25 //
26 //=============================================================================
27 
28 #ifndef AGS_ENGINE_MEDIA_AUDIO_SOUNDCLIP_H
29 #define AGS_ENGINE_MEDIA_AUDIO_SOUNDCLIP_H
30 
31 #include "audio/mixer.h"
32 #include "audio/audiostream.h"
33 #include "common/stream.h"
34 
35 namespace AGS3 {
36 
37 // SOUNDCLIP's state and parameter updates sync with the audio core in
38 // batches, only when the engine updates the game, never while the user script
39 // is being executed. The sync is performed by calling update().
40 // This is to ensure that the clip reference, state and properties don't change
41 // in the middle of the script's command sequence.
42 
43 // TODO: one of the biggest problems with sound clips currently is that it
44 // provides several methods of applying volume, which may ignore or override
45 // each other, and does not shape a consistent interface.
46 // Improving this situation is only possible with massive refactory of
47 // sound clip use, taking backwards-compatible audio system in account.
48 
49 enum SoundClipState {
50  SoundClipInitial, SoundClipPlaying, SoundClipPaused, SoundClipStopped
51 };
52 
53 struct SOUNDCLIP {
54  SOUNDCLIP();
55  virtual ~SOUNDCLIP() {}
56 
57  // TODO: move these to private
58  int _sourceClipID;
59  int _sourceClipType;
60  bool _repeat;
61  int _priority;
62  int _xSource, _ySource; // Used for positioning sounds in game rooms
63  int _maximumPossibleDistanceAway;
64 
65  int _vol255;
66  int _vol100;
67  int _volModifier;
68  int _panning;
69  int _panningAsPercentage;
70  int _directionalVolModifier;
71 
72  virtual int play() = 0;
73  virtual void pause() = 0;
74  virtual void resume() = 0;
75 
83  virtual void seek(int offset) = 0;
84 
88  virtual void seek_ms(int pos_ms) = 0;
89 
90  virtual int play_from(int position) = 0;
91  virtual bool is_playing() = 0; // true if playing or paused. false if never played or stopped.
92  virtual bool is_paused() = 0; // true if paused
93 
97  virtual int get_sound_type() const = 0;
98 
102  virtual int get_pos() = 0;
103 
107  virtual int get_pos_ms() = 0;
108 
112  virtual int get_length_ms() = 0;
113 
114  virtual void set_panning(int newPanning) = 0;
115  virtual void set_speed(int new_speed) = 0;
116  virtual void poll() = 0;
117 
122  inline int get_volume100() const { return _vol100; }
123 
127  inline int get_volume255() const { return _vol255; }
128 
132  inline int get_panning() const { return _panning; }
133 
137  inline int get_speed() const { return _speed; }
138 
142  inline bool is_muted() const { return _muted; }
143 
147  void set_volume100(int volume);
148 
152  void set_volume255(int volume);
153 
159  void set_volume_direct(int vol_percent, int vol_absolute);
160 
166  void set_mute(bool mute);
167 
173  void apply_volume_modifier(int mod);
174 
180  void apply_directional_modifier(int mod);
181 
182  inline bool is_ready() { return is_playing(); }
183 
187  bool update();
188 
189 protected:
190  virtual void adjust_volume() = 0;
191 
192  // mute mode overrides the volume; if set, any volume assigned is stored
193  // in properties, but not applied to playback itself
194  bool _muted = false;
195 
196  // speed of playback, in clip ms per real second
197  int _speed = 0;
198 
199  bool _paramsChanged = false;
200 
201  // helper function for calculating volume with applied modifiers
202  inline int get_final_volume() const {
203  int final_vol = _vol255 + _volModifier + _directionalVolModifier;
204  return final_vol >= 0 ? final_vol : 0;
205  }
206 };
207 
208 struct SoundClipWaveBase : public SOUNDCLIP {
209 private:
211 
212  int pos_to_posms(int pos) const;
213 
214 public:
215  Audio::Mixer *_mixer;
216  Audio::AudioStream *_stream;
217  Audio::SoundHandle _soundHandle;
218  SoundClipState _state;
219  bool _waitingToPlay = false;
220 
221  SoundClipWaveBase(Audio::AudioStream *stream, bool repeat = false);
222  ~SoundClipWaveBase() override;
223 
224  void setType(Audio::Mixer::SoundType type);
225 
226  void poll() override;
227  int play() override;
228  int play_from(int position) override;
229  void pause() override;
230  void resume() override;
231  bool is_playing() override;
232  bool is_paused() override;
233  void seek(int offset) override;
234  void seek_ms(int pos_ms) override;
235  int get_pos() override;
236  int get_pos_ms() override;
237  int get_length_ms() override;
238  void set_panning(int newPanning) override;
239  void set_speed(int new_speed) override;
240  void adjust_volume() override;
241 };
242 
243 template<int SOUND_TYPE>
245  SoundClipWave(Audio::AudioStream *stream, bool repeat = false) :
246  SoundClipWaveBase(stream, repeat) {}
247  int get_sound_type() const {
248  return SOUND_TYPE;
249  }
250 };
251 
252 } // namespace AGS3
253 
254 #endif
void apply_volume_modifier(int mod)
virtual int get_sound_type() const =0
void set_volume_direct(int vol_percent, int vol_absolute)
virtual int get_length_ms()=0
int get_volume255() const
Definition: sound_clip.h:127
bool is_muted() const
Definition: sound_clip.h:142
int get_volume100() const
Definition: sound_clip.h:122
void set_volume100(int volume)
Definition: sound_clip.h:208
Definition: mixer.h:49
Definition: sound_clip.h:53
SoundType
Definition: mixer.h:62
Definition: mixer.h:59
int get_sound_type() const
Definition: sound_clip.h:247
virtual int get_pos_ms()=0
Definition: sound_clip.h:244
Definition: audiostream.h:50
virtual void seek_ms(int pos_ms)=0
int get_speed() const
Definition: sound_clip.h:137
int get_panning() const
Definition: sound_clip.h:132
virtual void seek(int offset)=0
void apply_directional_modifier(int mod)
virtual int get_pos()=0
void set_volume255(int volume)
Definition: ags.h:40
Definition: mixer.h:63
void set_mute(bool mute)