unity wav 格式音訊轉換為二進位制檔案

NO IMAGE

/// 音訊WAV格式的位元組陣列
/// </summary>
/// <returns>The to WA.</returns>
/// <param name="clip">Clip.</param>
public static byte[] EncodeToWAV(this AudioClip clip)
{
byte[] bytes = null;
using (var memoryStream = new MemoryStream())
{
memoryStream.Write(new byte[44], 0, 44);//預留44位元組頭部資訊
byte[] bytesData = clip.GetData16();
memoryStream.Write(bytesData, 0, bytesData.Length);
memoryStream.Seek(0, SeekOrigin.Begin);
byte[] riff = System.Text.Encoding.UTF8.GetBytes("RIFF");
memoryStream.Write(riff, 0, 4);
byte[] chunkSize = BitConverter.GetBytes(memoryStream.Length - 8);
memoryStream.Write(chunkSize, 0, 4);
byte[] wave = System.Text.Encoding.UTF8.GetBytes("WAVE");
memoryStream.Write(wave, 0, 4);
byte[] fmt = System.Text.Encoding.UTF8.GetBytes("fmt ");
memoryStream.Write(fmt, 0, 4);
byte[] subChunk1 = BitConverter.GetBytes(16);
memoryStream.Write(subChunk1, 0, 4);
UInt16 two = 2;
UInt16 one = 1;
byte[] audioFormat = BitConverter.GetBytes(one);
memoryStream.Write(audioFormat, 0, 2);
byte[] numChannels = BitConverter.GetBytes(clip.channels);
memoryStream.Write(numChannels, 0, 2);
byte[] sampleRate = BitConverter.GetBytes(clip.frequency);
memoryStream.Write(sampleRate, 0, 4);
byte[] byteRate = BitConverter.GetBytes(clip.frequency * clip.channels * 2); // sampleRate * bytesPerSample*number of channels
memoryStream.Write(byteRate, 0, 4);
UInt16 blockAlign = (ushort)(clip.channels * 2);
memoryStream.Write(BitConverter.GetBytes(blockAlign), 0, 2);
UInt16 bps = 16;
byte[] bitsPerSample = BitConverter.GetBytes(bps);
memoryStream.Write(bitsPerSample, 0, 2);
byte[] datastring = System.Text.Encoding.UTF8.GetBytes("data");
memoryStream.Write(datastring, 0, 4);
byte[] subChunk2 = BitConverter.GetBytes(clip.samples * clip.channels * 2);
memoryStream.Write(subChunk2, 0, 4);
bytes = memoryStream.ToArray();
}
return bytes;
}