Unity AudioClip EncodeToWAV
http://blog.csdn.net/xsjm206/article/details/6727605
https://gist.github.com/darktable/2317063
using UnityEngine;
using System;
using System.IO;
public static class AudioClipExtension
{
public static byte[] GetData(this AudioClip clip)
{
var data = new float[clip.samples * clip.channels];
clip.GetData(data, 0);
byte[] bytes = new byte[data.Length * 4];
Buffer.BlockCopy(data, 0, bytes, 0, bytes.Length);
return bytes;
}
public static void SetData(this AudioClip clip, byte[] bytes)
{
float[] data = new float[bytes.Length / 4];
Buffer.BlockCopy(bytes, 0, data, 0, bytes.Length);
clip.SetData(data, 0);
}
public static byte[] GetData16(this AudioClip clip)
{
var data = new float[clip.samples * clip.channels];
clip.GetData(data, 0);
byte[] bytes = new byte[data.Length * 2];
int rescaleFactor = 32767;
for (int i = 0; i < data.Length; i++)
{
short value = (short)(data[i] * rescaleFactor);
BitConverter.GetBytes(value).CopyTo(bytes, i * 2);
}
return bytes;
}
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;
}
}
相關文章
- Unity——Js和Unity互相呼叫UnityJS
- environmentmap in unityUnity
- unity xchartsUnity
- Unity元件Unity元件
- unity backbufferUnity
- [Unity]StringBuilderUnityUI
- Unity Shader 00 - 梳理 Unity Shader 的基本結構Unity
- 【Unity】相關Unity
- unity 常用APIUnityAPI
- Unity常用宏Unity
- unity學習Unity
- unity render pipelineUnity
- Unity GC垃圾回收UnityGC
- Unity AssetBundle 載入Unity
- Unity 2018.2之粒子Unity
- Unity 體積光Unity
- unity+pico+logUnity
- Unity幀動畫Unity動畫
- [Unity]常用技巧收集Unity
- dynamic bone unity githubUnityGithub
- unity destory的坑Unity
- Unity FPSSample Demo研究Unity
- Unity 讀取xmlUnityXML
- Unity基礎——InputUnity
- Unity-2DUnity
- Unity——物件池管理Unity物件
- Unity的Deferred ShadingUnity
- Unity XLua熱更新Unity
- [Unity3D] 2D畫素遊戲(一) Hello Unity!Unity3D遊戲
- Unity3D學習筆記3——Unity Shader的初步使用Unity3D筆記
- unity 自動尋路Unity
- Unity3d 初探Unity3D
- unity中播放視訊Unity
- unity 智慧巡邏兵Unity
- Unity SRP 02 Draw CallsUnity
- Unity IMGUI 文件筆記UnityGUI筆記
- Unity AssetBundle(2):工具UnityStudioUnity
- Unity 6 今日釋出!Unity
- Unity匯入xLua流程Unity