Repository navigation
Expand file tree
/
Copy pathWaveFile.cs
More file actions
277 lines (241 loc) · 9.89 KB
/
Copy pathWaveFile.cs
File metadata and controls
277 lines (241 loc) · 9.89 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using IBM.Watson.DeveloperCloud.Logging;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;
namespace IBM.Watson.DeveloperCloud.Utilities
{
/// <summary>
/// WAV Utility functions.
/// </summary>
static public class WaveFile
{
#region Private Types
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct IFF_FORM_CHUNK
{
public uint form_id;
public uint form_length;
public uint id;
};
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct IFF_CHUNK
{
public uint id;
public uint length;
};
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct WAV_PCM
{
public ushort format_tag;
public ushort channels;
public uint sample_rate;
public uint average_data_rate;
public ushort alignment;
public ushort bits_per_sample;
};
#endregion
#region Private Functions
private static T ReadType<T>(BinaryReader reader)
{
#if NETFX_CORE
byte[] bytes = reader.ReadBytes(Marshal.SizeOf<T>());
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
T theStructure = (T)Marshal.PtrToStructure<T>(handle.AddrOfPinnedObject());
handle.Free();
#else
byte[] bytes = reader.ReadBytes(Marshal.SizeOf(typeof(T)));
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
T theStructure = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
handle.Free();
#endif
return theStructure;
}
private static void WriteType<T>(BinaryWriter writer, T data)
{
int size = Marshal.SizeOf(data);
byte[] bytes = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(data, ptr, true);
Marshal.Copy(ptr, bytes, 0, size);
Marshal.FreeHGlobal(ptr);
writer.Write(bytes, 0, size);
}
private static uint MakeID(string id)
{
byte[] bytes = Encoding.ASCII.GetBytes(id);
return BitConverter.ToUInt32(bytes, 0);
}
private static string GetID(uint id)
{
byte[] bytes = BitConverter.GetBytes(id);
return new string(new char[] { (char)bytes[0], (char)bytes[1], (char)bytes[2], (char)bytes[3] });
}
#endregion
#region Public Functions
/// <summary>
/// Creates a AudioClip object from WAV file data.
/// </summary>
/// <param name="clipName">What name to give the AudioClip.</param>
/// <param name="data">The raw data of the WAV file.</param>
/// <returns>Returns an AudioClip object on success, null on failure.</returns>
public static AudioClip ParseWAV(string clipName, byte[] data)
{
MemoryStream stream = new MemoryStream(data, false);
BinaryReader reader = new BinaryReader(stream);
IFF_FORM_CHUNK form = ReadType<IFF_FORM_CHUNK>(reader);
if (GetID(form.form_id) != "RIFF" || GetID(form.id) != "WAVE")
{
Log.Error("WaveFile.ParseWAV()", "Malformed WAV header: {0} != RIFF || {1} != WAVE", GetID(form.form_id), GetID(form.id));
return null;
}
WAV_PCM header = new WAV_PCM();
bool bHeaderFound = false;
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
IFF_CHUNK chunk = ReadType<IFF_CHUNK>(reader);
int ChunkLength = (int)chunk.length;
if (ChunkLength < 0) // HACK: Deal with TextToSpeech bug where the chunk length is not set for the data chunk..
ChunkLength = (int)(reader.BaseStream.Length - reader.BaseStream.Position);
if ((ChunkLength & 0x1) != 0)
ChunkLength += 1;
long ChunkEnd = reader.BaseStream.Position + ChunkLength;
if (GetID(chunk.id) == "fmt ")
{
bHeaderFound = true;
header = ReadType<WAV_PCM>(reader);
}
else if (GetID(chunk.id) == "data")
{
if (!bHeaderFound)
{
Log.Error("WaveFile.ParseWAV()", "Failed to find header.");
return null;
}
byte[] waveform = reader.ReadBytes(ChunkLength);
// convert into a float based wave form..
int channels = (int)header.channels;
int bps = (int)header.bits_per_sample;
float divisor = 1 << (bps - 1);
int bytesps = bps / 8;
int samples = waveform.Length / bytesps;
Log.Debug("WaveFile.ParseWAV()", "WAV INFO, channels = {0}, bps = {1}, samples = {2}, rate = {3}",
channels, bps, samples, header.sample_rate);
float[] wf = new float[samples];
if (bps == 16)
{
for (int s = 0; s < samples; ++s)
wf[s] = ((float)BitConverter.ToInt16(waveform, s * bytesps)) / divisor;
}
else if (bps == 32)
{
for (int s = 0; s < samples; ++s)
wf[s] = ((float)BitConverter.ToInt32(waveform, s * bytesps)) / divisor;
}
else if (bps == 8)
{
for (int s = 0; s < samples; ++s)
wf[s] = ((float)BitConverter.ToChar(waveform, s * bytesps)) / divisor;
}
else
{
Log.Error("WaveFile.ParseWAV()", "Unspported BPS {0} in WAV data.", bps.ToString());
return null;
}
AudioClip clip = AudioClip.Create(clipName, samples, channels, (int)header.sample_rate, false);
clip.SetData(wf, 0);
return clip;
}
reader.BaseStream.Position = ChunkEnd;
}
return null;
}
/// <summary>
/// Creates a WAV file from a AudioClip object.
/// </summary>
/// <param name="clip">The AudioClip object to generate the WAV file from.</param>
/// <param name="bps">How many bits per sample we should use in the WAV file, 8, 16, or 32.</param>
/// <returns>Returns a byte array of the raw data of the WAV file.</returns>
public static byte[] CreateWAV(AudioClip clip, int bps = 16)
{
MemoryStream stream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(stream);
IFF_FORM_CHUNK form = new IFF_FORM_CHUNK();
form.form_id = MakeID("RIFF");
form.form_length = 0xffffffff;
form.id = MakeID("WAVE");
WriteType(writer, form);
long form_start = writer.BaseStream.Position;
float divisor = 1 << (bps - 1);
WAV_PCM header = new WAV_PCM();
header.format_tag = 1;
header.alignment = 2;
header.bits_per_sample = (ushort)bps;
header.sample_rate = (uint)clip.frequency;
header.channels = (ushort)clip.channels;
header.average_data_rate = (uint)((bps / 8) * clip.channels * clip.frequency);
IFF_CHUNK fmt = new IFF_CHUNK();
fmt.id = MakeID("fmt ");
fmt.length = (uint)Marshal.SizeOf(header);
WriteType(writer, fmt);
WriteType(writer, header);
IFF_CHUNK data = new IFF_CHUNK();
data.id = MakeID("data");
data.length = (uint)((bps / 8) * clip.samples * clip.channels);
WriteType(writer, data);
float[] samples = new float[clip.samples * clip.channels];
clip.GetData(samples, 0);
if (bps == 16)
{
for (int i = 0; i < samples.Length; ++i)
writer.Write((short)(samples[i] * divisor));
}
else if (bps == 32)
{
for (int i = 0; i < samples.Length; ++i)
writer.Write((int)(samples[i] * divisor));
}
else if (bps == 8)
{
for (int i = 0; i < samples.Length; ++i)
writer.Write((char)(samples[i] * divisor));
}
else
{
Log.Error("WaveFile.CreateWAV()", "Unsupported BPS {0} in WAV data.", bps.ToString());
return null;
}
// lastly, update the form length..
form.form_length = (uint)(writer.BaseStream.Position - form_start);
writer.Seek(0, SeekOrigin.Begin);
WriteType(writer, form);
#if NETFX_CORE
ArraySegment<byte> bytes;
if (stream.TryGetBuffer(out bytes))
return bytes.Array;
else
return null;
#else
return stream.GetBuffer();
#endif
}
#endregion
}
}