Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
SoundInstanceBase.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 
5 using SiliconStudio.Core.Mathematics;
6 
7 namespace SiliconStudio.Paradox.Audio
8 {
9  /// <summary>
10  /// Base class for sound that creates voices
11  /// </summary>
12  public abstract class SoundInstanceBase: SoundBase, IPlayableSound
13  {
14  internal SoundInstanceBase(AudioEngine engine)
15  : base(engine)
16  {
17  DataBufferLoaded = false;
18  PlayState = SoundPlayState.Stopped;
19  }
20 
21  #region Buffer Management
22 
23  internal bool DataBufferLoaded;
24 
25  private void CheckBufferNotLoaded(string msg)
26  {
27  if (DataBufferLoaded)
28  throw new InvalidOperationException(msg);
29  }
30 
31  #endregion
32 
33  protected override void Destroy()
34  {
35  base.Destroy();
36 
37  if (IsDisposed)
38  return;
39 
40  Stop();
41  DestroyImpl();
42  }
43 
44  internal abstract void DestroyImpl();
45 
46  #region IPlayableSound
47 
48  public virtual float Volume
49  {
50  get
51  {
52  CheckNotDisposed();
53  return volume;
54  }
55  set
56  {
57  CheckNotDisposed();
58  volume = MathUtil.Clamp(value, 0, 1);
59 
60  if(EngineState != AudioEngineState.Invalidated)
61  UpdateVolume();
62  }
63  }
64  private float volume;
65  internal abstract void UpdateVolume();
66 
67 
68  public virtual bool IsLooped
69  {
70  get
71  {
72  CheckNotDisposed();
73  return isLooped;
74  }
75  set
76  {
77  CheckNotDisposed();
78  CheckBufferNotLoaded("The looping status of the sound can not be modified after it started playing.");
79  isLooped = value;
80 
81  if (EngineState != AudioEngineState.Invalidated)
82  UpdateLooping();
83  }
84  }
85 
86  private bool isLooped;
87  internal abstract void UpdateLooping();
88 
89 
90  public virtual SoundPlayState PlayState { get; internal set; }
91 
92  public virtual void Play()
93  {
94  PlayExtended(true);
95  }
96 
97  /// <summary>
98  /// Play or resume the current sound instance with extended parameters.
99  /// </summary>
100  /// <param name="stopSiblingInstances">Indicate if the sibling instances should be stopped or not</param>
101  protected void PlayExtended(bool stopSiblingInstances)
102  {
103  CheckNotDisposed();
104 
105  if (EngineState == AudioEngineState.Invalidated)
106  return;
107 
108  if (AudioEngine.State == AudioEngineState.Paused) // drop the call to play if the audio engine is paused.
109  return;
110 
111  if (PlayState == SoundPlayState.Playing)
112  return;
113 
114  if (stopSiblingInstances)
115  StopConcurrentInstances();
116 
117  if (!DataBufferLoaded)
118  LoadBuffer();
119 
120  PlayImpl();
121 
122  DataBufferLoaded = true;
123 
124  PlayState = SoundPlayState.Playing;
125  }
126 
127  /// <summary>
128  /// Stops the sound instances in competition with this sound instance.
129  /// </summary>
130  protected virtual void StopConcurrentInstances()
131  {
132  // does nothing by default should be override
133  }
134 
135  internal virtual void LoadBuffer()
136  {
137  // does nothing by default should be override
138  }
139 
140  internal abstract void PlayImpl();
141 
142 
143  public virtual void Pause()
144  {
145  CheckNotDisposed();
146 
147  if (EngineState == AudioEngineState.Invalidated)
148  return;
149 
150  if(PlayState != SoundPlayState.Playing)
151  return;
152 
153  PauseImpl();
154 
155  PlayState = SoundPlayState.Paused;
156  }
157  internal abstract void PauseImpl();
158 
159 
160  public virtual void Stop()
161  {
162  CheckNotDisposed();
163 
164  if (EngineState == AudioEngineState.Invalidated)
165  return;
166 
167  if (PlayState == SoundPlayState.Stopped)
168  return;
169 
170  StopImpl();
171 
172  DataBufferLoaded = false;
173 
174  PlayState = SoundPlayState.Stopped;
175  }
176  internal abstract void StopImpl();
177 
178 
179  public virtual void ExitLoop()
180  {
181  CheckNotDisposed();
182 
183  if (EngineState == AudioEngineState.Invalidated)
184  return;
185 
186  if (PlayState == SoundPlayState.Stopped || IsLooped == false)
187  return;
188 
189  ExitLoopImpl();
190  }
191  internal abstract void ExitLoopImpl();
192 
193  #endregion
194  }
195 }
AudioEngineState
Describe the possible states of the AudioEngine.
virtual void Play()
Start or resume playing the sound.
void PlayExtended(bool stopSiblingInstances)
Play or resume the current sound instance with extended parameters.
SoundPlayState
Current state (playing, paused, or stopped) of a sound implementing the IPlayableSound interface...
virtual void StopConcurrentInstances()
Stops the sound instances in competition with this sound instance.
AudioEngineState State
The current state of the AudioEngine.
Definition: AudioEngine.cs:153
Base class for sound that creates voices
Represents the audio engine. In current version, the audio engine necessarily creates its context on ...
Definition: AudioEngine.cs:80
Base class for all the sounds and sound instances.
Definition: SoundBase.cs:15
virtual void Stop()
Stop playing the sound immediately and reset the sound to the beginning of the track.
override void Destroy()
Disposes of object resources.
Interface for a playable sound. A playable sound can loop (ref IsLooped), be played (ref Play)...
virtual void ExitLoop()
Stop looping. That is, do not start over at the end of the current loop, continue to play until the e...
virtual void Pause()
Pause the sounds.