Improved LinuxSoundManager.cs
This commit is contained in:
parent
26b067ff04
commit
fc043e0219
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,11 +1,8 @@
|
||||||
//#define WINDOWS
|
using System;
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using testexetrisathlon.SoundManagement;
|
using testexetrisathlon.SoundManagement;
|
||||||
using static System.Console;
|
using static System.Console;
|
||||||
|
|
||||||
|
@ -59,20 +56,6 @@ namespace testexetrisathlon
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
private static void Main()
|
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;
|
Debug = true;
|
||||||
#else
|
#else
|
||||||
private static void Main(string[] args)
|
private static void Main(string[] args)
|
||||||
|
@ -81,11 +64,20 @@ namespace testexetrisathlon
|
||||||
#endif
|
#endif
|
||||||
BackgroundColor = ConsoleColor.Red;
|
BackgroundColor = ConsoleColor.Red;
|
||||||
ForegroundColor = ConsoleColor.Yellow;
|
ForegroundColor = ConsoleColor.Yellow;
|
||||||
#if WINDOWS
|
soundManager = OSCheck.IsWindows ? (ISoundManager) new WindowsSoundManager() : new LinuxSoundManager();
|
||||||
SetWindowSize(42, 29);
|
soundManager.Init(new Dictionary<string, string>
|
||||||
if (Debug)
|
{
|
||||||
SetWindowSize(50, 40);
|
{"Intro", "testexetrisathlon.Intro.wav"},
|
||||||
#endif
|
{"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);
|
SetCursorPosition(0, 0);
|
||||||
Clear();
|
Clear();
|
||||||
bool playing = true;
|
bool playing = true;
|
||||||
|
@ -220,10 +212,11 @@ namespace testexetrisathlon
|
||||||
{
|
{
|
||||||
Clear();
|
Clear();
|
||||||
DrawSymbol();
|
DrawSymbol();
|
||||||
#if !WINDOWS
|
if (!OSCheck.IsWindows)
|
||||||
SetCursorPosition(2, 19);
|
{
|
||||||
Write("Volume is not supported in this build!");
|
SetCursorPosition(2, 19);
|
||||||
#endif
|
Write("Volume is not supported in this build!");
|
||||||
|
}
|
||||||
bool barActive = true;
|
bool barActive = true;
|
||||||
int currentSetting = 0;
|
int currentSetting = 0;
|
||||||
while (barActive)
|
while (barActive)
|
||||||
|
|
|
@ -6,11 +6,10 @@ namespace testexetrisathlon.SoundManagement
|
||||||
{
|
{
|
||||||
public static void Beep(int frequency, int duration)
|
public static void Beep(int frequency, int duration)
|
||||||
{
|
{
|
||||||
#if WINDOWS
|
if (OSCheck.IsWindows)
|
||||||
Console.Beep(frequency, duration);
|
Console.Beep(frequency, duration);
|
||||||
#else
|
else
|
||||||
Console.Write("\a");
|
Console.Write("\a");
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -8,12 +8,14 @@ namespace testexetrisathlon.SoundManagement
|
||||||
public class LinuxSoundManager : ISoundManager
|
public class LinuxSoundManager : ISoundManager
|
||||||
{
|
{
|
||||||
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
|
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
|
||||||
private Dictionary<string, string> _files;
|
private ProcessLoop? _alsaProc;
|
||||||
private string _current;
|
private string _current;
|
||||||
private Process? alsa;
|
private Dictionary<string, string> _files;
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
foreach (string file in _files.Values) File.Delete(file);
|
foreach (string file in _files.Values) File.Delete(file);
|
||||||
|
if (_alsaProc != null) _alsaProc.IsRunning = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Init(Dictionary<string, string> manifestResources)
|
public void Init(Dictionary<string, string> manifestResources)
|
||||||
|
@ -35,27 +37,20 @@ namespace testexetrisathlon.SoundManagement
|
||||||
public void SetCurrent(string id)
|
public void SetCurrent(string id)
|
||||||
{
|
{
|
||||||
if (_current == id) return;
|
if (_current == id) return;
|
||||||
alsa?.Kill();
|
if (_alsaProc != null) _alsaProc.IsRunning = false;
|
||||||
_current = id;
|
_current = id;
|
||||||
//TODO fix actually killing, remove orphan processes
|
//TODO fix actually killing, remove orphan processes
|
||||||
alsa = new Process
|
_alsaProc = new ProcessLoop(new ProcessStartInfo
|
||||||
{
|
{
|
||||||
StartInfo = new ProcessStartInfo
|
FileName = "aplay",
|
||||||
{
|
Arguments = $"-q {_files[id].Replace("\"", "\\\"")}",
|
||||||
FileName = "/bin/sh",
|
CreateNoWindow = true
|
||||||
Arguments = $"-c \"while [ true ]; do aplay -q {_files[id].Replace("\"", "\\\"")}; done\"",
|
});
|
||||||
RedirectStandardOutput = false,
|
_alsaProc.CreateLoopThread().Start();
|
||||||
RedirectStandardInput = false,
|
|
||||||
UseShellExecute = false,
|
|
||||||
CreateNoWindow = true,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
alsa.Start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetVolume(int percent)
|
public void SetVolume(int percent)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,10 +8,10 @@ namespace testexetrisathlon.SoundManagement
|
||||||
public sealed class WindowsSoundManager : ISoundManager
|
public sealed class WindowsSoundManager : ISoundManager
|
||||||
{
|
{
|
||||||
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
|
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
|
||||||
private WaveOutEvent _output;
|
|
||||||
private WaveStream _current;
|
private WaveStream _current;
|
||||||
private string _currentId;
|
private string _currentId;
|
||||||
private Dictionary<string, LoopStream> _loadedSounds;
|
private Dictionary<string, LoopStream> _loadedSounds;
|
||||||
|
private WaveOutEvent _output;
|
||||||
|
|
||||||
public void Init(Dictionary<string, string> manifestResources)
|
public void Init(Dictionary<string, string> manifestResources)
|
||||||
{
|
{
|
||||||
|
|
Reference in New Issue