似乎需要超能力才能看到,所以复制粘贴一份过来。
1 using System; 2 using System.IO; 3 using UnityEngine; 4 using System.Collections.Generic; 5 using System.Threading; 6 7 public class SavWav { 8 9 const int HEADER_SIZE = 44; 10 struct ClipData{ 11 12 public int samples; 13 public int channels; 14 public float[] samplesData; 15 16 } 17 18 public bool Save(string filename, AudioClip clip) { 19 if (!filename.ToLower().EndsWith(".wav")) { 20 filename += ".wav"; 21 } 22 23 var filepath = filename; 24 25 Debug.Log(filepath); 26 27 // Make sure directory exists if user is saving to sub dir. 28 Directory.CreateDirectory(Path.GetDirectoryName(filepath)); 29 ClipData clipdata = new ClipData (); 30 clipdata.samples = clip.samples; 31 clipdata.channels = clip.channels; 32 float[] dataFloat = new float[clip.samples*clip.channels]; 33 clip.GetData (dataFloat, 0); 34 clipdata.samplesData = dataFloat; 35 using (var fileStream = CreateEmpty(filepath)) { 36 MemoryStream memstrm = new MemoryStream (); 37 ConvertAndWrite(memstrm, clipdata); 38 memstrm.WriteTo (fileStream); 39 WriteHeader(fileStream, clip); 40 } 41 42 return true; // TODO: return false if there's a failure saving the file 43 } 44 45 public AudioClip TrimSilence(AudioClip clip, float min) { 46 var samples = new float[clip.samples]; 47 48 clip.GetData(samples, 0); 49 50 return TrimSilence(new List(samples), min, clip.channels, clip.frequency); 51 } 52 53 public AudioClip TrimSilence(List samples, float min, int channels, int hz) { 54 return TrimSilence(samples, min, channels, hz, false, false); 55 } 56 57 public AudioClip TrimSilence(List samples, float min, int channels, int hz, bool _3D, bool stream) { 58 int i; 59 60 for (i=0; i min) { 62 break; 63 } 64 } 65 66 samples.RemoveRange(0, i); 67 68 for (i=samples.Count - 1; i>0; i--) { 69 if (Mathf.Abs(samples[i]) > min) { 70 break; 71 } 72 } 73 74 samples.RemoveRange(i, samples.Count - i); 75 76 var clip = AudioClip.Create("TempClip", samples.Count, channels, hz, _3D, stream); 77 78 clip.SetData(samples.ToArray(), 0); 79 80 return clip; 81 } 82 83 FileStream CreateEmpty(string filepath) { 84 var fileStream = new FileStream(filepath, FileMode.Create); 85 byte emptyByte = new byte(); 86 87 for(int i = 0; i < HEADER_SIZE; i++) //preparing the header 88 { 89 fileStream.WriteByte(emptyByte); 90 } 91 92 return fileStream; 93 } 94 95 void ConvertAndWrite(MemoryStream memStream, ClipData clipData) 96 { 97 float[] samples = new float[clipData.samples*clipData.channels]; 98 99 samples = clipData.samplesData;100 101 Int16[] intData = new Int16[samples.Length];102 103 Byte[] bytesData = new Byte[samples.Length * 2];104 105 const float rescaleFactor = 32767; //to convert float to Int16106 107 for (int i = 0; i < samples.Length; i++)108 {109 intData[i] = (short)(samples[i] * rescaleFactor);110 //Debug.Log (samples [i]);111 }112 Buffer.BlockCopy(intData, 0, bytesData, 0, bytesData.Length);113 memStream.Write(bytesData, 0, bytesData.Length);114 }115 116 void WriteHeader(FileStream fileStream, AudioClip clip) {117 118 var hz = clip.frequency;119 var channels = clip.channels;120 var samples = clip.samples;121 122 fileStream.Seek(0, SeekOrigin.Begin);123 124 Byte[] riff = System.Text.Encoding.UTF8.GetBytes("RIFF");125 fileStream.Write(riff, 0, 4);126 127 Byte[] chunkSize = BitConverter.GetBytes(fileStream.Length - 8);128 fileStream.Write(chunkSize, 0, 4);129 130 Byte[] wave = System.Text.Encoding.UTF8.GetBytes("WAVE");131 fileStream.Write(wave, 0, 4);132 133 Byte[] fmt = System.Text.Encoding.UTF8.GetBytes("fmt ");134 fileStream.Write(fmt, 0, 4);135 136 Byte[] subChunk1 = BitConverter.GetBytes(16);137 fileStream.Write(subChunk1, 0, 4);138 139 UInt16 two = 2;140 UInt16 one = 1;141 142 Byte[] audioFormat = BitConverter.GetBytes(one);143 fileStream.Write(audioFormat, 0, 2);144 145 Byte[] numChannels = BitConverter.GetBytes(channels);146 fileStream.Write(numChannels, 0, 2);147 148 Byte[] sampleRate = BitConverter.GetBytes(hz);149 fileStream.Write(sampleRate, 0, 4);150 151 Byte[] byteRate = BitConverter.GetBytes(hz * channels * 2); // sampleRate * bytesPerSample*number of channels, here 44100*2*2152 fileStream.Write(byteRate, 0, 4);153 154 UInt16 blockAlign = (ushort) (channels * 2);155 fileStream.Write(BitConverter.GetBytes(blockAlign), 0, 2);156 157 UInt16 bps = 16;158 Byte[] bitsPerSample = BitConverter.GetBytes(bps);159 fileStream.Write(bitsPerSample, 0, 2);160 161 Byte[] datastring = System.Text.Encoding.UTF8.GetBytes("data");162 fileStream.Write(datastring, 0, 4);163 164 Byte[] subChunk2 = BitConverter.GetBytes(samples * channels * 2);165 fileStream.Write(subChunk2, 0, 4);166 167 // fileStream.Close();168 }169 }