Skip to content

Commit

Permalink
Disable music if there was an error initializing the audio device (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekolis committed May 29, 2022
1 parent 8d1d8ab commit 66ce6a7
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions FrEee.WinForms/Objects/Music.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using FrEee.Utility.Extensions;
using FrEee.Utility.Extensions;
using NAudio.Vorbis;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
Expand All @@ -16,21 +16,31 @@ public static class Music
{
static Music()
{
OperatingSystem os = Environment.OSVersion;
PlatformID pid = os.Platform;
if (pid == PlatformID.Unix)
try
{
Console.WriteLine("Linux detected, disabling Music");
linux = true;
return;
}
OperatingSystem os = Environment.OSVersion;
PlatformID pid = os.Platform;
if (pid == PlatformID.Unix)
{
Console.WriteLine("Linux detected, disabling Music");
disableMusic = true;
return;
}

waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(48000, 2);
mixer = new MixingSampleProvider(waveFormat);
waveFormat = WaveFormat.CreateIeeeFloatWaveFormat(48000, 2);
mixer = new MixingSampleProvider(waveFormat);

waveout.Init(mixer);
waveout.PlaybackStopped += waveout_PlaybackStopped;
waveout.Play();
waveout.Init(mixer);
waveout.PlaybackStopped += waveout_PlaybackStopped;
waveout.Play();
}
catch (Exception ex)
{
Console.WriteLine("Error initializing music; disabling it");
Console.WriteLine("See errorlog.txt for details.");
disableMusic = true;
ex.Log();
}
}

// milliseconds
Expand Down Expand Up @@ -77,13 +87,13 @@ public static MusicMood CurrentMood
private static float musicVolume = 1.0f;
private static WaveFormat waveFormat;
private static WaveOutEvent waveout = new WaveOutEvent();
private static bool linux = false;
private static bool disableMusic = false;

public static void Play(MusicMode mode, MusicMood mood)
{
if (mode == CurrentMode && mood == CurrentMood)
return;
if (linux)
if (disableMusic)
return;

currentMode = mode;
Expand All @@ -93,14 +103,14 @@ public static void Play(MusicMode mode, MusicMood mood)

public static void setVolume(float volume)
{
if (linux)
if (disableMusic)
return;
musicVolume = volume;
}

public static void StartNewTrack()
{
if (linux)
if (disableMusic)
return;
// find out what to play
var tracks = FindTracks().ToArray();
Expand Down Expand Up @@ -250,4 +260,4 @@ public enum MusicMood
Upbeat,
Sad,
}
}
}

0 comments on commit 66ce6a7

Please sign in to comment.