2014-11-07 06:44:22 +08:00
|
|
|
//! \file AudioWAV.cs
|
|
|
|
//! \date Tue Nov 04 18:22:37 2014
|
|
|
|
//! \brief WAVE audio format implementation.
|
|
|
|
//
|
2015-05-02 03:30:57 +08:00
|
|
|
// Copyright (C) 2014-2015 by morkt
|
2014-11-07 06:44:22 +08:00
|
|
|
//
|
|
|
|
// 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-05-16 04:35:32 +08:00
|
|
|
using System.Collections.Generic;
|
2014-11-07 06:44:22 +08:00
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
using System.IO;
|
2015-03-31 18:38:47 +08:00
|
|
|
using System.Text;
|
2015-05-03 09:40:34 +08:00
|
|
|
using NAudio.Wave;
|
2014-11-07 06:44:22 +08:00
|
|
|
|
|
|
|
namespace GameRes
|
|
|
|
{
|
|
|
|
public class WaveInput : SoundInput
|
|
|
|
{
|
2015-05-14 15:26:47 +08:00
|
|
|
WaveStream m_reader;
|
|
|
|
|
2014-11-07 06:44:22 +08:00
|
|
|
public override long Position
|
|
|
|
{
|
2015-05-14 15:26:47 +08:00
|
|
|
get { return m_reader.Position; }
|
|
|
|
set { m_reader.Position = value; }
|
2014-11-07 06:44:22 +08:00
|
|
|
}
|
|
|
|
|
2015-05-14 15:26:47 +08:00
|
|
|
public override bool CanSeek { get { return m_reader.CanSeek; } }
|
2014-11-07 06:44:22 +08:00
|
|
|
|
|
|
|
public override int SourceBitrate
|
|
|
|
{
|
|
|
|
get { return (int)Format.AverageBytesPerSecond * 8; }
|
|
|
|
}
|
|
|
|
|
2015-05-14 15:26:47 +08:00
|
|
|
public override string SourceFormat { get { return "wav"; } }
|
|
|
|
|
2015-05-16 04:35:32 +08:00
|
|
|
static readonly HashSet<WaveFormatEncoding> ConversionRequired = new HashSet<WaveFormatEncoding> {
|
|
|
|
WaveFormatEncoding.Adpcm, // 2
|
|
|
|
WaveFormatEncoding.MuLaw, // 7
|
|
|
|
WaveFormatEncoding.DviAdpcm, // 0x11
|
|
|
|
};
|
|
|
|
|
2014-11-07 06:44:22 +08:00
|
|
|
public WaveInput (Stream file) : base (file)
|
|
|
|
{
|
2015-05-14 15:26:47 +08:00
|
|
|
m_reader = new WaveFileReader (file);
|
|
|
|
var wf = m_reader.WaveFormat;
|
2015-05-16 04:35:32 +08:00
|
|
|
if (ConversionRequired.Contains (wf.Encoding))
|
2014-11-07 06:44:22 +08:00
|
|
|
{
|
2015-05-14 15:26:47 +08:00
|
|
|
var wav = WaveFormatConversionStream.CreatePcmStream (m_reader);
|
2015-05-03 09:40:34 +08:00
|
|
|
wf = wav.WaveFormat;
|
2015-05-14 15:26:47 +08:00
|
|
|
m_reader = wav;
|
2014-11-07 06:44:22 +08:00
|
|
|
}
|
2015-05-03 09:40:34 +08:00
|
|
|
var format = new GameRes.WaveFormat();
|
|
|
|
format.FormatTag = (ushort)wf.Encoding;
|
|
|
|
format.Channels = (ushort)wf.Channels;
|
|
|
|
format.SamplesPerSecond = (uint)wf.SampleRate;
|
|
|
|
format.BitsPerSample = (ushort)wf.BitsPerSample;
|
|
|
|
format.BlockAlign = (ushort)wf.BlockAlign;
|
|
|
|
format.AverageBytesPerSecond = (uint)wf.AverageBytesPerSecond;
|
|
|
|
this.Format = format;
|
2015-05-14 15:26:47 +08:00
|
|
|
this.PcmSize = m_reader.Length;
|
2014-11-07 06:44:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public override int Read (byte[] buffer, int offset, int count)
|
|
|
|
{
|
2015-05-14 15:26:47 +08:00
|
|
|
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);
|
|
|
|
}
|
2014-11-07 06:44:22 +08:00
|
|
|
}
|
2015-05-14 15:26:47 +08:00
|
|
|
#endregion
|
2014-11-07 06:44:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
[Export(typeof(AudioFormat))]
|
|
|
|
public class WaveAudio : AudioFormat
|
|
|
|
{
|
|
|
|
public override string Tag { get { return "WAV"; } }
|
|
|
|
public override string Description { get { return "Wave audio format"; } }
|
|
|
|
public override uint Signature { get { return 0x46464952; } } // 'RIFF'
|
2016-10-11 04:05:22 +08:00
|
|
|
public override bool CanWrite { get { return true; } }
|
2014-11-07 06:44:22 +08:00
|
|
|
|
2015-11-04 08:02:42 +08:00
|
|
|
static readonly HashSet<ushort> EmbeddedFormats = new HashSet<ushort> {
|
|
|
|
0x674f, 0x6751, 0x6771, // Vorbis
|
|
|
|
0x0055, // MpegLayer3
|
|
|
|
};
|
|
|
|
|
2016-10-15 13:34:46 +08:00
|
|
|
public override SoundInput TryOpen (IBinaryStream file)
|
2014-11-07 06:44:22 +08:00
|
|
|
{
|
2017-12-31 14:36:10 +08:00
|
|
|
var header = file.ReadHeader (0x16);
|
2016-12-17 22:45:38 +08:00
|
|
|
if (!header.AsciiEqual (8, "WAVE"))
|
|
|
|
return null;
|
2018-01-22 21:27:24 +08:00
|
|
|
ushort tag = header.ToUInt16 (0x14);
|
2018-04-01 12:07:43 +08:00
|
|
|
if (0xFFFF == tag || 0x676F == tag || 0x6770 == tag || 0x674F == tag)
|
2017-12-31 14:36:10 +08:00
|
|
|
return null;
|
2016-12-17 22:45:38 +08:00
|
|
|
file.Position = 0;
|
2016-10-15 13:34:46 +08:00
|
|
|
SoundInput sound = new WaveInput (file.AsStream);
|
2015-11-04 08:02:42 +08:00
|
|
|
if (EmbeddedFormats.Contains (sound.Format.FormatTag))
|
2015-05-02 03:30:57 +08:00
|
|
|
{
|
2016-10-15 13:34:46 +08:00
|
|
|
var bin = new BinaryStream (sound, file.Name);
|
2015-05-02 03:30:57 +08:00
|
|
|
try
|
|
|
|
{
|
2016-10-15 13:34:46 +08:00
|
|
|
var embedded = AudioFormat.Read (bin);
|
2015-05-15 21:33:08 +08:00
|
|
|
if (null != embedded)
|
2015-05-02 03:30:57 +08:00
|
|
|
{
|
2015-05-15 21:33:08 +08:00
|
|
|
sound = embedded;
|
2015-05-02 03:30:57 +08:00
|
|
|
}
|
|
|
|
}
|
2015-05-16 21:11:15 +08:00
|
|
|
catch
|
|
|
|
{
|
|
|
|
sound.Position = 0;
|
|
|
|
}
|
2015-05-02 03:30:57 +08:00
|
|
|
}
|
|
|
|
return sound;
|
2014-11-07 06:44:22 +08:00
|
|
|
}
|
2015-03-31 18:38:47 +08:00
|
|
|
|
|
|
|
public override void Write (SoundInput source, Stream output)
|
2017-01-31 19:34:33 +08:00
|
|
|
{
|
|
|
|
WriteRiffHeader (output, source.Format, (uint)source.PcmSize);
|
|
|
|
source.Position = 0;
|
|
|
|
source.CopyTo (output);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void WriteRiffHeader (Stream output, WaveFormat format, uint pcm_size)
|
2015-03-31 18:38:47 +08:00
|
|
|
{
|
|
|
|
using (var buffer = new BinaryWriter (output, Encoding.ASCII, true))
|
|
|
|
{
|
2017-01-31 19:34:33 +08:00
|
|
|
uint total_size = (uint)(0x24 + pcm_size);
|
|
|
|
buffer.Write (Wav.Signature);
|
2015-03-31 18:38:47 +08:00
|
|
|
buffer.Write (total_size);
|
|
|
|
buffer.Write (0x45564157); // 'WAVE'
|
|
|
|
buffer.Write (0x20746d66); // 'fmt '
|
2017-01-31 19:34:33 +08:00
|
|
|
buffer.Write (0x10);
|
|
|
|
buffer.Write (format.FormatTag);
|
|
|
|
buffer.Write (format.Channels);
|
|
|
|
buffer.Write (format.SamplesPerSecond);
|
|
|
|
buffer.Write (format.AverageBytesPerSecond);
|
|
|
|
buffer.Write (format.BlockAlign);
|
|
|
|
buffer.Write (format.BitsPerSample);
|
2015-03-31 18:38:47 +08:00
|
|
|
buffer.Write (0x61746164); // 'data'
|
2017-01-31 19:34:33 +08:00
|
|
|
buffer.Write (pcm_size);
|
2015-03-31 18:38:47 +08:00
|
|
|
}
|
|
|
|
}
|
2014-11-07 06:44:22 +08:00
|
|
|
}
|
|
|
|
}
|