Organized the sound effect initialization

master
shoaib11120 2022-01-21 12:28:04 +05:30
parent e916e27427
commit b611b01d9a
5 changed files with 88 additions and 157 deletions

View File

@ -0,0 +1,65 @@
using Microsoft.Xna.Framework.Audio;
using StardewModdingAPI;
using StardewValley;
namespace stardew_access
{
internal class CustomSoundEffects
{
internal enum TYPE{
Sound,
Footstep
}
internal static void Initialize(IModHelper helper)
{
try
{
Dictionary<String, TYPE> soundEffects = new Dictionary<String, TYPE>();
soundEffects.Add("drop_item", TYPE.Sound);
soundEffects.Add("colliding", TYPE.Sound);
soundEffects.Add("npc_top", TYPE.Footstep);
soundEffects.Add("npc_right", TYPE.Footstep);
soundEffects.Add("npc_left", TYPE.Footstep);
soundEffects.Add("npc_bottom", TYPE.Footstep);
soundEffects.Add("obj_top", TYPE.Footstep);
soundEffects.Add("obj_right", TYPE.Footstep);
soundEffects.Add("obj_left", TYPE.Footstep);
soundEffects.Add("obj_bottom", TYPE.Footstep);
for(int i = 0; i < soundEffects.Count; i++)
{
KeyValuePair<String, TYPE> soundEffect = soundEffects.ElementAt(i);
CueDefinition cueDefinition = new CueDefinition();
cueDefinition.name = soundEffect.Key;
if (soundEffect.Value == TYPE.Sound)
{
cueDefinition.instanceLimit = 1;
cueDefinition.limitBehavior = CueDefinition.LimitBehavior.ReplaceOldest;
}
SoundEffect effect;
string filePath = Path.Combine(helper.DirectoryPath, $"sounds/{soundEffect.Key}.wav");
using (FileStream stream = new(filePath, FileMode.Open))
{
effect = SoundEffect.FromStream(stream);
}
if(soundEffect.Value == TYPE.Sound)
cueDefinition.SetSound(effect, Game1.audioEngine.GetCategoryIndex("Sound"), false);
else if(soundEffect.Value == TYPE.Footstep)
cueDefinition.SetSound(effect, Game1.audioEngine.GetCategoryIndex("Footsteps"), false);
Game1.soundBank.AddCue(cueDefinition);
}
}
catch (Exception e)
{
MainClass.monitor.Log($"Unable to initialize custom sounds:\n{e.Message}\n{e.StackTrace}", LogLevel.Error);
}
}
}
}

View File

@ -165,15 +165,6 @@ namespace stardew_access.Game
case 41:
toReturn = "Mail Box";
break;
case 173:
toReturn = "Fridge";
break;
case 169:
case 170:
case 171:
case 172:
toReturn = "Kitchen";
break;
case 1003:
toReturn = "Street lamp";
break;
@ -187,6 +178,22 @@ namespace stardew_access.Game
toReturn = "Calender";
break;
}
if(Game1.currentLocation is FarmHouse || Game1.currentLocation is IslandFarmHouse)
{
switch (index)
{
case 173:
toReturn = "Fridge";
break;
case 169:
case 170:
case 171:
case 172:
toReturn = "Kitchen";
break;
}
}
}
}
catch (Exception) {}

View File

