Files
cuetools.net/AudioCodecsDotNet/AudioCodecsDotNet.cs

680 lines
15 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace AudioCodecsDotNet
{
public interface IAudioSource
{
uint Read(int[,] buff, uint sampleCount);
ulong Length { get; }
ulong Position { get; set; }
NameValueCollection Tags { get; set; }
ulong Remaining { get; }
void Close();
int BitsPerSample { get; }
int ChannelCount { get; }
int SampleRate { get; }
string Path { get; }
}
public interface IAudioDest
{
void Write(int[,] buff, uint sampleCount);
bool SetTags(NameValueCollection tags);
void Close();
void Delete();
int BitsPerSample { get; }
long FinalSampleCount { set; }
2008-11-21 20:06:11 +00:00
long BlockSize { set; }
string Path { get; }
}
2008-11-10 23:54:03 +00:00
public class AudioSamples
{
public static unsafe void FLACSamplesToBytes_16(int[,] inSamples, uint inSampleOffset,
byte[] outSamples, uint outByteOffset, uint sampleCount, int channelCount)
{
uint loopCount = sampleCount * (uint)channelCount;
if ((inSamples.GetLength(0) - inSampleOffset < sampleCount) ||
(outSamples.Length - outByteOffset < loopCount * 2))
{
throw new IndexOutOfRangeException();
}
fixed (int* pInSamplesFixed = &inSamples[inSampleOffset, 0])
{
fixed (byte* pOutSamplesFixed = &outSamples[outByteOffset])
{
int* pInSamples = pInSamplesFixed;
short* pOutSamples = (short*)pOutSamplesFixed;
for (int i = 0; i < loopCount; i++)
{
*(pOutSamples++) = (short)*(pInSamples++);
}
}
}
}
public static unsafe void FLACSamplesToBytes_24(int[,] inSamples, uint inSampleOffset,
byte[] outSamples, uint outByteOffset, uint sampleCount, int channelCount, int wastedBits)
{
uint loopCount = sampleCount * (uint)channelCount;
if ((inSamples.GetLength(0) - inSampleOffset < sampleCount) ||
(outSamples.Length - outByteOffset < loopCount * 3))
{
throw new IndexOutOfRangeException();
}
fixed (int* pInSamplesFixed = &inSamples[inSampleOffset, 0])
{
fixed (byte* pOutSamplesFixed = &outSamples[outByteOffset])
{
int* pInSamples = pInSamplesFixed;
byte* pOutSamples = pOutSamplesFixed;
for (int i = 0; i < loopCount; i++)
{
uint sample_out = (uint)*(pInSamples++) << wastedBits;
*(pOutSamples++) = (byte)(sample_out & 0xFF);
sample_out >>= 8;
*(pOutSamples++) = (byte)(sample_out & 0xFF);
sample_out >>= 8;
*(pOutSamples++) = (byte)(sample_out & 0xFF);
}
}
}
}
public static unsafe void FLACSamplesToBytes(int[,] inSamples, uint inSampleOffset,
byte[] outSamples, uint outByteOffset, uint sampleCount, int channelCount, int bitsPerSample)
{
if (bitsPerSample == 16)
2008-11-10 23:54:03 +00:00
AudioSamples.FLACSamplesToBytes_16(inSamples, inSampleOffset, outSamples, outByteOffset, sampleCount, channelCount);
else if (bitsPerSample > 16 && bitsPerSample <= 24)
AudioSamples.FLACSamplesToBytes_24(inSamples, inSampleOffset, outSamples, outByteOffset, sampleCount, channelCount, 24 - bitsPerSample);
else
throw new Exception("Unsupported bitsPerSample value");
}
public static unsafe void BytesToFLACSamples_16(byte[] inSamples, int inByteOffset,
int[,] outSamples, int outSampleOffset, uint sampleCount, int channelCount)
{
uint loopCount = sampleCount * (uint)channelCount;
if ((inSamples.Length - inByteOffset < loopCount * 2) ||
(outSamples.GetLength(0) - outSampleOffset < sampleCount))
{
throw new IndexOutOfRangeException();
}
fixed (byte* pInSamplesFixed = &inSamples[inByteOffset])
{
fixed (int* pOutSamplesFixed = &outSamples[outSampleOffset, 0])
{
short* pInSamples = (short*)pInSamplesFixed;
int* pOutSamples = pOutSamplesFixed;
for (int i = 0; i < loopCount; i++)
{
*(pOutSamples++) = (int)*(pInSamples++);
}
}
}
}
}
public class DummyWriter : IAudioDest
{
public DummyWriter(string path, int bitsPerSample, int channelCount, int sampleRate)
{
_bitsPerSample = bitsPerSample;
}
public bool SetTags(NameValueCollection tags)
{
return false;
}
public void Close()
{
}
public void Delete()
{
}
public long FinalSampleCount
{
2008-11-21 20:06:11 +00:00
set { }
}
public long BlockSize
{
set { }
}
public int BitsPerSample
{
get { return _bitsPerSample; }
}
public void Write(int[,] buff, uint sampleCount)
{
}
public string Path { get { return null; } }
int _bitsPerSample;
}
public class SilenceGenerator : IAudioSource
{
private ulong _sampleOffset, _sampleCount;
public SilenceGenerator(uint sampleCount)
{
_sampleOffset = 0;
_sampleCount = sampleCount;
}
public ulong Length
{
get
{
return _sampleCount;
}
}
public ulong Remaining
{
get
{
return _sampleCount - _sampleOffset;
}
}
public ulong Position
{
get
{
return _sampleOffset;
}
set
{
_sampleOffset = value;
}
}
public int BitsPerSample
{
get
{
return 16;
}
}
public int ChannelCount
{
get
{
return 2;
}
}
public int SampleRate
{
get
{
return 44100;
}
}
public NameValueCollection Tags
{
get
{
return new NameValueCollection();
}
set
{
}
}
public uint Read(int [,] buff, uint sampleCount)
{
uint samplesRemaining = (uint)(_sampleCount - _sampleOffset);
if (sampleCount > samplesRemaining)
sampleCount = samplesRemaining;
for (uint i = 0; i < sampleCount; i++)
for (int j = 0; j < buff.GetLength(1); j++)
buff[i,j] = 0;
_sampleOffset += sampleCount;
return sampleCount;
}
public void Close()
{
}
public string Path { get { return null; } }
}
public class WAVReader : IAudioSource
{
2008-11-10 09:31:14 +00:00
Stream _IO;
BinaryReader _br;
ulong _dataOffset, _dataLen;
ulong _samplePos, _sampleLen;
int _bitsPerSample, _channelCount, _sampleRate, _blockAlign;
bool _largeFile;
string _path;
private byte[] _sampleBuffer;
2008-11-10 09:31:14 +00:00
public WAVReader(string path, Stream IO)
{
_path = path;
2008-11-10 09:31:14 +00:00
_IO = IO != null ? IO : new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 0x10000, FileOptions.SequentialScan);
_br = new BinaryReader(_IO);
ParseHeaders();
_sampleLen = _dataLen / (uint)_blockAlign;
}
public void Close()
{
2008-11-10 09:31:14 +00:00
if (_br != null)
{
_br.Close();
_br = null;
}
_IO = null;
}
private void ParseHeaders()
{
const long maxFileSize = 0x7FFFFFFEL;
const uint fccRIFF = 0x46464952;
const uint fccWAVE = 0x45564157;
const uint fccFormat = 0x20746D66;
const uint fccData = 0x61746164;
uint lenRIFF;
long fileEnd;
bool foundFormat, foundData;
if (_br.ReadUInt32() != fccRIFF)
{
throw new Exception("Not a valid RIFF file.");
}
lenRIFF = _br.ReadUInt32();
fileEnd = (long)lenRIFF + 8;
if (_br.ReadUInt32() != fccWAVE)
{
throw new Exception("Not a valid WAVE file.");
}
_largeFile = false;
foundFormat = false;
foundData = false;
long pos = 12;
do
{
uint ckID, ckSize, ckSizePadded;
long ckEnd;
ckID = _br.ReadUInt32();
ckSize = _br.ReadUInt32();
ckSizePadded = (ckSize + 1U) & ~1U;
pos += 8;
ckEnd = pos + (long)ckSizePadded;
if (ckID == fccFormat)
{
foundFormat = true;
if (_br.ReadUInt16() != 1)
{
throw new Exception("WAVE must be PCM format.");
}
_channelCount = _br.ReadInt16();
_sampleRate = _br.ReadInt32();
_br.ReadInt32();
_blockAlign = _br.ReadInt16();
_bitsPerSample = _br.ReadInt16();
pos += 16;
}
else if (ckID == fccData)
{
foundData = true;
_dataOffset = (ulong)pos;
if (!_IO.CanSeek || _IO.Length <= maxFileSize)
{
_dataLen = ckSize;
}
else
{
_largeFile = true;
2008-11-10 09:31:14 +00:00
_dataLen = ((ulong)_IO.Length) - _dataOffset;
}
}
if ((foundFormat & foundData) || _largeFile)
break;
if (_IO.CanSeek)
_IO.Seek(ckEnd, SeekOrigin.Begin);
else
_br.ReadBytes((int)(ckEnd - pos));
pos = ckEnd;
} while (true);
if ((foundFormat & foundData) == false)
throw new Exception("Format or data chunk not found.");
if (_channelCount <= 0)
throw new Exception("Channel count is invalid.");
if (_sampleRate <= 0)
throw new Exception("Sample rate is invalid.");
if (_blockAlign != (_channelCount * ((_bitsPerSample + 7) / 8)))
throw new Exception("Block align is invalid.");
if ((_bitsPerSample <= 0) || (_bitsPerSample > 32))
throw new Exception("Bits per sample is invalid.");
if (pos != (long)_dataOffset)
Position = 0;
}
public ulong Position
{
get
{
return _samplePos;
}
set
{
ulong seekPos;
if (value > _sampleLen)
{
_samplePos = _sampleLen;
}
else
{
_samplePos = value;
}
seekPos = _dataOffset + (_samplePos * (uint)_blockAlign);
2008-11-10 09:31:14 +00:00
_IO.Seek((long)seekPos, SeekOrigin.Begin);
}
}
public ulong Length
{
get
{
return _sampleLen;
}
}
public ulong Remaining
{
get
{
return _sampleLen - _samplePos;
}
}
public int ChannelCount
{
get
{
return _channelCount;
}
}
public int SampleRate
{
get
{
return _sampleRate;
}
}
public int BitsPerSample
{
get
{
return _bitsPerSample;
}
}
public int BlockAlign
{
get
{
return _blockAlign;
}
}
public NameValueCollection Tags
{
get
{
return new NameValueCollection();
}
set
{
}
}
public void GetTags(out List<string> names, out List<string> values)
{
names = new List<string>();
values = new List<string>();
}
public uint Read(int[,] buff, uint sampleCount)
{
if (sampleCount > Remaining)
sampleCount = (uint)Remaining;
if (sampleCount == 0)
return 0;
int byteCount = (int) sampleCount * _blockAlign;
if (_sampleBuffer == null || _sampleBuffer.Length < byteCount)
_sampleBuffer = new byte[byteCount];
2008-11-10 09:31:14 +00:00
if (_IO.Read(_sampleBuffer, 0, (int)byteCount) != byteCount)
throw new Exception("Incomplete file read.");
2008-11-10 23:54:03 +00:00
AudioSamples.BytesToFLACSamples_16(_sampleBuffer, 0, buff, 0,
sampleCount, _channelCount);
_samplePos += sampleCount;
return sampleCount;
}
public string Path { get { return _path; } }
}
public class WAVWriter : IAudioDest
{
2008-11-10 09:31:14 +00:00
FileStream _IO;
BinaryWriter _bw;
int _bitsPerSample, _channelCount, _sampleRate, _blockAlign;
long _sampleLen;
string _path;
private byte[] _sampleBuffer;
2008-11-21 20:06:11 +00:00
long hdrLen = 44;
public WAVWriter(string path, int bitsPerSample, int channelCount, int sampleRate)
{
_path = path;
_bitsPerSample = bitsPerSample;
_channelCount = channelCount;
_sampleRate = sampleRate;
_blockAlign = _channelCount * ((_bitsPerSample + 7) / 8);
2008-11-10 09:31:14 +00:00
_IO = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read);
_bw = new BinaryWriter(_IO);
WriteHeaders();
}
public bool SetTags(NameValueCollection tags)
{
return false;
}
2008-11-21 20:06:11 +00:00
public void WriteChunk(uint fcc, byte[] data)
{
if (_sampleLen > 0)
throw new Exception("data already written, no chunks allowed");
const uint fccData = 0x61746164;
_bw.Seek((int)hdrLen - 8, SeekOrigin.Begin);
_bw.Write(fcc);
_bw.Write((uint)data.Length);
_bw.Write(data);
if ((data.Length & 1) != 0)
_bw.Write((byte) 0);
_bw.Write(fccData);
_bw.Write((uint)0);
hdrLen += data.Length + (data.Length & 1) + 8;
}
private void WriteHeaders()
{
const uint fccRIFF = 0x46464952;
const uint fccWAVE = 0x45564157;
const uint fccFormat = 0x20746D66;
const uint fccData = 0x61746164;
_bw.Write(fccRIFF);
_bw.Write((uint)0);
_bw.Write(fccWAVE);
_bw.Write(fccFormat);
if (_bitsPerSample != 16 && _bitsPerSample != 24)
{
_bw.Write((uint)40);
//_bw.Write((uint)16);
_bw.Write((ushort)0xfffe); // WAVEX follows
}
else
{
_bw.Write((uint)16);
_bw.Write((ushort)1); // PCM
}
_bw.Write((ushort)_channelCount);
_bw.Write((uint)_sampleRate);
_bw.Write((uint)(_sampleRate * _blockAlign));
_bw.Write((ushort)_blockAlign);
_bw.Write((ushort)((_bitsPerSample+7)/8*8));
hdrLen = 36;
if (_bitsPerSample != 16 && _bitsPerSample != 24)
{
_bw.Write((ushort)22); // length of WAVEX structure
_bw.Write((ushort)_bitsPerSample);
_bw.Write((uint)3); // speaker positions (3 == stereo)
_bw.Write((ushort)1); // PCM
_bw.Write((ushort)0);
_bw.Write((ushort)0);
_bw.Write((ushort)0x10);
_bw.Write((byte)0x80);
_bw.Write((byte)0x00);
_bw.Write((byte)0x00);
_bw.Write((byte)0xaa);
_bw.Write((byte)0x00);
_bw.Write((byte)0x38);
_bw.Write((byte)0x9b);
_bw.Write((byte)0x71);
hdrLen += 24;
}
_bw.Write(fccData);
_bw.Write((uint)0);
hdrLen += 8;
}
public void Close()
{
const long maxFileSize = 0x7FFFFFFEL;
2008-11-21 20:06:11 +00:00
long dataLen, dataLenPadded;
dataLen = _sampleLen * _blockAlign;
if ((dataLen & 1) == 1)
_bw.Write((byte)0);
2008-11-21 20:06:11 +00:00
if (dataLen + hdrLen > maxFileSize)
dataLen = ((maxFileSize - hdrLen) / _blockAlign) * _blockAlign;
2008-11-21 20:06:11 +00:00
dataLenPadded = dataLen + (dataLen & 1);
_bw.Seek(4, SeekOrigin.Begin);
2008-11-21 20:06:11 +00:00
_bw.Write((uint)(dataLenPadded + hdrLen - 8));
2008-11-21 20:06:11 +00:00
_bw.Seek((int)hdrLen-4, SeekOrigin.Begin);
_bw.Write((uint)dataLen);
_bw.Close();
_bw = null;
2008-11-10 09:31:14 +00:00
_IO = null;
}
public void Delete()
{
_bw.Close();
_bw = null;
_IO = null;
File.Delete(_path);
}
public long Position
{
get
{
return _sampleLen;
}
}
public long FinalSampleCount
{
2008-11-21 20:06:11 +00:00
set { }
}
public long BlockSize
{
set { }
}
public int BitsPerSample
{
get { return _bitsPerSample; }
}
public void Write(int[,] buff, uint sampleCount)
{
if (sampleCount == 0)
return;
if (_sampleBuffer == null || _sampleBuffer.Length < sampleCount * _blockAlign)
_sampleBuffer = new byte[sampleCount * _blockAlign];
2008-11-10 23:54:03 +00:00
AudioSamples.FLACSamplesToBytes(buff, 0, _sampleBuffer, 0,
sampleCount, _channelCount, _bitsPerSample);
2008-11-10 09:31:14 +00:00
_IO.Write(_sampleBuffer, 0, (int)sampleCount * _blockAlign);
_sampleLen += sampleCount;
}
public string Path { get { return _path; } }
}
}