Skip to content

Commit 2681128

Browse files
committed
add support for Apple-formatted WAV files #12
1 parent 16acd5a commit 2681128

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

src/Spectrogram/WavFile.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Format here is based on http://soundfile.sapp.org/doc/WaveFormat/
33

44
using System;
5+
using System.Diagnostics;
56
using System.IO;
67

78
namespace Spectrogram
@@ -37,23 +38,42 @@ public static (int sampleRate, double[] L, double[] R) ReadStereo(string filePat
3738
throw new NotImplementedException("unsupported WAV header (chunk 1 must be 16 bytes in length)");
3839

3940
int audioFormat = br.ReadUInt16();
41+
Debug.WriteLine($"audio format: {audioFormat}");
4042
if (audioFormat != 1)
4143
throw new NotImplementedException("unsupported WAV header (audio format must be 1, indicating uncompressed PCM data)");
4244

4345
int channelCount = br.ReadUInt16();
46+
Debug.WriteLine($"channel count: {channelCount}");
47+
4448
int sampleRate = (int)br.ReadUInt32();
49+
Debug.WriteLine($"sample rate: {sampleRate} Hz");
50+
4551
int byteRate = (int)br.ReadUInt32();
52+
Debug.WriteLine($"byteRate: {byteRate}");
53+
4654
ushort blockSize = br.ReadUInt16();
55+
Debug.WriteLine($"block size: {blockSize} bytes per sample");
56+
4757
ushort bitsPerSample = br.ReadUInt16();
58+
Debug.WriteLine($"resolution: {bitsPerSample}-bit");
4859
if (bitsPerSample != 16)
4960
throw new NotImplementedException("Only 16-bit WAV files are supported");
5061

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}");
5376

54-
// read data values from the WAV file
55-
int dataByteCount = (int)br.ReadUInt32();
56-
int timePoints = dataByteCount / blockSize;
5777
if (channelCount == 1)
5878
{
5979
double[] L = new double[timePoints];

0 commit comments

Comments
 (0)