-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wave.cs
82 lines (73 loc) · 2.35 KB
/
Wave.cs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using NAudio.Wave;
namespace ToWhisperNet
{
public class Wave
{
public double[] Data { get; set; }
public double[] EData { get; set; }
public WaveFormat Format { get; private set; }
public Wave(double[] data, WaveFormat format)
{
Data = data;
Format = format;
}
public double[] Read(string filename)
{
Data = null;
Format = new WaveFormat(48000, 16, 1);
string tmpFile = "resampled.wav";
using (WaveFileReader reader = new WaveFileReader(filename))
{
using (var resampler = new MediaFoundationResampler(reader, Format))
{
WaveFileWriter.CreateWaveFile(tmpFile, resampler);
}
}
using (WaveFileReader reader = new WaveFileReader(tmpFile))
{
byte[] src = new byte[reader.Length];
reader.Read(src, 0, src.Length);
Data = ConvertToDouble(src);
}
return Data;
}
public void Write(string filename, double[] data)
{
using (WaveFileWriter writer = new WaveFileWriter(filename, Format))
{
float scale = 5f;
writer.Write(ConvertToByte(data, scale), 0, data.Length * 2);
}
}
short nonzero = 1;
private double[] ConvertToDouble(byte[] data)
{
double[] result = new double[data.Length / 2];
for (int i = 0; i < data.Length; i += 2)
{
short d = (short)(data[i] | (data[i + 1] << 8));
if (d == 0)
{
d = nonzero;
}
result[i / 2] = d / 32767.0;
}
return result;
}
private byte[] ConvertToByte(double[] data, float scale)
{
byte[] result = new byte[data.Length * 2];
for (int i = 0; i < data.Length; i++)
{
short d = (short)(data[i] * 32767.0 * scale);
if (d == nonzero)
{
d = 0;
}
result[i * 2] = (byte)(d & 255);
result[i * 2 + 1] = (byte)((d >> 8) & 255);
}
return result;
}
}
}