@ -51,7 +51,7 @@ namespace stardew_access
ScreenReader.initializeScreenReader(); // Initialize the screen reader
this.initializeSounds();
CustomSoundEffects.Initialize(helper);
harmony = new Harmony(ModManifest.UniqueID); // Init harmony
@ -325,147 +325,6 @@ namespace stardew_access
helper.Events.GameLoop.UpdateTicked += this.onUpdateTicked;
}
private void initializeSounds()
{
try
{
#region Drop Item Sound
CueDefinition dropItemCueDef = new CueDefinition();
dropItemCueDef.name = "sa_drop_item";
dropItemCueDef.instanceLimit = 1;
dropItemCueDef.limitBehavior = CueDefinition.LimitBehavior.ReplaceOldest;
SoundEffect dropItemAudio;
string dropItemFilePath = Path.Combine(this.Helper.DirectoryPath, "sounds/drop_item.wav");
using (FileStream stream = new(dropItemFilePath, FileMode.Open))
{
dropItemAudio = SoundEffect.FromStream(stream);
}
dropItemCueDef.SetSound(dropItemAudio, Game1.audioEngine.GetCategoryIndex("Sound"), false);
#endregion
#region Colliding sound
CueDefinition collidingCueDef = new CueDefinition();
collidingCueDef.name = "sa_colliding";
collidingCueDef.instanceLimit = 1;
collidingCueDef.limitBehavior = CueDefinition.LimitBehavior.ReplaceOldest;
SoundEffect collidingAudio;
string collidingFilePath = Path.Combine(Path.Combine(this.Helper.DirectoryPath), "sounds/colliding.wav");
using (FileStream stream = new(collidingFilePath, FileMode.Open))
{
collidingAudio = SoundEffect.FromStream(stream);
}
collidingCueDef.SetSound(collidingAudio, Game1.audioEngine.GetCategoryIndex("Sound"), false);
#endregion
#region Nearby NPCs sound
// Npc top
CueDefinition npcTopCD = new CueDefinition();
npcTopCD.name = "npc_top";
SoundEffect npcTopSE;
string npcTopFP = Path.Combine(Path.Combine(this.Helper.DirectoryPath), "sounds/npc_top.wav");
using (FileStream stream = new(npcTopFP, FileMode.Open))
{
npcTopSE = SoundEffect.FromStream(stream);
}
npcTopCD.SetSound(npcTopSE, Game1.audioEngine.GetCategoryIndex("Footsteps"), false);
// Npc left
CueDefinition npcLeftCD = new CueDefinition();
npcLeftCD.name = "npc_left";
SoundEffect npcLeftSE;
string npcLeftFP = Path.Combine(Path.Combine(this.Helper.DirectoryPath), "sounds/npc_left.wav");
using (FileStream stream = new(npcLeftFP, FileMode.Open))
{
npcLeftSE = SoundEffect.FromStream(stream);
}
npcLeftCD.SetSound(npcLeftSE, Game1.audioEngine.GetCategoryIndex("Footsteps"), false);
// Npc right
CueDefinition npcRightCD = new CueDefinition();
npcRightCD.name = "npc_right";
SoundEffect npcRightSE;
string npcRightFP = Path.Combine(Path.Combine(this.Helper.DirectoryPath), "sounds/npc_right.wav");
using (FileStream stream = new(npcRightFP, FileMode.Open))
{
npcRightSE = SoundEffect.FromStream(stream);
}
npcRightCD.SetSound(npcRightSE, Game1.audioEngine.GetCategoryIndex("Footsteps"), false);
// Npc bottom
CueDefinition npcBottomCD = new CueDefinition();
npcBottomCD.name = "npc_bottom";
SoundEffect npcBottomSE;
string npcBottomFP = Path.Combine(Path.Combine(this.Helper.DirectoryPath), "sounds/npc_bottom.wav");
using (FileStream stream = new(npcBottomFP, FileMode.Open))
{
npcBottomSE = SoundEffect.FromStream(stream);
}
npcBottomCD.SetSound(npcBottomSE, Game1.audioEngine.GetCategoryIndex("Footsteps"), false);
#endregion
#region Nearby objects sound
// Object top
CueDefinition objTopCD = new CueDefinition();
objTopCD.name = "obj_top";
SoundEffect objTopSE;
string objTopFP = Path.Combine(Path.Combine(this.Helper.DirectoryPath), "sounds/obj_top.wav");
using (FileStream stream = new(objTopFP, FileMode.Open))
{
objTopSE = SoundEffect.FromStream(stream);
}
objTopCD.SetSound(objTopSE, Game1.audioEngine.GetCategoryIndex("Footsteps"), false);
// Object left
CueDefinition objLeftCD = new CueDefinition();
objLeftCD.name = "obj_left";
SoundEffect objLeftSE;
string objLeftFP = Path.Combine(Path.Combine(this.Helper.DirectoryPath), "sounds/obj_left.wav");
using (FileStream stream = new(objLeftFP, FileMode.Open))
{
objLeftSE = SoundEffect.FromStream(stream);
}
objLeftCD.SetSound(objLeftSE, Game1.audioEngine.GetCategoryIndex("Footsteps"), false);
// Object right
CueDefinition objRightCD = new CueDefinition();
objRightCD.name = "obj_right";
SoundEffect objRightSE;
string objRightFP = Path.Combine(Path.Combine(this.Helper.DirectoryPath), "sounds/obj_right.wav");
using (FileStream stream = new(objRightFP, FileMode.Open))
{
objRightSE = SoundEffect.FromStream(stream);
}
objRightCD.SetSound(objRightSE, Game1.audioEngine.GetCategoryIndex("Footsteps"), false);
// Object bottom
CueDefinition objBottomCD = new CueDefinition();
objBottomCD.name = "obj_bottom";
SoundEffect objBottomSE;
string objBottomFP = Path.Combine(Path.Combine(this.Helper.DirectoryPath), "sounds/obj_bottom.wav");
using (FileStream stream = new(objBottomFP, FileMode.Open))
{
objBottomSE = SoundEffect.FromStream(stream);
}
objBottomCD.SetSound(objBottomSE, Game1.audioEngine.GetCategoryIndex("Footsteps"), false);
#endregion
Game1.soundBank.AddCue(dropItemCueDef);
Game1.soundBank.AddCue(collidingCueDef);
Game1.soundBank.AddCue(objTopCD);
Game1.soundBank.AddCue(objLeftCD);
Game1.soundBank.AddCue(objRightCD);
Game1.soundBank.AddCue(objBottomCD);
Game1.soundBank.AddCue(npcTopCD);
Game1.soundBank.AddCue(npcLeftCD);
Game1.soundBank.AddCue(npcRightCD);
Game1.soundBank.AddCue(npcBottomCD);
}
catch (Exception e)
{
monitor.Log($"Unable to initialize custom sounds:\n{e.Message}\n{e.StackTrace}", LogLevel.Error);
}
}
private void onUpdateTicked(object sender, UpdateTickedEventArgs e)
{
if (!Context.IsPlayerFree)

View File

@ -48,7 +48,7 @@ namespace stardew_access.Patches
shopMenuQueryKey = toSpeak;
hoveredItemQueryKey = "";
ScreenReader.say(toSpeak, true);
Game1.playSound("sa_drop_item");
Game1.playSound("drop_item");
}
return;
}
@ -201,7 +201,7 @@ namespace stardew_access.Patches
{
geodeMenuQueryKey = toSpeak;
ScreenReader.say(toSpeak, true);
Game1.playSound("sa_drop_item");
Game1.playSound("drop_item");
}
return;
}
@ -363,7 +363,7 @@ namespace stardew_access.Patches
gameMenuQueryKey = "";
hoveredItemQueryKey = "";
ScreenReader.say(toSpeak, true);
Game1.playSound("sa_drop_item");
Game1.playSound("drop_item");
}
return;
}
@ -579,7 +579,7 @@ namespace stardew_access.Patches
craftingPageQueryKey = toSpeak;
hoveredItemQueryKey = "";
ScreenReader.say(toSpeak, true);
Game1.playSound("sa_drop_item");
Game1.playSound("drop_item");
}
return;
}
@ -723,7 +723,7 @@ namespace stardew_access.Patches
gameMenuQueryKey = "";
hoveredItemQueryKey = "";
ScreenReader.say(toSpeak, true);
Game1.playSound("sa_drop_item");
Game1.playSound("drop_item");
}
return;
}

View File

@ -31,7 +31,7 @@ namespace stardew_access.Patches
if (prevTile != nextTile)
{
prevTile = nextTile;
Game1.playSound("sa_colliding");
Game1.playSound("colliding");
}
return false;
}