|
2 | 2 | // Format here is based on http://soundfile.sapp.org/doc/WaveFormat/
|
3 | 3 |
|
4 | 4 | using System;
|
| 5 | +using System.Diagnostics; |
5 | 6 | using System.IO;
|
6 | 7 |
|
7 | 8 | namespace Spectrogram
|
@@ -37,23 +38,42 @@ public static (int sampleRate, double[] L, double[] R) ReadStereo(string filePat
|
37 | 38 | throw new NotImplementedException("unsupported WAV header (chunk 1 must be 16 bytes in length)");
|
38 | 39 |
|
39 | 40 | int audioFormat = br.ReadUInt16();
|
| 41 | + Debug.WriteLine($"audio format: {audioFormat}"); |
40 | 42 | if (audioFormat != 1)
|
41 | 43 | throw new NotImplementedException("unsupported WAV header (audio format must be 1, indicating uncompressed PCM data)");
|
42 | 44 |
|
43 | 45 | int channelCount = br.ReadUInt16();
|
| 46 | + Debug.WriteLine($"channel count: {channelCount}"); |
| 47 | + |
44 | 48 | int sampleRate = (int)br.ReadUInt32();
|
| 49 | + Debug.WriteLine($"sample rate: {sampleRate} Hz"); |
| 50 | + |
45 | 51 | int byteRate = (int)br.ReadUInt32();
|
| 52 | + Debug.WriteLine($"byteRate: {byteRate}"); |
| 53 | + |
46 | 54 | ushort blockSize = br.ReadUInt16();
|
| 55 | + Debug.WriteLine($"block size: {blockSize} bytes per sample"); |
| 56 | + |
47 | 57 | ushort bitsPerSample = br.ReadUInt16();
|
| 58 | + Debug.WriteLine($"resolution: {bitsPerSample}-bit"); |
48 | 59 | if (bitsPerSample != 16)
|
49 | 60 | throw new NotImplementedException("Only 16-bit WAV files are supported");
|
50 | 61 |
|
51 |
| - if (new string(br.ReadChars(4)) != "data") |
52 |
| - throw new ArgumentException($"invalid WAV file (expected 'data' at byte {br.BaseStream.Position - 4})"); |
| 62 | + string dataChars = new string(br.ReadChars(4)); |
| 63 | + Debug.WriteLine($"Data characters: {dataChars}"); |
| 64 | + |
| 65 | + // this may be the number of data bytes, but don't rely on it to be. |
| 66 | + int finalNumber = (int)br.ReadUInt32(); |
| 67 | + Debug.WriteLine($"Final UInt32: {finalNumber}"); |
| 68 | + |
| 69 | + int bytesRemaining = (int)(fs.Length - br.BaseStream.Position); |
| 70 | + Debug.WriteLine($"Bytes remaining: {bytesRemaining}"); |
| 71 | + int sampleCount = bytesRemaining / blockSize; |
| 72 | + Debug.WriteLine($"Samples in file: {sampleCount}"); |
| 73 | + int timePoints = sampleCount / channelCount; |
| 74 | + Debug.WriteLine($"Time points in file: {timePoints}"); |
| 75 | + Debug.WriteLine($"First data byte: {br.BaseStream.Position}"); |
53 | 76 |
|
54 |
| - // read data values from the WAV file |
55 |
| - int dataByteCount = (int)br.ReadUInt32(); |
56 |
| - int timePoints = dataByteCount / blockSize; |
57 | 77 | if (channelCount == 1)
|
58 | 78 | {
|
59 | 79 | double[] L = new double[timePoints];
|
|
0 commit comments