Start to implement linux sound management

This commit is contained in:
CreepyCrafter24 2020-03-17 00:22:58 +01:00
parent 64f0a3f302
commit 26b067ff04
6 changed files with 100 additions and 26 deletions

View File

@ -1,4 +1,4 @@
#define WINDOWS
//#define WINDOWS
using System;
using System.Collections.Generic;
@ -33,14 +33,6 @@ namespace testexetrisathlon
internal static class Program
{
public const string Sqr = "■";
/*private static readonly WaveStream Intro = new WaveFileReader(Assembly.GetManifestResourceStream("testexetrisathlon.Intro.wav"));
private static readonly WaveStream InGame1 = new WaveFileReader(Assembly.GetManifestResourceStream("testexetrisathlon.InGame1.wav"));
private static readonly WaveStream InGame2 = new WaveFileReader(Assembly.GetManifestResourceStream("testexetrisathlon.InGame2.wav"));
private static readonly WaveStream GameOver = new WaveFileReader(Assembly.GetManifestResourceStream("testexetrisathlon.GameOver.wav"));
private static WaveStream _inGame = SettingsMan.UsingAltTrack ? InGame2 : InGame1;
private static WaveStream _current = Intro;
private static WaveOutEvent _output = new WaveOutEvent();*/
private const string Intro = "Intro";
private const string GameOver = "GameOver";
public static int[,] Grid = new int[23, 10];
@ -63,15 +55,17 @@ namespace testexetrisathlon
public static readonly Random Rnd = new Random();
private static ISoundManager soundManager;
private static string InGame => SettingsMan.UsingAltTrack ? "InGame2" : "InGame1";
#if WINDOWS
[DllImport("winmm.dll")]
private static extern int waveOutSetVolume(IntPtr hwo, uint dwVolume);
#endif
#if DEBUG
private static void Main()
{
soundManager = new WindowsSoundManager();
soundManager = new
#if WINDOW
WindowsSoundManager
#else
LinuxSoundManager
#endif
();
soundManager.Init(new Dictionary<string, string>
{
{"Intro", "testexetrisathlon.Intro.wav"},
@ -87,15 +81,13 @@ namespace testexetrisathlon
#endif
BackgroundColor = ConsoleColor.Red;
ForegroundColor = ConsoleColor.Yellow;
#if WINDOWS
SetWindowSize(42, 29);
SetCursorPosition(0, 0);
Clear();
if (Debug)
SetWindowSize(50, 40);
#if WINDOWS
int newVolume = (ushort.MaxValue / 10) * SettingsMan.Volume;
waveOutSetVolume(IntPtr.Zero, ((uint) newVolume & 0x0000ffff) | ((uint) newVolume << 16));
#endif
SetCursorPosition(0, 0);
Clear();
bool playing = true;
GameState state = GameState.Menu;
try
@ -325,7 +317,7 @@ namespace testexetrisathlon
{
_linesCleared++;
combo++;
Beep(400, 200);
Beeper.Beep(400, 200);
for (int j = 0; j < 10; j++) DroppedTetrominoeLocationGrid[i, j] = 0;
int[,] newDroppedTetrominoeLocationGrid = new int[23, 10];
for (int k = 1; k < i; k++)

View File

@ -0,0 +1,16 @@
using System;
namespace testexetrisathlon.SoundManagement
{
public static class Beeper
{
public static void Beep(int frequency, int duration)
{
#if WINDOWS
Console.Beep(frequency, duration);
#else
Console.Write("\a");
#endif
}
}
}

View File

@ -0,0 +1,61 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace testexetrisathlon.SoundManagement
{
public class LinuxSoundManager : ISoundManager
{
private static readonly Assembly Assembly = Assembly.GetExecutingAssembly();
private Dictionary<string, string> _files;
private string _current;
private Process? alsa;
public void Dispose()
{
foreach (string file in _files.Values) File.Delete(file);
}
public void Init(Dictionary<string, string> manifestResources)
{
_files = new Dictionary<string, string>();
foreach ((string name, string key) in manifestResources)
{
string file = Path.GetTempFileName();
File.Move(file, Path.ChangeExtension(file, "wav"));
file = Path.ChangeExtension(file, "wav");
using Stream resource = Assembly.GetManifestResourceStream(key);
using FileStream fileStream = File.Create(file);
resource.Seek(0, SeekOrigin.Begin);
resource.CopyTo(fileStream);
_files.Add(name, file);
}
}
public void SetCurrent(string id)
{
if (_current == id) return;
alsa?.Kill();
_current = id;
//TODO fix actually killing, remove orphan processes
alsa = new Process
{
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();
}
public void SetVolume(int percent)
{
}
}
}

View File

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

View File

@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using testexetrisathlon.SoundManagement;
using static System.Console;
namespace testexetrisathlon
@ -85,7 +86,7 @@ namespace testexetrisathlon
{
Location.ForEach(s => Program.DroppedTetrominoeLocationGrid[s[0], s[1]] = 1);
Program.IsDropped = true;
Beep(800, 200);
Beeper.Beep(800, 200);
}
else
{

View File

@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<OutputType>Exe</OutputType>
<LangVersion>8</LangVersion>
<Nullable>enable</Nullable>
@ -9,7 +8,12 @@
<Deterministic>false</Deterministic>
</PropertyGroup>
<PropertyGroup>
<PostBuildEvent>if exist "$(SolutionDir)Data\pkgtool.exe" ($(SolutionDir)Data\pkgtool.exe build --noLogo --binDir .) else if exist "%appdata%\UpTool2\Apps\0e35d154-d0d3-45e0-b080-62f521263a44\app\pkgtool.exe" ("%appdata%\UpTool2\Apps\0e35d154-d0d3-45e0-b080-62f521263a44\app\pkgtool.exe" build --noLogo --binDir .) else echo Cound not find Package build tools, skipping</PostBuildEvent>
<PostBuildEvent Condition="'$(OS)' == 'Windows_NT' ">
if exist "$(SolutionDir)Data\pkgtool.exe" ($(SolutionDir)Data\pkgtool.exe build --noLogo --binDir .) else if exist "%appdata%\UpTool2\Apps\0e35d154-d0d3-45e0-b080-62f521263a44\app\pkgtool.exe" ("%appdata%\UpTool2\Apps\0e35d154-d0d3-45e0-b080-62f521263a44\app\pkgtool.exe" build --noLogo --binDir .) else echo Cound not find Package build tools, skipping
</PostBuildEvent>
<PostBuildEvent Condition="'$(OS)' != 'Windows_NT'">
if [[ -f "$(SolutionDir)Data/pkgtool.exe" ]]; then $(SolutionDir)Data/pkgtool.exe build --noLogo --binDir .; else echo Cound not find Package build tools, skipping; fi
</PostBuildEvent>
</PropertyGroup>
<PropertyGroup>
<ApplicationIcon>tetris_yUxH6t_256px.ico</ApplicationIcon>