2014-11-07 02:44:22 +04:00
|
|
|
//! \file Audio.cs
|
|
|
|
//! \date Tue Nov 04 17:35:54 2014
|
|
|
|
//! \brief audio format class.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2014 by morkt
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to
|
|
|
|
// deal in the Software without restriction, including without limitation the
|
|
|
|
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
// sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
// IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
|
2015-08-14 06:05:15 +04:00
|
|
|
using System;
|
2014-11-07 02:44:22 +04:00
|
|
|
using System.IO;
|
2015-08-14 06:05:15 +04:00
|
|
|
using System.Linq;
|
2014-11-07 02:44:22 +04:00
|
|
|
|
|
|
|
namespace GameRes
|
|
|
|
{
|
|
|
|
public struct WaveFormat
|
|
|
|
{
|
|
|
|
public ushort FormatTag;
|
|
|
|
public ushort Channels;
|
|
|
|
public uint SamplesPerSecond;
|
|
|
|
public uint AverageBytesPerSecond;
|
|
|
|
public ushort BlockAlign;
|
|
|
|
public ushort BitsPerSample;
|
2015-03-31 14:35:44 +04:00
|
|
|
public ushort ExtraSize;
|
2017-02-25 02:03:11 +04:00
|
|
|
|
|
|
|
public void SetBPS ()
|
|
|
|
{
|
|
|
|
AverageBytesPerSecond = (uint)(SamplesPerSecond * Channels * BitsPerSample / 8);
|
|
|
|
}
|
2014-11-07 02:44:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public abstract class SoundInput : Stream
|
|
|
|
{
|
2015-05-14 11:26:47 +04:00
|
|
|
public abstract int SourceBitrate { get; }
|
|
|
|
public abstract string SourceFormat { get; }
|
2014-11-07 02:44:22 +04:00
|
|
|
|
|
|
|
public WaveFormat Format { get; protected set; }
|
2015-05-14 11:26:47 +04:00
|
|
|
public Stream Source { get; protected set; }
|
|
|
|
public long PcmSize { get; protected set; }
|
|
|
|
|
2014-11-07 02:44:22 +04:00
|
|
|
|
|
|
|
protected SoundInput (Stream input)
|
|
|
|
{
|
2015-05-14 11:26:47 +04:00
|
|
|
Source = input;
|
2014-11-07 02:44:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public virtual void Reset ()
|
|
|
|
{
|
|
|
|
Position = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#region System.IO.Stream methods
|
2015-05-14 11:26:47 +04:00
|
|
|
public override bool CanRead { get { return Source.CanRead; } }
|
|
|
|
public override bool CanWrite { get { return false; } }
|
|
|
|
public override long Length { get { return PcmSize; } }
|
|
|
|
|
2014-11-07 02:44:22 +04:00
|
|
|
public override void Flush()
|
|
|
|
{
|
2015-05-14 11:26:47 +04:00
|
|
|
Source.Flush();
|
2014-11-07 02:44:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
|
|
{
|
|
|
|
if (origin == SeekOrigin.Begin)
|
|
|
|
Position = offset;
|
|
|
|
else if (origin == SeekOrigin.Current)
|
|
|
|
Position += offset;
|
|
|
|
else
|
|
|
|
Position = Length + offset;
|
|
|
|
return Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void SetLength (long length)
|
|
|
|
{
|
|
|
|
throw new System.NotSupportedException ("SoundInput.SetLength method is not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void Write (byte[] buffer, int offset, int count)
|
|
|
|
{
|
|
|
|
throw new System.NotSupportedException ("SoundInput.Write method is not supported");
|
|
|
|
}
|
|
|
|
|
|
|
|
public override void WriteByte (byte value)
|
|
|
|
{
|
|
|
|
throw new System.NotSupportedException ("SoundInput.WriteByte method is not supported");
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region IDisposable Members
|
|
|
|
bool disposed = false;
|
|
|
|
protected override void Dispose (bool disposing)
|
|
|
|
{
|
|
|
|
if (!disposed)
|
|
|
|
{
|
|
|
|
if (disposing)
|
|
|
|
{
|
2015-05-14 11:26:47 +04:00
|
|
|
Source.Dispose();
|
2014-11-07 02:44:22 +04:00
|
|
|
}
|
|
|
|
disposed = true;
|
|
|
|
base.Dispose (disposing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
2016-03-03 08:51:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Class representing raw PCM sound input.
|
|
|
|
/// </summary>
|
|
|
|
public class RawPcmInput : SoundInput
|
|
|
|
{
|
|
|
|
public override string SourceFormat { get { return "raw"; } }
|
|
|
|
|
|
|
|
public override int SourceBitrate
|
|
|
|
{
|
|
|
|
get { return (int)Format.AverageBytesPerSecond * 8; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public RawPcmInput (Stream file, WaveFormat format) : base (file)
|
|
|
|
{
|
|
|
|
this.Format = format;
|
|
|
|
this.PcmSize = file.Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
#region IO.Stream methods
|
|
|
|
public override long Position
|
|
|
|
{
|
|
|
|
get { return Source.Position; }
|
|
|
|
set { Source.Position = value; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool CanSeek { get { return Source.CanSeek; } }
|
|
|
|
|
|
|
|
public override long Seek (long offset, SeekOrigin origin)
|
|
|
|
{
|
|
|
|
return Source.Seek (offset, origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int Read (byte[] buffer, int offset, int count)
|
|
|
|
{
|
|
|
|
return Source.Read (buffer, offset, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override int ReadByte ()
|
|
|
|
{
|
|
|
|
return Source.ReadByte();
|
|
|
|
}
|
|
|
|
#endregion
|
2014-11-07 02:44:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public abstract class AudioFormat : IResource
|
|
|
|
{
|
|
|
|
public override string Type { get { return "audio"; } }
|
|
|
|
|
2016-10-15 09:34:46 +04:00
|
|
|
public abstract SoundInput TryOpen (IBinaryStream file);
|
2014-11-07 02:44:22 +04:00
|
|
|
|
2015-03-31 14:35:44 +04:00
|
|
|
public virtual void Write (SoundInput source, Stream output)
|
|
|
|
{
|
|
|
|
throw new System.NotImplementedException ("AudioFormat.Write not implemenented");
|
|
|
|
}
|
|
|
|
|
2016-10-15 09:34:46 +04:00
|
|
|
public static SoundInput Read (IBinaryStream file)
|
2014-11-07 02:44:22 +04:00
|
|
|
{
|
2016-12-17 18:51:33 +04:00
|
|
|
foreach (var impl in FormatCatalog.Instance.FindFormats<AudioFormat> (file.Name, file.Signature))
|
2014-11-07 02:44:22 +04:00
|
|
|
{
|
2016-12-17 18:51:33 +04:00
|
|
|
try
|
2014-11-07 02:44:22 +04:00
|
|
|
{
|
2016-12-17 18:51:33 +04:00
|
|
|
file.Position = 0;
|
|
|
|
SoundInput sound = impl.TryOpen (file);
|
|
|
|
if (null != sound)
|
|
|
|
return sound;
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (System.Exception X)
|
|
|
|
{
|
|
|
|
FormatCatalog.Instance.LastError = X;
|
2014-11-07 02:44:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-08-14 06:05:15 +04:00
|
|
|
|
2023-10-11 18:53:12 +04:00
|
|
|
public static AudioFormat Wav => s_WavFormat.Value;
|
2015-08-14 06:05:15 +04:00
|
|
|
|
2023-10-11 18:53:12 +04:00
|
|
|
static readonly ResourceInstance<AudioFormat> s_WavFormat = new ResourceInstance<AudioFormat> ("WAV");
|
2014-11-07 02:44:22 +04:00
|
|
|
}
|
|
|
|
}
|