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
@@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user