Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
AudioSystem.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;
6 using SiliconStudio.Core.Collections;
7 using SiliconStudio.Paradox.Audio;
8 using SiliconStudio.Paradox.Games;
9 
10 namespace SiliconStudio.Paradox.Engine
11 {
12  /// <summary>
13  /// The Audio System.
14  /// It creates an underlying instance of <see cref="AudioEngine"/>.
15  /// </summary>
16  public class AudioSystem : GameSystem
17  {
18  /// <summary>
19  /// Create an new instance of AudioSystem
20  /// </summary>
21  /// <param name="registry">The service registry in which to register the <see cref="AudioSystem"/> services</param>
22  public AudioSystem(IServiceRegistry registry)
23  : base(registry)
24  {
25  Enabled = true;
26  AudioEngine = new AudioEngine();
27 
28  registry.AddService(typeof(AudioSystem), this);
29  }
30 
31  /// <summary>
32  /// The underlying <see cref="AudioEngine" />. This instance can be used to possibly create <see cref="DynamicSoundEffectInstance" />.
33  /// </summary>
34  /// <value>The audio engine.</value>
35  public AudioEngine AudioEngine { get; private set; }
36 
37  /// <summary>
38  /// A collection containing the <see cref="AudioListenerComponent"/>-<see cref="AudioListener"/> associations.
39  /// The AudioListenerComponent keys are added/removed by the user by calls to <see cref="AddListener"/>/<see cref="RemoveListener"/>.
40  /// The AudioListener values are created/updated by the <see cref="AudioListenerProcessor"/>.
41  /// </summary>
42  /// <remarks>When a AudioListenerComponent is added to the AudioSystem but not present in the Entity System,
43  /// a valid AudioListener can not be computed. Thus we set its value to 'null'.</remarks>
44  internal readonly TrackingDictionary<AudioListenerComponent, AudioListener> Listeners = new TrackingDictionary<AudioListenerComponent, AudioListener>();
45 
46  public override void Initialize()
47  {
48  base.Initialize();
49 
50  Game.Activated += OnActivated;
51  Game.Deactivated += OnDeactivated;
52  }
53 
54  /// <summary>
55  /// Add and activate a <see cref="AudioListenerComponent" /> to the Audio System.
56  /// After this call sounds played via <see cref="AudioEmitterSoundController" />s will be heard by this listener.
57  /// </summary>
58  /// <param name="listener">The listener to add to the audio system.</param>
59  /// <remarks>Adding a listener already added as no effects.</remarks>
60  public void AddListener(AudioListenerComponent listener)
61  {
62  if(!Listeners.ContainsKey(listener))
63  Listeners[listener] = null;
64  }
65 
66  /// <summary>
67  /// Remove a <see cref="AudioListenerComponent" /> from the Audio System.
68  /// After this call sounds played via <see cref="AudioEmitterSoundController" />s will not be heard by this listener anymore.
69  /// </summary>
70  /// <param name="listener">The listener to remove from the audio system.</param>
71  /// <exception cref="System.ArgumentException">The provided listener was not present in the Audio System.</exception>
72  public void RemoveListener(AudioListenerComponent listener)
73  {
74  if(!Listeners.ContainsKey(listener))
75  throw new ArgumentException("The provided listener was not present in the Audio System.");
76 
77  Listeners.Remove(listener);
78  }
79 
80  public override void Update(GameTime gameTime)
81  {
82  base.Update(gameTime);
83 
84  AudioEngine.Update();
85  }
86 
87  // called on dispose
88  protected override void Destroy()
89  {
90  Game.Activated -= OnActivated;
91  Game.Deactivated -= OnDeactivated;
92 
93  base.Destroy();
94 
95  AudioEngine.Dispose();
96  AudioEngine = null;
97  }
98 
99  private void OnActivated(object sender, EventArgs e)
100  {
101  // resume the audio
102  AudioEngine.ResumeAudio();
103  }
104 
105  private void OnDeactivated(object sender, EventArgs e)
106  {
107  // pause the audio
108  AudioEngine.PauseAudio();
109  AudioEngine.Update(); // force the update of the audio to pause the Musics
110  }
111 
112  }
113 }
override void Destroy()
Disposes of object resources.
Definition: AudioSystem.cs:88
Component representing an audio listener.
override void Update(GameTime gameTime)
This method is called when this game component is updated.
Definition: AudioSystem.cs:80
A service registry is a IServiceProvider that provides methods to register and unregister services...
void AddListener(AudioListenerComponent listener)
Add and activate a AudioListenerComponent to the Audio System. After this call sounds played via Audi...
Definition: AudioSystem.cs:60
Represents the audio engine. In current version, the audio engine necessarily creates its context on ...
Definition: AudioEngine.cs:80
Current timing used for variable-step (real time) or fixed-step (game time) games.
Definition: GameTime.cs:31
void RemoveListener(AudioListenerComponent listener)
Remove a AudioListenerComponent from the Audio System. After this call sounds played via AudioEmitter...
Definition: AudioSystem.cs:72
AudioSystem(IServiceRegistry registry)
Create an new instance of AudioSystem
Definition: AudioSystem.cs:22
The Audio System. It creates an underlying instance of AudioEngine.
Definition: AudioSystem.cs:16
override void Initialize()
This method is called when the component is added to the game.
Definition: AudioSystem.cs:46