DffAudioStream.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System.Runtime.InteropServices;
  2. using System.Text;
  3. using Bmp.Core.Common.Utility;
  4. using NAudio.Wave;
  5. using WaveFormat = NAudio.Wave.WaveFormat;
  6. namespace Bmp.Core.Lite.Playback.Inputs;
  7. public class DffAudioStream : DsdAudioStream,IHaveDecoderInfo
  8. {
  9. private const string ID_FRM8 = "FRM8";
  10. private const string ID_DSD = "DSD ";
  11. private const string ID_SND = "SND ";
  12. private const string ID_FS = "FS ";
  13. private const string ID_CHNL = "CHNL";
  14. private const string ID_CMPR = "CMPR";
  15. private const string ID_DST = "DST ";
  16. private const string ID_LSCO = "LSCO";
  17. private const string ID_PROP = "PROP";
  18. private const string ID_DIIN = "DIIN";
  19. private const string ID_MARK = "MARK";
  20. private const int FILE_VERSION = 0x01050000;
  21. string IHaveDecoderInfo.DecoderName => "DFF";
  22. string? IHaveDecoderInfo.FileFormat => "V1.5";
  23. public static bool IsDffFile(Stream stream)
  24. {
  25. if (stream.Length > 4)
  26. {
  27. using var binaryReader = new BinaryReader(stream, Encoding.UTF8, true);
  28. var signatureDSD = Encoding.ASCII.GetString(binaryReader.ReadBytes(4));
  29. if (signatureDSD == ID_FRM8) return true;
  30. }
  31. return false;
  32. }
  33. [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
  34. public struct ID
  35. {
  36. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
  37. public string Value;
  38. }
  39. [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
  40. public struct Chunk
  41. {
  42. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
  43. public string Id;
  44. private readonly long chunkDataSize;
  45. public long DataSize => chunkDataSize.NetworkOrderToHostOrder();
  46. }
  47. private readonly Stream _underlyingStream;
  48. private readonly bool _disposeStream;
  49. private readonly int _fileVersion;
  50. private readonly int _sampleRate;
  51. private readonly short _channelCount;
  52. private readonly IReadOnlyCollection<string> _channelIds;
  53. private readonly bool _isDstEncoded;
  54. private readonly short _loudspeakerConfig;
  55. private readonly long _dataOffset;
  56. private readonly long _dataSize;
  57. public DffAudioStream(string urlOrPath) : this(InputSourceProviderLite.ReadContentAsSeekableStream(urlOrPath), true)
  58. {
  59. }
  60. public DffAudioStream(Stream underlyingStream, bool disposeStream)
  61. {
  62. _underlyingStream = underlyingStream;
  63. _disposeStream = disposeStream;
  64. var ck = new Chunk();
  65. var sizeOfCk = Marshal.SizeOf<Chunk>();
  66. var sizeOfId = Marshal.SizeOf<ID>();
  67. if (underlyingStream.Length < sizeOfCk) throw new InvalidDataException("too small as dff file");
  68. underlyingStream.ReadStruct(ref ck);
  69. if (ck.Id != ID_FRM8) throw new InvalidDataException("invalid FRM8 signature");
  70. var frm8Size = ck.DataSize;
  71. var id = underlyingStream.ReadStruct<ID>();
  72. if (id.Value != ID_DSD) throw new InvalidDataException("invalid DSD signature");
  73. while (_underlyingStream.Position < (frm8Size + sizeOfCk))
  74. {
  75. underlyingStream.ReadStruct(ref ck);
  76. switch ((ck.Id, ck.DataSize))
  77. {
  78. case ("FVER", 4):
  79. _fileVersion = _underlyingStream.ReadStruct<int>().NetworkOrderToHostOrder();
  80. if (_fileVersion != FILE_VERSION) throw new InvalidDataException("invalid File version");
  81. continue;
  82. default:
  83. switch (ck.Id)
  84. {
  85. case ID_PROP:
  86. underlyingStream.ReadStruct(ref id);
  87. if (id.Value != ID_SND) throw new InvalidDataException("invalid SND signature");
  88. var id_prop_end = _underlyingStream.Position - sizeOfId + ck.DataSize;
  89. while (_underlyingStream.Position < id_prop_end)
  90. {
  91. underlyingStream.ReadStruct(ref ck);
  92. switch (ck.Id, ck.DataSize)
  93. {
  94. case (ID_FS, 4):
  95. _sampleRate = _underlyingStream.ReadStruct<int>().NetworkOrderToHostOrder();
  96. break;
  97. case (ID_LSCO, 2):
  98. _loudspeakerConfig = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
  99. break;
  100. default:
  101. switch (ck.Id)
  102. {
  103. case ID_CHNL:
  104. _channelCount = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
  105. _channelIds = Enumerable.Range(0, _channelCount).Select(_ => _underlyingStream.ReadStruct<ID>().Value).ToArray();
  106. break;
  107. case ID_CMPR:
  108. var compressionId = _underlyingStream.ReadStruct<ID>().Value;
  109. _isDstEncoded = compressionId == ID_DST;
  110. var len = _underlyingStream.ReadStruct<byte>();
  111. var text = Encoding.UTF8.GetString(_underlyingStream.ReadBytes(len));
  112. break;
  113. default:
  114. //skip unknown chunk
  115. _underlyingStream.Seek(ck.DataSize, SeekOrigin.Current);
  116. break;
  117. }
  118. break;
  119. }
  120. //align: pad byte char with value 0, used for making chunks an even length.
  121. _underlyingStream.Seek(_underlyingStream.Position % 2, SeekOrigin.Current);
  122. }
  123. break; //case ID_PROP:
  124. case ID_DIIN:
  125. var id_diin_end = _underlyingStream.Position - sizeOfId + ck.DataSize;
  126. while (_underlyingStream.Position < id_diin_end)
  127. {
  128. underlyingStream.ReadStruct(ref ck);
  129. switch (ck.Id)
  130. {
  131. case ID_MARK:
  132. var hours = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
  133. var minutes = _underlyingStream.ReadStruct<byte>();
  134. var second = _underlyingStream.ReadStruct<byte>();
  135. var samples = _underlyingStream.ReadStruct<uint>().NetworkOrderToHostOrder();
  136. var offset = _underlyingStream.ReadStruct<int>().NetworkOrderToHostOrder();
  137. // 0 TrackStart
  138. // 1 TrackStop
  139. // 2 ProgramStart
  140. // 4 Index Entry point of an Index
  141. var type = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
  142. // 0 All channels
  143. var channel = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
  144. var trackFlags = _underlyingStream.ReadStruct<short>().NetworkOrderToHostOrder();
  145. var textLen = _underlyingStream.ReadStruct<int>().NetworkOrderToHostOrder();
  146. var text = Encoding.UTF8.GetString(_underlyingStream.ReadBytes(textLen));
  147. break;
  148. default:
  149. //skip unknown chunk
  150. _underlyingStream.Seek(ck.DataSize, SeekOrigin.Current);
  151. break;
  152. }
  153. _underlyingStream.Seek(_underlyingStream.Position % 2, SeekOrigin.Current);
  154. }
  155. break; // case ID_DIIN:
  156. case ID_DSD:
  157. _dataOffset = _underlyingStream.Position;
  158. _dataSize = ck.DataSize;
  159. _underlyingStream.Seek(ck.DataSize, SeekOrigin.Current);
  160. continue;
  161. default:
  162. //skip unknown chunk
  163. underlyingStream.Seek(ck.DataSize, SeekOrigin.Current);
  164. break;
  165. }
  166. break;
  167. }
  168. //align: pad byte char with value 0, used for making chunks an even length.
  169. _underlyingStream.Seek(_underlyingStream.Position % 2, SeekOrigin.Current);
  170. if (_isDstEncoded) throw new NotSupportedException("DST is not support");
  171. WaveFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Unknown, _sampleRate, _channelCount, _sampleRate / 8 * _channelCount, _channelCount, 1);
  172. }
  173. _underlyingStream.Position = _dataOffset;
  174. }
  175. protected override void Dispose(bool disposing)
  176. {
  177. base.Dispose(disposing);
  178. if (_disposeStream) _underlyingStream.Dispose();
  179. }
  180. public override int Read(byte[] buffer, int offset, int countRequired)
  181. {
  182. var mod = countRequired % _channelCount;
  183. var count = countRequired;
  184. if (mod != 0) count -= mod;
  185. var read = _underlyingStream.Read(buffer, offset, count);
  186. // MSB / LSB Convert?
  187. for (var i = 0; i < read; i++) buffer[i] = BitReverseTable[buffer[i]];
  188. return read;
  189. }
  190. public override WaveFormat WaveFormat { get; }
  191. public override long Length => _dataSize;
  192. public override long Position { get => _underlyingStream.Position - _dataOffset; set => _underlyingStream.Position = _dataOffset + value - value % _channelCount; }
  193. private static readonly byte[] BitReverseTable =
  194. {
  195. 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
  196. 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
  197. 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
  198. 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
  199. 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
  200. 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
  201. 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
  202. 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
  203. 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
  204. 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
  205. 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
  206. 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
  207. 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
  208. 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
  209. 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
  210. 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
  211. 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
  212. 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
  213. 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
  214. 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
  215. 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
  216. 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
  217. 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
  218. 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
  219. 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
  220. 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
  221. 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
  222. 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
  223. 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
  224. 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
  225. 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
  226. 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
  227. };
  228. }