refactored SoundInput interface.

added Source property for access to underlying stream.
This commit is contained in:
morkt 2015-05-14 11:26:47 +04:00
parent 7e5dc325c0
commit 9a43b6b055
7 changed files with 64 additions and 43 deletions

View File

@ -47,9 +47,11 @@ namespace GameRes.Formats
get { return m_bitrate; } get { return m_bitrate; }
} }
public override string SourceFormat { get { return "mp3"; } }
public Mp3Input (Stream file) : base (file) public Mp3Input (Stream file) : base (file)
{ {
m_reader = new Mp3FileReader (m_input); m_reader = new Mp3FileReader (file);
m_bitrate = m_reader.Mp3WaveFormat.AverageBytesPerSecond*8; m_bitrate = m_reader.Mp3WaveFormat.AverageBytesPerSecond*8;
var format = new GameRes.WaveFormat(); var format = new GameRes.WaveFormat();
format.FormatTag = (ushort)m_reader.WaveFormat.Encoding; format.FormatTag = (ushort)m_reader.WaveFormat.Encoding;

View File

@ -48,16 +48,18 @@ namespace GameRes.Formats
} }
} }
public override bool CanSeek { get { return m_input.CanSeek; } } public override bool CanSeek { get { return Source.CanSeek; } }
public override int SourceBitrate public override int SourceBitrate
{ {
get { return m_reader.NominalBitrate; } get { return m_reader.NominalBitrate; }
} }
public override string SourceFormat { get { return "ogg"; } }
public OggInput (Stream file) : base (file) public OggInput (Stream file) : base (file)
{ {
m_reader = new VorbisReader (m_input, false); m_reader = new VorbisReader (Source, false);
var format = new GameRes.WaveFormat(); var format = new GameRes.WaveFormat();
format.FormatTag = 3; // WAVE_FORMAT_IEEE_FLOAT format.FormatTag = 3; // WAVE_FORMAT_IEEE_FLOAT
format.Channels = (ushort)m_reader.Channels; format.Channels = (ushort)m_reader.Channels;

View File

@ -57,6 +57,8 @@ namespace GameRes.Formats.ScenePlayer
{ {
if (null == sound) if (null == sound)
wav.Dispose(); wav.Dispose();
else
file.Dispose();
} }
return sound; return sound;
} }

View File

@ -36,30 +36,32 @@ namespace GameRes.Formats.Marble
public override long Position public override long Position
{ {
get { return m_input.Position; } get { return Source.Position; }
set { m_input.Position = value; } set { Source.Position = value; }
} }
public override bool CanSeek { get { return m_input.CanSeek; } } public override bool CanSeek { get { return Source.CanSeek; } }
public override int SourceBitrate public override int SourceBitrate
{ {
get { return (int)Format.AverageBytesPerSecond * 8; } get { return (int)Format.AverageBytesPerSecond * 8; }
} }
public override string SourceFormat { get { return "raw"; } }
public override long Seek (long offset, SeekOrigin origin) public override long Seek (long offset, SeekOrigin origin)
{ {
return m_input.Seek (offset, origin); return Source.Seek (offset, origin);
} }
public override int Read (byte[] buffer, int offset, int count) public override int Read (byte[] buffer, int offset, int count)
{ {
return m_input.Read (buffer, offset, count); return Source.Read (buffer, offset, count);
} }
public override int ReadByte () public override int ReadByte ()
{ {
return m_input.ReadByte(); return Source.ReadByte();
} }
public WadyInput (Stream file) : base (new MemoryStream()) public WadyInput (Stream file) : base (new MemoryStream())
@ -83,13 +85,13 @@ namespace GameRes.Formats.Marble
int remaining = (int)(input.BaseStream.Length-input.BaseStream.Position); int remaining = (int)(input.BaseStream.Length-input.BaseStream.Position);
if (remaining == src_size) if (remaining == src_size)
{ {
(m_input as MemoryStream).Capacity = src_size * 2; (Source as MemoryStream).Capacity = src_size * 2;
Decode (input, src_size, m_input); Decode (input, src_size, Source);
} }
else else
Decode2 (input, m_input); Decode2 (input, Source);
m_input.Position = 0; Source.Position = 0;
this.PcmSize = m_input.Length; this.PcmSize = Source.Length;
} }
} }

View File

@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion ("1.0.4.50")] [assembly: AssemblyVersion ("1.0.4.51")]
[assembly: AssemblyFileVersion ("1.0.4.50")] [assembly: AssemblyFileVersion ("1.0.4.51")]

