-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWavReader.cs
More file actions
56 lines (49 loc) · 1.73 KB
/
Copy pathWavReader.cs
File metadata and controls
56 lines (49 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace BeatTimer
{
// https://stackoverflow.com/questions/8754111/how-to-read-the-data-in-a-wav-file-to-an-array
public class WavReader
{
static double bytesToDouble(byte firstByte, byte secondByte)
{
short s = (short)((secondByte << 8) | firstByte);
return s / 32768.0;
}
static double bytesToDouble(byte b1, byte b2, byte b3, byte b4)
{
return (double)((b4 << 24) | (b3 << 16) | (b2 << 8) | b1);
}
public static void readWav(string filename, out double[] audio, out double samplerate)
{
byte[] wav = File.ReadAllBytes(filename);
int channels = wav[22];
int pos = 12;
while (!(wav[pos] == 100 && wav[pos + 1] == 97 && wav[pos + 2] == 116 && wav[pos + 3] == 97))
{
pos += 4;
int chunkSize = wav[pos] + wav[pos + 1] * 256 + wav[pos + 2] * 65536 + wav[pos + 3] * 16777216;
pos += 4 + chunkSize;
}
pos += 8;
int samples = (wav.Length - pos) / 2;
if (channels == 2) samples /= 2;
audio = new double[samples];
int i = 0;
while (pos + 4 < wav.Length)
{
audio[i] = bytesToDouble(wav[pos], wav[pos + 1]);
pos += 2;
if (channels == 2)
{
audio[i] = (audio[i] + bytesToDouble(wav[pos], wav[pos + 1])) / 2;
pos += 2;
}
i++;
}
samplerate = bytesToDouble(wav[24], wav[25], wav[26], wav[27]);
}
}
}