2018-12-30 18:00:05 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using Microsoft.Xna.Framework.Audio;
|
2017-08-22 07:18:21 +08:00
|
|
|
using StardewModdingAPI;
|
|
|
|
using StardewValley;
|
|
|
|
|
2018-06-26 12:13:40 +08:00
|
|
|
namespace SimpleSoundManager.Framework
|
2017-08-22 07:18:21 +08:00
|
|
|
{
|
2018-08-07 05:01:44 +08:00
|
|
|
public class SoundManager
|
2017-08-22 07:18:21 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
public Dictionary<string, Sound> sounds;
|
2018-06-01 05:38:24 +08:00
|
|
|
public Dictionary<string, XACTMusicPair> musicBanks;
|
2017-08-22 07:18:21 +08:00
|
|
|
|
2018-08-17 02:50:50 +08:00
|
|
|
public float volume;
|
2017-08-22 07:18:21 +08:00
|
|
|
|
2018-08-07 05:01:44 +08:00
|
|
|
public List<Sound> currentlyPlayingSounds = new List<Sound>();
|
2018-12-30 18:00:05 +08:00
|
|
|
|
|
|
|
/// <summary>Constructor for this class.</summary>
|
2018-06-01 05:38:24 +08:00
|
|
|
public SoundManager()
|
2017-08-22 07:18:21 +08:00
|
|
|
{
|
2018-06-01 05:38:24 +08:00
|
|
|
this.sounds = new Dictionary<string, Sound>();
|
|
|
|
this.musicBanks = new Dictionary<string, XACTMusicPair>();
|
2018-12-30 18:00:05 +08:00
|
|
|
this.currentlyPlayingSounds = new List<Sound>();
|
2018-08-17 02:50:50 +08:00
|
|
|
this.volume = 1.0f;
|
2017-08-22 07:18:21 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Constructor for wav files.</summary>
|
|
|
|
public void loadWavFile(string soundName, string pathToWav)
|
2017-08-22 07:18:21 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
WavSound wav = new WavSound(soundName, pathToWav);
|
2018-08-16 04:22:31 +08:00
|
|
|
SimpleSoundManagerMod.ModMonitor.Log("Getting sound file:" + soundName);
|
2018-08-16 14:59:39 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
this.sounds.Add(soundName, wav);
|
|
|
|
}
|
2018-12-30 18:00:05 +08:00
|
|
|
catch { }
|
2017-08-22 07:18:21 +08:00
|
|
|
}
|
2018-12-30 18:00:05 +08:00
|
|
|
|
|
|
|
/// <summary>Constructor for wav files.</summary>
|
2019-01-06 13:31:40 +08:00
|
|
|
public void loadWavFile(IModHelper helper, string soundName, string relativePath)
|
2017-08-22 07:18:21 +08:00
|
|
|
{
|
2019-01-06 13:31:40 +08:00
|
|
|
WavSound wav = new WavSound(helper, soundName, relativePath);
|
2018-08-16 04:22:31 +08:00
|
|
|
SimpleSoundManagerMod.ModMonitor.Log("Getting sound file:" + soundName);
|
2018-08-16 14:59:39 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
this.sounds.Add(soundName, wav);
|
|
|
|
}
|
2018-12-30 18:00:05 +08:00
|
|
|
catch
|
2018-08-16 14:59:39 +08:00
|
|
|
{
|
|
|
|
//Sound already added so no need to worry?
|
|
|
|
}
|
2017-08-22 07:18:21 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Constructor for wav files.</summary>
|
|
|
|
public void loadWavFile(IModHelper helper, string songName, List<string> pathToWav)
|
2017-08-22 07:18:21 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
WavSound wav = new WavSound(helper, songName, pathToWav);
|
2018-08-16 04:22:31 +08:00
|
|
|
SimpleSoundManagerMod.ModMonitor.Log("Getting sound file:" + songName);
|
2018-08-16 14:59:39 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
this.sounds.Add(songName, wav);
|
|
|
|
}
|
2018-12-30 18:00:05 +08:00
|
|
|
catch { }
|
2017-08-22 07:18:21 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Constructor for XACT files.</summary>
|
2018-06-01 05:38:24 +08:00
|
|
|
public void loadXACTFile(WaveBank waveBank, ISoundBank soundBank, string songName)
|
2017-08-22 07:18:21 +08:00
|
|
|
{
|
2018-06-01 05:38:24 +08:00
|
|
|
XACTSound xactSound = new XACTSound(waveBank, soundBank, songName);
|
|
|
|
this.sounds.Add(songName, xactSound);
|
2017-08-22 07:18:21 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Constructor for XACT files based on already added music packs.</summary>
|
2018-06-01 05:38:24 +08:00
|
|
|
public void loadXACTFile(string pairName, string songName)
|
2017-08-22 07:18:21 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
XACTMusicPair musicPair = this.getMusicPack(pairName);
|
2018-06-01 05:38:24 +08:00
|
|
|
if (pairName == null)
|
|
|
|
return;
|
2018-12-30 18:00:05 +08:00
|
|
|
this.loadXACTFile(musicPair.waveBank, musicPair.soundBank, songName);
|
2017-08-22 07:18:21 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Creates a music pack pair that holds .xwb and .xsb music files.</summary>
|
2018-06-01 05:38:24 +08:00
|
|
|
/// <param name="helper">The mod's helper that will handle the path of the files.</param>
|
|
|
|
/// <param name="pairName">The name of this music pack pair.</param>
|
|
|
|
/// <param name="wavName">The relative path to the .xwb file</param>
|
|
|
|
/// <param name="soundName">The relative path to the .xsb file</param>
|
2018-12-30 18:00:05 +08:00
|
|
|
public void loadXACTMusicBank(IModHelper helper, string pairName, string wavName, string soundName)
|
2017-08-22 07:18:21 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
this.musicBanks.Add(pairName, new XACTMusicPair(helper, wavName, soundName));
|
2017-08-22 07:18:21 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Gets the music pack pair from the sound pool.</summary>
|
2018-06-01 05:38:24 +08:00
|
|
|
public XACTMusicPair getMusicPack(string name)
|
2017-08-22 07:18:21 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
foreach (var pack in this.musicBanks)
|
2017-08-22 07:18:21 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (name == pack.Key)
|
|
|
|
return pack.Value;
|
2017-08-22 07:18:21 +08:00
|
|
|
}
|
2018-06-01 05:38:24 +08:00
|
|
|
return null;
|
2017-08-22 07:18:21 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Gets a clone of the loaded sound.</summary>
|
2018-06-01 05:38:24 +08:00
|
|
|
public Sound getSoundClone(string name)
|
2017-08-22 07:18:21 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
foreach (var sound in this.sounds)
|
2017-08-22 07:18:21 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (sound.Key == name)
|
|
|
|
return sound.Value.clone();
|
2017-08-22 07:18:21 +08:00
|
|
|
}
|
2018-06-01 05:38:24 +08:00
|
|
|
return null;
|
2017-08-22 07:18:21 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Play the sound with the given name.</summary>
|
2018-08-07 05:01:44 +08:00
|
|
|
public void playSound(string soundName)
|
|
|
|
{
|
2018-08-16 04:22:31 +08:00
|
|
|
SimpleSoundManagerMod.ModMonitor.Log("Trying to play sound: " + soundName);
|
2018-12-30 18:00:05 +08:00
|
|
|
foreach (var sound in this.sounds)
|
2018-08-07 05:01:44 +08:00
|
|
|
{
|
|
|
|
if (sound.Key == soundName)
|
|
|
|
{
|
2018-08-16 04:22:31 +08:00
|
|
|
SimpleSoundManagerMod.ModMonitor.Log("Time to play sound: " + soundName);
|
2018-12-30 18:00:05 +08:00
|
|
|
var s = this.getSoundClone(soundName);
|
2018-08-17 02:50:50 +08:00
|
|
|
s.play(this.volume);
|
|
|
|
this.currentlyPlayingSounds.Add(s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-30 18:00:05 +08:00
|
|
|
|
|
|
|
/// <summary>Play the sound with the given name and volume.</summary>
|
|
|
|
public void playSound(string soundName, float volume = 1.0f)
|
2018-08-17 02:50:50 +08:00
|
|
|
{
|
|
|
|
SimpleSoundManagerMod.ModMonitor.Log("Trying to play sound: " + soundName);
|
|
|
|
foreach (var sound in this.sounds)
|
|
|
|
{
|
|
|
|
if (sound.Key == soundName)
|
|
|
|
{
|
|
|
|
SimpleSoundManagerMod.ModMonitor.Log("Time to play sound: " + soundName);
|
2018-12-30 18:00:05 +08:00
|
|
|
var s = this.getSoundClone(soundName);
|
2018-08-17 02:50:50 +08:00
|
|
|
s.play(volume);
|
2018-08-07 05:01:44 +08:00
|
|
|
this.currentlyPlayingSounds.Add(s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Stop the sound that is playing.</summary>
|
2018-08-07 05:01:44 +08:00
|
|
|
public void stopSound(string soundName)
|
|
|
|
{
|
|
|
|
List<Sound> removalList = new List<Sound>();
|
|
|
|
foreach (var sound in this.currentlyPlayingSounds)
|
|
|
|
{
|
|
|
|
if (sound.getSoundName() == soundName)
|
|
|
|
{
|
|
|
|
sound.stop();
|
|
|
|
removalList.Add(sound);
|
|
|
|
}
|
|
|
|
}
|
2018-12-30 18:00:05 +08:00
|
|
|
foreach (var v in removalList)
|
2018-08-07 05:01:44 +08:00
|
|
|
this.currentlyPlayingSounds.Remove(v);
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Pause the sound with this name?</summary>
|
2018-08-07 05:01:44 +08:00
|
|
|
public void pauseSound(string soundName)
|
|
|
|
{
|
|
|
|
List<Sound> removalList = new List<Sound>();
|
|
|
|
foreach (var sound in this.currentlyPlayingSounds)
|
|
|
|
{
|
|
|
|
if (sound.getSoundName() == soundName)
|
|
|
|
{
|
|
|
|
sound.pause();
|
|
|
|
removalList.Add(sound);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach (var v in removalList)
|
|
|
|
this.currentlyPlayingSounds.Remove(v);
|
|
|
|
}
|
|
|
|
|
2018-08-16 04:22:31 +08:00
|
|
|
public void swapSounds(string newSong)
|
|
|
|
{
|
|
|
|
this.playSound(newSong);
|
|
|
|
}
|
|
|
|
|
2018-08-07 05:01:44 +08:00
|
|
|
public void update()
|
|
|
|
{
|
|
|
|
List<Sound> removalList = new List<Sound>();
|
2018-12-30 18:00:05 +08:00
|
|
|
foreach (Sound song in this.currentlyPlayingSounds)
|
2018-08-07 05:01:44 +08:00
|
|
|
{
|
|
|
|
if (song.isStopped())
|
|
|
|
removalList.Add(song);
|
|
|
|
}
|
2018-12-30 18:00:05 +08:00
|
|
|
foreach (var v in removalList)
|
2018-08-07 05:01:44 +08:00
|
|
|
this.currentlyPlayingSounds.Remove(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void stopAllSounds()
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
foreach (var v in this.currentlyPlayingSounds)
|
2018-08-07 05:01:44 +08:00
|
|
|
v.stop();
|
|
|
|
}
|
2017-08-22 07:18:21 +08:00
|
|
|
}
|
|
|
|
}
|