Improved LinuxSoundManager.cs

This commit is contained in:
CreepyCrafter24 2020-03-17 13:45:29 +01:00
parent 26b067ff04
commit fc043e0219
6 changed files with 93 additions and 49 deletions

View File

@ -0,0 +1,24 @@
using System;
using System.Linq;
namespace testexetrisathlon
{
internal static class OSCheck
{
private static bool _checkedWindows;
private static bool _isWindows;
public static bool IsWindows
{
get
{
if (_checkedWindows) return _isWindows;
_isWindows =
new[] {PlatformID.Win32S, PlatformID.Win32Windows, PlatformID.Win32NT, PlatformID.WinCE}
.Contains(Environment.OSVersion.Platform);
_checkedWindows = true;
return _isWindows;
}
}
}
}

View File

@ -1,11 +1,8 @@
//#define WINDOWS
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using testexetrisathlon.SoundManagement;
using static System.Console;
@ -59,20 +56,6 @@ namespace testexetrisathlon
#if DEBUG
private static void Main()
{
soundManager = new
#if WINDOW
WindowsSoundManager
#else
LinuxSoundManager
#endif
();
soundManager.Init(new Dictionary<string, string>
{
{"Intro", "testexetrisathlon.Intro.wav"},
{"InGame1", "testexetrisathlon.InGame1.wav"},
{"InGame2", "testexetrisathlon.InGame2.wav"},
{"GameOver", "testexetrisathlon.GameOver.wav"}
});
Debug = true;
#else
private static void Main(string[] args)
@ -81,11 +64,20 @@ namespace testexetrisathlon
#endif
BackgroundColor = ConsoleColor.Red;
ForegroundColor = ConsoleColor.Yellow;
#if WINDOWS
SetWindowSize(42, 29);
if (Debug)
SetWindowSize(50, 40);
#endif
soundManager = OSCheck.IsWindows ? (ISoundManager) new WindowsSoundManager() : new LinuxSoundManager();
soundManager.Init(new Dictionary<string, string>
{
{"Intro", "testexetrisathlon.Intro.wav"},
{"InGame1", "testexetrisathlon.InGame1.wav"},
{"InGame2", "testexetrisathlon.InGame2.wav"},
{"GameOver", "testexetrisathlon.GameOver.wav"}
});
if (OSCheck.IsWindows)
{
SetWindowSize(42, 29);
if (Debug)
SetWindowSize(50, 40);
}
SetCursorPosition(0, 0);
Clear();
bool playing = true;
@ -220,10 +212,11 @@ namespace testexetrisathlon
{
Clear();
DrawSymbol();
#if !WINDOWS
SetCursorPosition(2, 19);
Write("Volume is not supported in this build!");
#endif
if (!OSCheck.IsWindows)
{
SetCursorPosition(2, 19);
Write("Volume is not supported in this build!");
}
bool barActive = true;
int currentSetting = 0;
while (barActive)

View File

@ -6,11 +6,10 @@ namespace testexetrisathlon.SoundManagement
{
public static void Beep(int frequency, int duration)
{
#if WINDOWS
Console.Beep(frequency, duration);
#else
Console.Write("\a");
#endif
if (OSCheck.IsWindows)
Console.Beep(frequency, duration);
else
Console.Write("\a");
}
}
}

View File

@ -8,12 +8,14 @@ namespace testexetrisathlon.SoundManagement
public class LinuxSoundManager : ISoundManager
{
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
private Dictionary<string, string> _files;
private ProcessLoop? _alsaProc;
private string _current;
private Process? alsa;
private Dictionary<string, string> _files;
public void Dispose()
{
foreach (string file in _files.Values) File.Delete(file);
if (_alsaProc != null) _alsaProc.IsRunning = false;
}
public void Init(Dictionary<string, string> manifestResources)
@ -35,27 +37,20 @@ namespace testexetrisathlon.SoundManagement
public void SetCurrent(string id)
{
if (_current == id) return;
alsa?.Kill();
if (_alsaProc != null) _alsaProc.IsRunning = false;
_current = id;
//TODO fix actually killing, remove orphan processes
alsa = new Process
_alsaProc = new ProcessLoop(new ProcessStartInfo
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/sh",
Arguments = $"-c \"while [ true ]; do aplay -q {_files[id].Replace("\"", "\\\"")}; done\"",
RedirectStandardOutput = false,
RedirectStandardInput = false,
UseShellExecute = false,
CreateNoWindow = true,
}
};
alsa.Start();
FileName = "aplay",
Arguments = $"-q {_files[id].Replace("\"", "\\\"")}",
CreateNoWindow = true
});
_alsaProc.CreateLoopThread().Start();
}
public void SetVolume(int percent)
{
}
}
}

View File

@ -0,0 +1,33 @@
using System.Diagnostics;
using System.Threading;
namespace testexetrisathlon.SoundManagement
{
internal class ProcessLoop
{
private readonly ProcessStartInfo _info;
private Process _currentProc;
private bool _isRunning = true;
public ProcessLoop(ProcessStartInfo info) => _info = info;
public bool IsRunning
{
set
{
if (_isRunning && !value)
_currentProc.Kill();
_isRunning = value;
}
}
public Thread CreateLoopThread() => new Thread(() =>
{
while (_isRunning)
{
_currentProc = Process.Start(_info);
_currentProc.WaitForExit();
}
});
}
}

View File

@ -8,10 +8,10 @@ namespace testexetrisathlon.SoundManagement
public sealed class WindowsSoundManager : ISoundManager
{
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
private WaveOutEvent _output;
private WaveStream _current;
private string _currentId;
private Dictionary<string, LoopStream> _loadedSounds;
private WaveOutEvent _output;
public void Init(Dictionary<string, string> manifestResources)
{