diff --git a/OpenRa.FileFormats/AudLoader.cs b/OpenRa.FileFormats/AudLoader.cs new file mode 100644 index 0000000000..5248897cfa --- /dev/null +++ b/OpenRa.FileFormats/AudLoader.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.IO; + +namespace OpenRa.FileFormats +{ + [Flags] + enum SoundFlags + { + Stereo = 0x1, + _16Bit = 0x2, + } + + enum SoundFormat + { + WestwoodCompressed = 1, + ImaAdpcm = 99, + } + + struct Chunk + { + public int CompressedSize; + public int OutputSize; + + public static Chunk Read(BinaryReader r) + { + Chunk c; + c.CompressedSize = r.ReadUInt16(); + c.OutputSize = r.ReadUInt16(); + if (0xdeaf != r.ReadUInt32()) + throw new InvalidDataException("Chunk header is bogus"); + return c; + } + } + + public static class AudLoader + { + static int[] IndexAdjust = { -1, -1, -1, -1, 2, 4, 6, 8 }; + static int[] StepTable = { + 7, 8, 9, 10, 11, 12, 13, 14, 16, + 17, 19, 21, 23, 25, 28, 31, 34, 37, + 41, 45, 50, 55, 60, 66, 73, 80, 88, + 97, 107, 118, 130, 143, 157, 173, 190, 209, + 230, 253, 279, 307, 337, 371, 408, 449, 494, + 544, 598, 658, 724, 796, 876, 963, 1060, 1166, + 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, + 3024, 3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, + 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, + 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767 }; + + static short DecodeSample(byte b, ref int index, ref int current) + { + var sb = (b & 8) != 0; + b &= 7; + + var delta = (StepTable[index] * b) / 4 + StepTable[index] / 8; + if (sb) delta = -delta; + + current += delta; + if (current > short.MaxValue) current = short.MaxValue; + if (current < short.MinValue) current = short.MinValue; + + index += IndexAdjust[b]; + if (index < 0) index = 0; + if (index > 88) index = 88; + + return (short)current; + } + + public static byte[] LoadSound(Stream s) + { + var br = new BinaryReader(s); + var sampleRate = br.ReadUInt16(); + var dataSize = br.ReadInt32(); + var outputSize = br.ReadInt32(); + var flags = (SoundFlags)br.ReadByte(); + var format = (SoundFormat)br.ReadByte(); + + var output = new byte[outputSize]; + var offset = 0; + var index = 0; + var currentSample = 0; + + while (dataSize > 0) + { + var chunk = Chunk.Read(br); + for (int n = 0; n < chunk.CompressedSize; n++) + { + var b = br.ReadByte(); + + var t = DecodeSample(b, ref index, ref currentSample); + output[offset++] = (byte)t; + output[offset++] = (byte)(t >> 8); + + t = DecodeSample((byte)(b >> 4), ref index, ref currentSample); + output[offset++] = (byte)t; + output[offset++] = (byte)(t >> 8); + } + + dataSize -= 8 + chunk.CompressedSize; + } + + return output; + } + } +} diff --git a/OpenRa.FileFormats/OpenRa.FileFormats.csproj b/OpenRa.FileFormats/OpenRa.FileFormats.csproj index d62caf0414..e0540c5965 100644 --- a/OpenRa.FileFormats/OpenRa.FileFormats.csproj +++ b/OpenRa.FileFormats/OpenRa.FileFormats.csproj @@ -44,6 +44,7 @@ + diff --git a/OpenRa.Game/Game.cs b/OpenRa.Game/Game.cs index ae839b54af..5ed79a37f0 100644 --- a/OpenRa.Game/Game.cs +++ b/OpenRa.Game/Game.cs @@ -3,7 +3,8 @@ using OpenRa.FileFormats; using OpenRa.Game.Graphics; using OpenRa.TechTree; using System.Drawing; -using System.Linq; +using System.Linq; +using IrrKlang; namespace OpenRa.Game { @@ -24,7 +25,9 @@ namespace OpenRa.Game public readonly Dictionary players = new Dictionary(); public Player LocalPlayer { get { return players[localPlayerIndex]; } } - public BuildingInfluenceMap LocalPlayerBuildings; + public BuildingInfluenceMap LocalPlayerBuildings; + + ISoundEngine soundEngine; public Game(string mapName, Renderer renderer, int2 clientSize) { @@ -52,7 +55,22 @@ namespace OpenRa.Game network = new Network(); controller = new Controller(this); // CAREFUL THERES AN UGLY HIDDEN DEPENDENCY HERE STILL - worldRenderer = new WorldRenderer(renderer, this); + worldRenderer = new WorldRenderer(renderer, this); + + var sound = AudLoader.LoadSound(FileSystem.Open("intro.aud")); + + soundEngine = new ISoundEngine(); + + var soundSource = soundEngine.AddSoundSourceFromPCMData(sound, "intro.aud", + new AudioFormat() + { + ChannelCount = 1, + FrameCount = sound.Length / 2, + Format = SampleFormat.Signed16Bit, + SampleRate = 22050 + }); + + soundEngine.Play2D(soundSource, true, false, true); } public void Tick() diff --git a/OpenRa.Game/MainWindow.cs b/OpenRa.Game/MainWindow.cs index 3f05bf0e80..709c7de954 100644 --- a/OpenRa.Game/MainWindow.cs +++ b/OpenRa.Game/MainWindow.cs @@ -29,7 +29,8 @@ namespace OpenRa.Game FileSystem.Mount(new Package("redalert.mix")); FileSystem.Mount(new Package("conquer.mix")); FileSystem.Mount(new Package("hires.mix")); - FileSystem.Mount(new Package("general.mix")); + FileSystem.Mount(new Package("general.mix")); + FileSystem.Mount(new Package("local.mix")); FormBorderStyle = FormBorderStyle.None; BackColor = Color.Black; diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index 82f0380c62..07c8bbd518 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -60,6 +60,10 @@ False ..\Ijw.DirectX\Ijw.Framework\IjwFramework\bin\Debug\IjwFramework.dll + + False + ..\thirdparty\irrKlang.NET2.0.dll + 3.5 diff --git a/OpenRa.Game/Sidebar.cs b/OpenRa.Game/Sidebar.cs index e58d11485c..0045f131f0 100644 --- a/OpenRa.Game/Sidebar.cs +++ b/OpenRa.Game/Sidebar.cs @@ -148,7 +148,7 @@ namespace OpenRa.Game { if (mi.Button == MouseButtons.Left && mi.Event == MouseInputEvent.Down) { - var point = new float2(mi.Location.X, mi.Location.Y); + var point = mi.Location.ToFloat2(); var item = GetItem(point); if (item != null) { @@ -162,7 +162,7 @@ namespace OpenRa.Game } else if( mi.Button == MouseButtons.Right && mi.Event == MouseInputEvent.Down ) { - var point = new float2(mi.Location.X, mi.Location.Y); + var point = mi.Location.ToFloat2(); var item = GetItem(point); if( item != null ) { diff --git a/thirdparty/irrKlang.NET2.0.dll b/thirdparty/irrKlang.NET2.0.dll new file mode 100644 index 0000000000..a558e4fb00 Binary files /dev/null and b/thirdparty/irrKlang.NET2.0.dll differ