tModLoader v0.11.8.9
A mod to make and play Terraria mods
SoundLoader.cs
Go to the documentation of this file.
1using Microsoft.Xna.Framework.Audio;
2using System;
3using System.Collections.Generic;
4using Terraria.Audio;
5
6namespace Terraria.ModLoader
7{
11 public static class SoundLoader
12 {
13 private static readonly IDictionary<SoundType, int> nextSound = new Dictionary<SoundType, int>();
14 internal static readonly IDictionary<SoundType, IDictionary<string, int>> sounds = new Dictionary<SoundType, IDictionary<string, int>>();
15 internal static readonly IDictionary<SoundType, IDictionary<int, ModSound>> modSounds = new Dictionary<SoundType, IDictionary<int, ModSound>>();
16 internal static SoundEffect[] customSounds = new SoundEffect[0];
17 internal static SoundEffectInstance[] customSoundInstances = new SoundEffectInstance[0];
21 public const int customSoundType = 50;
22 internal static readonly IDictionary<int, int> musicToItem = new Dictionary<int, int>();
23 internal static readonly IDictionary<int, int> itemToMusic = new Dictionary<int, int>();
24 internal static readonly IDictionary<int, IDictionary<int, int>> tileToMusic = new Dictionary<int, IDictionary<int, int>>();
25
26 static SoundLoader() {
27 foreach (SoundType type in Enum.GetValues(typeof(SoundType))) {
28 nextSound[type] = GetNumVanilla(type);
29 sounds[type] = new Dictionary<string, int>();
30 modSounds[type] = new Dictionary<int, ModSound>();
31 }
32 }
33
34 internal static int ReserveSoundID(SoundType type) {
35 int reserveID = nextSound[type];
36 nextSound[type]++;
37 return reserveID;
38 }
39
40 public static int SoundCount(SoundType type) {
41 return nextSound[type];
42 }
43
47 public static int GetSoundSlot(SoundType type, string sound) {
48 if (sounds[type].ContainsKey(sound)) {
49 return sounds[type][sound];
50 }
51 else {
52 return 0;
53 }
54 }
55
56 // TODO: Should we just get rid of the soundType Enum?
57
61 internal static LegacySoundStyle GetLegacySoundSlot(SoundType type, string sound) {
62 if (sounds[type].ContainsKey(sound)) {
63 return new LegacySoundStyle((int)type, sounds[type][sound]);
64 }
65 else {
66 return null;
67 }
68 }
69
70 internal static void ResizeAndFillArrays() {
71 customSounds = new SoundEffect[nextSound[SoundType.Custom]];
72 customSoundInstances = new SoundEffectInstance[nextSound[SoundType.Custom]];
73 Array.Resize(ref Main.soundItem, nextSound[SoundType.Item]);
74 Array.Resize(ref Main.soundInstanceItem, nextSound[SoundType.Item]);
75 Array.Resize(ref Main.soundNPCHit, nextSound[SoundType.NPCHit]);
76 Array.Resize(ref Main.soundInstanceNPCHit, nextSound[SoundType.NPCHit]);
77 Array.Resize(ref Main.soundNPCKilled, nextSound[SoundType.NPCKilled]);
78 Array.Resize(ref Main.soundInstanceNPCKilled, nextSound[SoundType.NPCKilled]);
79 Array.Resize(ref Main.music, nextSound[SoundType.Music]);
80 Array.Resize(ref Main.musicFade, nextSound[SoundType.Music]);
81 foreach (SoundType type in Enum.GetValues(typeof(SoundType))) {
82 foreach (string sound in sounds[type].Keys) {
83 int slot = GetSoundSlot(type, sound);
84 if (type != SoundType.Music) {
85 GetSoundArray(type)[slot] = ModContent.GetSound(sound);
86 GetSoundInstanceArray(type)[slot] = GetSoundArray(type)[slot]?.CreateInstance() ?? null;
87 }
88 else {
89 Main.music[slot] = ModContent.GetMusic(sound) ?? null;
90 }
91 }
92 }
93 }
94
95 internal static void Unload() {
96 //for (int i = Main.maxMusic; i < Main.music.Length; i++)
97 //{
98 // Main.music[i].Stop(AudioStopOptions.Immediate);
99 //}
100 foreach (SoundType type in Enum.GetValues(typeof(SoundType))) {
101 nextSound[type] = GetNumVanilla(type);
102 sounds[type].Clear();
103 modSounds[type].Clear();
104 }
105 musicToItem.Clear();
106 itemToMusic.Clear();
107 tileToMusic.Clear();
108 }
109 //in Terraria.Main.PlaySound before checking type to play sound add
110 // if (SoundLoader.PlayModSound(type, num, num2, num3)) { return; }
111 internal static bool PlayModSound(int type, int style, float volume, float pan, ref SoundEffectInstance soundEffectInstance) {
112 SoundType soundType;
113 switch (type) {
114 case 2:
115 soundType = SoundType.Item;
116 break;
117 case 3:
118 soundType = SoundType.NPCHit;
119 break;
120 case 4:
121 soundType = SoundType.NPCKilled;
122 break;
123 case customSoundType:
124 soundType = SoundType.Custom;
125 break;
126 default:
127 return false;
128 }
129 if (!modSounds[soundType].ContainsKey(style)) {
130 return false;
131 }
132 soundEffectInstance = modSounds[soundType][style].PlaySound(ref GetSoundInstanceArray(soundType)[style], volume, pan, soundType);
133 return true;
134 }
135
136 internal static int GetNumVanilla(SoundType type) {
137 switch (type) {
138 case SoundType.Custom:
139 return 0;
140 case SoundType.Item:
141 return Main.maxItemSounds + 1;
142 case SoundType.NPCHit:
143 return Main.maxNPCHitSounds + 1;
144 case SoundType.NPCKilled:
145 return Main.maxNPCKilledSounds + 1;
146 case SoundType.Music:
147 return Main.maxMusic;
148 }
149 return 0;
150 }
151
152 internal static SoundEffect[] GetSoundArray(SoundType type) {
153 switch (type) {
154 case SoundType.Custom:
155 return customSounds;
156 case SoundType.Item:
157 return Main.soundItem;
158 case SoundType.NPCHit:
159 return Main.soundNPCHit;
160 case SoundType.NPCKilled:
161 return Main.soundNPCKilled;
162 }
163 return null;
164 }
165
166 internal static SoundEffectInstance[] GetSoundInstanceArray(SoundType type) {
167 switch (type) {
168 case SoundType.Custom:
169 return customSoundInstances;
170 case SoundType.Item:
171 return Main.soundInstanceItem;
172 case SoundType.NPCHit:
173 return Main.soundInstanceNPCHit;
174 case SoundType.NPCKilled:
175 return Main.soundInstanceNPCKilled;
176 }
177 return null;
178 }
179 }
180}
This class is used to keep track of and support the existence of custom sounds that have been added t...
Definition: SoundLoader.cs:12
const int customSoundType
This value should be passed as the first parameter to Main.PlaySound whenever you want to play a cust...
Definition: SoundLoader.cs:21
static readonly IDictionary< SoundType, int > nextSound
Definition: SoundLoader.cs:13
static int GetSoundSlot(SoundType type, string sound)
Returns the style (last parameter passed to Main.PlaySound) of the sound corresponding to the given S...
Definition: SoundLoader.cs:47
static int SoundCount(SoundType type)
Definition: SoundLoader.cs:40
SoundType
This is an enum of the types of sound you can add to the game. This is used for determining whether a...
Definition: SoundType.cs:13