Providing streaming AudFormat data.
AudFormat.GetPCMInputStream now returns data that is streamed, rather than a MemoryStream.
This commit is contained in:
committed by
Paul Chote
parent
85c948fd8d
commit
9413d9595c
@@ -161,6 +161,7 @@
|
||||
<Compile Include="Player.cs" />
|
||||
<Compile Include="Primitives\MergedStream.cs" />
|
||||
<Compile Include="Primitives\PlayerDictionary.cs" />
|
||||
<Compile Include="Primitives\ReadOnlyAdapterStream.cs" />
|
||||
<Compile Include="Primitives\SegmentStream.cs" />
|
||||
<Compile Include="Primitives\SpatiallyPartitioned.cs" />
|
||||
<Compile Include="Primitives\ConcurrentCache.cs" />
|
||||
|
||||
89
OpenRA.Game/Primitives/ReadOnlyAdapterStream.cs
Normal file
89
OpenRA.Game/Primitives/ReadOnlyAdapterStream.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2017 The OpenRA Developers (see AUTHORS)
|
||||
* 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.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace OpenRA.Primitives
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a read-only buffering layer so data can be streamed from sources where reading arbitrary amounts of
|
||||
/// data is difficult.
|
||||
/// </summary>
|
||||
public abstract class ReadOnlyAdapterStream : Stream
|
||||
{
|
||||
readonly Queue<byte> data = new Queue<byte>(1024);
|
||||
readonly Stream baseStream;
|
||||
|
||||
protected ReadOnlyAdapterStream(Stream stream)
|
||||
{
|
||||
if (stream == null)
|
||||
throw new ArgumentNullException("stream");
|
||||
if (!stream.CanRead)
|
||||
throw new ArgumentException("stream must be readable.", "stream");
|
||||
|
||||
baseStream = stream;
|
||||
}
|
||||
|
||||
public sealed override bool CanSeek { get { return false; } }
|
||||
public sealed override bool CanRead { get { return true; } }
|
||||
public sealed override bool CanWrite { get { return false; } }
|
||||
|
||||
public sealed override long Length { get { throw new NotSupportedException(); } }
|
||||
public sealed override long Position
|
||||
{
|
||||
get { throw new NotSupportedException(); }
|
||||
set { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
public sealed override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
|
||||
public sealed override void SetLength(long value) { throw new NotSupportedException(); }
|
||||
public sealed override void Write(byte[] buffer, int offset, int count) { throw new NotSupportedException(); }
|
||||
public sealed override void Flush() { throw new NotSupportedException(); }
|
||||
|
||||
public sealed override int Read(byte[] buffer, int offset, int count)
|
||||
{
|
||||
var copied = 0;
|
||||
ConsumeData(buffer, offset, count, ref copied);
|
||||
|
||||
var finished = false;
|
||||
while (copied < count && !finished)
|
||||
{
|
||||
finished = BufferData(baseStream, data);
|
||||
ConsumeData(buffer, offset, count, ref copied);
|
||||
}
|
||||
|
||||
return copied;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads data into a buffer, which will be used to satisfy <see cref="Read(byte[], int, int)"/> calls.
|
||||
/// </summary>
|
||||
/// <param name="baseStream">The source stream from which bytes should be read.</param>
|
||||
/// <param name="data">The queue where bytes should be enqueued. Do not dequeue from this buffer.</param>
|
||||
/// <returns>Return true if all data has been read; otherwise, false.</returns>
|
||||
protected abstract bool BufferData(Stream baseStream, Queue<byte> data);
|
||||
|
||||
void ConsumeData(byte[] buffer, int offset, int count, ref int copied)
|
||||
{
|
||||
while (copied < count && data.Count > 0)
|
||||
buffer[offset + copied++] = data.Dequeue();
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
baseStream.Dispose();
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,15 @@ namespace OpenRA.Primitives
|
||||
public readonly long BaseOffset;
|
||||
public readonly long BaseCount;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="SegmentStream"/> that wraps a subset of the source stream. This takes ownership of
|
||||
/// the source stream. The <see cref="SegmentStream"/> is dependent on the source and changes its underlying
|
||||
/// position.
|
||||
/// </summary>
|
||||
/// <param name="stream">The source stream, of which only a segment should be exposed. Ownership is transferred
|
||||
/// to the <see cref="SegmentStream"/>.</param>
|
||||
/// <param name="offset">The offset at which the segment starts.</param>
|
||||
/// <param name="count">The length of the segment.</param>
|
||||
public SegmentStream(Stream stream, long offset, long count)
|
||||
{
|
||||
if (stream == null)
|
||||
@@ -90,7 +99,7 @@ namespace OpenRA.Primitives
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
public static long GetOverallNestedOffset(Stream stream, out Stream overallBaseStream)
|
||||
static long GetOverallNestedOffset(Stream stream, out Stream overallBaseStream)
|
||||
{
|
||||
var offset = 0L;
|
||||
overallBaseStream = stream;
|
||||
@@ -99,5 +108,34 @@ namespace OpenRA.Primitives
|
||||
offset += segmentStream.BaseOffset + GetOverallNestedOffset(segmentStream.BaseStream, out overallBaseStream);
|
||||
return offset;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Stream"/> that wraps a subset of the source stream without taking ownership of it,
|
||||
/// allowing it to be reused by the caller. The <see cref="Stream"/> is independent of the source stream and
|
||||
/// won't affect its position.
|
||||
/// </summary>
|
||||
/// <param name="stream">The source stream, of which only a segment should be exposed. Ownership is retained by
|
||||
/// the caller.</param>
|
||||
/// <param name="offset">The offset at which the segment starts.</param>
|
||||
/// <param name="count">The length of the segment.</param>
|
||||
public static Stream CreateWithoutOwningStream(Stream stream, long offset, int count)
|
||||
{
|
||||
Stream parentStream;
|
||||
var nestedOffset = offset + GetOverallNestedOffset(stream, out parentStream);
|
||||
|
||||
// Special case FileStream - instead of creating an in-memory copy,
|
||||
// just reference the portion of the on-disk file that we need to save memory.
|
||||
// We use GetType instead of 'is' here since we can't handle any derived classes of FileStream.
|
||||
if (parentStream.GetType() == typeof(FileStream))
|
||||
{
|
||||
var path = ((FileStream)parentStream).Name;
|
||||
return new SegmentStream(File.OpenRead(path), nestedOffset, count);
|
||||
}
|
||||
|
||||
// For all other streams, create a copy in memory.
|
||||
// This uses more memory but is the only way in general to ensure the returned streams won't clash.
|
||||
stream.Seek(offset, SeekOrigin.Begin);
|
||||
return new MemoryStream(stream.ReadBytes(count));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace OpenRA
|
||||
@@ -117,7 +118,17 @@ namespace OpenRA
|
||||
public static byte[] ReadAllBytes(this Stream s)
|
||||
{
|
||||
using (s)
|
||||
return s.ReadBytes((int)(s.Length - s.Position));
|
||||
{
|
||||
if (s.CanSeek)
|
||||
return s.ReadBytes((int)(s.Length - s.Position));
|
||||
|
||||
var bytes = new List<byte>();
|
||||
var buffer = new byte[1024];
|
||||
int count;
|
||||
while ((count = s.Read(buffer, 0, buffer.Length)) > 0)
|
||||
bytes.AddRange(buffer.Take(count));
|
||||
return bytes.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public static void Write(this Stream s, byte[] data)
|
||||
|
||||
@@ -183,23 +183,7 @@ namespace OpenRA.Mods.Cnc.FileSystem
|
||||
|
||||
public Stream GetContent(PackageEntry entry)
|
||||
{
|
||||
Stream parentStream;
|
||||
var baseOffset = dataStart + entry.Offset;
|
||||
var nestedOffset = baseOffset + SegmentStream.GetOverallNestedOffset(s, out parentStream);
|
||||
|
||||
// Special case FileStream - instead of creating an in-memory copy,
|
||||
// just reference the portion of the on-disk file that we need to save memory.
|
||||
// We use GetType instead of 'is' here since we can't handle any derived classes of FileStream.
|
||||
if (parentStream.GetType() == typeof(FileStream))
|
||||
{
|
||||
var path = ((FileStream)parentStream).Name;
|
||||
return new SegmentStream(File.OpenRead(path), nestedOffset, entry.Length);
|
||||
}
|
||||
|
||||
// For all other streams, create a copy in memory.
|
||||
// This uses more memory but is the only way in general to ensure the returned streams won't clash.
|
||||
s.Seek(baseOffset, SeekOrigin.Begin);
|
||||
return new MemoryStream(s.ReadBytes((int)entry.Length));
|
||||
return SegmentStream.CreateWithoutOwningStream(s, dataStart + entry.Offset, (int)entry.Length);
|
||||
}
|
||||
|
||||
public Stream GetStream(string filename)
|
||||
|
||||
@@ -56,34 +56,20 @@ namespace OpenRA.Mods.Common.AudioLoaders
|
||||
public int Channels { get { return 1; } }
|
||||
public int SampleBits { get { return 16; } }
|
||||
public int SampleRate { get { return sampleRate; } }
|
||||
public float LengthInSeconds { get { return AudReader.SoundLength(stream); } }
|
||||
public Stream GetPCMInputStream() { return new MemoryStream(rawData.Value); }
|
||||
public void Dispose() { stream.Dispose(); }
|
||||
public float LengthInSeconds { get { return AudReader.SoundLength(sourceStream); } }
|
||||
public Stream GetPCMInputStream() { return audStreamFactory(); }
|
||||
public void Dispose() { sourceStream.Dispose(); }
|
||||
|
||||
int sampleRate;
|
||||
Lazy<byte[]> rawData;
|
||||
|
||||
Stream stream;
|
||||
readonly Stream sourceStream;
|
||||
readonly Func<Stream> audStreamFactory;
|
||||
readonly int sampleRate;
|
||||
|
||||
public AudFormat(Stream stream)
|
||||
{
|
||||
this.stream = stream;
|
||||
sourceStream = stream;
|
||||
|
||||
var position = stream.Position;
|
||||
rawData = Exts.Lazy(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] data;
|
||||
if (!AudReader.LoadSound(stream, out data, out sampleRate))
|
||||
throw new InvalidDataException();
|
||||
return data;
|
||||
}
|
||||
finally
|
||||
{
|
||||
stream.Position = position;
|
||||
}
|
||||
});
|
||||
if (!AudReader.LoadSound(stream, out audStreamFactory, out sampleRate))
|
||||
throw new InvalidDataException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,9 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using OpenRA.Primitives;
|
||||
|
||||
namespace OpenRA.Mods.Common.FileFormats
|
||||
{
|
||||
@@ -44,7 +46,7 @@ namespace OpenRA.Mods.Common.FileFormats
|
||||
}
|
||||
}
|
||||
|
||||
public class AudReader
|
||||
public static class AudReader
|
||||
{
|
||||
static readonly int[] IndexAdjust = { -1, -1, -1, -1, 2, 4, 6, 8 };
|
||||
static readonly int[] StepTable =
|
||||
@@ -116,55 +118,87 @@ namespace OpenRA.Mods.Common.FileFormats
|
||||
var samples = outputSize;
|
||||
if (0 != (flags & SoundFlags.Stereo)) samples /= 2;
|
||||
if (0 != (flags & SoundFlags._16Bit)) samples /= 2;
|
||||
return samples / sampleRate;
|
||||
return (float)samples / sampleRate;
|
||||
}
|
||||
|
||||
public static bool LoadSound(Stream s, out byte[] rawData, out int sampleRate)
|
||||
public static bool LoadSound(Stream s, out Func<Stream> result, out int sampleRate)
|
||||
{
|
||||
rawData = null;
|
||||
|
||||
sampleRate = s.ReadUInt16();
|
||||
var dataSize = s.ReadInt32();
|
||||
var outputSize = s.ReadInt32();
|
||||
|
||||
var readFlag = s.ReadByte();
|
||||
if (!Enum.IsDefined(typeof(SoundFlags), readFlag))
|
||||
return false;
|
||||
|
||||
var readFormat = s.ReadByte();
|
||||
if (!Enum.IsDefined(typeof(SoundFormat), readFormat))
|
||||
return false;
|
||||
|
||||
var output = new byte[outputSize];
|
||||
var offset = 0;
|
||||
var index = 0;
|
||||
var currentSample = 0;
|
||||
|
||||
while (dataSize > 0)
|
||||
result = null;
|
||||
var startPosition = s.Position;
|
||||
try
|
||||
{
|
||||
var chunk = Chunk.Read(s);
|
||||
sampleRate = s.ReadUInt16();
|
||||
var dataSize = s.ReadInt32();
|
||||
var outputSize = s.ReadInt32();
|
||||
|
||||
var readFlag = s.ReadByte();
|
||||
if (!Enum.IsDefined(typeof(SoundFlags), readFlag))
|
||||
return false;
|
||||
|
||||
var readFormat = s.ReadByte();
|
||||
if (!Enum.IsDefined(typeof(SoundFormat), readFormat))
|
||||
return false;
|
||||
|
||||
var offsetPosition = s.Position;
|
||||
|
||||
result = () =>
|
||||
{
|
||||
var audioStream = SegmentStream.CreateWithoutOwningStream(s, offsetPosition, (int)(s.Length - offsetPosition));
|
||||
return new AudStream(audioStream, outputSize, dataSize);
|
||||
};
|
||||
}
|
||||
finally
|
||||
{
|
||||
s.Position = startPosition;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
sealed class AudStream : ReadOnlyAdapterStream
|
||||
{
|
||||
readonly int outputSize;
|
||||
int dataSize;
|
||||
|
||||
int currentSample;
|
||||
int baseOffset;
|
||||
int index;
|
||||
|
||||
public AudStream(Stream stream, int outputSize, int dataSize) : base(stream)
|
||||
{
|
||||
this.outputSize = outputSize;
|
||||
this.dataSize = dataSize;
|
||||
}
|
||||
|
||||
protected override bool BufferData(Stream baseStream, Queue<byte> data)
|
||||
{
|
||||
if (dataSize <= 0)
|
||||
return true;
|
||||
|
||||
var chunk = Chunk.Read(baseStream);
|
||||
for (var n = 0; n < chunk.CompressedSize; n++)
|
||||
{
|
||||
var b = s.ReadUInt8();
|
||||
var b = baseStream.ReadUInt8();
|
||||
|
||||
var t = DecodeSample(b, ref index, ref currentSample);
|
||||
output[offset++] = (byte)t;
|
||||
output[offset++] = (byte)(t >> 8);
|
||||
data.Enqueue((byte)t);
|
||||
data.Enqueue((byte)(t >> 8));
|
||||
baseOffset += 2;
|
||||
|
||||
if (offset < outputSize)
|
||||
if (baseOffset < outputSize)
|
||||
{
|
||||
/* possible that only half of the final byte is used! */
|
||||
t = DecodeSample((byte)(b >> 4), ref index, ref currentSample);
|
||||
output[offset++] = (byte)t;
|
||||
output[offset++] = (byte)(t >> 8);
|
||||
data.Enqueue((byte)t);
|
||||
data.Enqueue((byte)(t >> 8));
|
||||
baseOffset += 2;
|
||||
}
|
||||
}
|
||||
|
||||
dataSize -= 8 + chunk.CompressedSize;
|
||||
}
|
||||
|
||||
rawData = output;
|
||||
return true;
|
||||
return dataSize <= 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user