123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- using System.Runtime.InteropServices;
- using System.Text;
- using Bmp.Core.Common.Utility;
- using Bmp.Core.Lite.Playback.Outputs.NAudioASIO.Originals;
- using NAudio.Wave;
- using WaveFormat = NAudio.Wave.WaveFormat;
- namespace Bmp.Core.Lite.Playback.Inputs;
- public class DffAudioStream : DsdAudioStream, IHaveDecoderInfo, IHaveAsioSampleFormat
- {
- private const string ID_FRM8 = "FRM8";
- private const string ID_DSD = "DSD ";
- private const string ID_SND = "SND ";
- private const string ID_FS = "FS ";
- private const string ID_CHNL = "CHNL";
- private const string ID_CMPR = "CMPR";
- private const string ID_DST = "DST ";
- private const string ID_LSCO = "LSCO";
- private const string ID_PROP = "PROP";
- private const string ID_DIIN = "DIIN";
- private const string ID_MARK = "MARK";
- private const int FILE_VERSION = 0x01050000;
- string IHaveDecoderInfo.DecoderName => "DFF";
- string? IHaveDecoderInfo.FileFormat => "V1.5";
- public AsioSampleType? AsioSampleFormat => AsioSampleType.DSDInt8MSB1;
- public static bool IsDffFile(Stream stream)
- {
- if (stream.Length > 4)
- {
- using var binaryReader = new BinaryReader(stream, Encoding.UTF8, true);
- var signatureDSD = Encoding.ASCII.GetString(binaryReader.ReadBytes(4));
- if (signatureDSD == ID_FRM8) return true;
- }
- return false;
- }
- [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
- public struct ID
- {
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
- public string Value;
- }
- [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
- public struct Chunk
- {
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
- public string Id;
- private readonly long chunkDataSize;
- public long DataSize => chunkDataSize.NetworkOrderToHostOrder();
- }
- private readonly Stream _underlyingStream;
- private readonly bool _disposeStream;
- private readonly int _fileVersion;
- private readonly int _sampleRate;
- private readonly short _channelCount;
- private readonly IReadOnlyCollection<string> _channelIds;
- private readonly bool _isDstEncoded;
- private readonly short _loudspeakerConfig;
- private readonly long _dataOffset;
- private readonly long _dataSize;
- public DffAudioStream(string urlOrPath) : this(InputSourceProviderLite.ReadContentAsSeekableStream(urlOrPath), true)
- {
- }
- public DffAudioStream(Stream underlyingStream, bool disposeStream)
- {
- _underlyingStream = underlyingStream;
- _disposeStream = disposeStream;
- var ck = new Chunk();
- var sizeOfCk = Marshal.SizeOf<Chunk>();
- var sizeOfId = Marshal.SizeOf<ID>();
- if (underlyingStream.Length < sizeOfCk) throw new InvalidDataException("too small as dff file");
- underlyingStream.ReadStruct(ref ck);
- if (ck.Id != ID_FRM8) throw new InvalidDataException("invalid FRM8 signature");
- var frm8Size = ck.DataSize;
- var id = underlyingStream.ReadStruct<ID>();
- if (id.Value != ID_DSD) throw new InvalidDataException("invalid DSD signature");
- while (_underlyingStream.Position < (frm8Size + sizeOfCk))
- {
- underlyingStream.ReadStruct(ref ck);
- switch ((ck.Id, ck.DataSize))
- {
- case ("FVER", 4):
- _fileVersion = _underlyingStream.ReadStruct<int>().NetworkOrderToHostOrder();
- if (_fileVersion != FILE_VERSION) throw new InvalidDataException("invalid File version");
- continue;
- default:
- switch (ck.Id)
- {
- case ID_PROP:
- underlyingStream.ReadStruct(ref id);
- if (id.Value != ID_SND) throw new InvalidDataException("invalid SND signature");
- var id_prop_end = _underlyingStream.Position - sizeOfId + ck.DataSize;
- while (_underlyingStream.Position < id_prop_end)
- {
- underlyingStream.ReadStruct(ref ck);
- switch (ck.Id, ck.DataSize)
- {
- case (ID_FS, 4):
- _sampleRate = _underlyingStream.ReadStruct<int>().NetworkOrderToHostOrder();
- break;
- case (ID_LSCO, 2):
- _loudspeakerConfig = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
- break;
- default:
- switch (ck.Id)
- {
- case ID_CHNL:
- _channelCount = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
- _channelIds = Enumerable.Range(0, _channelCount).Select(_ => _underlyingStream.ReadStruct<ID>().Value).ToArray();
- break;
- case ID_CMPR:
- var compressionId = _underlyingStream.ReadStruct<ID>().Value;
- _isDstEncoded = compressionId == ID_DST;
- var len = _underlyingStream.ReadStruct<byte>();
- var text = Encoding.UTF8.GetString(_underlyingStream.ReadBytes(len));
- break;
- default:
- //skip unknown chunk
- _underlyingStream.Seek(ck.DataSize, SeekOrigin.Current);
- break;
- }
- break;
- }
- //align: pad byte char with value 0, used for making chunks an even length.
- _underlyingStream.Seek(_underlyingStream.Position % 2, SeekOrigin.Current);
- }
- break; //case ID_PROP:
- case ID_DIIN:
- var id_diin_end = _underlyingStream.Position - sizeOfId + ck.DataSize;
- while (_underlyingStream.Position < id_diin_end)
- {
- underlyingStream.ReadStruct(ref ck);
- switch (ck.Id)
- {
- case ID_MARK:
- var hours = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
- var minutes = _underlyingStream.ReadStruct<byte>();
- var second = _underlyingStream.ReadStruct<byte>();
- var samples = _underlyingStream.ReadStruct<uint>().NetworkOrderToHostOrder();
- var offset = _underlyingStream.ReadStruct<int>().NetworkOrderToHostOrder();
- // 0 TrackStart
- // 1 TrackStop
- // 2 ProgramStart
- // 4 Index Entry point of an Index
- var type = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
- // 0 All channels
- var channel = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
- var trackFlags = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
- var textLen = _underlyingStream.ReadStruct<int>().NetworkOrderToHostOrder();
- var text = Encoding.UTF8.GetString(_underlyingStream.ReadBytes(textLen));
- break;
- default:
- //skip unknown chunk
- _underlyingStream.Seek(ck.DataSize, SeekOrigin.Current);
- break;
- }
- _underlyingStream.Seek(_underlyingStream.Position % 2, SeekOrigin.Current);
- }
- break; // case ID_DIIN:
- case ID_DSD:
- _dataOffset = _underlyingStream.Position;
- _dataSize = ck.DataSize;
- _underlyingStream.Seek(ck.DataSize, SeekOrigin.Current);
- continue;
- default:
- //skip unknown chunk
- underlyingStream.Seek(ck.DataSize, SeekOrigin.Current);
- break;
- }
- break;
- }
- //align: pad byte char with value 0, used for making chunks an even length.
- _underlyingStream.Seek(_underlyingStream.Position % 2, SeekOrigin.Current);
- if (_isDstEncoded) throw new NotSupportedException("DST is not support");
- WaveFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Unknown, _sampleRate, _channelCount, _sampleRate / 8 * _channelCount, _channelCount, 1);
- }
- _underlyingStream.Position = _dataOffset;
- }
- protected override void Dispose(bool disposing)
- {
- base.Dispose(disposing);
- if (_disposeStream) _underlyingStream.Dispose();
- }
- public override int Read(byte[] buffer, int offset, int countRequired)
- {
- var mod = countRequired % _channelCount;
- var count = countRequired;
- if (mod != 0) count -= mod;
- var read = _underlyingStream.Read(buffer, offset, count);
- // MSB / LSB Convert?
- //for (var i = 0; i < read; i++) buffer[i] = BitReverseTable[buffer[i]];
- return read;
- }
- public override WaveFormat WaveFormat { get; }
- public override long Length => _dataSize;
- public override long Position { get => _underlyingStream.Position - _dataOffset; set => _underlyingStream.Position = _dataOffset + value - value % _channelCount; }
- }
|