-
Notifications
You must be signed in to change notification settings - Fork 90
/
WaveFileHelper.cs
104 lines (90 loc) · 3.92 KB
/
WaveFileHelper.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
namespace Microsoft.Psi.Audio
{
using System;
using System.IO;
using System.Text;
/// <summary>
/// Provides static Wave file helper methods.
/// </summary>
public static class WaveFileHelper
{
/// <summary>
/// Reads a Wave file header from a binary stream.
/// </summary>
/// <param name="br">The binary reader to read from.</param>
/// <returns>A WaveFormat object representing the Wave header.</returns>
public static WaveFormat ReadWaveFileHeader(BinaryReader br)
{
if (Encoding.UTF8.GetString(BitConverter.GetBytes(br.ReadInt32())) != "RIFF")
{
throw new FormatException("RIFF header missing");
}
uint fileSize = br.ReadUInt32();
if (Encoding.UTF8.GetString(BitConverter.GetBytes(br.ReadInt32())) != "WAVE")
{
throw new FormatException("WAVE header missing");
}
if (Encoding.UTF8.GetString(BitConverter.GetBytes(br.ReadInt32())) != "fmt ")
{
throw new FormatException("Format header missing");
}
uint headerLength = br.ReadUInt32();
WaveFormat format = WaveFormat.FromStream(br.BaseStream, (int)headerLength);
return format;
}
/// <summary>
/// Reads a Wave file header from a Wave file.
/// </summary>
/// <param name="filename">The name of the file to read from.</param>
/// <returns>A WaveFormat object representing the Wave header.</returns>
public static WaveFormat ReadWaveFileHeader(string filename)
{
using (BinaryReader br = new BinaryReader(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)))
{
return ReadWaveFileHeader(br);
}
}
/// <summary>
/// Reads the length in bytes of the data section of a Wave file.
/// </summary>
/// <param name="br">The binary reader to read from.</param>
/// <returns>The number of byte of wave data that follow.</returns>
public static long ReadWaveDataLength(BinaryReader br)
{
var name = Encoding.UTF8.GetString(BitConverter.GetBytes(br.ReadInt32()));
if (name != "data")
{
if (name == "fact" || name == "LIST")
{
// Some formats (e.g. IEEE float) contain fact and LIST chunks (which we skip).
// see fhe "fact Chunk" section of the spec: http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
// "IEEE float data (introduced after the Rev. 3 documention) need a fact"
br.ReadBytes((int)br.ReadUInt32()); // skip
return ReadWaveDataLength(br);
}
throw new FormatException("Data header missing");
}
return br.ReadUInt32();
}
/// <summary>
/// Reads the data section of a Wave file.
/// </summary>
/// <param name="br">The binary reader to read from.</param>
/// <returns>An array of the raw audio data bytes.</returns>
public static byte[] ReadWaveData(BinaryReader br)
{
return br.ReadBytes((int)ReadWaveDataLength(br));
}
/// <summary>
/// Extension method to write WaveFormat objects.
/// </summary>
/// <param name="writer">The BinaryWriter to which to write the WaveFormat.</param>
/// <param name="format">The WaveFormat object to write.</param>
public static void Write(this BinaryWriter writer, WaveFormat format)
{
format.WriteTo(writer);
}
}
}