fix the audio system

This commit is contained in:
Michael 2017-06-02 11:38:38 -04:00
parent bae2c83757
commit 11e80a6a61
4 changed files with 111 additions and 47 deletions

View file

@ -34,9 +34,9 @@ namespace ShiftOS.Objects
//Better to store this stuff server-side so we can do some neat stuff with hacking...
public class Save
{
public int MusicVolume { get; set; }
public int SfxVolume { get; set; }
public bool MusicEnabled = true;
public bool SoundEnabled = true;
public int MusicVolume = 100;
[Obsolete("This save variable is no longer used in Beta 2.4 and above of ShiftOS. Please use ShiftOS.Engine.SaveSystem.CurrentUser.Username to access the current user's username.")]
public string Username { get; set; }

View file

@ -88,23 +88,46 @@ namespace ShiftOS.WinForms
};
while (shuttingDown == false)
{
str = new MemoryStream(GetRandomSong());
mp3 = new NAudio.Wave.Mp3FileReader(str);
o = new NAudio.Wave.WaveOut();
o.Init(mp3);
bool c = false;
o.Play();
o.PlaybackStopped += (s, a) =>
if (Engine.SaveSystem.CurrentSave != null)
{
c = true;
};
while (!c)
{
try
if (Engine.SaveSystem.CurrentSave.MusicEnabled)
{
o.Volume = (float)Engine.SaveSystem.CurrentSave.MusicVolume / 100;
str = new MemoryStream(GetRandomSong());
mp3 = new NAudio.Wave.Mp3FileReader(str);
o = new NAudio.Wave.WaveOut();
o.Init(mp3);
bool c = false;
o.Play();
o.PlaybackStopped += (s, a) =>
{
c = true;
};
while (!c)
{
if (Engine.SaveSystem.CurrentSave.MusicEnabled)
{
try
{
o.Volume = (float)Engine.SaveSystem.CurrentSave.MusicVolume / 100;
}
catch { }
}
else
{
o.Stop();
c = true;
}
Thread.Sleep(10);
}
}
catch { }
else
{
Thread.Sleep(10);
}
}
else
{
Thread.Sleep(10);
}
}

View file

@ -83,21 +83,28 @@ namespace ShiftOS.Engine
/// <param name="file">The file to play.</param>
public static void Play(string file)
{
try
bool play = true;
float volume = 1f;
if (SaveSystem.CurrentSave != null)
{
_reader = new AudioFileReader(file);
_out = new WaveOut();
_out.Init(_reader);
_out.Play();
if (SaveSystem.CurrentSave == null)
_out.Volume = 1.0f;
else
_out.Volume = (float)SaveSystem.CurrentSave.MusicVolume / 100;
_out.PlaybackStopped += (o, a) => { PlayCompleted?.Invoke(); };
play = (SaveSystem.CurrentSave.SoundEnabled);
volume = (float)SaveSystem.CurrentSave.MusicVolume / 100f;
}
catch(Exception ex)
if (play)
{
Console.WriteLine("Audio error: " + ex.Message);
try
{
_reader = new AudioFileReader(file);
_out = new WaveOut();
_out.Init(_reader);
_out.Volume = volume;
_out.Play();
_out.PlaybackStopped += (o, a) => { PlayCompleted?.Invoke(); };
}
catch (Exception ex)
{
Console.WriteLine("Audio error: " + ex.Message);
}
}
}
@ -107,15 +114,27 @@ namespace ShiftOS.Engine
/// <param name="str">The stream to read from.</param>
public static void PlayStream(Stream str)
{
var bytes = new byte[str.Length];
str.Read(bytes, 0, bytes.Length);
ShiftOS.Engine.AudioManager.Stop();
if (File.Exists("snd.wav"))
File.Delete("snd.wav");
File.WriteAllBytes("snd.wav", bytes);
ShiftOS.Engine.AudioManager.Play("snd.wav");
try
{
bool play = true;
float volume = 1f;
if (SaveSystem.CurrentSave != null)
{
play = (SaveSystem.CurrentSave.SoundEnabled);
volume = (float)SaveSystem.CurrentSave.MusicVolume / 100f;
}
if (play)
{
ShiftOS.Engine.AudioManager.Stop();
_out = new WaveOut();
var mp3 = new WaveFileReader(str);
_out.Init(mp3);
_out.Volume = volume;
_out.Play();
_out.PlaybackStopped += (o, a) => { PlayCompleted?.Invoke(); };
}
}
catch { }
}
public static event Action PlayCompleted;

View file

@ -383,26 +383,48 @@ namespace ShiftOS.Engine
[Namespace("sos")]
public static class ShiftOSCommands
{
[Command("setsfxvolume", description = "Set the volume of various sound effects to a value between 1 and 100.")]
[Command("setsfxenabled", description = "Set whether or not sound effects are enabled in the system.")]
[RequiresArgument("value")]
public static bool SetSfxVolume(Dictionary<string, object> args)
public static bool SetSfxEnabled(Dictionary<string, object> args)
{
int value = int.Parse(args["value"].ToString());
if (value >= 0 && value <= 100)
try
{
SaveSystem.CurrentSave.SfxVolume = value;
bool value = Convert.ToBoolean(args["value"].ToString());
SaveSystem.CurrentSave.SoundEnabled = value;
SaveSystem.SaveGame();
}
else
catch
{
Console.WriteLine("Volume must be between 0 and 100!");
Console.WriteLine("Error: Value must be either true or false.");
}
return true;
}
[Command("setmusicvolume", description ="Set the music volume to a value between 1 and 100.")]
[Command("setmusicenabled", description = "Set whether or not music is enabled in the system.")]
[RequiresArgument("value")]
public static bool SetMusicVolume(Dictionary<string, object> args)
public static bool SetMusicEnabled(Dictionary<string, object> args)
{
try
{
bool value = Convert.ToBoolean(args["value"].ToString());
SaveSystem.CurrentSave.MusicEnabled = value;
SaveSystem.SaveGame();
}
catch
{
Console.WriteLine("Error: Value must be either true or false.");
}
return true;
}
[Command("setsfxvolume", description ="Set the system sound volume to a value between 1 and 100.")]
[RequiresArgument("value")]
public static bool SetSfxVolume(Dictionary<string, object> args)
{
int value = int.Parse(args["value"].ToString());
if(value >= 0 && value <= 100)