Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SoundEngine.cs
Go to the documentation of this file.
1using System;
2using System.IO;
4using Microsoft.Xna.Framework.Audio;
6
7namespace Terraria.Audio;
8
9public static class SoundEngine
10{
12
13 public static SoundPlayer SoundPlayer;
14
15 public static bool AreSoundsPaused;
16
17 public static bool IsAudioSupported { get; private set; }
18
19 public static void Initialize()
20 {
22 }
23
24 public static void Load(IServiceProvider services)
25 {
27 {
30 }
31 }
32
33 public static void Update()
34 {
36 {
37 if (Main.audioSystem != null)
38 {
39 Main.audioSystem.UpdateAudioEngine();
40 }
42 bool flag = (!Main.hasFocus || Main.gamePaused) && Main.netMode == 0;
43 if (!AreSoundsPaused && flag)
44 {
46 }
47 else if (AreSoundsPaused && !flag)
48 {
50 }
51 AreSoundsPaused = flag;
53 }
54 }
55
56 public static void Reload()
57 {
59 {
60 if (LegacySoundPlayer != null)
61 {
63 }
64 if (SoundPlayer != null)
65 {
67 }
68 }
69 }
70
71 public static void PlaySound(int type, Vector2 position, int style = 1)
72 {
73 PlaySound(type, (int)position.X, (int)position.Y, style);
74 }
75
76 public static SoundEffectInstance PlaySound(LegacySoundStyle type, Vector2 position)
77 {
78 return PlaySound(type, (int)position.X, (int)position.Y);
79 }
80
81 public static SoundEffectInstance PlaySound(LegacySoundStyle type, int x = -1, int y = -1)
82 {
83 if (type == null)
84 {
85 return null;
86 }
87 return PlaySound(type.SoundId, x, y, type.Style, type.Volume, type.GetRandomPitch());
88 }
89
90 public static SoundEffectInstance PlaySound(int type, int x = -1, int y = -1, int Style = 1, float volumeScale = 1f, float pitchOffset = 0f)
91 {
93 {
94 return null;
95 }
96 return LegacySoundPlayer.PlaySound(type, x, y, Style, volumeScale, pitchOffset);
97 }
98
100 {
101 //IL_0015: Unknown result type (might be due to invalid IL or missing references)
103 {
104 return null;
105 }
106 return SoundPlayer.GetActiveSound(id);
107 }
108
109 public static SlotId PlayTrackedSound(SoundStyle style, Vector2 position)
110 {
111 //IL_000e: Unknown result type (might be due to invalid IL or missing references)
112 //IL_001b: Unknown result type (might be due to invalid IL or missing references)
114 {
115 return SlotId.Invalid;
116 }
117 return SoundPlayer.Play(style, position);
118 }
119
120 public static SlotId PlayTrackedLoopedSound(SoundStyle style, Vector2 position, ActiveSound.LoopedPlayCondition loopingCondition = null)
121 {
122 //IL_000e: Unknown result type (might be due to invalid IL or missing references)
123 //IL_001c: Unknown result type (might be due to invalid IL or missing references)
125 {
126 return SlotId.Invalid;
127 }
128 return SoundPlayer.PlayLooped(style, position, loopingCondition);
129 }
130
131 public static SlotId PlayTrackedSound(SoundStyle style)
132 {
133 //IL_000e: Unknown result type (might be due to invalid IL or missing references)
134 //IL_001a: Unknown result type (might be due to invalid IL or missing references)
136 {
137 return SlotId.Invalid;
138 }
139 return SoundPlayer.Play(style);
140 }
141
142 public static void StopTrackedSounds()
143 {
145 {
147 }
148 }
149
150 public static SoundEffect GetTrackableSoundByStyleId(int id)
151 {
153 {
154 return null;
155 }
157 }
158
159 public static void StopAmbientSounds()
160 {
162 {
164 }
165 }
166
168 {
170 {
171 return null;
172 }
173 return SoundPlayer.FindActiveSound(style);
174 }
175
176 private static bool TestAudioSupport()
177 {
178 byte[] buffer = new byte[166]
179 {
180 82, 73, 70, 70, 158, 0, 0, 0, 87, 65,
181 86, 69, 102, 109, 116, 32, 16, 0, 0, 0,
182 1, 0, 1, 0, 68, 172, 0, 0, 136, 88,
183 1, 0, 2, 0, 16, 0, 76, 73, 83, 84,
184 26, 0, 0, 0, 73, 78, 70, 79, 73, 83,
185 70, 84, 14, 0, 0, 0, 76, 97, 118, 102,
186 53, 54, 46, 52, 48, 46, 49, 48, 49, 0,
187 100, 97, 116, 97, 88, 0, 0, 0, 0, 0,
188 126, 4, 240, 8, 64, 13, 95, 17, 67, 21,
189 217, 24, 23, 28, 240, 30, 94, 33, 84, 35,
190 208, 36, 204, 37, 71, 38, 64, 38, 183, 37,
191 180, 36, 58, 35, 79, 33, 1, 31, 86, 28,
192 92, 25, 37, 22, 185, 18, 42, 15, 134, 11,
193 222, 7, 68, 4, 196, 0, 112, 253, 86, 250,
194 132, 247, 6, 245, 230, 242, 47, 241, 232, 239,
195 25, 239, 194, 238, 231, 238, 139, 239, 169, 240,
196 61, 242, 67, 244, 180, 246
197 };
198 try
199 {
201 SoundEffect.FromStream(stream);
202 }
203 catch (NoAudioHardwareException)
204 {
205 Console.WriteLine("No audio hardware found. Disabling all audio.");
206 return false;
207 }
208 catch
209 {
210 return false;
211 }
212 return true;
213 }
214}
static void WriteLine()
Definition Console.cs:733
delegate bool LoopedPlayCondition()
SoundEffect GetTrackableSoundByStyleId(int id)
SoundEffectInstance PlaySound(int type, int x=-1, int y=-1, int Style=1, float volumeScale=1f, float pitchOffset=0f)
static bool TestAudioSupport()
static SoundPlayer SoundPlayer
static SoundEffectInstance PlaySound(LegacySoundStyle type, Vector2 position)
static SlotId PlayTrackedLoopedSound(SoundStyle style, Vector2 position, ActiveSound.LoopedPlayCondition loopingCondition=null)
static void Load(IServiceProvider services)
static SlotId PlayTrackedSound(SoundStyle style, Vector2 position)
static void PlaySound(int type, Vector2 position, int style=1)
static LegacySoundPlayer LegacySoundPlayer
static void StopAmbientSounds()
static ActiveSound GetActiveSound(SlotId id)
static ActiveSound FindActiveSound(SoundStyle style)
static SoundEffectInstance PlaySound(LegacySoundStyle type, int x=-1, int y=-1)
static SoundEffectInstance PlaySound(int type, int x=-1, int y=-1, int Style=1, float volumeScale=1f, float pitchOffset=0f)
static SoundEffect GetTrackableSoundByStyleId(int id)
static void StopTrackedSounds()
static SlotId PlayTrackedSound(SoundStyle style)
ActiveSound FindActiveSound(SoundStyle style)
SlotId Play(SoundStyle style, Vector2 position)
SlotId PlayLooped(SoundStyle style, Vector2 position, ActiveSound.LoopedPlayCondition loopingCondition)
ActiveSound GetActiveSound(SlotId id)
static IAudioSystem audioSystem
Definition Main.cs:1415
static int netMode
Definition Main.cs:2095
static bool dedServ
Definition Main.cs:1226
static bool gamePaused
Definition Main.cs:1072
static readonly SlotId Invalid
Definition SlotId.cs:5