Initial commit: OpenRA game engine
Fork from OpenRA/OpenRA with one-click launch script (start-ra.cmd) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
120
OpenRA.Mods.Common/AudioLoaders/Mp3Loader.cs
Normal file
120
OpenRA.Mods.Common/AudioLoaders/Mp3Loader.cs
Normal file
@@ -0,0 +1,120 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright (c) The OpenRA Developers and Contributors
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using MP3Sharp;
|
||||
using OpenRA.Primitives;
|
||||
|
||||
namespace OpenRA.Mods.Common.AudioLoaders
|
||||
{
|
||||
public class Mp3Loader : ISoundLoader
|
||||
{
|
||||
static bool IsMp3(Stream s)
|
||||
{
|
||||
var start = s.Position;
|
||||
|
||||
// First try: MP3 may have ID3 meta data in front.
|
||||
var idTag = s.ReadASCII(3);
|
||||
s.Position = start;
|
||||
|
||||
if (idTag == "ID3")
|
||||
return true;
|
||||
|
||||
// Second try: MP3 without metadata, starts with MPEG chunk.
|
||||
var frameSync = s.ReadUInt16();
|
||||
s.Position = start;
|
||||
|
||||
if (frameSync == 0xfbff)
|
||||
return true;
|
||||
|
||||
// Neither found, not an MP3!
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ISoundLoader.TryParseSound(Stream stream, out ISoundFormat sound)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsMp3(stream))
|
||||
{
|
||||
sound = new Mp3Format(stream);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Not a (supported) MP3
|
||||
}
|
||||
|
||||
sound = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class Mp3Format : ISoundFormat
|
||||
{
|
||||
public int Channels => mp3.ChannelCount;
|
||||
public int SampleBits => 16;
|
||||
public int SampleRate => mp3.Frequency;
|
||||
public float LengthInSeconds { get; }
|
||||
public Stream GetPCMInputStream() { return new MP3Stream(Clone(this)); }
|
||||
public void Dispose() { mp3.Dispose(); }
|
||||
|
||||
readonly MP3Stream mp3;
|
||||
readonly Stream stream;
|
||||
|
||||
public Mp3Format(Stream stream)
|
||||
{
|
||||
var startPosition = stream.Position;
|
||||
try
|
||||
{
|
||||
mp3 = new MP3Stream(stream);
|
||||
this.stream = stream;
|
||||
|
||||
// Make a first guess based on the file size and bitrate
|
||||
// This should be fine for constant bitrate files
|
||||
LengthInSeconds = mp3.Length * 8f / (2f * Channels * SampleRate);
|
||||
|
||||
try
|
||||
{
|
||||
// Attempt to parse a more accurate length from the file metadata;
|
||||
LengthInSeconds = (float)new TagLib.Mpeg.AudioFile(new StreamAbstraction(stream)).Properties.Duration.TotalSeconds;
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
finally
|
||||
{
|
||||
stream.Position = startPosition;
|
||||
}
|
||||
}
|
||||
|
||||
static Stream Clone(Mp3Format cloneFrom)
|
||||
{
|
||||
return SegmentStream.CreateWithoutOwningStream(cloneFrom.stream, 0, (int)cloneFrom.stream.Length);
|
||||
}
|
||||
|
||||
public class StreamAbstraction : TagLib.File.IFileAbstraction
|
||||
{
|
||||
public StreamAbstraction(Stream s)
|
||||
{
|
||||
ReadStream = s;
|
||||
}
|
||||
|
||||
public Stream ReadStream { get; }
|
||||
|
||||
public Stream WriteStream => throw new NotImplementedException();
|
||||
|
||||
public void CloseStream(Stream stream) { }
|
||||
public string Name => "";
|
||||
}
|
||||
}
|
||||
}
|
||||
150
OpenRA.Mods.Common/AudioLoaders/OggLoader.cs
Normal file
150
OpenRA.Mods.Common/AudioLoaders/OggLoader.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright (c) The OpenRA Developers and Contributors
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using NVorbis;
|
||||
using OpenRA.Primitives;
|
||||
|
||||
namespace OpenRA.Mods.Common.AudioLoaders
|
||||
{
|
||||
public class OggLoader : ISoundLoader
|
||||
{
|
||||
bool ISoundLoader.TryParseSound(Stream stream, out ISoundFormat sound)
|
||||
{
|
||||
try
|
||||
{
|
||||
sound = new OggFormat(stream);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Unsupported file
|
||||
}
|
||||
|
||||
sound = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class OggFormat : ISoundFormat
|
||||
{
|
||||
public int SampleBits => 16;
|
||||
public int Channels => reader.Channels;
|
||||
public int SampleRate => reader.SampleRate;
|
||||
public float LengthInSeconds { get; }
|
||||
public Stream GetPCMInputStream() { return new OggStream(new OggFormat(this)); }
|
||||
public void Dispose() { reader.Dispose(); }
|
||||
|
||||
readonly VorbisReader reader;
|
||||
readonly Stream stream;
|
||||
|
||||
public OggFormat(Stream stream)
|
||||
{
|
||||
var startPosition = stream.Position;
|
||||
try
|
||||
{
|
||||
this.stream = stream;
|
||||
reader = new VorbisReader(stream, false);
|
||||
LengthInSeconds = (float)reader.TotalTime.TotalSeconds;
|
||||
}
|
||||
finally
|
||||
{
|
||||
stream.Position = startPosition;
|
||||
}
|
||||
}
|
||||
|
||||
OggFormat(OggFormat cloneFrom)
|
||||
{
|
||||
stream = SegmentStream.CreateWithoutOwningStream(cloneFrom.stream, 0, (int)cloneFrom.stream.Length);
|
||||
reader = new VorbisReader(stream, false)
|
||||
{
|
||||
// Tell NVorbis to clip samples so we don't have to range-check during reading.
|
||||
ClipSamples = true
|
||||
};
|
||||
}
|
||||
|
||||
public class OggStream : Stream
|
||||
{
|
||||
readonly OggFormat format;
|
||||
|
||||
float[] conversionBuffer;
|
||||
|
||||
public OggStream(OggFormat format)
|
||||
{
|
||||
this.format = format;
|
||||
}
|
||||
|
||||
public override bool CanRead => true;
|
||||
public override bool CanSeek => false;
|
||||
public override bool CanWrite => false;
|
||||
|
||||
public override long Length => format.reader.TotalSamples;
|
||||
|
||||
public override long Position
|
||||
{
|
||||
get => format.reader.SamplePosition;
|
||||
set => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
return Read(buffer.AsSpan(offset, count));
|
||||
}
|
||||
|
||||
public override int Read(Span<byte> buffer)
|
||||
{
|
||||
// Adjust count so it is in 16-bit samples instead of bytes.
|
||||
var count = buffer.Length / 2;
|
||||
|
||||
// Make sure we don't have an odd count.
|
||||
count -= count % format.reader.Channels;
|
||||
|
||||
var floatBuffer = EnsureArraySize(ref conversionBuffer, count);
|
||||
|
||||
// Let NVorbis do the actual reading.
|
||||
var samples = format.reader.ReadSamples(floatBuffer);
|
||||
|
||||
// Move the data back to the request buffer and convert to 16-bit signed samples for OpenAL.
|
||||
for (var i = 0; i < samples; i++)
|
||||
{
|
||||
var conversion = (short)(floatBuffer[i] * 32767);
|
||||
buffer[i * 2 + 0] = (byte)(conversion & 255);
|
||||
buffer[i * 2 + 1] = (byte)(conversion >> 8);
|
||||
}
|
||||
|
||||
// Adjust count back to bytes.
|
||||
return samples * 2;
|
||||
}
|
||||
|
||||
public override void Flush() { throw new NotImplementedException(); }
|
||||
public override long Seek(long offset, SeekOrigin origin) { throw new NotImplementedException(); }
|
||||
public override void SetLength(long value) { throw new NotImplementedException(); }
|
||||
public override void Write(byte[] buffer, int offset, int count) { throw new NotImplementedException(); }
|
||||
public override void Write(ReadOnlySpan<byte> buffer) { throw new NotImplementedException(); }
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
format.reader.Dispose();
|
||||
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
static Span<float> EnsureArraySize(ref float[] array, int desiredSize)
|
||||
{
|
||||
if (array == null || array.Length < desiredSize)
|
||||
array = new float[desiredSize];
|
||||
return array.AsSpan(..desiredSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
75
OpenRA.Mods.Common/AudioLoaders/WavLoader.cs
Normal file
75
OpenRA.Mods.Common/AudioLoaders/WavLoader.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright (c) The OpenRA Developers and Contributors
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using OpenRA.Mods.Common.FileFormats;
|
||||
|
||||
namespace OpenRA.Mods.Common.AudioLoaders
|
||||
{
|
||||
public class WavLoader : ISoundLoader
|
||||
{
|
||||
static bool IsWave(Stream s)
|
||||
{
|
||||
var start = s.Position;
|
||||
var type = s.ReadASCII(4);
|
||||
s.Position += 4;
|
||||
var format = s.ReadASCII(4);
|
||||
s.Position = start;
|
||||
|
||||
return type == "RIFF" && format == "WAVE";
|
||||
}
|
||||
|
||||
bool ISoundLoader.TryParseSound(Stream stream, out ISoundFormat sound)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsWave(stream))
|
||||
{
|
||||
sound = new WavFormat(stream);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Not a (supported) WAV
|
||||
}
|
||||
|
||||
sound = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class WavFormat : ISoundFormat
|
||||
{
|
||||
public int Channels => channels;
|
||||
public int SampleBits => sampleBits;
|
||||
public int SampleRate => sampleRate;
|
||||
public float LengthInSeconds => lengthInSeconds;
|
||||
public Stream GetPCMInputStream() { return wavStreamFactory(); }
|
||||
public void Dispose() { sourceStream.Dispose(); }
|
||||
|
||||
readonly Stream sourceStream;
|
||||
readonly Func<Stream> wavStreamFactory;
|
||||
readonly short channels;
|
||||
readonly int sampleBits;
|
||||
readonly int sampleRate;
|
||||
readonly float lengthInSeconds;
|
||||
|
||||
public WavFormat(Stream stream)
|
||||
{
|
||||
sourceStream = stream;
|
||||
|
||||
if (!WavReader.LoadSound(stream, out wavStreamFactory, out channels, out sampleBits, out sampleRate, out lengthInSeconds))
|
||||
throw new InvalidDataException();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user