WaveReader.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang)
  2. using System;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. namespace SherpaNcnn {
  6. [StructLayout(LayoutKind.Sequential)]
  7. public struct WaveHeader {
  8. public Int32 ChunkID;
  9. public Int32 ChunkSize;
  10. public Int32 Format;
  11. public Int32 SubChunk1ID;
  12. public Int32 SubChunk1Size;
  13. public Int16 AudioFormat;
  14. public Int16 NumChannels;
  15. public Int32 SampleRate;
  16. public Int32 ByteRate;
  17. public Int16 BlockAlign;
  18. public Int16 BitsPerSample;
  19. public Int32 SubChunk2ID;
  20. public Int32 SubChunk2Size;
  21. public bool Validate() {
  22. if (ChunkID != 0x46464952) {
  23. Console.WriteLine($"Invalid chunk ID: 0x{ChunkID:X}. Expect 0x46464952");
  24. return false;
  25. }
  26. // E V A W
  27. if (Format != 0x45564157) {
  28. Console.WriteLine($"Invalid format: 0x{Format:X}. Expect 0x45564157");
  29. return false;
  30. }
  31. // t m f
  32. if (SubChunk1ID != 0x20746d66) {
  33. Console.WriteLine($"Invalid SubChunk1ID: 0x{SubChunk1ID:X}. Expect 0x20746d66");
  34. return false;
  35. }
  36. if (SubChunk1Size != 16) {
  37. Console.WriteLine($"Invalid SubChunk1Size: {SubChunk1Size}. Expect 16");
  38. return false;
  39. }
  40. if (AudioFormat != 1) {
  41. Console.WriteLine($"Invalid AudioFormat: {AudioFormat}. Expect 1");
  42. return false;
  43. }
  44. if (NumChannels != 1) {
  45. Console.WriteLine($"Invalid NumChannels: {NumChannels}. Expect 1");
  46. return false;
  47. }
  48. if (ByteRate != (SampleRate * NumChannels * BitsPerSample / 8)) {
  49. Console.WriteLine($"Invalid byte rate: {ByteRate}.");
  50. return false;
  51. }
  52. if (BlockAlign != (NumChannels * BitsPerSample / 8)) {
  53. Console.WriteLine($"Invalid block align: {ByteRate}.");
  54. return false;
  55. }
  56. if (BitsPerSample != 16) { // we support only 16 bits per sample
  57. Console.WriteLine($"Invalid bits per sample: {BitsPerSample}. Expect 16");
  58. return false;
  59. }
  60. return true;
  61. }
  62. }
  63. // It supports only 16-bit, single channel WAVE format.
  64. // The sample rate can be any value.
  65. public class WaveReader {
  66. public WaveReader(String fileName) {
  67. if (!File.Exists(fileName)) {
  68. throw new ApplicationException($"{fileName} does not exist!");
  69. }
  70. using (var stream = File.Open(fileName, FileMode.Open)) {
  71. using (var reader = new BinaryReader(stream)) {
  72. _header = ReadHeader(reader);
  73. if(!_header.Validate()) {
  74. throw new ApplicationException($"Invalid wave file ${fileName}");
  75. }
  76. SkipMetaData(reader);
  77. // now read samples
  78. // _header.SubChunk2Size contains number of bytes in total.
  79. // we assume each sample is of type int16
  80. byte[] buffer = reader.ReadBytes(_header.SubChunk2Size);
  81. short[] samples_int16 = new short[_header.SubChunk2Size/2];
  82. Buffer.BlockCopy(buffer, 0, samples_int16, 0, buffer.Length);
  83. _samples = new float[samples_int16.Length];
  84. for(var i = 0; i < samples_int16.Length; ++i) {
  85. _samples[i] = samples_int16[i] / 32768.0F;
  86. }
  87. }
  88. }
  89. }
  90. private static WaveHeader ReadHeader(BinaryReader reader) {
  91. byte[] bytes = reader.ReadBytes(Marshal.SizeOf(typeof(WaveHeader)));
  92. GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
  93. WaveHeader header = (WaveHeader)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(WaveHeader));
  94. handle.Free();
  95. return header;
  96. }
  97. private void SkipMetaData(BinaryReader reader) {
  98. var bs = reader.BaseStream;
  99. Int32 subChunk2ID = _header.SubChunk2ID;
  100. Int32 subChunk2Size = _header.SubChunk2Size;
  101. while (bs.Position != bs.Length && subChunk2ID != 0x61746164) {
  102. bs.Seek(subChunk2Size, SeekOrigin.Current);
  103. subChunk2ID = reader.ReadInt32();
  104. subChunk2Size = reader.ReadInt32();
  105. }
  106. _header.SubChunk2ID = subChunk2ID;
  107. _header.SubChunk2Size = subChunk2Size;
  108. }
  109. private WaveHeader _header;
  110. // Samples are normalized to the range [-1, 1]
  111. private float[] _samples;
  112. public int SampleRate => _header.SampleRate;
  113. public float [] Samples => _samples;
  114. public static void Test(String fileName) {
  115. WaveReader reader = new WaveReader(fileName);
  116. Console.WriteLine($"samples length: {reader.Samples.Length}");
  117. Console.WriteLine($"samples rate: {reader.SampleRate}");
  118. }
  119. }
  120. }