View File

@ -40,26 +40,17 @@ namespace GameRes
public abstract class SoundInput : Stream public abstract class SoundInput : Stream
{ {
protected Stream m_input; public abstract int SourceBitrate { get; }
protected long m_pcm_size; public abstract string SourceFormat { get; }
public override bool CanRead { get { return m_input.CanRead; } }
public override bool CanWrite { get { return false; } }
public override long Length { get { return m_pcm_size; } }
public long PcmSize
{
get { return m_pcm_size; }
protected set { m_pcm_size = value; }
}
public abstract int SourceBitrate { get; }
public WaveFormat Format { get; protected set; } public WaveFormat Format { get; protected set; }
public Stream Source { get; protected set; }
public long PcmSize { get; protected set; }
protected SoundInput (Stream input) protected SoundInput (Stream input)
{ {
m_input = input; Source = input;
} }
public virtual void Reset () public virtual void Reset ()
@ -68,9 +59,13 @@ namespace GameRes
} }
#region System.IO.Stream methods #region System.IO.Stream methods
public override bool CanRead { get { return Source.CanRead; } }
public override bool CanWrite { get { return false; } }
public override long Length { get { return PcmSize; } }
public override void Flush() public override void Flush()
{ {
m_input.Flush(); Source.Flush();
} }
public override long Seek(long offset, SeekOrigin origin) public override long Seek(long offset, SeekOrigin origin)
@ -108,7 +103,7 @@ namespace GameRes
{ {
if (disposing) if (disposing)
{ {
m_input.Dispose(); Source.Dispose();
} }
disposed = true; disposed = true;
base.Dispose (disposing); base.Dispose (disposing);

View File

@ -32,29 +32,32 @@ namespace GameRes
{ {
public class WaveInput : SoundInput public class WaveInput : SoundInput
{ {
WaveStream m_reader;
public override long Position public override long Position
{ {
get { return m_input.Position; } get { return m_reader.Position; }
set { m_input.Position = value; } set { m_reader.Position = value; }
} }
public override bool CanSeek { get { return m_input.CanSeek; } } public override bool CanSeek { get { return m_reader.CanSeek; } }
public override int SourceBitrate public override int SourceBitrate
{ {
get { return (int)Format.AverageBytesPerSecond * 8; } get { return (int)Format.AverageBytesPerSecond * 8; }
} }
public override string SourceFormat { get { return "wav"; } }
public WaveInput (Stream file) : base (file) public WaveInput (Stream file) : base (file)
{ {
var reader = new WaveFileReader (file); m_reader = new WaveFileReader (file);
m_input = reader; var wf = m_reader.WaveFormat;
var wf = reader.WaveFormat;
if (WaveFormatEncoding.Adpcm == wf.Encoding || WaveFormatEncoding.MuLaw == wf.Encoding) // 2 || 7 if (WaveFormatEncoding.Adpcm == wf.Encoding || WaveFormatEncoding.MuLaw == wf.Encoding) // 2 || 7
{ {
var wav = WaveFormatConversionStream.CreatePcmStream (reader); var wav = WaveFormatConversionStream.CreatePcmStream (m_reader);
wf = wav.WaveFormat; wf = wav.WaveFormat;
m_input = wav; m_reader = wav;
} }
var format = new GameRes.WaveFormat(); var format = new GameRes.WaveFormat();
format.FormatTag = (ushort)wf.Encoding; format.FormatTag = (ushort)wf.Encoding;
@ -64,13 +67,28 @@ namespace GameRes
format.BlockAlign = (ushort)wf.BlockAlign; format.BlockAlign = (ushort)wf.BlockAlign;
format.AverageBytesPerSecond = (uint)wf.AverageBytesPerSecond; format.AverageBytesPerSecond = (uint)wf.AverageBytesPerSecond;
this.Format = format; this.Format = format;
this.PcmSize = m_input.Length; this.PcmSize = m_reader.Length;
} }
public override int Read (byte[] buffer, int offset, int count) public override int Read (byte[] buffer, int offset, int count)
{ {
return m_input.Read (buffer, offset, count); return m_reader.Read (buffer, offset, count);
} }
#region IDisposable Members
protected override void Dispose (bool disposing)
{
if (null != m_reader)
{
if (disposing)
{
m_reader.Dispose();
}
m_reader = null;
base.Dispose (disposing);
}
}
#endregion
} }
[Export(typeof(AudioFormat))] [Export(typeof(AudioFormat))]