DffAudioStream.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System.Runtime.InteropServices;
  2. using System.Text;
  3. using Bmp.Core.Common.Utility;
  4. using Bmp.Core.Lite.Playback.Outputs.NAudioASIO.Originals;
  5. using NAudio.Wave;
  6. using WaveFormat = NAudio.Wave.WaveFormat;
  7. namespace Bmp.Core.Lite.Playback.Inputs;
  8. public class DffAudioStream : DsdAudioStream, IHaveDecoderInfo, IHaveAsioSampleFormat
  9. {
  10. private const string ID_FRM8 = "FRM8";
  11. private const string ID_DSD = "DSD ";
  12. private const string ID_SND = "SND ";
  13. private const string ID_FS = "FS ";
  14. private const string ID_CHNL = "CHNL";
  15. private const string ID_CMPR = "CMPR";
  16. private const string ID_DST = "DST ";
  17. private const string ID_LSCO = "LSCO";
  18. private const string ID_PROP = "PROP";
  19. private const string ID_DIIN = "DIIN";
  20. private const string ID_MARK = "MARK";
  21. private const int FILE_VERSION = 0x01050000;
  22. string IHaveDecoderInfo.DecoderName => "DFF";
  23. string? IHaveDecoderInfo.FileFormat => "V1.5";
  24. public AsioSampleType? AsioSampleFormat => AsioSampleType.DSDInt8MSB1;
  25. public static bool IsDffFile(Stream stream)
  26. {
  27. if (stream.Length > 4)
  28. {
  29. using var binaryReader = new BinaryReader(stream, Encoding.UTF8, true);
  30. var signatureDSD = Encoding.ASCII.GetString(binaryReader.ReadBytes(4));
  31. if (signatureDSD == ID_FRM8) return true;
  32. }
  33. return false;
  34. }
  35. [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
  36. public struct ID
  37. {
  38. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
  39. public string Value;
  40. }
  41. [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
  42. public struct Chunk
  43. {
  44. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
  45. public string Id;
  46. private readonly long chunkDataSize;
  47. public long DataSize => chunkDataSize.NetworkOrderToHostOrder();
  48. }
  49. private readonly Stream _underlyingStream;
  50. private readonly bool _disposeStream;
  51. private readonly int _fileVersion;
  52. private readonly int _sampleRate;
  53. private readonly short _channelCount;
  54. private readonly IReadOnlyCollection<string> _channelIds;
  55. private readonly bool _isDstEncoded;
  56. private readonly short _loudspeakerConfig;
  57. private readonly long _dataOffset;
  58. private readonly long _dataSize;
  59. public DffAudioStream(string urlOrPath) : this(InputSourceProviderLite.ReadContentAsSeekableStream(urlOrPath), true)
  60. {
  61. }
  62. public DffAudioStream(Stream underlyingStream, bool disposeStream)
  63. {
  64. _underlyingStream = underlyingStream;
  65. _disposeStream = disposeStream;
  66. var ck = new Chunk();
  67. var sizeOfCk = Marshal.SizeOf<Chunk>();
  68. var sizeOfId = Marshal.SizeOf<ID>();
  69. if (underlyingStream.Length < sizeOfCk) throw new InvalidDataException("too small as dff file");
  70. underlyingStream.ReadStruct(ref ck);
  71. if (ck.Id != ID_FRM8) throw new InvalidDataException("invalid FRM8 signature");
  72. var frm8Size = ck.DataSize;
  73. var id = underlyingStream.ReadStruct<ID>();
  74. if (id.Value != ID_DSD) throw new InvalidDataException("invalid DSD signature");
  75. while (_underlyingStream.Position < (frm8Size + sizeOfCk))
  76. {
  77. underlyingStream.ReadStruct(ref ck);
  78. switch ((ck.Id, ck.DataSize))
  79. {
  80. case ("FVER", 4):
  81. _fileVersion = _underlyingStream.ReadStruct<int>().NetworkOrderToHostOrder();
  82. if (_fileVersion != FILE_VERSION) throw new InvalidDataException("invalid File version");
  83. continue;
  84. default:
  85. switch (ck.Id)
  86. {
  87. case ID_PROP:
  88. underlyingStream.ReadStruct(ref id);
  89. if (id.Value != ID_SND) throw new InvalidDataException("invalid SND signature");
  90. var id_prop_end = _underlyingStream.Position - sizeOfId + ck.DataSize;
  91. while (_underlyingStream.Position < id_prop_end)
  92. {
  93. underlyingStream.ReadStruct(ref ck);
  94. switch (ck.Id, ck.DataSize)
  95. {
  96. case (ID_FS, 4):
  97. _sampleRate = _underlyingStream.ReadStruct<int>().NetworkOrderToHostOrder();
  98. break;
  99. case (ID_LSCO, 2):
  100. _loudspeakerConfig = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
  101. break;
  102. default:
  103. switch (ck.Id)
  104. {
  105. case ID_CHNL:
  106. _channelCount = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
  107. _channelIds = Enumerable.Range(0, _channelCount).Select(_ => _underlyingStream.ReadStruct<ID>().Value).ToArray();
  108. break;
  109. case ID_CMPR:
  110. var compressionId = _underlyingStream.ReadStruct<ID>().Value;
  111. _isDstEncoded = compressionId == ID_DST;
  112. var len = _underlyingStream.ReadStruct<byte>();
  113. var text = Encoding.UTF8.GetString(_underlyingStream.ReadBytes(len));
  114. break;
  115. default:
  116. //skip unknown chunk
  117. _underlyingStream.Seek(ck.DataSize, SeekOrigin.Current);
  118. break;
  119. }
  120. break;
  121. }
  122. //align: pad byte char with value 0, used for making chunks an even length.
  123. _underlyingStream.Seek(_underlyingStream.Position % 2, SeekOrigin.Current);
  124. }
  125. break; //case ID_PROP:
  126. case ID_DIIN:
  127. var id_diin_end = _underlyingStream.Position - sizeOfId + ck.DataSize;
  128. while (_underlyingStream.Position < id_diin_end)
  129. {
  130. underlyingStream.ReadStruct(ref ck);
  131. switch (ck.Id)
  132. {
  133. case ID_MARK:
  134. var hours = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
  135. var minutes = _underlyingStream.ReadStruct<byte>();
  136. var second = _underlyingStream.ReadStruct<byte>();
  137. var samples = _underlyingStream.ReadStruct<uint>().NetworkOrderToHostOrder();
  138. var offset = _underlyingStream.ReadStruct<int>().NetworkOrderToHostOrder();
  139. // 0 TrackStart
  140. // 1 TrackStop
  141. // 2 ProgramStart
  142. // 4 Index Entry point of an Index
  143. var type = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
  144. // 0 All channels
  145. var channel = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
  146. var trackFlags = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
  147. var textLen = _underlyingStream.ReadStruct<int>().NetworkOrderToHostOrder();
  148. var text = Encoding.UTF8.GetString(_underlyingStream.ReadBytes(textLen));
  149. break;
  150. default:
  151. //skip unknown chunk
  152. _underlyingStream.Seek(ck.DataSize, SeekOrigin.Current);
  153. break;
  154. }
  155. _underlyingStream.Seek(_underlyingStream.Position % 2, SeekOrigin.Current);
  156. }
  157. break; // case ID_DIIN:
  158. case ID_DSD:
  159. _dataOffset = _underlyingStream.Position;
  160. _dataSize = ck.DataSize;
  161. _underlyingStream.Seek(ck.DataSize, SeekOrigin.Current);
  162. continue;
  163. default:
  164. //skip unknown chunk
  165. underlyingStream.Seek(ck.DataSize, SeekOrigin.Current);
  166. break;
  167. }
  168. break;
  169. }
  170. //align: pad byte char with value 0, used for making chunks an even length.
  171. _underlyingStream.Seek(_underlyingStream.Position % 2, SeekOrigin.Current);
  172. if (_isDstEncoded) throw new NotSupportedException("DST is not support");
  173. WaveFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Unknown, _sampleRate, _channelCount, _sampleRate / 8 * _channelCount, _channelCount, 1);
  174. }
  175. _underlyingStream.Position = _dataOffset;
  176. }
  177. protected override void Dispose(bool disposing)
  178. {
  179. base.Dispose(disposing);
  180. if (_disposeStream) _underlyingStream.Dispose();
  181. }
  182. public override int Read(byte[] buffer, int offset, int countRequired)
  183. {
  184. var mod = countRequired % _channelCount;
  185. var count = countRequired;
  186. if (mod != 0) count -= mod;
  187. var read = _underlyingStream.Read(buffer, offset, count);
  188. // MSB / LSB Convert?
  189. //for (var i = 0; i < read; i++) buffer[i] = BitReverseTable[buffer[i]];
  190. return read;
  191. }
  192. public override WaveFormat WaveFormat { get; }
  193. public override long Length => _dataSize;
  194. public override long Position { get => _underlyingStream.Position - _dataOffset; set => _underlyingStream.Position = _dataOffset + value - value % _channelCount; }
  195. }